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