1 (* Copyright (C
) 2013,2017 Matthew Fluet
.
2 * Copyright (C
) 1999-2006 Henry Cejtin
, Matthew Fluet
, Suresh
3 * Jagannathan
, and Stephen Weeks
.
5 * MLton is released under a BSD
-style license
.
6 * See the file MLton
-LICENSE for details
.
9 functor Tree (S
: TREE_STRUCTS
): TREE
=
14 datatype 'a t
= T
of 'a
* 'a t Seq
.t
16 fun children (T (_
, v
)) = v
18 fun foldPre (T (a
, v
), b
, f
) =
19 Seq
.fold (v
, f (a
, b
), fn (t
, b
) => foldPre (t
, b
, f
))
21 fun foldPost (T (a
, v
), b
, f
) =
22 f (a
, Seq
.fold (v
, b
, fn (t
, b
) => foldPost (t
, b
, f
)))
29 val _
= Seq
.foreach (v
, loop
)
38 fun foreachPre (t
, f
: 'a
-> unit
) = traverse (t
, fn a
=> (f a
; fn () => ()))
39 fun foreachPost (t
, f
) = traverse (t
, fn a
=> fn () => f a
)
42 fun mkLayoutDot
{nodeOptions
: 'a
-> Dot
.NodeOption
.t list
,
46 fun loopTree (next
, nodes
, T (x
, ts
)) =
52 options
= nodeOptions x
,
53 successors
= loopForest (next
, nodes
, ts
)})
57 and loopForest (next
, nodes
, ts
) =
58 rev (Seq
.fold (ts
, [], fn (t
, ac
) =>
59 {name
= loopTree (next
, nodes
, t
),
61 fun wrap (loop
, arg
) =
64 fun next () = concat
["n", Int.toString (Counter
.next c
)]
66 val _
= loop (next
, nodes
, arg
)
68 Dot
.layout
{nodes
= !nodes
,
73 {layoutDotTree
= fn t
=> wrap (loopTree
, t
),
74 layoutDotForest
= fn ts
=> wrap (loopForest
, ts
)}
77 fun layoutDotTree (t
, opts
) = (#
layoutDotTree (mkLayoutDot opts
)) t
78 fun layoutDotForest (ts
, opts
) = (#
layoutDotForest (mkLayoutDot opts
)) ts
80 val layoutDot
= layoutDotTree
86 fun layoutTree (T (x
, ts
)) =
87 paren (seq
[lay x
, str
", ", layoutForest ts
])
89 Seq
.layout (ts
, layoutTree
)
91 {layoutTree
= layoutTree
, layoutForest
= layoutForest
}
94 fun layoutTree (t
, lay
) = (#
layoutTree (mkLayout lay
)) t
95 fun layoutForest (ts
, lay
) = (#
layoutForest (mkLayout lay
)) ts
97 val layout
= layoutTree
99 fun map (T (a
, ts
), f
) = T (f a
, Seq
.map (ts
, fn t
=> map (t
, f
)))
103 type 'a t
= 'a t Seq
.t
104 val layoutDot
= layoutDotForest
105 val layout
= layoutForest
110 structure Tree
= Tree (structure Seq
=
114 fun layout (v
, l
) = Vector.layout l v