src/Pure/sequence.ML
author wenzelm
Tue Aug 23 19:31:05 1994 +0200 (1994-08-23)
changeset 573 2fa5ef27bd0a
parent 0 a5a9c433f639
child 669 a6dd3796009d
permissions -rw-r--r--
removed constant _constrain from Pure sig;
clasohm@0
     1
(*  Title: 	sequence
clasohm@0
     2
    ID:         $Id$
clasohm@0
     3
    Author: 	Lawrence C Paulson, Cambridge University Computer Laboratory
clasohm@0
     4
    Copyright   1988  University of Cambridge
clasohm@0
     5
*)
clasohm@0
     6
clasohm@0
     7
(*Unbounded sequences implemented by closures.
clasohm@0
     8
clasohm@0
     9
  Could use   'a seq = Seq of ('a * (unit -> 'a seq)) option.
clasohm@0
    10
  Recomputes if sequence is re-inspected; memoing would need polymorphic refs.
clasohm@0
    11
*)
clasohm@0
    12
clasohm@0
    13
clasohm@0
    14
signature SEQUENCE = 
clasohm@0
    15
  sig
clasohm@0
    16
  type 'a seq
clasohm@0
    17
  val append : 'a seq * 'a seq -> 'a seq
clasohm@0
    18
  val chop: int * 'a seq -> 'a list * 'a seq
clasohm@0
    19
  val cons: 'a * 'a seq -> 'a seq
clasohm@0
    20
  val filters: ('a -> bool) -> 'a seq -> 'a seq
clasohm@0
    21
  val flats: 'a seq seq -> 'a seq
clasohm@0
    22
  val interleave : 'a seq * 'a seq -> 'a seq
clasohm@0
    23
  val its_right: ('a * 'b seq -> 'b seq) -> 'a seq * 'b seq -> 'b seq
clasohm@0
    24
  val list_of_s: 'a seq -> 'a list
clasohm@0
    25
  val mapp: ('a -> 'b) -> 'a seq -> 'b seq -> 'b seq
clasohm@0
    26
  val maps: ('a -> 'b) -> 'a seq -> 'b seq
clasohm@0
    27
  val null: 'a seq
clasohm@0
    28
  val prints: (int -> 'a -> unit) -> int -> 'a seq -> unit
clasohm@0
    29
  val pull: 'a seq -> ('a * 'a seq) option
clasohm@0
    30
  val s_of_list: 'a list -> 'a seq
clasohm@0
    31
  val seqof: (unit -> ('a * 'a seq) option) -> 'a seq
clasohm@0
    32
  val single: 'a -> 'a seq
clasohm@0
    33
  end;
clasohm@0
    34
clasohm@0
    35
clasohm@0
    36
functor SequenceFun () : SEQUENCE = 
clasohm@0
    37
struct
clasohm@0
    38
    
clasohm@0
    39
datatype 'a seq = Seq of unit -> ('a * 'a seq)option;
clasohm@0
    40
clasohm@0
    41
(*Return next sequence element as None or Some(x,str) *)
clasohm@0
    42
fun pull(Seq f) = f();
clasohm@0
    43
clasohm@0
    44
(*the abstraction for making a sequence*)
clasohm@0
    45
val seqof = Seq;
clasohm@0
    46
clasohm@0
    47
(*prefix an element to the sequence
clasohm@0
    48
    use cons(x,s) only if evaluation of s need not be delayed,
clasohm@0
    49
      otherwise use seqof(fn()=> Some(x,s)) *)
clasohm@0
    50
fun cons all = Seq(fn()=>Some all);
clasohm@0
    51
clasohm@0
    52
(*the empty sequence*)
clasohm@0
    53
val null = Seq(fn()=>None);
clasohm@0
    54
clasohm@0
    55
fun single(x) = cons (x, null);
clasohm@0
    56
clasohm@0
    57
(*The list of the first n elements, paired with rest of sequence.
clasohm@0
    58
  If length of list is less than n, then sequence had less than n elements.*)
clasohm@0
    59
fun chop (n:int, s: 'a seq): 'a list * 'a seq =
clasohm@0
    60
  if n<=0 then ([],s)
clasohm@0
    61
  else case pull(s) of
clasohm@0
    62
           None => ([],s)
clasohm@0
    63
         | Some(x,s') => let val (xs,s'') = chop (n-1,s')
clasohm@0
    64
		         in  (x::xs, s'')  end;
clasohm@0
    65
clasohm@0
    66
(*conversion from sequence to list*)
clasohm@0
    67
fun list_of_s (s: 'a seq) : 'a list = case pull(s) of
clasohm@0
    68
        None => []
clasohm@0
    69
      | Some(x,s') => x :: list_of_s s';
clasohm@0
    70
clasohm@0
    71
clasohm@0
    72
(*conversion from list to sequence*)
clasohm@0
    73
fun s_of_list []     = null
clasohm@0
    74
  | s_of_list (x::l) = cons (x, s_of_list l);
clasohm@0
    75
clasohm@0
    76
clasohm@0
    77
(*functional to print a sequence, up to "count" elements
clasohm@0
    78
  the function prelem should print the element number and also the element*)
clasohm@0
    79
fun prints (prelem: int -> 'a -> unit) count (s: 'a seq) : unit =
clasohm@0
    80
  let fun pr (k,s) = 
clasohm@0
    81
	   if k>count then ()
clasohm@0
    82
	   else case pull(s) of
clasohm@0
    83
	       None => ()
clasohm@0
    84
	     | Some(x,s') => (prelem k x;  prs"\n";  pr (k+1, s'))
clasohm@0
    85
  in  pr(1,s)  end;
clasohm@0
    86
clasohm@0
    87
(*Map the function f over the sequence, making a new sequence*)
clasohm@0
    88
fun maps f xq = seqof (fn()=> case pull(xq) of
clasohm@0
    89
        None       => None
clasohm@0
    90
      | Some(x,yq) => Some(f x,  maps f yq));
clasohm@0
    91
clasohm@0
    92
(*Sequence append:  put the elements of xq in front of those of yq*)
clasohm@0
    93
fun append (xq,yq)  =
clasohm@0
    94
  let fun copy xq = seqof (fn()=>
clasohm@0
    95
	    case pull xq of  
clasohm@0
    96
		 None       => pull yq
clasohm@0
    97
	       | Some(x,xq') => Some (x, copy xq'))
clasohm@0
    98
  in  copy xq end;
clasohm@0
    99
clasohm@0
   100
(*Interleave elements of xq with those of yq -- fairer than append*)
clasohm@0
   101
fun interleave (xq,yq) = seqof (fn()=>
clasohm@0
   102
      case pull(xq) of  
clasohm@0
   103
          None       => pull yq
clasohm@0
   104
	| Some(x,xq') => Some (x, interleave(yq, xq')));
clasohm@0
   105
clasohm@0
   106
(*map over a sequence xq, append the sequence yq*)
clasohm@0
   107
fun mapp f xq yq =
clasohm@0
   108
  let fun copy s = seqof (fn()=> 
clasohm@0
   109
            case pull(s) of
clasohm@0
   110
	        None       => pull(yq)
clasohm@0
   111
              | Some(x,xq') => Some(f x, copy xq'))
clasohm@0
   112
  in  copy xq end;
clasohm@0
   113
clasohm@0
   114
(*Flatten a sequence of sequences to a single sequence. *)
clasohm@0
   115
fun flats ss = seqof (fn()=> case pull(ss) of
clasohm@0
   116
        None => None
clasohm@0
   117
      | Some(s,ss') =>  pull(append(s, flats ss')));
clasohm@0
   118
clasohm@0
   119
(*accumulating a function over a sequence;  this is lazy*)
clasohm@0
   120
fun its_right (f: 'a * 'b seq -> 'b seq) (s: 'a seq, bstr: 'b seq) : 'b seq =
clasohm@0
   121
  let fun its s = seqof (fn()=>
clasohm@0
   122
            case pull(s) of
clasohm@0
   123
	        None       => pull bstr
clasohm@0
   124
	      | Some(a,s') => pull(f(a, its s')))
clasohm@0
   125
  in  its s end; 
clasohm@0
   126
clasohm@0
   127
fun filters pred xq =
clasohm@0
   128
  let fun copy s = seqof (fn()=> 
clasohm@0
   129
            case pull(s) of
clasohm@0
   130
	        None       => None
clasohm@0
   131
              | Some(x,xq') => if pred x then Some(x, copy xq')
clasohm@0
   132
			       else pull (copy xq') )
clasohm@0
   133
  in  copy xq end
clasohm@0
   134
clasohm@0
   135
end;