| author | wenzelm | 
| Wed, 08 Jun 2011 22:16:21 +0200 | |
| changeset 43328 | 10d731b06ed7 | 
| parent 32195 | d77476e4040c | 
| child 47060 | e2741ec9ae36 | 
| permissions | -rw-r--r-- | 
| 6118 | 1 | (* Title: Pure/General/seq.ML | 
| 5014 | 2 | Author: Lawrence C Paulson, Cambridge University Computer Laboratory | 
| 12851 | 3 | Author: Markus Wenzel, TU Munich | 
| 5014 | 4 | |
| 5 | Unbounded sequences implemented by closures. RECOMPUTES if sequence | |
| 6 | is re-inspected. Memoing, using polymorphic refs, was found to be | |
| 7 | slower! (More GCs) | |
| 8 | *) | |
| 9 | ||
| 10 | signature SEQ = | |
| 11 | sig | |
| 12 | type 'a seq | |
| 13 |   val make: (unit -> ('a * 'a seq) option) -> 'a seq
 | |
| 14 |   val pull: 'a seq -> ('a * 'a seq) option
 | |
| 15 | val empty: 'a seq | |
| 19475 | 16 | val cons: 'a -> 'a seq -> 'a seq | 
| 5014 | 17 | val single: 'a -> 'a seq | 
| 11721 | 18 |   val try: ('a -> 'b) -> 'a -> 'b seq
 | 
| 5014 | 19 | val hd: 'a seq -> 'a | 
| 20 | val tl: 'a seq -> 'a seq | |
| 19865 | 21 | val chop: int -> 'a seq -> 'a list * 'a seq | 
| 29857 
2cc976ed8a3c
FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
 kleing parents: 
29606diff
changeset | 22 | val take: int -> 'a seq -> 'a seq | 
| 5014 | 23 | val list_of: 'a seq -> 'a list | 
| 24 | val of_list: 'a list -> 'a seq | |
| 19865 | 25 | val append: 'a seq -> 'a seq -> 'a seq | 
| 5014 | 26 |   val mapp: ('a -> 'b) -> 'a seq -> 'b seq -> 'b seq
 | 
