anchor.diff:      | s -> s
   
   type IterateeCPS<'el,'a,'r when 'el : equality> =
  -  Iteratee of (((Stream<'el> -> IterateeCPS<'el,'a,'r>) -> 'r) -> (exn -> 'r) -> ('a -> Stream<'el> -> 'r) -> 'r)
  +  Iteratee of (('a -> Stream<'el> -> 'r) -> ((Stream<'el> -> IterateeCPS<'el,'a,'r>) -> exn option -> 'r) -> 'r)
   
   type EnumeratorCPS<'el,'a,'r when 'el : equality> =
     IterateeCPS<'el,'a,IterateeCPS<'el,'a,'r>> -> IterateeCPS<'el,'a,'r>
  
positive.code:   member this.Zero() = returnI ()
    member this.Combine(comp1, comp2) = bind comp1 (fun () -> comp2)
    member this.Delay(f) = bind (returnI ()) f
    member this.TryWith(m, h) = catchError h m
    member this.TryFinally(m, compensation) = tryFinally compensation m
    member this.Using(res:#IDisposable, body) =
      this.TryFinally(body res, (fun () -> match res with null -> () | disp -> disp.Dispose()))
    member this.While(guard, m) =
      if not(guard()) then this.Zero() else
        this.Bind(m, (fun () -> this.While(guard, m)))
    member this.For(sequence:#seq<_>, body) =
      this.Using(sequence.GetEnumerator(),
                 (fun enum -> this.While(enum.MoveNext, this.Delay(fun () -> body enum.Current))))
  let iterateeCPS = IterateeCPSBuilder()
  
  module List =
  
    let enumPure1Chunk str i =
      let rec onDone a str' =
        match str' with
        | Chunk str' -> doneI a (Chunk (str @ str'))
        | _ -> doneI a (Chunk str)
      and onCont k = runIter (k (Chunk str)) onCont onError onDone
      and onError e = throw e
      in runIter i onCont onError onDone
  
    let enumChunk = function
      | Empty -> enumPure1Chunk []
      | Chunk xs -> enumPure1Chunk xs
      | EOF None -> enumEOF
      | EOF (Some e) -> enumErr e
  
  //  let rec enumList input i =
  //    match input with
  //    | [] -> i
  //    | _ -> let rec onCont l k =
  //             match l with
  //             | x::xs -> enumList xs <| k (Chunk x)
  //             | _ -> contI k
  //           in runIter i (onCont input) throw doneI
  
    let streamToList<'a,'b when 'a : equality> : IterateeCPS<'a list,'a list,'b> =
      let rec step acc str =
        match str with
        | Empty | Chunk [] -> liftI (step acc)
        | Chunk ls -> liftI (step (acc @ ls))
        | str -> doneI acc str
      liftI (step [])
    
negative.code: 
  [<AutoOpen>]
  module Primitives =
    let runIter (Iteratee i) onDone onCont = i onDone onCont
    let doneI x str = Iteratee <| fun onDone _ -> onDone x str
    let contI k e = Iteratee <| fun _ onCont -> onCont k e
    let liftI k = Iteratee <| fun _ onCont -> onCont k None
    let rec fmap f m =
      Iteratee <| fun onDone onCont ->
        let od = onDone << f
        let oc k e = onCont (fun s -> fmap f (k s)) e
        runIter m od oc
  
    let returnI x = Iteratee <| fun onDone _ -> onDone x Empty
    let bind (m: IterateeCPS<'el,'a,'r>) (f: 'a -> IterateeCPS<'el,'b,'r>) : IterateeCPS<'el,'b,'r> =
      let rec inner m f =
        Iteratee <| fun onDone onCont ->
          let mdone a s =
            let fcont k = function
              | None -> runIter (k s) onDone onCont
              | Some e -> onCont k (Some e) 
            in match s with
               | Empty -> runIter (f a) onDone onCont
               | _ -> runIter (f a) (fun x _ -> onDone x s) fcont
          in runIter m mdone (fun k e -> onCont (fun s -> inner (k s) f) e)
      inner m f
  
    let inline (>>=) m f = bind m f
    let inline (<*>) f m = f >>= fun f' -> fmap f' m
  
    let run i =
      let rec onDone x _ = x
      and onCont k = function
        | None -> runIter (k (EOF None)) onDone onCont'
        | Some e -> raise e
      and onCont' k = function
        | None -> failwith "divergent iteratee"
        | Some e -> raise e
      in runIter i onDone onCont
  
    let tryRun i =
      let rec onDone x _ = Choice1Of2 x
      and onCont k = function
        | None -> runIter (k (EOF None)) onDone onCont'
        | Some e -> Choice2Of2 e
      and onCont' k = function
        | None -> Choice2Of2 (Exception("divergent iteratee"))
        | Some e -> Choice2Of2 e
      in runIter i onDone onCont
  
    let either f g = function
      | Choice1Of2 x -> f x
      | Choice2Of2 y -> g y
      
    let rec lift f i =
      Iteratee <| fun onDone onCont ->
        let od a str = Choice1Of2(a,str)
        let oc k e = Choice2Of2(lift f << k, e)
        f (runIter i od oc) >>= either (fun (a,b) -> onDone a b) (fun (k,e) -> onCont k e)
    
    let rec throw e = contI (fun _ -> throw e) (Some e)
    let throwRecoverable e i = contI i (Some e)
    let rec checkErr i =
      Iteratee <| fun onDone onCont ->
        let od = onDone << Choice2Of2
        let oc k = function
          | None -> onCont (checkErr << k) None
          | Some e -> onDone (Choice1Of2 e) Empty
        runIter i od oc
  
    let identity<'a when 'a : equality> = doneI () (Empty:Stream<'a>)
    let skipToEof<'a when 'a : equality> =
      let rec loop() =
        let check = function
          | Chunk _ -> loop() 
          | s -> doneI () (s:Stream<'a>)
        in contI check None
      loop ()
  
    let joinI outer = bind outer (fun inner ->
      Iteratee <| fun onDone onCont ->
        let od x _ = onDone x Empty
        let rec oc k = function
          | None -> runIter (k (EOF None)) od oc'
          | Some e -> runIter (throw e) onDone onCont
        and oc' _ e = runIter (throw (Exception("divergent iteratee"))) onDone onCont
        runIter inner od oc)
  
  //  let enumEOF i =
  //    let rec onDone x _ = doneI x (EOF None)
  //    and onCont k = function
  //      | None -> runIter (k (EOF None)) onDone onCont'
  //      | Some e -> contI k e
  //    and onCont' k = function
  //      | None -> throw (Exception("divergent iteratee"))
  //      | Some e -> contI k e
  //    in runIter i onDone onCont
  //
  //  let enumErr e i =
  //    let rec onDone x _ = doneI x (EOF (Some e))
  //    and onCont k = function
  //      | None -> runIter (k (EOF (Some e))) onDone onCont'
  //      | Some e' -> contI k e'
  //    and onCont' k = function
  //      | None -> throw (Exception("divergent iteratee"))
  //      | Some e' -> contI k e'
  //    in runIter i onDone onCont
  
  type IterateeCPSBuilder() =
    member this.Return(x) = returnI x
  