From: Goswin von Brederlow <goswin-v-b@web.de>
To: caml-list@inria.fr
Subject: Re: [Caml-list] Type of term
Date: Mon, 10 Feb 2014 09:58:34 +0100 [thread overview]
Message-ID: <20140210085834.GC26593@frosties> (raw)
In-Reply-To: <52F3C710.4020500@gmail.com>
On Thu, Feb 06, 2014 at 05:32:00PM +0000, Mário José Parreira Pereira wrote:
> Hi Lukasz,
>
> Thank you for your answer but I really can't see how GADTs can help me.
>
> I was just simply wandering if there wasn't any OCaml function that
> would work like:
> type_of(let f x = x) = 'a->'a
>
> There is, something that would compute exactly the same outcome as
> the Damas-Milner algorithm W.
>
> Bests,
> Mário
>
> Em 06-02-2014 17:17, Lukasz Stafiniak escreveu:
> >On Thu, Feb 6, 2014 at 6:17 PM, Mário José Parreira Pereira
> ><mariojppereira@gmail.com <mailto:mariojppereira@gmail.com>>
> >wrote:
> >
> > Hi all,
> >
> > Is there any way to get the type of (part of) a program? Something
> > like:
> > type_of(M) = sigma
> > computing the type of program M as sigma so I can pattern match it.
> >
> >
> >No. However, if you really need this rather than being confused by
> >programming patterns from Java / C# / C++, you should learn about
> >GADTs.
> >http://caml.inria.fr/pub/docs/manual-ocaml-400/manual021.html#toc85
> >
> >Cheers.
# let f x = x;;
val f : 'a -> 'a = <fun>
# let g = (f : 'a -> 'a);;
val g : 'a -> 'a = <fun>
But this only unifies the types:
# let f x = x + 1;;
val f : int -> int = <fun>
# let g = (f : 'a -> 'a);;
val g : int -> int = <fun>
To test for equality you need to use a module with signature (or .mli file):
# module M : sig val g : 'a -> 'a end = struct let g = f end;;
Error: Signature mismatch:
Modules do not match:
sig val g : int -> int end
is not included in
sig val g : 'a -> 'a end
Values do not match:
val g : int -> int
is not included in
val g : 'a -> 'a
If you don't want a static compile time check then GADTs are the way
to go. With GADTs you can construct runtime type informations, pass
them as values and match on them. But you would have to construct the
GADTs for every function you may want to type_of() or use a
preprocessor module to do so implicitly. I think there is also a ocaml
branch that adds such type values implicitly in the compiler.
MfG
Goswin
next prev parent reply other threads:[~2014-02-10 8:59 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-02-06 17:17 Mário José Parreira Pereira
2014-02-06 17:17 ` Lukasz Stafiniak
2014-02-06 17:32 ` Mário José Parreira Pereira
2014-02-06 17:45 ` Raphaël Proust
2014-02-06 19:01 ` Gabriel Scherer
2014-02-06 19:05 ` Simon Cruanes
2014-02-10 8:58 ` Goswin von Brederlow [this message]
2014-02-06 17:23 ` Lukasz Stafiniak
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=20140210085834.GC26593@frosties \
--to=goswin-v-b@web.de \
--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