caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Stephen Dolan <stephen.dolan@cl.cam.ac.uk>
To: Jiten Pathy <jpathy@fssrv.net>
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 12:51:11 +0100	[thread overview]
Message-ID: <CA+mHimOLF3fCgkwQ9ObwmfHexQzoqi75VzVHFJuueebjC56D2A@mail.gmail.com> (raw)
In-Reply-To: <CAAxsn=GiUeB17MWJVaLcu-=2E2kzP3R2cmdHR_BiEPb1Q1gu+g@mail.gmail.com>

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

  parent reply	other threads:[~2015-04-27 11:51 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 [this message]
2015-04-27 12:17     ` Jiten Pathy
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=CA+mHimOLF3fCgkwQ9ObwmfHexQzoqi75VzVHFJuueebjC56D2A@mail.gmail.com \
    --to=stephen.dolan@cl.cam.ac.uk \
    --cc=caml-list@inria.fr \
    --cc=jpathy@fssrv.net \
    --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;
as well as URLs for NNTP newsgroup(s).