Practical Generic Programming in Ocaml.pdf

(240 KB) Pobierz
Practical Generic Programming with OCaml
Jeremy Yallop
LFCS, University of Edinburgh
ML Workshop 2007
Instead of this . . .
type
α
tree = Node
of
α
| Branch
of
(α tree)
×
(α tree)
val
show_tree : (α
string)
(α tree
string)
let rec
show_tree show_a =
function
Node a -> "Node " ^ show_a a
| Branch (l, r) -> "Branch ("^ show_tree l ^ ","
^ show_tree r ^ ")"
show_list (show_pair (show_tree show_int) show_bool) t
You can write this!
type
α
tree = Node
of
α
| Branch
of
(α tree)
×
(α tree)
deriving
(Show)
Show.show<(int tree * bool) list> t
Outline
Basic idea
Customization
More customization: pickling
Conclusions
Haskell type classes as OCaml modules
1
class
Show a
where
show :: a
String
module type
Show =
sig
type
a
val
show : a
string
end
Type class as signature
1
Dreyer, Harper, Chakravarty and Keller.
Modular Type Classes
(POPL 07)
Zgłoś jeśli naruszono regulamin