caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] OR-patterns with GADTs
@ 2014-11-21 18:39 Ashish Agarwal
  2014-11-21 21:04 ` Leo White
  0 siblings, 1 reply; 3+ messages in thread
From: Ashish Agarwal @ 2014-11-21 18:39 UTC (permalink / raw)
  To: Caml List

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

The following works fine:

type foo
type bar
type _ t =
| Foo : string -> foo t
| Bar : string -> bar t

let to_string : type a . a t -> string = function
  | Foo x -> x
  | Bar x -> x


However, if you try to avoid the redundant code of the two branches, you
get a compile error:

let to_string : type a . a t -> string = function
  | Foo x
  | Bar x -> x

Error: This pattern matches values of type foo t
       but a pattern was expected which matches values of type a t
       Type foo is not compatible with type a

Is there a real reason for this?

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

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

* Re: [Caml-list] OR-patterns with GADTs
  2014-11-21 18:39 [Caml-list] OR-patterns with GADTs Ashish Agarwal
@ 2014-11-21 21:04 ` Leo White
  2014-11-22 12:44   ` Ashish Agarwal
  0 siblings, 1 reply; 3+ messages in thread
From: Leo White @ 2014-11-21 21:04 UTC (permalink / raw)
  To: Ashish Agarwal; +Cc: Caml List

Ashish Agarwal <agarwal1975@gmail.com> writes:

> The following works fine:
>
> type foo
> type bar
> type _ t =
> | Foo : string -> foo t
> | Bar : string -> bar t
>
> let to_string : type a . a t -> string = function
>   | Foo x -> x
>   | Bar x -> x
>
> However, if you try to avoid the redundant code of the two branches, you get a compile error:
>
> let to_string : type a . a t -> string = function
>   | Foo x
>   | Bar x -> x
>
> Error: This pattern matches values of type foo t
>        but a pattern was expected which matches values of type a t
>        Type foo is not compatible with type a
>
> Is there a real reason for this?

I think the reason is that full support for or-patterns with GADTs is
difficult. Matching on one GADT pattern generates one set of equations
and matching on the other generates a different set of equations and you
then need to check the body under the (parts of the) equations that are
in both patterns. There might be something simpler than full support
which wouldn't be that hard, but its not clear what that would be, so at
the moment things just fail if you use GADTs in an or-pattern.

There is a Mantis issue for it
(http://caml.inria.fr/mantis/view.php?id=5736), so hopefully it will be
resolved one day.

Regards,

Leo

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

* Re: [Caml-list] OR-patterns with GADTs
  2014-11-21 21:04 ` Leo White
@ 2014-11-22 12:44   ` Ashish Agarwal
  0 siblings, 0 replies; 3+ messages in thread
From: Ashish Agarwal @ 2014-11-22 12:44 UTC (permalink / raw)
  To: Leo White; +Cc: Caml List

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

Thanks for the link to the Mantis issue.

On Fri, Nov 21, 2014 at 4:04 PM, Leo White <lpw25@cam.ac.uk> wrote:

> Ashish Agarwal <agarwal1975@gmail.com> writes:
>
> > The following works fine:
> >
> > type foo
> > type bar
> > type _ t =
> > | Foo : string -> foo t
> > | Bar : string -> bar t
> >
> > let to_string : type a . a t -> string = function
> >   | Foo x -> x
> >   | Bar x -> x
> >
> > However, if you try to avoid the redundant code of the two branches, you
> get a compile error:
> >
> > let to_string : type a . a t -> string = function
> >   | Foo x
> >   | Bar x -> x
> >
> > Error: This pattern matches values of type foo t
> >        but a pattern was expected which matches values of type a t
> >        Type foo is not compatible with type a
> >
> > Is there a real reason for this?
>
> I think the reason is that full support for or-patterns with GADTs is
> difficult. Matching on one GADT pattern generates one set of equations
> and matching on the other generates a different set of equations and you
> then need to check the body under the (parts of the) equations that are
> in both patterns. There might be something simpler than full support
> which wouldn't be that hard, but its not clear what that would be, so at
> the moment things just fail if you use GADTs in an or-pattern.
>
> There is a Mantis issue for it
> (http://caml.inria.fr/mantis/view.php?id=5736), so hopefully it will be
> resolved one day.
>
> Regards,
>
> Leo
>

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

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

end of thread, other threads:[~2014-11-22 12:44 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-11-21 18:39 [Caml-list] OR-patterns with GADTs Ashish Agarwal
2014-11-21 21:04 ` Leo White
2014-11-22 12:44   ` Ashish Agarwal

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