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