Coccinelle release-1.0.0-rc11
[bpt/coccinelle.git] / ctl / wrapper_ctl.ml
1 (*
2 * Copyright 2012, INRIA
3 * Julia Lawall, Gilles Muller
4 * Copyright 2010-2011, INRIA, University of Copenhagen
5 * Julia Lawall, Rene Rydhof Hansen, Gilles Muller, Nicolas Palix
6 * Copyright 2005-2009, Ecole des Mines de Nantes, University of Copenhagen
7 * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix
8 * This file is part of Coccinelle.
9 *
10 * Coccinelle is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, according to version 2 of the License.
13 *
14 * Coccinelle is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with Coccinelle. If not, see <http://www.gnu.org/licenses/>.
21 *
22 * The authors reserve the right to distribute this or future versions of
23 * Coccinelle under other licenses.
24 *)
25
26
27 (* **********************************************************************
28 *
29 * Wrapping for FUNCTORS and MODULES
30 *
31 *
32 * $Id$
33 *
34 * **********************************************************************)
35
36 type info = int
37
38 type ('pred, 'mvar) wrapped_ctl =
39 ('pred * 'mvar Ast_ctl.modif, 'mvar, info) Ast_ctl.generic_ctl
40
41 type ('value, 'pred) wrapped_binding =
42 | ClassicVal of 'value
43 | PredVal of 'pred Ast_ctl.modif
44
45 type ('pred,'state,'mvar,'value) labelfunc =
46 'pred ->
47 ('state * ('pred * ('mvar, 'value) Ast_ctl.generic_substitution)) list
48
49 (* pad: what is 'wit ? *)
50 type ('pred,'state,'mvar,'value,'wit) wrapped_labelfunc =
51 ('pred * 'mvar Ast_ctl.modif) ->
52 ('state *
53 ('mvar,('value,'pred) wrapped_binding) Ast_ctl.generic_substitution *
54 'wit
55 ) list
56
57 (* ********************************************************************** *)
58 (* Module type: CTL_ENGINE_BIS (wrapper for CTL_ENGINE) *)
59 (* ********************************************************************** *)
60
61 (* This module must convert the labelling function passed as parameter, by
62 using convert_label. Then create a SUBST2 module handling the
63 wrapped_binding. Then it can instantiates the generic CTL_ENGINE
64 module. Call sat. And then process the witness tree to remove all that
65 is not revelevant for the transformation phase.
66 *)
67
68 module CTL_ENGINE_BIS =
69 functor (SUB : Ctl_engine.SUBST) ->
70 functor (G : Ctl_engine.GRAPH) ->
71 functor(P : Ctl_engine.PREDICATE) ->
72 struct
73
74 exception TODO_CTL of string (* implementation still not quite done so... *)
75 exception NEVER_CTL of string (* Some things should never happen *)
76
77 module A = Ast_ctl
78
79 type predicate = P.t
80 module WRAPPER_ENV =
81 struct
82 type mvar = SUB.mvar
83 type value = (SUB.value,predicate) wrapped_binding
84 let eq_mvar = SUB.eq_mvar
85 let eq_val wv1 wv2 =
86 match (wv1,wv2) with
87 | (ClassicVal(v1),ClassicVal(v2)) -> SUB.eq_val v1 v2
88 | (PredVal(v1),PredVal(v2)) -> v1 = v2 (* FIX ME: ok? *)
89 | _ -> false
90 let merge_val wv1 wv2 =
91 match (wv1,wv2) with
92 | (ClassicVal(v1),ClassicVal(v2)) -> ClassicVal(SUB.merge_val v1 v2)
93 | _ -> wv1 (* FIX ME: ok? *)
94
95
96 let print_mvar x = SUB.print_mvar x
97 let print_value x =
98 match x with
99 ClassicVal v -> SUB.print_value v
100 | PredVal(A.Modif v) -> P.print_predicate v
101 | PredVal(A.UnModif v) -> P.print_predicate v
102 | PredVal(A.Control) -> Format.print_string "no value"
103 end
104
105 module WRAPPER_PRED =
106 struct
107 type t = P.t * SUB.mvar Ast_ctl.modif
108 let print_predicate (pred, modif) =
109 begin
110 P.print_predicate pred;
111 (match modif with
112 Ast_ctl.Modif x | Ast_ctl.UnModif x ->
113 Format.print_string " with <modifTODO>"
114 | Ast_ctl.Control -> ())
115 end
116 end
117
118 (* Instantiate a wrapped version of CTL_ENGINE *)
119 module WRAPPER_ENGINE =
120 Ctl_engine.CTL_ENGINE (WRAPPER_ENV) (G) (WRAPPER_PRED)
121
122 (* Wrap a label function *)
123 let (wrap_label: ('pred,'state,'mvar,'value) labelfunc ->
124 ('pred,'state,'mvar,'value,'wit) wrapped_labelfunc) =
125 fun oldlabelfunc ->
126 fun (p, predvar) ->
127
128 let penv p' =
129 match predvar with
130 | A.Modif(x) -> [A.Subst(x,PredVal(A.Modif(p')))]
131 | A.UnModif(x) -> [A.Subst(x,PredVal(A.UnModif(p')))]
132 | A.Control -> [] in
133
134 let conv_sub sub =
135 match sub with
136 | A.Subst(x,v) -> A.Subst(x,ClassicVal(v))
137 | A.NegSubst(x,v) -> A.NegSubst(x,ClassicVal(v)) in
138
139 let conv_trip (s,(p',env)) =
140 (s,penv p' @ (List.map conv_sub env),[](*pad: ?*))
141 in
142 List.map conv_trip (oldlabelfunc p)
143
144 (* ---------------------------------------------------------------- *)
145
146 (* FIX ME: what about negative witnesses and negative substitutions *)
147 let unwrap_wits modifonly wits =
148 let mkth th =
149 Common.map_filter
150 (function A.Subst(x,ClassicVal(v)) -> Some (x,v) | _ -> None)
151 th in
152 let rec loop neg acc = function
153 A.Wit(st,[A.Subst(x,PredVal(A.Modif(v)))],anno,wit) ->
154 (match wit with
155 [] -> [(st,acc,v)]
156 | _ -> raise (NEVER_CTL "predvar tree should have no children"))
157 | A.Wit(st,[A.Subst(x,PredVal(A.UnModif(v)))],anno,wit)
158 when not modifonly or !Flag.track_iso_usage ->
159 (match wit with
160 [] -> [(st,acc,v)]
161 | _ -> raise (NEVER_CTL "predvar tree should have no children"))
162 | A.Wit(st,th,anno,wit) ->
163 List.concat (List.map (loop neg ((mkth th) @ acc)) wit)
164 | A.NegWit(_) -> [] (* why not failure? *) in
165 List.concat (List.map (function wit -> loop false [] wit) wits)
166 ;;
167
168 (*
169 (* a match can return many trees, but within each tree, there has to be
170 at most one value for each variable that is in the used_after list *)
171 let collect_used_after used_after envs =
172 let print_var var = SUB.print_mvar var; Format.print_flush() in
173 List.concat
174 (List.map
175 (function used_after_var ->
176 let vl =
177 List.fold_left
178 (function rest ->
179 function env ->
180 try
181 let vl = List.assoc used_after_var env in
182 match rest with
183 None -> Some vl
184 | Some old_vl when SUB.eq_val vl old_vl -> rest
185 | Some old_vl -> print_var used_after_var;
186 Format.print_newline();
187 SUB.print_value old_vl;
188 Format.print_newline();
189 SUB.print_value vl;
190 Format.print_newline();
191 failwith "incompatible values"
192 with Not_found -> rest)
193 None envs in
194 match vl with
195 None -> []
196 | Some vl -> [(used_after_var, vl)])
197 used_after)
198 *)
199
200 (* a match can return many trees, but within each tree, there has to be
201 at most one value for each variable that is in the used_after list *)
202 (* actually, this should always be the case, because these variables
203 should be quantified at the top level. so the more complicated
204 definition above should not be needed. *)
205 let collect_used_after used_after envs =
206 List.concat
207 (List.map
208 (function used_after_var ->
209 let vl =
210 List.fold_left
211 (function rest ->
212 function env ->
213 try
214 let vl = List.assoc used_after_var env in
215 if List.exists (function x -> SUB.eq_val x vl) rest
216 then rest
217 else vl::rest
218 with Not_found -> rest)
219 [] envs in
220 List.map (function x -> (used_after_var, x)) vl)
221 used_after)
222
223 (* ----------------------------------------------------- *)
224
225 (* The wrapper for sat from the CTL_ENGINE *)
226 let satbis_noclean (grp,lab,states) (phi,reqopt) :
227 ('pred,'anno) WRAPPER_ENGINE.triples =
228 WRAPPER_ENGINE.sat (grp,wrap_label lab,states) phi reqopt
229
230 (* Returns the "cleaned up" result from satbis_noclean *)
231 let (satbis :
232 G.cfg *
233 (predicate,G.node,SUB.mvar,SUB.value) labelfunc *
234 G.node list ->
235 ((predicate,SUB.mvar) wrapped_ctl *
236 (WRAPPER_PRED.t list list)) ->
237 (WRAPPER_ENV.mvar list * (SUB.mvar * SUB.value) list) ->
238 ((WRAPPER_PRED.t, 'a) WRAPPER_ENGINE.triples *
239 ((G.node * (SUB.mvar * SUB.value) list * predicate)
240 list list *
241 bool *
242 (WRAPPER_ENV.mvar * SUB.value) list list))) =
243 fun m phi (used_after, binding) ->
244 let noclean = satbis_noclean m phi in
245 let witness_trees = List.map (fun (_,_,w) -> w) noclean in
246 let res = List.map (unwrap_wits true) witness_trees in
247 let new_bindings =
248 List.map
249 (function bindings_per_witness_tree ->
250 (List.map (function (_,env,_) -> env) bindings_per_witness_tree))
251 (List.map (unwrap_wits false) witness_trees) in
252 (noclean,
253 (res,not(noclean = []),
254 (* throw in the old binding. By construction it doesn't conflict
255 with any of the new things, and it is useful if there are no new
256 things. *)
257 (List.map (collect_used_after used_after) new_bindings)))
258
259 let print_bench _ = WRAPPER_ENGINE.print_bench()
260
261 (* END OF MODULE: CTL_ENGINE_BIS *)
262 end