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