-
Notifications
You must be signed in to change notification settings - Fork 4
/
util.sml
210 lines (178 loc) · 5.6 KB
/
util.sml
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
(* Generally useful utilities, primarily used for emitting code *)
signature UTIL =
sig
structure Sum2 : sig
datatype ('a, 'b) t = In1 of 'a | In2 of 'b
end
structure Sum3 : sig
datatype ('a, 'b, 'c) t = In1 of 'a | In2 of 'b | In3 of 'c
end
structure Sum4 : sig
datatype ('a, 'b, 'c, 'd) t = In1 of 'a | In2 of 'b | In3 of 'c | In4 of 'd
end
val >> : unit * 'a -> 'a
(* Stateful; set or unset the indent *)
val emit : string list -> unit
val incr : unit -> unit
val decr : unit -> unit
val flush : unit -> unit
(* Write: output to a particular stream *)
val write : TextIO.outstream -> (unit -> unit) -> unit
(* Useful list functions missing form the sml standard library. *)
val mapi : (int * 'a -> 'b) -> 'a list -> 'b list
val intify : 'a list -> (int * 'a) list
val foldli : (int * 'a * 'b -> 'b) -> 'b -> 'a list -> 'b
val mapfilter : ('a -> 'b option) -> 'a list -> 'b list
val concatWith : 'a list -> 'a list list -> 'a list
val unzip3 : ('a * 'b * 'c) list -> 'a list * 'b list * 'c list
(* Application utility functions *)
val transSuper : (* Analogy: String.translate *)
(unit -> 'c list) (* There are no items *)
-> ('a -> 'c list) (* There is one item *)
-> ('a -> 'c list) * ('a -> 'c list) * ('a -> 'c list) (* F,M,L *)
-> 'a list (* List *)
-> 'c list
val appSuper :
(unit -> unit) (* There are no items *)
-> ('a -> unit) (* There is one item *)
-> ('a -> unit) * ('a -> unit) * ('a -> unit) (* First, middle, last *)
-> 'a list (* List *)
-> unit
val transFirst :
(unit -> 'c list)
-> ('pre * 'a -> 'c list)
-> ('pre * 'pre)
-> 'a list
-> 'c list
val appFirst :
(unit -> unit) (* Function if there are no items *)
-> ('a * 'b -> unit) (* Function for each item *)
-> ('a * 'a) (* Data for first item, data for other items *)
-> 'b list (* List of items *)
-> unit
val interleave : 'a -> 'a list -> 'a list
end
structure Util :> UTIL =
struct
structure Sum2 = struct
datatype ('a, 'b) t = In1 of 'a | In2 of 'b
end
structure Sum3 = struct
datatype ('a, 'b, 'c) t = In1 of 'a | In2 of 'b | In3 of 'c
end
structure Sum4 = struct
datatype ('a, 'b, 'c, 'd) t = In1 of 'a | In2 of 'b | In3 of 'c | In4 of 'd
end
fun >> (_, e) = e
fun mapi' f n [] alloc = rev alloc
| mapi' f n (x :: xs) alloc = mapi' f (n+1) xs (f (n, x) :: alloc)
fun mapi f xs = mapi' f 0 xs []
fun intify xs = mapi (fn x => x) xs
fun foldli f e l =
#2 (List.foldl (fn (a, (i, b)) => (i + 1, f (i, a, b))) (0, e) l)
fun mapfilter f l =
List.foldr
(fn (x, acc) =>
case f x of
NONE => acc
| SOME y => y :: acc)
[] l
fun concatWith l ll =
case List.filter (fn [] => false | _::_ => true) ll of
[] => []
| [x] => x
| x::xs =>
List.rev
(List.foldl
(fn (x, acc) => List.revAppend (x, revAppend (l, acc)))
(List.rev x) xs)
fun unzip3 l =
case l of
[] => ([], [], [])
| (x, y, z)::xs =>
let val (xs, ys, zs) = unzip3 xs
in (x::xs, y::ys, z::zs)
end
fun transSuper none one (first, middle, last) xs =
let
fun loop [ x ] accum = List.concat (rev (last x :: accum))
| loop (x :: xs) accum = loop xs (middle x :: accum)
in case xs of
[] => none ()
| [ x ] => one x
| x :: xs => loop xs [first x]
end
fun appSuper none one (first: 'a -> unit, middle: 'a -> unit, last) xs: unit =
let
(*[ val loop: ('a conslist) -> unit ]*)
fun loop [ x ] = last x
| loop (x :: (xs as _ :: _)) = (middle x; loop xs)
in case xs of
[] => none ()
| [ x ] => one x
| x :: (xs as _ :: _) => (first x; loop xs)
end
fun transFirst none some (first, rest) =
let
fun first' x = some (first, x)
fun rest' x = some (rest, x)
in
transSuper none first' (first', rest', rest')
end
fun appFirst none some (first, rest) =
let
fun first' x = some (first, x)
fun rest' x = some (rest, x)
in
appSuper none first' (first', rest', rest')
end
local
val outstream: TextIO.outstream option ref =
(ref NONE (*[ <: TextIO.outstream option ref ]*))
val ind = ref ""
in
fun emit1 s =
let
val s' =
case s of
"" => "\n"
| _ => !ind ^ s ^ "\n"
in
case !outstream of
NONE => print s'
| SOME stream => TextIO.output (stream, s')
end
val emit = app emit1
fun flush () =
case !outstream of
NONE => TextIO.flushOut TextIO.stdOut
| SOME s => TextIO.flushOut s
fun incr () = ind := !ind ^ " "
fun decr () =
case !ind of
"" => raise Fail "Unbalanced increment/decrement"
| s => ind := String.extract (!ind, 2, NONE)
fun write stream (f: unit -> unit) =
case !outstream of
NONE =>
let in
( outstream := SOME stream
; f ()
; outstream := NONE)
handle exn => (outstream := NONE; raise exn)
end
| SOME _ => raise Fail "Nested calls to write"
fun interleave x l =
let
fun interleave' l =
case l of
[y] => [x, y]
| y::ys => x :: y :: interleave' ys
in
case l of
[] => []
| [y] => [y]
| y::ys => y :: interleave' ys
end
end
end