Version française
Home     About     Download     Resources     Contact us    
Browse thread
Re: [Caml-list] type generalization of recursive calls
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Damien Guichard <alphablock@o...>
Subject: Re: [Caml-list] type generalization of recursive calls
Hello,

First, you really need strong tyupe checking because your definition is actually
incorrect.

Second, see polymorphic-recursion :
http://alaska-kamtchatka.blogspot.com/2009/05/polymorphic-recursion.html

Be sure to read the comments by bluestorm.

There is also pa_polyrec, a syntax extension for polymorphic recursion in OCaml
:
http://www.mail-archive.com/caml-list@yquem.inria.fr/msg04795.html


Finally, here is your (corrected) code using vanilla ocaml : 

type map =
  { map : 'a 'b. ('a -> 'b) -> 'a t -> 'b t }
let map =
  let rec map =
    { map = fun f -> function
    | E -> E
    | D (t1, t2) -> D (map.map f t1, map.map f t2)
    | O a -> O (f a)
    | I tt -> I (map.map (map.map f) tt) 
    }
  in map.map   


- damien



Le 17/02/2010 à 17:34:24, "Stéphane Gimenez" <stephane.gimenez@pps.jussieu.fr>
à écrit :
>
>Hi,
>
>I just realized that ocaml generalizes the type of a recursively
>defined function *only* for calls which are outside it's own
>definition. Recursive calls cannot use a generalized type.
>
>In fact, I'm tring to work with such a data type:
>
>type 'a t =
>  | E
>  | D of 'a t * 'a t
>  | O of 'a
>  | I of 'a t t
>
>And, I'm forced to use some dark magic to define simple operations on
>it:
>
>let rec map (f : 'a -> 'b) : 'a t -> 'b t =
>  begin function
>  | E -> E
>  | D (t1, t2) -> D (map f t1, map f t2)
>  | O a -> O (f a)
>  | I tt ->
>      I ((Obj.magic map : ('a t -> 'b t) -> 'a t t -> 'b t t) (map f) tt)
>  end
>
>Questions:
>  - Is it theoreticaly safe to generalize recursive calls ?
>  - Is there a syntactical trick to use generalized recursive calls ?
>  - Could such a generalization be added to the type checker ?
>      - Performance issues ?
>      - More obfusctated type checking errors ?
>
>In a related disscution I found, one asked about generalization
>between mutualy recursive definitions (same problem). No answers, but
>maybe I just lack pointers.
>
>Cheers,
>Stéphane
>
>_______________________________________________
>Caml-list mailing list. Subscription management:
>http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
>Archives: http://caml.inria.fr
>Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
>Bug reports: http://caml.inria.fr/bin/caml-bugs
>


--
Mail created using EssentialPIM Free - www.essentialpim.com