caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Polymorphic recursion
@ 2007-04-03 16:59 Loup Vaillant
  2007-04-03 17:20 ` [Caml-list] " Jeremy Yallop
                   ` (3 more replies)
  0 siblings, 4 replies; 25+ messages in thread
From: Loup Vaillant @ 2007-04-03 16:59 UTC (permalink / raw)
  To: caml-list

I was reading Okasaki's book, "Purely functionnal data structures",
and discovered that ML (and Ocaml) doesn't support non uniforms
function definitions.

So, even if:

(**)
type 'a seq = Unit | Seq of ('a * ('a * 'a)seq);;
(**)

is correct,

(**)
let rec size = fun
   | Unit -> 0
   | Seq(_, b) -> 1 + 2 * size b;;
(**)

is not. Here is the error:
#
| Seq(_, b) -> 1 + 2 * size b;;
This expression (the last 'b') has type seq ('a * 'a) but is here used
with type seq 'a
#

Why?
Can't we design a type system which allow this "size" function?
Can't we implement non uniform recursive function (efficiently, or at all)?.

I suppose the problem is somewhat difficult, but I can't see where.

Regards,
Loup Vaillant


^ permalink raw reply	[flat|nested] 25+ messages in thread
* polymorphic recursion
@ 2008-05-12 21:55 Jacques Le Normand
  2008-05-12 22:16 ` [Caml-list] " Christophe TROESTLER
  0 siblings, 1 reply; 25+ messages in thread
From: Jacques Le Normand @ 2008-05-12 21:55 UTC (permalink / raw)
  To: caml-list caml-list

[-- Attachment #1: Type: text/plain, Size: 142 bytes --]

Hello list,
I'm having trouble finding information on this; does ocaml support
polymorphic recursion and, if so, what's the syntax?
--Jacques

[-- Attachment #2: Type: text/html, Size: 160 bytes --]

^ permalink raw reply	[flat|nested] 25+ messages in thread
* [Caml-list] Polymorphic recursion
@ 2003-08-24 18:01 Lukasz Stafiniak
  2003-08-25  0:30 ` Jacques Garrigue
  0 siblings, 1 reply; 25+ messages in thread
From: Lukasz Stafiniak @ 2003-08-24 18:01 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 1650 bytes --]

Hi,

I have recently encountered some very interesting notions (cited below).
Although I've been using OCaml for some time, up to now I didn't know about
the existence of polymorphic records and (their reference to) the problem of
polymorphic recursion. Perhaps a word about it should be stated in the
manual (more evidently)? (I don't even recall to have seen the syntax in the
manual for this.)

With Your help I ventured again to "typecheck my programs before they are
generated" etc. But now I get an error I totally don't understand. (Still it
somehow resembles the problem it posed as an ordinary function.) Could
someone explain? I have attached the file.

Thanks in advance,
Best wishes,
Lukasz

Jacques Garrigue <garrigue@kurims.kyoto-u.ac.jp>
Re: [Caml-list] Variant parameterized method?
>>>>>>>>>

This precise type is not admissible in the ocaml type system.
In ocaml recursive types must be regular: only 'a foo may occur in the
expansion of 'a foo.

This problem is discussed in an answer to PR#1730 in the caml bug
database.
This can be solved by introducing an explicit wrapper.

<<<<<<<<<

Jacques Garrigue <garrigue@kurims.kyoto-u.ac.jp>
Re: [Caml-list] does class polymorphism need to be so complicated?
>>>>>>>>>

P.S. Having a lighter syntax for polymorphic methods might be a good
idea.
[...]
An advantage of such a syntax is that it could also be used in normal
"let rec" to provide polymorphic recursion.

<<<<<<<<<<




Uwaga! Do końca sierpnia przedłużyliśmy promocje, do pakietów
wielostanowiskowych dokładamy PenDrive  Sprawdź:
http://www.mks.com.pl/promocja-mobile.html

