temp commit.
[tatoo.git] / src / eval.ml
1 (***********************************************************************)
2 (*                                                                     *)
3 (*                               TAToo                                 *)
4 (*                                                                     *)
5 (*                     Kim Nguyen, LRI UMR8623                         *)
6 (*                   Université Paris-Sud & CNRS                       *)
7 (*                                                                     *)
8 (*  Copyright 2010-2013 Université Paris-Sud and Centre National de la *)
9 (*  Recherche Scientifique. All rights reserved.  This file is         *)
10 (*  distributed under the terms of the GNU Lesser General Public       *)
11 (*  License, with the special exception on linking described in file   *)
12 (*  ../LICENSE.                                                        *)
13 (*                                                                     *)
14 (***********************************************************************)
15
16 (*
17   Time-stamp: <Last modified on 2013-04-24 21:54:43 CEST by Kim Nguyen>
18 *)
19
20 INCLUDE "utils.ml"
21 open Format
22
23 module Make (T : Tree.S) :
24   sig
25     val eval : Ata.t -> T.t -> T.node -> T.node list
26   end
27  = struct
28
29
30 IFDEF HTMLTRACE
31   THEN
32 DEFINE TRACE(e) = (e)
33   ELSE
34 DEFINE TRACE(e) = ()
35 END
36
37    let html tree node i config msg =
38      let config = config.Ata.Config.node in
39      let oldi = config.Ata.round in
40      Html.trace (T.preorder tree node) i oldi
41        "%s<br/>sat: %a<br/>unsat: %a<br/>todo: %around: %i<br/>unstable_subtree: %b<br/>"
42        msg
43        StateSet.print config.Ata.sat
44        StateSet.print config.Ata.unsat
45        (Ata.TransList.print ~sep:"<br/>") config.Ata.todo oldi config.Ata.unstable_subtree
46
47
48   type cache = StateSet.t Cache.N1.t
49   let get c t n = Cache.N1.find c (T.preorder t n)
50
51   let set c t n v i =
52     v.Ata.Config.node.Ata.round <- i;
53     Cache.N1.add c (T.preorder t n) v
54
55
56   let top_down_run auto tree node cache _i =
57     let rec loop node =
58       if node == T.nil then false else begin
59         let parent = T.parent tree node in
60         let fc = T.first_child tree node in
61         let ns = T.next_sibling tree node in
62         let tag = T.tag tree node in
63         let config0 =
64           let c = get cache tree node in
65           if c == Cache.N1.dummy cache then
66            Ata.Config.make
67              { c.Ata.Config.node with
68                Ata.todo = Ata.get_trans auto tag auto.Ata.states;
69                summary = Ata.node_summary
70                  (node == T.first_child tree parent) (* is_left *)
71                  (node == T.next_sibling tree parent) (* is_right *)
72                  (fc != T.nil) (* has_left *)
73                  (ns != T.nil) (* has_right *)
74                  (T.kind tree node) (* kind *)
75              }
76           else c
77         in
78
79         TRACE(html tree node _i config0 "Entering node");
80
81         let ps = get cache tree parent in
82         let fcs = get cache tree fc in
83         let nss = get cache tree ns in
84         let config1 = Ata.eval_trans auto fcs nss ps config0 in
85
86         TRACE(html tree node _i config1 "Updating transitions");
87
88         if config0 != config1 then set cache tree node config1 _i;
89         if not Ata.(fcs.Config.node.unstable_subtree) then
90           Printf.eprintf "Could skip left subtree!%!";
91         let unstable_left = Ata.(fcs.Config.node.unstable_subtree) && loop fc in
92         let fcs1 = get cache tree fc in
93         let config2 = Ata.eval_trans auto fcs1 nss ps config1 in
94
95         TRACE(html tree node _i config2 "Updating transitions (after first-child)");
96
97         if config1 != config2 then set cache tree node config2 _i;
98         if not Ata.(nss.Config.node.unstable_subtree) then
99           Printf.eprintf "Could skip right subtree!%!";
100         let unstable_right = Ata.(nss.Config.node.unstable_subtree) && loop ns in
101         let nss1 = get cache tree ns in
102         let config3 = Ata.eval_trans auto fcs1 nss1 ps config2 in
103
104         TRACE(html tree node _i config3 "Updating transitions (after next-sibling)");
105
106         if config2 != config3 then set cache tree node config3 _i;
107         let unstable =
108           unstable_left
109           || unstable_right
110           || Ata.(TransList.nil != config3.Config.node.todo)
111         in
112         if Ata.(unstable && not config3.Config.node.unstable_subtree) then
113           Ata.(config3.Config.node.unstable_subtree <- true);
114         unstable
115       end
116     in
117     loop node
118
119   let get_results auto tree node cache =
120     let rec loop node acc =
121       if node == T.nil then acc
122       else
123         let acc0 = loop (T.next_sibling tree node) acc in
124         let acc1 = loop (T.first_child tree node) acc0 in
125
126         if StateSet.intersect
127           (get cache tree node).Ata.Config.node.Ata.sat
128           auto.Ata.selection_states then node::acc1
129         else acc1
130     in
131     loop node []
132
133   let eval auto tree node =
134     let cache = Cache.N1.create
135       Ata.(Config.make { sat = StateSet.empty;
136                          unsat = StateSet.empty;
137                          todo = TransList.nil;
138                          summary = dummy_summary;
139                          round = ~-1;
140                          unstable_subtree = true;
141                        })
142     in
143     let redo = ref true in
144     let iter = ref 0 in
145     Ata.reset auto;
146     while !redo do
147       redo := false;
148       Ata.reset auto; (* prevents the .cache2 and .cache4 memoization tables from growing too much *)
149       redo := top_down_run auto tree node cache !iter;
150       incr iter;
151     done;
152     at_exit (fun () -> eprintf "@[STATS: %i iterations@]@." !iter);
153     let r = get_results auto tree node cache in
154     TRACE(Html.gen_trace (module T : Tree.S with type t = T.t) (tree));
155     r
156
157 end