Example of Tywith Usage
The following type definition:
type 'a tree =
| Leaf of 'a
| Node of 'a tree * 'a tree
with string_of, map, fold
Will generate the following functions:
let rec map_tree f_a =
function
Leaf p1 -> Leaf (f_a p1)
| Node (p1, p2) -> Node (map_tree f_a p1, map_tree f_a p2)
and fold_tree of_Leaf of_Node =
let rec_fold x = fold_tree of_Leaf of_Node x in
function
Leaf p1 -> of_Leaf ((fun x -> x) p1)
| Node (p1, p2) -> of_Node (rec_fold p1) (rec_fold p2)
and string_of_tree of__a =
function
Leaf p1 -> "Leaf (" ^ of__a p1 ^ ")"
| Node (p1, p2) ->
"Node (" ^ (string_of_tree of__a p1 ^ "," ^ string_of_tree of__a p2) ^
")"
and parse_tree parse__a strm =
match Tywith.parse_id strm with
"Leaf" ->
let v0 = Tywith.ParserTools.sec (Tywith.parse_spec "(") parse__a strm in
let _ = Tywith.parse_spec ")" strm in Leaf v0
| "Node" ->
let v0 =
Tywith.ParserTools.sec (Tywith.parse_spec "(") (parse_tree parse__a)
strm
in
let v1 =
Tywith.ParserTools.sec (Tywith.parse_spec ",") (parse_tree parse__a)
strm
in
let _ = Tywith.parse_spec ")" strm in Node (v0, v1)
| _ -> assert false
With the following signatures:
val map_tree : ('a -> 'b) -> 'a tree -> 'b tree
val fold_tree : ('a -> 'b) -> ('b -> 'b -> 'b) -> 'a tree -> 'b
val string_of_tree : ('a -> string) -> 'a tree -> string
val parse_tree : (char Stream.t -> 'a) -> char Stream.t -> 'a tree