Home     About     Download     Resources     Contact us

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at ocaml.org.

Browse thread
Oddness with recursive polymorphic variants
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
 Date: -- (:) From: Michael Wohlwend Subject: Re: [Caml-list] Oddness with recursive polymorphic variants
```On Thursday 04 May 2006 19:10, Luc Maranget wrote:
> > I have two polymorphic variant types, as follows:
> >
> >   type f = [`A | `B of f]
> >   type g = [f | `C]
> >
> > Next, I have a function from f to g:
> >
> >   let s1 : f -> g = function
> >
> >     | `A -> `A
> >     | `B b -> b
> >
> > Sadly, the compiler rejects this:
> >
> >     Characters 57-58:
> >       | `B b -> b;;
> >

that may be too complicated, but it works :-)

type 'a f = [`A | `B of 'a];;
type 'a g = ['a f | `C];;

let s1 (p : 'a f) : 'a g = match p with
`A -> `A
| `B b -> b;;

# s1 (`B (`B `A));;
- : 'a g as 'a = `B `A

Michael

--
C offers you enough rope to hang yourself.
C++ offers a fully equipped firing squad, a last cigarette and
a blindfold.

```