Permalink
Browse files

Playing with lazy lists and gens.

  • Loading branch information...
rizo committed Sep 14, 2015
1 parent 1080b1c commit 67c13958aa07871a62ef910eddefcf1dc2ff13af
Showing with 185 additions and 6,797 deletions.
  1. +1 −1 _oasis
  2. +30 −4 playground/lazy_list.ml
  3. +81 −0 playground/lazy_monad_gen.ml
  4. +18 −13 playground/rec_monad.ml
  5. +31 −6,767 setup.ml
  6. +7 −1 src/elements.ml
  7. +17 −11 src/pipes.ml
View
2 _oasis
@@ -17,7 +17,7 @@ Description:
Library "iter"
Path: src
CompiledObject: best
Modules: Iter
Modules: Iter, Elements
Install: true
SourceRepository master
View
@@ -1,22 +1,48 @@
open Elements
type 'a node =
| Nil
| Cons of 'a * 'a lazy_list
and
'a lazy_list = 'a node lazy_t
let rec map f l =
lazy begin match Lazy.force l with
let rec map f ll =
lazy begin match Lazy.force ll with
| Nil -> Nil
| Cons (x, xs) -> Cons (f x, map f xs)
end
let rec take n l =
let rec take n ll =
lazy begin
if n = 0 then Nil
else match Lazy.force l with
else match Lazy.force ll with
| Nil -> Nil
| Cons (x, xs) -> Cons (x, (take (n - 1) xs))
end
let rec len ll =
let rec loop total ll =
match Lazy.force ll with
| Nil -> total
| Cons (_, xs) -> loop (total + 1) xs
in
loop 0 ll
let rec of_list l =
lazy begin match l with
| [] -> Nil
| x :: xs -> Cons (x, of_list xs)
end
let rec to_list ll =
match Lazy.force ll with
| Nil -> []
| Cons (x, xs) -> x :: to_list xs
let rec of_chan ch =
lazy begin match Exn.as_option End_of_file input_line ch with
| Some line -> Cons (line, of_chan ch)
| None -> Nil
end
@@ -0,0 +1,81 @@
open Elements
type 'a step =
| Stop
| Next of 'a * 'a gen
and 'a gen = 'a step lazy_t
let return x =
lazy (Next (x, lazy Stop))
(* val bind : 'a gen -> ('b -> 'b gen) -> 'b gen *)
let rec (>>=) m f =
match Lazy.force m with
| Stop -> lazy Stop
| Next (x, m') -> f x
let rec (>>|) m f =
m >>= fun x -> return (f x)
let (>>) m1 m2 =
m1 >>= fun () -> m2
let rec forever m = m >> forever m
let map f m = forever (m >>= fun x -> return (f x))
let rec fold f z g =
match Lazy.force g with
| Stop -> z
| Next (x, g') -> fold f (f z x) g'
let rec of_list l : 'a gen =
lazy begin match l with
| [] -> Stop
| x::xs -> Next (x, of_list xs)
end
let rec of_chan ch : string gen =
lazy begin
match Exn.as_option End_of_file input_line ch with
| None -> Stop
| Some l -> Next (l, of_chan ch)
end
let rec of_file filename : string gen =
let ch = open_in filename in
lazy begin
match Exn.as_option End_of_file input_line ch with
| None -> close_in ch; Stop
| Some l -> Next (l, of_chan ch)
end
let rec to_list gen =
match Lazy.force gen with
| Stop -> []
| Next (x, xs) -> x :: to_list xs
let len gen =
fold (fun n _ -> n + 1) 0 gen
let rec take n gen =
lazy begin
if n = 0 then Stop
else match Lazy.force gen with
| Stop -> Stop
| Next (x, xs) -> Next (x, (take (n - 1) xs))
end
let rec yes =
lazy (Next ("y", yes))
let count () =
let rec loop n =
lazy (Next (n, loop (n + 1))) in
loop 0
(* let count () = *)
(* let rec loop n = *)
(* yield n >> loop (n + 1) in *)
(* loop 0 *)
View
@@ -1,32 +1,37 @@
module type Gen = sig
type 'a t
type 'a gen
val return : 'a -> 'a t
val (>>=) : 'a t -> ('a -> 'b t) -> 'b t
val (>>) : unit t -> 'a t Lazy.t -> 'a t
val return : 'a -> 'a gen
val (>>=) : 'a gen -> ('a -> 'b gen) -> 'b gen
val (>>) : unit gen -> 'a gen Lazy.t -> 'a gen
val forever : unit t -> unit
val map : ('a -> 'b) -> 'a t -> 'b t
val yes : string t
val forever : unit gen -> unit
val map : ('a -> 'b) -> 'a gen -> 'b gen
val yes : string gen
val input : string t
val print : string t -> unit
val input : string gen
val print : string gen -> unit
end
module Gen : Gen = struct
type 'a t = unit -> 'a
type 'a gen = unit -> 'a option
let return x = fun () -> x
let return x = fun () -> Some x
let (>>=) m f = f (m ())
let (>>) ma mb = ma >>= fun _ -> Lazy.force mb
let rec forever x = x >> lazy (forever x)
let map f g = forever (fun () -> f (g ()))
let yes = return "yes"
let input = fun () -> input_line stdin
let input = fun () -> Some (input_line stdin)
let print = map print_endline
let of_list l =
fun () ->
match l with
| [] -> None
| x::xs -> Some x
end
module G = Gen
open Gen
Oops, something went wrong.

0 comments on commit 67c1395

Please sign in to comment.