caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] phantom type
@ 2015-04-27 10:23 Jiten Pathy
  2015-04-27 10:36 ` Jeremy Yallop
  0 siblings, 1 reply; 7+ messages in thread
From: Jiten Pathy @ 2015-04-27 10:23 UTC (permalink / raw)
  To: caml-list

Hello,
Trying to use phantom types instead of gadt for well-constructed term
example. Is it possible to define an evaluator eval : 'a term -> 'a
using phantom types? Something like following doesn't work.

type expr = Zero | Succ of expr | Iszero of expr;;
type 'a term = Expr of expr;;

(* ways to construct a term *)
let zero : int term = Expr Zero;;
let succ : int term -> int term = fun (Expr x) -> Expr (Succ x);;
let iszero : int term -> bool term = fun (Expr x) -> Expr (Iszero x);;

let rec eval : 'a.'a term -> 'a = fun (type a) (t : a term) -> match t with
| Expr Zero -> 0
| Expr (Succ e) -> eval (Expr e) + 1
| Expr (Iszero e) -> if (eval (Expr e) == 0) then true else false
;;

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

end of thread, other threads:[~2015-04-28  4:35 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-04-27 10:23 [Caml-list] phantom type 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
2015-04-27 12:30       ` Jeremy Yallop
2015-04-28  4:35         ` Jiten Pathy

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