* Union of polymorphic variants...
@ 2007-12-04 12:38 Stephane Glondu
2007-12-04 13:57 ` [Caml-list] " Jacques Garrigue
0 siblings, 1 reply; 5+ messages in thread
From: Stephane Glondu @ 2007-12-04 12:38 UTC (permalink / raw)
To: caml-list
Hi,
Why isn't possible to do the following?
type b = [`A of [`B of bool]]
type c = [`A of [`C of char]]
type a = [b|c]
I expect the last declaration to be equivalent to:
type a = [`A of [`B of bool | `C of char]]
Thanks,
--
Stephane Glondu
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] Union of polymorphic variants...
2007-12-04 12:38 Union of polymorphic variants Stephane Glondu
@ 2007-12-04 13:57 ` Jacques Garrigue
2007-12-04 14:33 ` Stephane Glondu
0 siblings, 1 reply; 5+ messages in thread
From: Jacques Garrigue @ 2007-12-04 13:57 UTC (permalink / raw)
To: steph; +Cc: caml-list
From: Stephane Glondu <steph@glondu.net>
> Why isn't possible to do the following?
>
> type b = [`A of [`B of bool]]
> type c = [`A of [`C of char]]
> type a = [b|c]
>
> I expect the last declaration to be equivalent to:
>
> type a = [`A of [`B of bool | `C of char]]
Because union of polymorphic variant is flat.
So the above requires the parameter of `A to be of type both
[`B of bool] and [`C of char].
The reason is that dispatch only looks at the head constructor:
let f = function
| #b as x -> fb x
| #c as x -> fc x
is actually
let f = function
| `A _ as x -> fb x
| `A _ as x -> fc x
which only types if both parameters have the same type.
Polymorphic variants are _not_ XML types.
Use ocamlduce for that :-)
Jacques
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] Union of polymorphic variants...
2007-12-04 13:57 ` [Caml-list] " Jacques Garrigue
@ 2007-12-04 14:33 ` Stephane Glondu
2007-12-05 2:17 ` Jacques GARRIGUE
0 siblings, 1 reply; 5+ messages in thread
From: Stephane Glondu @ 2007-12-04 14:33 UTC (permalink / raw)
To: Jacques Garrigue; +Cc: caml-list
Jacques Garrigue a écrit :
> [...] union of polymorphic variant is flat [...]
> The reason is that dispatch only looks at the head constructor:
>
> let f = function
> | #b as x -> fb x
> | #c as x -> fc x
> [...]
Actually, I was faced to the problem in a similar pattern-matching, and
I reduced the problem to the example in my post :-)
> [...] is actually
>
> let f = function
> | `A _ as x -> fb x
> | `A _ as x -> fc x
>
> which only types if both parameters have the same type.
However, one can do:
let f = function
| `A (`B _) -> ...
| `A (`C _) -> ..
Would it be difficult to extend the system so that #b and #c expand to
the constructions above?
> Polymorphic variants are _not_ XML types.
> Use ocamlduce for that :-)
I am having a look at it...
--
Stéphane Glondu
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] Union of polymorphic variants...
2007-12-04 14:33 ` Stephane Glondu
@ 2007-12-05 2:17 ` Jacques GARRIGUE
2007-12-05 15:59 ` Stephane Glondu
0 siblings, 1 reply; 5+ messages in thread
From: Jacques GARRIGUE @ 2007-12-05 2:17 UTC (permalink / raw)
To: steph; +Cc: caml-list
From: Stephane Glondu <steph@glondu.net>
> However, one can do:
>
> let f = function
> | `A (`B _) -> ...
> | `A (`C _) -> ..
>
> Would it be difficult to extend the system so that #b and #c expand to
> the constructions above?
Nothing is "difficult". But not everything is well-defined...
The question is where do we start, where do we stop.
type a = [`S of a | `Z]
should #a be (`S _ | `Z) or (`S (`S _ | `Z) | `Z) or ...?
type b = [`A of [`B] option]
should #b be (`A (None | Some `B)) ?
Generally, when such questions start to pop up, it is better to
forget about it. It could still be a good idea, but we need more
examples to see that.
Note that you can still build the types by hand
type 'a t = [`A of 'a]
type b = [`B of bool]
type c = [`C of char]
type a = [b|c] t
let f = function
`A #b as x -> fb x
| `A #c as x -> fc x
Jacques Garrigue
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] Union of polymorphic variants...
2007-12-05 2:17 ` Jacques GARRIGUE
@ 2007-12-05 15:59 ` Stephane Glondu
0 siblings, 0 replies; 5+ messages in thread
From: Stephane Glondu @ 2007-12-05 15:59 UTC (permalink / raw)
To: Jacques GARRIGUE; +Cc: caml-list
Jacques GARRIGUE a écrit :
> Nothing is "difficult". But not everything is well-defined...
> The question is where do we start, where do we stop.
>
> type a = [`S of a | `Z]
>
> should #a be (`S _ | `Z) or (`S (`S _ | `Z) | `Z) or ...?
OK, I grasp now the difficulty of the problem.
> Note that you can still build the types by hand
>
> type 'a t = [`A of 'a]
> type b = [`B of bool]
> type c = [`C of char]
> type a = [b|c] t
>
> let f = function
> `A #b as x -> fb x
> | `A #c as x -> fc x
I didn't know this trick.
Thanks,
--
Stéphane Glondu
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2007-12-05 15:59 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-12-04 12:38 Union of polymorphic variants Stephane Glondu
2007-12-04 13:57 ` [Caml-list] " Jacques Garrigue
2007-12-04 14:33 ` Stephane Glondu
2007-12-05 2:17 ` Jacques GARRIGUE
2007-12-05 15:59 ` Stephane Glondu
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox