Maintain the set of unsatisfiable states.
[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-18 17:36:06 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
38
39   type cache = StateSet.t Cache.N1.t
40   let get c t n = Cache.N1.find c (T.preorder t n)
41
42   let set c t n v = Cache.N1.add c (T.preorder t n) v
43
44
45   let top_down_run auto tree node cache _i =
46     let redo = ref false in
47     let rec loop node =
48       if node != T.nil then begin
49         let parent = T.parent tree node in
50         let fc = T.first_child tree node in
51         let ns = T.next_sibling tree node in
52         let tag = T.tag tree node in
53         let config0 =
54           let c = get cache tree node in
55           if c == Cache.N1.dummy cache then
56             { c with Ata.todo = Ata.get_trans auto tag auto.Ata.states }
57           else c
58         in
59
60         let () =
61           TRACE(Html.trace (T.preorder tree node) _i "Config0<br/>sat: %a<br/>unsat: %a<br/>todo: %a<br/>"
62                   StateSet.print config0.Ata.sat
63                   StateSet.print config0.Ata.unsat
64                   (Ata.TransList.print ~sep:"<br/>") config0.Ata.todo)
65         in
66
67         let ps = get cache tree parent in
68         let fcs = get cache tree fc in
69         let nss = get cache tree ns in
70         let is_left = node == T.first_child tree parent
71         and is_right = node == T.next_sibling tree parent
72         and has_left = fc != T.nil
73         and has_right = ns != T.nil
74         and kind = T.kind tree node
75         in
76         let config1 =
77           Ata.eval_trans2 auto fcs nss ps config0
78             is_left is_right has_left has_right kind
79         in
80         let () =
81           TRACE(Html.trace (T.preorder tree node) _i "Config1<br/>sat: %a<br/>unsat: %a<br/>todo: %a<br/>"
82                   StateSet.print config1.Ata.sat
83                   StateSet.print config1.Ata.unsat
84                   (Ata.TransList.print ~sep:"<br/>") config1.Ata.todo)
85         in
86         if not (Ata.eq_config config0 config1) then set cache tree node config1;
87         let () = loop fc in
88         let fcs1 = get cache tree fc in
89         let config2 =
90           Ata.eval_trans2 auto
91             fcs1 nss ps config1
92             is_left is_right has_left has_right kind
93         in
94         let () =
95           TRACE(Html.trace (T.preorder tree node) _i "Config2<br/>sat: %a<br/>unsat: %a<br/>todo: %a<br/>"
96                   StateSet.print config2.Ata.sat
97                   StateSet.print config2.Ata.unsat
98                   (Ata.TransList.print ~sep:"<br/>") config2.Ata.todo)
99         in
100
101         if not (Ata.eq_config config1 config2) then set cache tree node config2;
102         let () = loop ns in
103         let config3 =
104           Ata.eval_trans2 auto
105             fcs1 (get cache tree ns) ps config2
106             is_left is_right has_left has_right kind
107         in
108         let () =
109           TRACE(Html.trace (T.preorder tree node) _i "Config3<br/>sat: %a<br/>unsat: %a<br/>todo: %a<br/>"
110                   StateSet.print config3.Ata.sat
111                   StateSet.print config3.Ata.unsat
112                   (Ata.TransList.print ~sep:"<br/>") config3.Ata.todo)
113         in
114         if not (Ata.eq_config config2 config3) then set cache tree node config3;
115         (* We do set the redo flat if : *)
116         if not (
117           !redo (* already set *)
118           || (Ata.TransList.nil == config3.Ata.todo) (* no more transition to check *)
119           || (Ata.eq_config config0 config3) (* did not gain any new information *)
120         )
121         then redo := true
122       end
123     in
124     loop node;
125     !redo
126
127   let get_results auto tree node cache =
128     let rec loop node acc =
129       if node == T.nil then acc
130       else
131         let acc0 = loop (T.next_sibling tree node) acc in
132         let acc1 = loop (T.first_child tree node) acc0 in
133
134         if StateSet.intersect (get cache tree node).Ata.sat auto.Ata.selection_states then
135           node::acc1
136         else
137           acc1
138     in
139     loop node []
140
141   let eval auto tree node =
142     let cache = Cache.N1.create { Ata.sat = StateSet.empty;
143                                   Ata.unsat = StateSet.empty;
144                                   Ata.todo = Ata.TransList.nil }
145     in
146     let redo = ref true in
147     let iter = ref 0 in
148     Ata.reset auto;
149     while !redo do
150       redo := false;
151       redo := top_down_run auto tree node cache !iter;
152       incr iter;
153     done;
154     at_exit (fun () -> eprintf "INFO: %i iterations\n" !iter);
155     let r = get_results auto tree node cache in
156     TRACE(Html.gen_trace (module T : Tree.S with type t = T.t) (tree));
157     r
158
159 end