Commits

Dmitry Grebeniuk  committed ee24fa4

added LazyLabelled.Make().rec_data for tying knots

  • Participants
  • Parent commits 878c02d

Comments (0)

Files changed (2)

File lazyLabelled.ml

     value force : t 'a -> 'a
     ;
 
+    value rec_data : Label.t -> (t 'a -> 'a) -> 'a
+    ;
+
   end
  =
   struct
       ]
     ;
 
+    value (rec_data : Label.t -> (t 'a -> 'a) -> 'a) l func =
+      let rec lazy_self = { l = l ; v = lazy (func lazy_self) }
+      in
+        force lazy_self
+    ;
+
   end
 ;
 ;
 
 
-(* todo: lazy_rec_data -- to functor maybe. *)
 
 value closure func nodes =
   let lazy_res =
-    lazy_rec_data
+    Lazy.rec_data "closure_array"
       (fun lazy_self ->
+         (* let () = ignore & Lazy.force lazy_self
+            (* uncomment it to see "cycle contains: closure_array" *)
+            in
+         *)
          Array.map
            (fun n ->
              { Lazy.l = n.node_val.name
              ; v = lazy
                 (
-                let self = StdLazy.force lazy_self in
+                let self = Lazy.force lazy_self in
                 List.fold_left
                   (fun acc link ->
                      func acc (Lazy.force self.(link))
 ;
 
 
+value test_rec_data () =
+  try
+    Lazy.rec_data "Q"
+      (fun lazy_self ->
+         Lazy.force &
+         { Lazy.l = "W"
+         ; v = lazy (Lazy.force lazy_self)
+         }
+      )
+  with
+  [ Lazy.Cycle lst -> printf "cycle contains: %s.\n" &
+      String.concat ", " lst
+  ]
+;
+
+
+
+value () =
+  test_gen test_rec_data () "cycle contains: Q, W."
+;
+
+
+
 value test nodes exp =
   test_gen do_test nodes exp
 ;