| 17347 | 27 | val interleave: 'a seq * 'a seq -> 'a seq | 
| 5014 | 28 |   val filter: ('a -> bool) -> 'a seq -> 'a seq
 | 
| 29 | val flat: 'a seq seq -> 'a seq | |
| 17347 | 30 |   val map: ('a -> 'b) -> 'a seq -> 'b seq
 | 
| 31 |   val maps: ('a -> 'b seq) -> 'a seq -> 'b seq
 | |
| 19484 | 32 |   val map_filter: ('a -> 'b option) -> 'a seq -> 'b seq
 | 
| 17347 | 33 |   val lift: ('a -> 'b -> 'c) -> 'a seq -> 'b -> 'c seq
 | 
| 34 |   val lifts: ('a -> 'b -> 'c seq) -> 'a seq -> 'b -> 'c seq
 | |
| 19912 | 35 |   val singleton: ('a list -> 'b list seq) -> 'a -> 'b seq
 | 
| 5014 | 36 | val print: (int -> 'a -> unit) -> int -> 'a seq -> unit | 
| 37 |   val it_right : ('a * 'b seq -> 'b seq) -> 'a seq * 'b seq -> 'b seq
 | |
| 38 | val succeed: 'a -> 'a seq | |
| 39 | val fail: 'a -> 'b seq | |
| 40 |   val THEN: ('a -> 'b seq) * ('b -> 'c seq) -> 'a -> 'c seq
 | |
| 41 |   val ORELSE: ('a -> 'b seq) * ('a -> 'b seq) -> 'a -> 'b seq
 | |
| 42 |   val APPEND: ('a -> 'b seq) * ('a -> 'b seq) -> 'a -> 'b seq
 | |
| 43 |   val EVERY: ('a -> 'a seq) list -> 'a -> 'a seq
 | |
| 44 |   val FIRST: ('a -> 'b seq) list -> 'a -> 'b seq
 | |
| 45 |   val TRY: ('a -> 'a seq) -> 'a -> 'a seq
 | |
| 46 |   val REPEAT: ('a -> 'a seq) -> 'a -> 'a seq
 | |
| 5558 | 47 |   val REPEAT1: ('a -> 'a seq) -> 'a -> 'a seq
 | 
| 8535 | 48 | val INTERVAL: (int -> 'a -> 'a seq) -> int -> int -> 'a -> 'a seq | 
| 12851 | 49 |   val DETERM: ('a -> 'b seq) -> 'a -> 'b seq
 | 
| 5014 | 50 | end; | 
| 51 | ||
| 52 | structure Seq: SEQ = | |
| 53 | struct | |
| 54 | ||
| 55 | ||
| 56 | (** lazy sequences **) | |
| 57 | ||
| 58 | datatype 'a seq = Seq of unit -> ('a * 'a seq) option;
 | |
| 59 | ||
| 60 | (*the abstraction for making a sequence*) | |
| 61 | val make = Seq; | |
| 62 | ||
| 15531 | 63 | (*return next sequence element as NONE or SOME (x, xq)*) | 
| 5014 | 64 | fun pull (Seq f) = f (); | 
| 65 | ||
| 66 | ||
| 67 | (*the empty sequence*) | |
| 15531 | 68 | val empty = Seq (fn () => NONE); | 
| 5014 | 69 | |
| 70 | (*prefix an element to the sequence -- use cons (x, xq) only if | |
| 71 | evaluation of xq need not be delayed, otherwise use | |
| 15531 | 72 | make (fn () => SOME (x, xq))*) | 
| 19475 | 73 | fun cons x xq = make (fn () => SOME (x, xq)); | 
| 5014 | 74 | |
| 19475 | 75 | fun single x = cons x empty; | 
| 5014 | 76 | |
| 77 | (*head and tail -- beware of calling the sequence function twice!!*) | |
| 16002 | 78 | fun hd xq = #1 (the (pull xq)) | 
| 79 | and tl xq = #2 (the (pull xq)); | |
| 5014 | 80 | |
| 11721 | 81 | (*partial function as procedure*) | 
| 82 | fun try f x = | |
| 21395 
f34ac19659ae
moved some fundamental concepts to General/basics.ML;
 wenzelm parents: 
21270diff
changeset | 83 | (case Basics.try f x of | 
| 15531 | 84 | SOME y => single y | 
| 85 | | NONE => empty); | |
| 11721 | 86 | |
| 5014 | 87 | |
| 88 | (*the list of the first n elements, paired with rest of sequence; | |
| 89 | if length of list is less than n, then sequence had less than n elements*) | |
| 19865 | 90 | fun chop n xq = | 
| 29897 | 91 | if n <= (0 : int) then ([], xq) | 
| 5014 | 92 | else | 
| 93 | (case pull xq of | |
| 15531 | 94 | NONE => ([], xq) | 
| 21395 
f34ac19659ae
moved some fundamental concepts to General/basics.ML;
 wenzelm parents: 
21270diff
changeset | 95 | | SOME (x, xq') => apfst (Basics.cons x) (chop (n - 1) xq')); | 
| 5014 | 96 | |
| 29897 | 97 | (*truncate the sequence after n elements*) | 
| 98 | fun take n xq = | |
| 99 | if n <= (0 : int) then empty | |
| 100 | else make (fn () => | |
| 101 | (Option.map o apsnd) (take (n - 1)) (pull xq)); | |
| 29857 
2cc976ed8a3c
FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
 kleing parents: 
29606diff
changeset | 102 | |
| 5014 | 103 | (*conversion from sequence to list*) | 
| 104 | fun list_of xq = | |
| 105 | (case pull xq of | |
| 15531 | 106 | NONE => [] | 
| 107 | | SOME (x, xq') => x :: list_of xq'); | |
| 5014 | 108 | |
| 109 | (*conversion from list to sequence*) | |
| 19475 | 110 | fun of_list xs = fold_rev cons xs empty; | 
| 5014 | 111 | |
| 112 | ||
| 17347 | 113 | (*sequence append: put the elements of xq in front of those of yq*) | 
| 19865 | 114 | fun append xq yq = | 
| 17347 | 115 | let | 
| 116 | fun copy s = | |
| 117 | make (fn () => | |
| 118 | (case pull s of | |
| 119 | NONE => pull yq | |
| 120 | | SOME (x, s') => SOME (x, copy s'))) | |
| 121 | in copy xq end; | |
| 5014 | 122 | |
| 123 | (*map over a sequence xq, append the sequence yq*) | |
| 124 | fun mapp f xq yq = | |
| 125 | let | |
| 126 | fun copy s = | |
| 127 | make (fn () => | |
| 128 | (case pull s of | |
| 15531 | 129 | NONE => pull yq | 
| 130 | | SOME (x, s') => SOME (f x, copy s'))) | |
| 5014 | 131 | in copy xq end; | 
| 132 | ||
| 17347 | 133 | (*interleave elements of xq with those of yq -- fairer than append*) | 
| 134 | fun interleave (xq, yq) = | |
| 135 | make (fn () => | |
| 136 | (case pull xq of | |
| 137 | NONE => pull yq | |
| 138 | | SOME (x, xq') => SOME (x, interleave (yq, xq')))); | |
| 5014 | 139 | |
| 140 | (*filter sequence by predicate*) | |
| 141 | fun filter pred xq = | |
| 142 | let | |
| 143 | fun copy s = | |
| 144 | make (fn () => | |
| 145 | (case pull s of | |
| 15531 | 146 | NONE => NONE | 
| 147 | | SOME (x, s') => if pred x then SOME (x, copy s') else pull (copy s'))); | |
| 5014 | 148 | in copy xq end; | 
| 149 | ||
| 150 | (*flatten a sequence of sequences to a single sequence*) | |
| 151 | fun flat xqq = | |
| 152 | make (fn () => | |
| 153 | (case pull xqq of | |
| 15531 | 154 | NONE => NONE | 
| 19865 | 155 | | SOME (xq, xqq') => pull (append xq (flat xqq')))); | 
| 5014 | 156 | |
| 17347 | 157 | (*map the function f over the sequence, making a new sequence*) | 
| 158 | fun map f xq = | |
| 5014 | 159 | make (fn () => | 
| 160 | (case pull xq of | |
| 17347 | 161 | NONE => NONE | 
| 162 | | SOME (x, xq') => SOME (f x, map f xq'))); | |
| 163 | ||
| 164 | fun maps f = flat o map f; | |
| 19484 | 165 | fun map_filter f = maps (fn x => (case f x of NONE => empty | SOME y => single y)); | 
| 17347 | 166 | |
| 167 | fun lift f xq y = map (fn x => f x y) xq; | |
| 168 | fun lifts f xq y = maps (fn x => f x y) xq; | |
| 5014 | 169 | |
| 19912 | 170 | fun singleton f x = f [x] |> map (fn [y] => y | _ => raise Empty); | 
| 171 | ||
| 16534 | 172 | (*print a sequence, up to "count" elements*) | 
| 173 | fun print print_elem count = | |
| 5014 | 174 | let | 
| 26887 | 175 | fun prnt (k: int) xq = | 
| 5014 | 176 | if k > count then () | 
| 177 | else | |
| 178 | (case pull xq of | |
| 15531 | 179 | NONE => () | 
| 16534 | 180 | | SOME (x, xq') => (print_elem k x; writeln ""; prnt (k + 1) xq')); | 
| 181 | in prnt 1 end; | |
| 5014 | 182 | |
| 183 | (*accumulating a function over a sequence; this is lazy*) | |
| 184 | fun it_right f (xq, yq) = | |
| 185 | let | |
| 186 | fun its s = | |
| 187 | make (fn () => | |
| 188 | (case pull s of | |
| 15531 | 189 | NONE => pull yq | 
| 190 | | SOME (a, s') => pull (f (a, its s')))) | |
| 5014 | 191 | in its xq end; | 
| 192 | ||
| 193 | ||
| 194 | ||
| 32169 | 195 | (** sequence functions **) (*cf. Pure/tactical.ML*) | 
| 5014 | 196 | |
| 197 | fun succeed x = single x; | |
| 198 | fun fail _ = empty; | |
| 199 | ||
| 21270 | 200 | fun op THEN (f, g) x = maps g (f x); | 
| 5014 | 201 | |
| 5864 | 202 | fun op ORELSE (f, g) x = | 
| 5014 | 203 | (case pull (f x) of | 
| 15531 | 204 | NONE => g x | 
| 5014 | 205 | | some => make (fn () => some)); | 
| 206 | ||
| 5864 | 207 | fun op APPEND (f, g) x = | 
| 19865 | 208 | append (f x) (make (fn () => pull (g x))); | 
| 5014 | 209 | |
| 23178 | 210 | fun EVERY fs = fold_rev (curry op THEN) fs succeed; | 
| 211 | fun FIRST fs = fold_rev (curry op ORELSE) fs fail; | |
| 5014 | 212 | |
| 213 | fun TRY f = ORELSE (f, succeed); | |
| 214 | ||
| 215 | fun REPEAT f = | |
| 216 | let | |
| 217 | fun rep qs x = | |
| 218 | (case pull (f x) of | |
| 15531 | 219 | NONE => SOME (x, make (fn () => repq qs)) | 
| 220 | | SOME (x', q) => rep (q :: qs) x') | |
| 221 | and repq [] = NONE | |
| 5014 | 222 | | repq (q :: qs) = | 
| 223 | (case pull q of | |
| 15531 | 224 | NONE => repq qs | 
| 225 | | SOME (x, q) => rep (q :: qs) x); | |
| 5014 | 226 | in fn x => make (fn () => rep [] x) end; | 
| 227 | ||
| 5558 | 228 | fun REPEAT1 f = THEN (f, REPEAT f); | 
| 229 | ||
| 26887 | 230 | fun INTERVAL f (i: int) j x = | 
| 8535 | 231 | if i > j then single x | 
| 232 | else op THEN (f j, INTERVAL f i (j - 1)) x; | |
| 233 | ||
| 12851 | 234 | fun DETERM f x = | 
| 235 | (case pull (f x) of | |
| 15531 | 236 | NONE => empty | 
| 19475 | 237 | | SOME (x', _) => cons x' empty); | 
| 8535 | 238 | |
| 5014 | 239 | end; |