src/Pure/General/seq.ML
author wenzelm
Fri Dec 14 11:52:54 2001 +0100 (2001-12-14)
changeset 12498 3b0091bf06e8
parent 11721 0d60622b668f
child 12851 e87496286934
permissions -rw-r--r--
changed Thm.varifyT';
     1 (*  Title:      Pure/General/seq.ML
     2     ID:         $Id$
     3     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
     4     Author:   	Markus Wenzel, TU Munich
     5     License:    GPL (GNU GENERAL PUBLIC LICENSE)
     6 
     7 Unbounded sequences implemented by closures.  RECOMPUTES if sequence
     8 is re-inspected.  Memoing, using polymorphic refs, was found to be
     9 slower!  (More GCs)
    10 *)
    11 
    12 signature SEQ =
    13 sig
    14   type 'a seq
    15   val make: (unit -> ('a * 'a seq) option) -> 'a seq
    16   val pull: 'a seq -> ('a * 'a seq) option
    17   val empty: 'a seq
    18   val cons: 'a * 'a seq -> 'a seq
    19   val single: 'a -> 'a seq
    20   val try: ('a -> 'b) -> 'a -> 'b seq
    21   val hd: 'a seq -> 'a
    22   val tl: 'a seq -> 'a seq
    23   val chop: int * 'a seq -> 'a list * 'a seq
    24   val list_of: 'a seq -> 'a list
    25   val of_list: 'a list -> 'a seq
    26   val map: ('a -> 'b) -> 'a seq -> 'b seq
    27   val mapp: ('a -> 'b) -> 'a seq -> 'b seq -> 'b seq
    28   val append: 'a seq * 'a seq -> 'a seq
    29   val filter: ('a -> bool) -> 'a seq -> 'a seq
    30   val flat: 'a seq seq -> 'a seq
    31   val interleave: 'a seq * 'a seq -> 'a seq
    32   val print: (int -> 'a -> unit) -> int -> 'a seq -> unit
    33   val it_right : ('a * 'b seq -> 'b seq) -> 'a seq * 'b seq -> 'b seq
    34   val commute: 'a seq list -> 'a list seq
    35   val succeed: 'a -> 'a seq
    36   val fail: 'a -> 'b seq
    37   val THEN: ('a -> 'b seq) * ('b -> 'c seq) -> 'a -> 'c seq
    38   val ORELSE: ('a -> 'b seq) * ('a -> 'b seq) -> 'a -> 'b seq
    39   val APPEND: ('a -> 'b seq) * ('a -> 'b seq) -> 'a -> 'b seq
    40   val EVERY: ('a -> 'a seq) list -> 'a -> 'a seq
    41   val FIRST: ('a -> 'b seq) list -> 'a -> 'b seq
    42   val TRY: ('a -> 'a seq) -> 'a -> 'a seq
    43   val REPEAT: ('a -> 'a seq) -> 'a -> 'a seq
    44   val REPEAT1: ('a -> 'a seq) -> 'a -> 'a seq
    45   val INTERVAL: (int -> 'a -> 'a seq) -> int -> int -> 'a -> 'a seq
    46 end;
    47 
    48 structure Seq: SEQ =
    49 struct
    50 
    51 
    52 (** lazy sequences **)
    53 
    54 datatype 'a seq = Seq of unit -> ('a * 'a seq) option;
    55 
    56 (*the abstraction for making a sequence*)
    57 val make = Seq;
    58 
    59 (*return next sequence element as None or Some (x, xq)*)
    60 fun pull (Seq f) = f ();
    61 
    62 
    63 (*the empty sequence*)
    64 val empty = Seq (fn () => None);
    65 
    66 (*prefix an element to the sequence -- use cons (x, xq) only if
    67   evaluation of xq need not be delayed, otherwise use
    68   make (fn () => Some (x, xq))*)
    69 fun cons x_xq = make (fn () => Some x_xq);
    70 
    71 fun single x = cons (x, empty);
    72 
    73 (*head and tail -- beware of calling the sequence function twice!!*)
    74 fun hd xq = #1 (the (pull xq))
    75 and tl xq = #2 (the (pull xq));
    76 
    77 (*partial function as procedure*)
    78 fun try f x =
    79   (case Library.try f x of
    80     Some y => single y
    81   | None => empty);
    82 
    83 
    84 (*the list of the first n elements, paired with rest of sequence;
    85   if length of list is less than n, then sequence had less than n elements*)
    86 fun chop (n, xq) =
    87   if n <= 0 then ([], xq)
    88   else
    89     (case pull xq of
    90       None => ([], xq)
    91     | Some (x, xq') => apfst (Library.cons x) (chop (n - 1, xq')));
    92 
    93 (*conversion from sequence to list*)
    94 fun list_of xq =
    95   (case pull xq of
    96     None => []
    97   | Some (x, xq') => x :: list_of xq');
    98 
    99 (*conversion from list to sequence*)
   100 fun of_list xs = foldr cons (xs, empty);
   101 
   102 
   103 (*map the function f over the sequence, making a new sequence*)
   104 fun map f xq =
   105   make (fn () =>
   106     (case pull xq of
   107       None => None
   108     | Some (x, xq') => Some (f x, map f xq')));
   109 
   110 (*map over a sequence xq, append the sequence yq*)
   111 fun mapp f xq yq =
   112   let
   113     fun copy s =
   114       make (fn () =>
   115         (case pull s of
   116           None => pull yq
   117         | Some (x, s') => Some (f x, copy s')))
   118   in copy xq end;
   119 
   120 (*sequence append:  put the elements of xq in front of those of yq*)
   121 fun append (xq, yq) =
   122   let
   123     fun copy s =
   124       make (fn () =>
   125         (case pull s of
   126           None => pull yq
   127         | Some (x, s') => Some (x, copy s')))
   128   in copy xq end;
   129 
   130 (*filter sequence by predicate*)
   131 fun filter pred xq =
   132   let
   133     fun copy s =
   134       make (fn () =>
   135         (case pull s of
   136           None => None
   137         | Some (x, s') => if pred x then Some (x, copy s') else pull (copy s')));
   138   in copy xq end;
   139 
   140 (*flatten a sequence of sequences to a single sequence*)
   141 fun flat xqq =
   142   make (fn () =>
   143     (case pull xqq of
   144       None => None
   145     | Some (xq, xqq') => pull (append (xq, flat xqq'))));
   146 
   147 (*interleave elements of xq with those of yq -- fairer than append*)
   148 fun interleave (xq, yq) =
   149   make (fn () =>
   150     (case pull xq of
   151       None => pull yq
   152     | Some (x, xq') => Some (x, interleave (yq, xq'))));
   153 
   154 
   155 (*functional to print a sequence, up to "count" elements;
   156   the function prelem should print the element number and also the element*)
   157 fun print prelem count seq =
   158   let
   159     fun pr (k, xq) =
   160       if k > count then ()
   161       else
   162         (case pull xq of
   163           None => ()
   164         | Some (x, xq') => (prelem k x; writeln ""; pr (k + 1, xq')))
   165   in pr (1, seq) end;
   166 
   167 (*accumulating a function over a sequence; this is lazy*)
   168 fun it_right f (xq, yq) =
   169   let
   170     fun its s =
   171       make (fn () =>
   172         (case pull s of
   173           None => pull yq
   174         | Some (a, s') => pull (f (a, its s'))))
   175   in its xq end;
   176 
   177 (*turn a list of sequences into a sequence of lists*)
   178 fun commute [] = single []
   179   | commute (xq :: xqs) =
   180       make (fn () =>
   181         (case pull xq of
   182           None => None
   183         | Some (x, xq') =>
   184             (case pull (commute xqs) of
   185               None => None
   186             | Some (xs, xsq) =>
   187                 Some (x :: xs, append (map (Library.cons x) xsq, commute (xq' :: xqs))))));
   188 
   189 
   190 
   191 (** sequence functions **)      (*some code duplicated from Pure/tctical.ML*)
   192 
   193 fun succeed x = single x;
   194 fun fail _ = empty;
   195 
   196 
   197 fun op THEN (f, g) x = flat (map g (f x));
   198 
   199 fun op ORELSE (f, g) x =
   200   (case pull (f x) of
   201     None => g x
   202   | some => make (fn () => some));
   203 
   204 fun op APPEND (f, g) x =
   205   append (f x, make (fn () => pull (g x)));
   206 
   207 
   208 fun EVERY fs = foldr THEN (fs, succeed);
   209 fun FIRST fs = foldr ORELSE (fs, fail);
   210 
   211 
   212 fun TRY f = ORELSE (f, succeed);
   213 
   214 fun REPEAT f =
   215   let
   216     fun rep qs x =
   217       (case pull (f x) of
   218         None => Some (x, make (fn () => repq qs))
   219       | Some (x', q) => rep (q :: qs) x')
   220     and repq [] = None
   221       | repq (q :: qs) =
   222           (case pull q of
   223             None => repq qs
   224           | Some (x, q) => rep (q :: qs) x);
   225   in fn x => make (fn () => rep [] x) end;
   226 
   227 fun REPEAT1 f = THEN (f, REPEAT f);
   228 
   229 
   230 fun INTERVAL f i j x =
   231   if i > j then single x
   232   else op THEN (f j, INTERVAL f i (j - 1)) x;
   233 
   234 
   235 end;