-
Notifications
You must be signed in to change notification settings - Fork 482
Expand file tree
/
Copy pathUnison.res
More file actions
39 lines (32 loc) · 938 Bytes
/
Unison.res
File metadata and controls
39 lines (32 loc) · 938 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Exmple of several DCE checks operating in unison
type break_ =
| IfNeed
| Never
| Always
type t = {
break_: break_,
doc: string,
}
type rec stack =
| Empty
| Cons(t, stack)
let group = (~break_=IfNeed, doc) => {break_, doc: doc}
let rec fits = (w, stack) =>
switch stack {
| _ if w < 0 => false
| Empty => true
| Cons({doc}, stack) => fits(w - String.length(doc), stack)
}
let rec toString = (~width, stack) =>
switch stack {
| Cons({break_, doc}, stack) =>
switch break_ {
| IfNeed => (fits(width, stack) ? "fits " : "no ") ++ (stack |> toString(~width=width - 1))
| Never => "never " ++ (doc ++ (stack |> toString(~width=width - 1)))
| Always => "always " ++ (doc ++ (stack |> toString(~width=width - 1)))
}
| Empty => ""
}
toString(~width=80, Empty)
toString(~width=80, Cons(group(~break_=Never, "abc"), Empty))
toString(~width=80, Cons(group(~break_=Always, "d"), Empty))