From: Rodolphe Lepigre <rodolphe.lepigre@univ-savoie.fr>
To: Caml List <caml-list@inria.fr>
Subject: [Caml-list] GADT magic
Date: Sat, 3 Oct 2015 09:02:26 +0200 [thread overview]
Message-ID: <20151003070226.GC22748@HPArchRod> (raw)
Dear list,
I am using a GADT and some phantom types to implement an abstract syntax
tree for some functional programming language, say the λ-calculus.
My first type definition was something like:
==========
type valu =
| LVar of string
| LAbs of valu -> term
and term =
| Valu of valu
| Appl of term * term
==========
This works fine, but it is annoying to work with because of the coercion
constructor "Valu". Then I decided to be to smarter and use some GADT and
phantom types:
==========
type v
type t
type _ expr =
| LVar : string -> 'a expr
| LAbs : (v expr -> 'b expr) -> 'a expr
| Appl : 'a expr * 'b expr -> t expr
type term = t expr
type valu = v expr
==========
This definition is much more convenient since the type "valu" is (kind of)
a subtype of the type "term". It is not a real subtype since I had to
define a function with the type:
==========
val expr_to_term : type a. a expr -> term
==========
This function is exactly the identity function, but for the type checker
to accept it I need to be quite explicit:
==========
let expr_to_term : type a. a expr -> term = function
| LVar(x) -> LVar(x)
| LAbs(f) -> LAbs(f)
| Appl(t,u) -> Appl(t,u)
==========
Of course, a better and more efficient implementation for this function
is "Obj.magic", but this is not the question here. I was expecting the
following definition to work:
==========
let expr_to_term : type a. a expr -> term = function
| LVar(x) -> LVar(x)
| LAbs(f) -> LAbs(f)
| t -> t
==========
But it does not type check. This is weird because all remaining patterns
captured by the "t" have type "term"...
Is this normal behaviour? Is this a bug?
Rodolphe
--
Rodolphe Lepigre
LAMA, Université Savoie Mont Blanc, FRANCE
http://lama.univ-smb.fr/~lepigre/
next reply other threads:[~2015-10-03 7:02 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-10-03 7:02 Rodolphe Lepigre [this message]
2015-10-03 8:54 ` Gabriel Scherer
2015-10-05 1:27 ` Jacques Garrigue
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20151003070226.GC22748@HPArchRod \
--to=rodolphe.lepigre@univ-savoie.fr \
--cc=caml-list@inria.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox