English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
[Caml-list] two unrelated questions
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2001-05-02 (11:21)
From: Ken Friis Larsen <kfl@i...>
Subject: [Caml-list] Re: two unrelated questions
"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 = 
    type dyn 
    val wrap: unit -> ('a -> dyn) * (dyn -> 'a)

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

module Dyn1 : DYN =
    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)      
(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 = 
    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")


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