caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Ken Friis Larsen <kfl@it.edu>
To: caml-list@inria.fr
Subject: [Caml-list] Re: two unrelated questions
Date: 01 May 2001 13:45:31 +0100	[thread overview]
Message-ID: <yruu235b5lg.fsf_-_@swordfish.cl.cam.ac.uk> (raw)
In-Reply-To: Jacques Garrigue's message of "Tue, 01 May 2001 10:31:00 +0900"

"Jacques" == Jacques Garrigue <garrigue@kurims.kyoto-u.ac.jp> writes:

 Jacques>  Particularly, local exception definitions are one of the
 Jacques>  rare features present in SML and absent in Caml, and that
 Jacques>  some SML developpers think that Caml is right about :-)

But some of us really like that feature because it allows an elegant
*user* implementation of dynamic typing.


The task is to implement a structure with the signature DYN:

module type DYN = 
  sig 
    type dyn 
    val wrap: unit -> ('a -> dyn) * (dyn -> 'a)
  end


The cannonical SML solution is to use exceptions for this.  Here is a
Caml version of that solution:

module Dyn1 : DYN =
  struct 
    type dyn = exn
    let wrap () =
      let module D = struct exception DYN of 'a end in  
      (function x -> D.DYN x), 
      (function D.DYN x -> x)      
  end 
    
(But I'm unfortunately not strong enough in O'Caml because this gives
the error "Unbound type parameter 'a".  How do I fix that?  Note that
the implementation works as expected if we make 'a a fixed type.)

You can argue that you don't need that feature of exceptions just to
implement dynamic typing, because DYN can be implemented using
closures and references:

module Dyn2 : DYN = 
  struct
    type dyn = unit -> unit
    let wrap () =
      let r = ref None in
      (fun x -> fun () -> r := Some x),
      (fun f -> ( r := None
                ; f()
                ; match !r with
                    Some x -> x
                  | None   -> raise (Failure"Using the wrong unwrapper")
                ))     
  end


Cheers,

--Ken
-------------------
To unsubscribe, mail caml-list-request@inria.fr.  Archives: http://caml.inria.fr


  reply	other threads:[~2001-05-02 11:21 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-04-25 21:08 [Caml-list] " Chris Hecker
2001-04-26  0:38 ` Patrick M Doane
2001-04-26  6:04   ` Judicaël Courant
2001-04-26 12:06     ` John Max Skaller
2001-04-27  9:12       ` Anton Moscal
2001-04-29 22:24         ` Brian Rogoff
2001-04-30 18:57           ` Fergus Henderson
2001-05-01  1:31           ` Jacques Garrigue
2001-05-01 12:45             ` Ken Friis Larsen [this message]
2001-04-27 15:09       ` Brian Rogoff
2001-04-27 17:49         ` John Max Skaller
2001-04-26  8:22   ` Andreas Rossberg
2001-04-26  1:13 ` Jacques Garrigue
2001-04-26 13:47 ` Xavier Leroy
2001-04-26 22:34   ` Chris Hecker
2001-04-26 16:57 ` Mark Seaborn
2001-04-26 22:20   ` John Max Skaller
2001-05-01 21:08     ` Brian Rogoff
2001-05-01 23:30       ` John Max Skaller
2001-05-02  0:03       ` John Max Skaller

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=yruu235b5lg.fsf_-_@swordfish.cl.cam.ac.uk \
    --to=kfl@it.edu \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).