Version française
Home     About     Download     Resources     Contact us    
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: -- (:)
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 = 
  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