From: Jiten Pathy <jpathy@fssrv.net>
To: Stephen Dolan <stephen.dolan@cl.cam.ac.uk>
Cc: Jeremy Yallop <yallop@gmail.com>,
"caml-list@inria.fr" <caml-list@inria.fr>
Subject: Re: [Caml-list] phantom type
Date: Mon, 27 Apr 2015 05:17:56 -0700 [thread overview]
Message-ID: <CAL2Z3DAga4ZzhCJga+jns3MXZh4g44ALd=JSgOeDQEf678zToQ@mail.gmail.com> (raw)
In-Reply-To: <CA+mHimOLF3fCgkwQ9ObwmfHexQzoqi75VzVHFJuueebjC56D2A@mail.gmail.com>
It seems this encoding using first-class modules has overheads in the
interpreter(due to the functor), whereas the gadt implementation would
have no overheads.
On Mon, Apr 27, 2015 at 4:51 AM, Stephen Dolan
<stephen.dolan@cl.cam.ac.uk> wrote:
> To expand a little on Jeremy's answer, you can encode the expr type as
> a module signature, and then pass terms around as first-class modules.
>
> First, you make a module signature that describes the ways of
> constructing a term:
>
> module type Ctors = sig
> type 'a term
> val zero : int term
> val succ : int term -> int term
> val iszero : int term -> bool term
> end
>
> Next, a Term is something which can construct a term using any set of Ctors:
>
> module type Term = sig
> type a
> module Build : functor (C : Ctors) -> sig
> val x : a C.term
> end
> end
>
> You can make a normal datatype from this using first-class modules:
>
> type 'a term = (module Term with type a = 'a)
>
> We're jumping between the module and the core language, so the
> wrapping and unwrapping makes things a bit verbose. Here's the
> value-level "succ" operation, the other two are similar:
>
> let succ ((module T) : int term) : int term =
> (module struct
> type a = int
> module Build = functor (C : Ctors) -> struct
> module TC = T.Build (C)
> let x = C.succ TC.x
> end
> end)
>
> Evaluation is one particular implementation of the term constructors,
> which implements the type 'a term as just 'a:
>
> module Eval = struct
> type 'a term = 'a
> let zero = 0
> let succ n = n + 1
> let iszero n = (n = 0)
> end
>
> Finally, you can use this evaluation module to interpret any term:
>
> let eval (type a) ((module T) : a term) : a =
> let module M = T.Build (Eval) in M.x
>
> Stephen
next prev parent reply other threads:[~2015-04-27 12:17 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-04-27 10:23 Jiten Pathy
2015-04-27 10:36 ` Jeremy Yallop
2015-04-27 11:32 ` Jiten Pathy
2015-04-27 11:51 ` Stephen Dolan
2015-04-27 12:17 ` Jiten Pathy [this message]
2015-04-27 12:30 ` Jeremy Yallop
2015-04-28 4:35 ` Jiten Pathy
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='CAL2Z3DAga4ZzhCJga+jns3MXZh4g44ALd=JSgOeDQEf678zToQ@mail.gmail.com' \
--to=jpathy@fssrv.net \
--cc=caml-list@inria.fr \
--cc=stephen.dolan@cl.cam.ac.uk \
--cc=yallop@gmail.com \
/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