Fix the build script.
[tatoo.git] / src / auto / eval.ml
index f3b81cb..4a27fa5 100644 (file)
 (***********************************************************************)
 
 (*
-  Time-stamp: <Last modified on 2013-03-05 18:35:19 CET by Kim Nguyen>
+  Time-stamp: <Last modified on 2013-03-15 18:32:22 CET by Kim Nguyen>
 *)
 
 INCLUDE "utils.ml"
 open Format
 open Utils
 
-module Make (T : Tree.Sig.S) = struct
+module Make (T : Tree.Sig.S) :
+  sig
+    val eval : Ata.t -> T.t -> T.node -> T.node list
+  end
+ = struct
 
-  type cache = (int, StateSet.t) Hashtbl.t
 
-  let get c t n =
-    try Hashtbl.find c (T.preorder t n)
-    with Not_found -> StateSet.empty
+IFDEF HTMLTRACE
+  THEN
+DEFINE TRACE(e) = (e)
+  ELSE
+DEFINE TRACE(e) = ()
+END
 
-  let set c t n v = Hashtbl.replace c (T.preorder t n) v
 
-  let eval_trans l ctx acc =
-    List.fold_left (fun (acct, accs) ((q, phi) as trs) ->
-      if StateSet.mem q accs then (acct, accs) else
-      if Ata.SFormula.eval ctx phi then
-        (acct, StateSet.add q accs)
-      else
-        (trs::acct, accs)
-    ) ([], acc) l
 
-  let top_down_run auto tree node cache i =
+  type cache = StateSet.t Cache.N1.t
+  let get c t n = Cache.N1.find c (T.preorder t n)
+
+  let set c t n v = Cache.N1.add c (T.preorder t n) v
+
+
+  let top_down_run auto tree node cache _i =
     let redo = ref false in
-    let rec loop node is_left =
+    let rec loop node =
       if node != T.nil then begin
         let parent = T.parent tree node in
         let fc = T.first_child tree node in
         let ns = T.next_sibling tree node in
-        let states0 = get cache tree node in
         let tag = T.tag tree node in
-        let trans0 = Ata.get_trans auto auto.Ata.states tag in
-        let parent_states = (*if parent == T.nil then auto.Ata.top_states else*) get cache tree parent in
-        let fc_states = (*if fc == T.nil then auto.Ata.bottom_states else*) get cache tree fc in
-        let ns_states = (*if ns == T.nil then auto.Ata.bottom_states else*) get cache tree ns in
-        let is_root = parent == T.nil in
-        let ctx0 =
-          if is_left then
-            Ata.make_ctx fc_states ns_states parent_states  StateSet.empty states0 is_left is_root
-          else
-            Ata.make_ctx fc_states ns_states StateSet.empty parent_states states0 is_left is_root
+        let states0 = get cache tree node in
+        let trans0 = Ata.get_trans auto tag auto.Ata.states in
+        let () =
+          TRACE(Html.trace (T.preorder tree node) _i "Pre States: %a<br/>Pre Trans: %a<br/>"
+                  StateSet.print states0 (Ata.TransList.print ~sep:"<br/>") trans0)
+        in
+        let ps = get cache tree parent in
+        let fcs = get cache tree fc in
+        let nss = get cache tree ns in
+        let is_left = node == T.first_child tree parent
+        and is_right = node == T.next_sibling tree parent
+        and has_left = fc != T.nil
+        and has_right = ns != T.nil
+        and kind = T.kind tree node
+        in
+        let trans1, states1 =
+          Ata.eval_trans auto trans0
+            fcs nss ps states0
+            is_left is_right has_left has_right kind
+        in
+        let () =
+          TRACE(Html.trace (T.preorder tree node) _i "TD States: %a<br/>TD Trans: %a<br/>" StateSet.print states1 (Ata.TransList.print ~sep:"<br/>") trans1)
         in
-        eprintf "-- [Iteration % 4d] --\n node: %a\n context: %a\n%!"
-          i T.print_node node Ata.print_ctx ctx0;
-        List.iter (fun (q, phi) -> eprintf " %a -> %a\n" State.print q Ata.SFormula.print phi) trans0;
-        eprintf "----------------------\n%!";
-        let trans1, states1 = eval_trans trans0 ctx0 StateSet.empty in
         if states1 != states0 then set cache tree node states1;
-        let () = loop fc true in
-        let ctx1 = {ctx0 with Ata.left = (get cache tree fc) ; Ata.epsilon = states1 } in
-        let trans2, states2 = eval_trans trans1 ctx1 states1 in
+        let () = loop fc in
+        let fcs1 = get cache tree fc in
+        let trans2, states2 =
+          Ata.eval_trans auto trans1
+            fcs1 nss ps states1
+            is_left is_right has_left has_right kind
+        in
+        let () =
+          TRACE(Html.trace (T.preorder tree node) _i "Left BU States: %a<br/>Left BU Trans: %a<br/>" StateSet.print states2 (Ata.TransList.print ~sep:"<br/>") trans2)
+        in
         if states2 != states1 then set cache tree node states2;
-        let () = loop ns false in
-        let ctx2 = { ctx1 with Ata.right = (get cache tree ns); Ata.epsilon = states2 } in
-        let _, states3 = eval_trans trans2 ctx2 states2 in
+        let () = loop ns in
+        let _trans3, states3 =
+          Ata.eval_trans auto trans2
+            fcs1 (get cache tree ns) ps states2
+            is_left is_right has_left has_right kind
+        in
+        let () =
+          TRACE(Html.trace (T.preorder tree node) _i "Right BU States: %a<br/>Right BU Trans: %a<br/>" StateSet.print states3 (Ata.TransList.print ~sep:"<br/>") _trans3)
+        in
         if states3 != states2 then set cache tree node states3;
         if states0 != states3 && (not !redo) then redo := true
       end
     in
-    loop node true;
+    loop node;
     !redo
 
   let get_results auto tree node cache =
@@ -95,13 +117,16 @@ module Make (T : Tree.Sig.S) = struct
     loop node []
 
   let eval auto tree node =
-    let cache = Hashtbl.create 511 in
+    let cache = Cache.N1.create StateSet.empty in
     let redo = ref true in
     let iter = ref 0 in
+    Ata.reset auto;
     while !redo do
       redo := top_down_run auto tree node cache !iter;
       incr iter;
     done;
-    get_results auto tree node cache
+    let r = get_results auto tree node cache in
+    TRACE(Html.gen_trace (module T : Tree.Sig.S with type t = T.t) (tree));
+    r
 
 end