Version française
Home     About     Download     Resources     Contact us    
Browse thread
Subtyping
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jacques Garrigue <garrigue@m...>
Subject: Re: [Caml-list] Subtyping
Here is a slightly more usable version, using helper functions to
avoid writing intermediate closures by hand.

  type 'a base = {x : 'a; fn : 'a -> unit}
  (* 4 next lines are boilerplate, could be auto-generated *)
  type 'b base_op = {bop: 'a. 'a base -> 'b}
  type base_wrapper = {base: 'b. 'b base_op -> 'b}
  let wrap a = {base = fun x -> x.bop a}
  let apply op w = w.base op

  let l =
    [wrap {x = 1; fn = print_int}; wrap {x = 1.2; fn = print_float}];;

  List.iter (apply {bop = fun r -> r.fn r.x}) l

The only thing you cannot abbreviate is the {bop = ...} part, as this
is where universality is checked, to ensure that no cross application
can be done.

Jacques Garrigue

> Then what you are asking for is existential types. There is no syntax
> for them in ocaml, but they can be encoded through universal types
> (interestingly, the dual is not true).
> 
> type 'a base = {x : 'a; fn : 'a -> unit}
> type 'b base_op = {bop: 'a. 'a base -> 'b}
> type base_wrapper = {base: 'b. 'b base_op -> 'b}
> 
> let l =
>   let a = {x = 1; fn = print_int}
>   and b = {x = 1.2; fn = print_float} in
>   [{base = fun x -> x.bop a}; {base = fun x -> x.bop b}]
> 
> List.iter (fun w -> w.base {bop = fun r -> r.fn r.x}) l
> 
> As you can see, the result is rather verbose, but this works.
> Fortunately, closure and objects are usually enough...