[-- Attachment #2: typedcode.ml --]
[-- Type: application/octet-stream, Size: 2621 bytes --]


(* this could be e.g.: type 'a annot = 'a code *)
type 'a annot = 'a
;;

type 'a valu =
  | Typeonly | Valu of 'a annot
  | Ord1bool of ('a, bool) ord1
  | Ord1int of ('a, int) ord1
and ('a, 'b) ord1 =
  | Ord1Red of ('b -> 'a) valu
  | Ord2bool of ('a, 'b, bool) ord2
  | Ord2int of ('a, 'b, int) ord2
and ('a, 'b, 'c) ord2 =
  | Ord2Red of ('a, 'c -> 'b) ord1
;;

type typterm =
  | Tbool of bool valu
  | Tint of int valu
;;

type 'a valu_env = {
  eValu : 'a annot list;
  eOrd1bool : ('a, bool) ord1_env option;
  eOrd1int : ('a, int) ord1_env option;
}
and ('a, 'b) ord1_env = {
  eOrd1Red : ('b -> 'a) valu_env option;
  eOrd2bool : ('a, 'b, bool) ord2_env option;
  eOrd2int : ('a, 'b, int) ord2_env option;
}
and ('a, 'b, 'c) ord2_env = {
  eOrd2Red : ('a, 'c -> 'b) ord1_env option;
}
;;

type env = {
  eBool : bool valu_env;
  eInt : int valu_env;
}
;;

type env_put_rec = {
  put_valu : 'a. 'a valu_env option -> 'a valu -> 'a valu_env;
  put_ord1 : 'a 'b. ('a, 'b) ord1_env option -> ('a, 'b) ord1 -> ('a,
  'b)ord1_env;
  put_ord2 : 'a 'b 'c. ('a, 'b, 'c) ord2_env option -> ('a, 'b, 'c)
	       ord2 -> ('a, 'b, 'c) ord2_env;
}
;;

let rec env_put = {
  put_valu =
  begin fun e ->
    let e = match e with
      | Some e -> e
      | None -> {eValu = []; eOrd1bool = None; eOrd1int = None}
    in
    function
      | Typeonly -> failwith "(Valu v) required"
      | Valu v ->
	  {e with eValu = v::e.eValu}
      | Ord1bool tv ->
	    {e with eOrd1bool = Some (env_put.put_ord1 e.eOrd1bool tv)}
      | Ord1int tv ->
	  {e with eOrd1int = Some (env_put.put_ord1 e.eOrd1int tv)}
  end;
  put_ord1 =
  begin fun e ->
    let e = match e with
      | Some e -> e
      | None -> {eOrd1Red = None; eOrd2bool = None; eOrd2int = None}
    in
    function
    | Ord1Red tv ->
	{e with eOrd1Red = Some (env_put.put_valu e.eOrd1Red tv)}
    | Ord2bool tv ->
	{e with eOrd2bool = Some (env_put.put_ord2 e.eOrd2bool tv)}
    | Ord2int tv ->
	{e with eOrd2int = Some (env_put.put_ord2 e.eOrd2int tv)}
  end;
  put_ord2 =
  begin fun e ->
    let e = match e with
      | Some e -> e
      | None -> {eOrd2Red = None}
    in
    function
    | Ord2Red tv ->
	{e with eOrd2Red = Some (env_put.put_ord1 e.eOrd2Red stages tv)}
  end;
}
;;

let env_put env =
  function
    | Tbool tv ->
	let v = env_put.put_valu env tv in
	{eBool = Some v; eInt = None}
    | Tint tv ->
	let v = env_put.put_valu env [Approx(id + env.stagevar,st)] tv in
	{eBool = None; eInt = Some v}
;;

(* similar function env_get, etc. *)


^ permalink raw reply	[flat|nested] 25+ messages in thread

end of thread, other threads:[~2008-05-12 22:16 UTC | newest]

Thread overview: 25+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-04-03 16:59 Polymorphic recursion Loup Vaillant
2007-04-03 17:20 ` [Caml-list] " Jeremy Yallop
2007-04-04  5:27   ` Alain Frisch
2007-04-04 12:54     ` Loup Vaillant
2007-04-03 17:35 ` Till Varoquaux
2007-04-03 20:00   ` brogoff
2007-04-04  1:27     ` skaller
2007-04-04  1:40       ` skaller
2007-04-04 13:49 ` Roland Zumkeller
2007-04-04 15:13   ` Alain Frisch
2007-04-04 15:20     ` Alain Frisch
2007-04-04 16:45       ` Roland Zumkeller
2007-04-04 19:58         ` Alain Frisch
2007-04-04 20:13           ` brogoff
2007-04-05  9:33           ` Roland Zumkeller
2007-04-05  9:54             ` Alain Frisch
2007-04-05 10:07               ` Daniel de Rauglaudre
2007-04-05  9:46           ` Francois Maurel
2007-04-04 15:50     ` Stefan Monnier
2007-04-04 23:36 ` [Caml-list] " Brian Hurt
2007-04-05  8:17   ` Loup Vaillant
  -- strict thread matches above, loose matches on Subject: below --
2008-05-12 21:55 polymorphic recursion Jacques Le Normand
2008-05-12 22:16 ` [Caml-list] " Christophe TROESTLER
2003-08-24 18:01 [Caml-list] Polymorphic recursion Lukasz Stafiniak
2003-08-25  0:30 ` Jacques Garrigue
2003-08-25  0:43   ` Jacques Garrigue

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).