From: Goswin von Brederlow <goswin-v-b@web.de>
To: caml-list@yquem.inria.fr
Subject: [Caml-list] GADT example for universal list
Date: Fri, 23 Mar 2012 02:07:47 +0100 [thread overview]
Message-ID: <87k42ci0po.fsf@frosties.localnet> (raw)
In-Reply-To: <87aa3mawg1.fsf@frosties.localnet> (Goswin von Brederlow's message of "Mon, 12 Mar 2012 12:30:06 +0100")
Hi,
I played some more with GADTs, this time with an universal list. Well,
not truely universal as you can't store arbitrary types. Only Int and
Float are allowed here:
type _ kind = (* used for searching in the list *)
| Int_kind : int kind
| Float_kind : float kind
type value = (* box the values so we have runtime type information *)
| Int of int
| Float of float
type list = (* the universal list *)
| Nil : list
| Cons : value * list -> list
(* find the first value in the list of a given kind *)
let rec get : type a . list -> a kind -> a = fun l k ->
match (k, l) with
| (_, Nil) -> raise Not_found
| (Int_kind, Cons (Int x, xs)) -> x
| (Float_kind, Cons (Float x, xs)) -> x
| (_, Cons (_, xs)) -> get xs k
(* print out list *)
let rec print = function
| Nil -> print_newline ()
| Cons ((Int x), xs) -> Printf.printf "%d " x; print xs
| Cons ((Float x), xs) -> Printf.printf "%f " x; print xs
(* testing *)
let empty = Nil
let l1 = Cons ((Int 1), empty)
let l2 = Cons ((Float 2.), l1)
let () = print l2
let i = get l2 Int_kind
let f = get l2 Float_kind;;
(*
2.000000 1
type _ kind = Int_kind : int kind | Float_kind : float kind
type value = Int of int | Float of float
type list = Nil : list | Cons : value * list -> list
val get : list -> 'a kind -> 'a = <fun>
val print : list -> unit = <fun>
val empty : list = Nil
val l1 : list = Cons (Int 1, Nil)
val l2 : list = Cons (Float 2., Cons (Int 1, Nil))
val i : int = 1
val f : float = 2.
*)
At first glance you might think: Why does that need GADTs? Why not
simply use
type value = Int of int | Float of float
for this?
Take a close look at the get funktion:
val get : list -> 'a kind -> 'a = <fun>
It does not return a value but directly the unboxed type. Because the
value is unboxed you can directly use it and ocaml will detect if you
screw up the type like in:
let () = Printf.printf "%d\n" (get l2 Float_kind);;
Error: This expression has type float but an expression was expected
of type int
MfG
Goswin
prev parent reply other threads:[~2012-03-23 1:07 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-03-12 11:30 [Caml-list] Wanted: GADT examples: string length, counting module x Goswin von Brederlow
2012-03-19 14:43 ` Gabriel Scherer
2012-03-19 16:13 ` Jesper Louis Andersen
2012-03-19 16:22 ` Raoul Duke
2012-03-21 9:48 ` Goswin von Brederlow
2012-03-23 1:07 ` Goswin von Brederlow [this message]
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=87k42ci0po.fsf@frosties.localnet \
--to=goswin-v-b@web.de \
--cc=caml-list@yquem.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