]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_paramodulation/superposition.ml
profile most operations, do not return a filtered varlist in unif (useless and expensive)
[helm.git] / helm / software / components / ng_paramodulation / superposition.ml
1 (*
2     ||M||  This file is part of HELM, an Hypertextual, Electronic        
3     ||A||  Library of Mathematics, developed at the Computer Science     
4     ||T||  Department, University of Bologna, Italy.                     
5     ||I||                                                                
6     ||T||  HELM is free software; you can redistribute it and/or         
7     ||A||  modify it under the terms of the GNU General Public License   
8     \   /  version 2 or (at your option) any later version.      
9      \ /   This software is distributed as is, NO WARRANTY.     
10       V_______________________________________________________________ *)
11
12 (* $Id: index.mli 9822 2009-06-03 15:37:06Z tassi $ *)
13
14 module Superposition (B : Terms.Blob) = 
15   struct
16     module IDX = Index.Index(B)
17     module Unif = FoUnif.Founif(B)
18     module Subst = FoSubst 
19     module Order = Orderings.Orderings(B)
20     module Utils = FoUtils.Utils(B)
21     module Pp = Pp.Pp(B)
22     
23     exception Success of B.t Terms.bag * int * B.t Terms.unit_clause
24
25     let debug s = prerr_endline s;;
26     let debug _ = ();;
27     let enable = true;;
28
29     let rec list_first f = function
30       | [] -> None
31       | x::tl -> match f x with Some _ as x -> x | _ -> list_first f tl
32     ;;
33
34     let first_position pos ctx t f =
35       let inject_pos pos ctx = function
36         | None -> None
37         | Some (a,b,c,d) -> Some(ctx a,b,c,d,pos)
38       in
39       let rec aux pos ctx = function
40       | Terms.Leaf _ as t -> inject_pos pos ctx (f t)
41       | Terms.Var _ -> None
42       | Terms.Node l as t->
43           match f t with
44           | Some _ as x -> inject_pos pos ctx x
45           | None ->
46               let rec first pre post = function
47                 | [] -> None
48                 | t :: tl -> 
49                      let newctx = fun x -> ctx (Terms.Node (pre@[x]@post)) in
50                      match aux (List.length pre :: pos) newctx t with
51                      | Some _ as x -> x
52                      | None -> 
53                          if post = [] then None (* tl is also empty *)
54                          else first (pre @ [t]) (List.tl post) tl
55               in
56                 first [] (List.tl l) l 
57       in
58         aux pos ctx t
59     ;;
60                                      
61     let all_positions pos ctx t f =
62       let rec aux pos ctx = function
63       | Terms.Leaf _ as t -> f t pos ctx 
64       | Terms.Var _ -> []
65       | Terms.Node l as t-> 
66           let acc, _, _ = 
67             List.fold_left
68             (fun (acc,pre,post) t -> (* Invariant: pre @ [t] @ post = l *)
69                 let newctx = fun x -> ctx (Terms.Node (pre@[x]@post)) in
70                 let acc = aux (List.length pre :: pos) newctx t @ acc in
71                 if post = [] then acc, l, []
72                 else acc, pre @ [t], List.tl post)
73              (f t pos ctx, [], List.tl l) l
74           in
75            acc
76       in
77         aux pos ctx t
78     ;;
79
80     let parallel_positions bag pos ctx id t f =
81       let rec aux bag pos ctx id = function
82       | Terms.Leaf _ as t -> f bag t pos ctx id
83       | Terms.Var _ as t -> bag,t,id
84       | Terms.Node l as t->
85           let bag,t,id1 = f bag t pos ctx id in
86             if id = id1 then
87               let bag, l, _, id = 
88                 List.fold_left
89                   (fun (bag,pre,post,id) t ->
90                      let newctx = fun x -> ctx (Terms.Node (pre@[x]@post)) in
91                      let newpos = (List.length pre)::pos in
92                      let bag,newt,id = aux bag newpos newctx id t in
93                        if post = [] then bag, pre@[newt], [], id
94                        else bag, pre @ [newt], List.tl post, id)
95                   (bag, [], List.tl l, id) l
96               in
97                 bag, Terms.Node l, id
98             else bag,t,id1
99       in
100         aux bag pos ctx id t
101     ;;
102     
103     let build_clause bag filter rule t subst id id2 pos dir =
104       let proof = Terms.Step(rule,id,id2,dir,pos,subst) in
105       let t = Subst.apply_subst subst t in
106       if filter t then
107         let literal = 
108           match t with
109           | Terms.Node [ Terms.Leaf eq ; ty; l; r ] when B.eq B.eqP eq ->
110                let o = Order.compare_terms l r in
111                Terms.Equation (l, r, ty, o)
112           | t -> Terms.Predicate t
113         in
114         let bag, uc = 
115           Terms.add_to_bag (0, literal, Terms.vars_of_term t, proof) bag
116         in
117         Some (bag, uc)
118       else
119         ((*prerr_endline ("Filtering: " ^ Pp.pp_foterm t);*)None)
120     ;;
121     let prof_build_clause = HExtlib.profile ~enable "build_clause";;
122     let build_clause bag filter rule t subst id id2 pos x =
123       prof_build_clause.HExtlib.profile (build_clause bag filter rule t subst id id2 pos) x
124     ;;
125       
126     
127     (* ============ simplification ================= *)
128     let prof_demod_u = HExtlib.profile ~enable "demod.unify";;
129     let prof_demod_r = HExtlib.profile ~enable "demod.retrieve_generalizations";;
130     let prof_demod_o = HExtlib.profile ~enable "demod.compare_terms";;
131
132     let demod table varlist subterm =
133       let cands = 
134         prof_demod_r.HExtlib.profile 
135          (IDX.DT.retrieve_generalizations table) subterm 
136       in
137       list_first
138         (fun (dir, (id,lit,vl,_)) ->
139            match lit with
140            | Terms.Predicate _ -> assert false
141            | Terms.Equation (l,r,_,o) ->
142                let side, newside = if dir=Terms.Left2Right then l,r else r,l in
143                try 
144                  let subst =
145                    prof_demod_u.HExtlib.profile 
146                      (Unif.unification (varlist@vl) varlist subterm) side 
147                  in 
148                  let side = Subst.apply_subst subst side in
149                  let newside = Subst.apply_subst subst newside in
150                  if o = Terms.Incomparable then
151                    let o = 
152                      prof_demod_o.HExtlib.profile 
153                       (Order.compare_terms newside) side in
154                    (* Riazanov, pp. 45 (ii) *)
155                    if o = Terms.Lt then
156                      Some (newside, subst, id, dir)
157                    else 
158                      ((*prerr_endline ("Filtering: " ^ 
159                         Pp.pp_foterm side ^ " =(< || =)" ^ 
160                         Pp.pp_foterm newside ^ " coming from " ^ 
161                         Pp.pp_unit_clause uc );*)None)
162                  else
163                    Some (newside, subst, id, dir)
164                with FoUnif.UnificationFailure _ -> None)
165         (IDX.ClauseSet.elements cands)
166     ;;
167     let prof_demod = HExtlib.profile ~enable "demod";;
168     let demod table varlist x =
169       prof_demod.HExtlib.profile (demod table varlist) x
170     ;;
171
172     let demodulate_once_old ~jump_to_right bag (id, literal, vl, pr) table =
173       match literal with
174       | Terms.Predicate t -> assert false
175       | Terms.Equation (l,r,ty,_) ->
176         let left_position = if jump_to_right then None else
177           first_position [2]
178             (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; x; r ]) l
179             (demod table vl)
180         in
181         match left_position with
182           | Some (newt, subst, id2, dir, pos) ->
183               begin
184                 match build_clause bag (fun _ -> true) Terms.Demodulation 
185                   newt subst id id2 pos dir
186                 with
187                   | None -> assert false
188                   | Some x -> Some (x,false)
189               end
190           | None ->
191               match first_position
192                 [3] (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; l; x ]) r
193                 (demod table vl)
194               with
195                 | None -> None
196                 | Some (newt, subst, id2, dir, pos) ->
197                     match build_clause bag (fun _ -> true)
198                       Terms.Demodulation newt subst id id2 pos dir
199                     with
200                         | None -> assert false
201                         | Some x -> Some (x,true)
202     ;;
203
204     let parallel_demod table vl bag t pos ctx id =
205       match demod table vl t with
206         | None -> (bag,t,id)
207         | Some (newside, subst, id2, dir) ->
208             match build_clause bag (fun _ -> true)
209               Terms.Demodulation (ctx newside) subst id id2 pos dir
210             with
211               | None -> assert false
212               | Some (bag,(id,_,_,_)) ->
213                     (bag,newside,id)
214     ;;
215
216     let demodulate_once ~jump_to_right bag (id, literal, vl, pr) table =
217       match literal with
218       | Terms.Predicate t -> assert false
219       | Terms.Equation (l,r,ty,_) ->
220           let bag,l,id1 = if jump_to_right then (bag,l,id) else
221             parallel_positions bag [2]
222               (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; x; r ]) id l
223               (parallel_demod table vl)
224           in
225           let jump_to_right = id1 = id in
226           let bag,r,id2 =
227             parallel_positions bag [3]
228               (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; l; x ]) id1 r
229               (parallel_demod table vl)
230           in
231             if id = id2 then None
232             else
233               let cl,_,_ = Terms.get_from_bag id2 bag in
234                 Some ((bag,cl),jump_to_right)
235     ;;
236
237     let rec demodulate ~jump_to_right bag clause table =
238       match demodulate_once ~jump_to_right bag clause table with
239       | None -> bag, clause
240       | Some ((bag, clause),r) -> demodulate ~jump_to_right:r
241           bag clause table
242     ;;
243
244     let rec demodulate_old ~jump_to_right bag clause table =
245       match demodulate_once_old ~jump_to_right bag clause table with
246         | None -> bag, clause
247         | Some ((bag, clause),r) -> demodulate_old ~jump_to_right:r
248           bag clause table
249     ;;
250
251     let are_alpha_eq cl1 cl2 =
252       let get_term (_,lit,_,_) =
253         match lit with
254           | Terms.Predicate _ -> assert false
255           | Terms.Equation (l,r,ty,_) ->
256               Terms.Node [Terms.Leaf B.eqP; ty; l ; r]
257       in
258         try ignore(Unif.alpha_eq (get_term cl1) (get_term cl2)) ; true
259         with FoUnif.UnificationFailure _ -> false
260     ;;
261
262     let demodulate bag clause table =
263 (*      let (bag1,c1), (_,c2) =*)
264          demodulate ~jump_to_right:false bag clause table 
265 (*         demodulate_old ~jump_to_right:false bag clause table *)
266 (*      in
267         if are_alpha_eq c1 c2 then bag1,c1
268         else begin
269           prerr_endline (Pp.pp_unit_clause c1);
270           prerr_endline (Pp.pp_unit_clause c2);
271           prerr_endline "Bag :";
272           prerr_endline (Pp.pp_bag bag1);
273           assert false
274         end*)
275     ;;
276     let prof_demodulate = HExtlib.profile ~enable "demodulate";;
277     let demodulate bag clause x =
278       prof_demodulate.HExtlib.profile (demodulate bag clause) x
279     ;;
280
281     (* move away *)
282     let is_identity_clause ~unify = function
283       | _, Terms.Equation (_,_,_,Terms.Eq), _, _ -> true
284       | _, Terms.Equation (l,r,_,_), vl, proof when unify ->
285           (try ignore(Unif.unification vl [] l r); true
286           with FoUnif.UnificationFailure _ -> false)
287       | _, Terms.Equation (_,_,_,_), _, _ -> false
288       | _, Terms.Predicate _, _, _ -> assert false          
289     ;;
290
291     let build_new_clause bag maxvar filter rule t subst id id2 pos dir =
292       let maxvar, _vl, subst = Utils.relocate maxvar (Terms.vars_of_term
293       (Subst.apply_subst subst t)) subst in
294       match build_clause bag filter rule t subst id id2 pos dir with
295       | Some (bag, c) -> Some ((bag, maxvar), c)
296       | None -> None
297     ;;
298     let prof_build_new_clause = HExtlib.profile ~enable "build_new_clause";;
299     let build_new_clause bag maxvar filter rule t subst id id2 pos x =
300       prof_build_new_clause.HExtlib.profile (build_new_clause bag maxvar filter
301       rule t subst id id2 pos) x
302     ;;
303
304     let fold_build_new_clause bag maxvar id rule filter res =
305       let (bag, maxvar), res =
306        HExtlib.filter_map_acc 
307          (fun (bag, maxvar) (t,subst,id2,pos,dir) ->
308             build_new_clause bag maxvar filter rule t subst id id2 pos dir)
309          (bag, maxvar) res
310       in
311        bag, maxvar, res
312     ;;
313
314     
315     let rewrite_eq ~unify l r ty vl table =
316       let retrieve = if unify then IDX.DT.retrieve_unifiables
317       else IDX.DT.retrieve_generalizations in
318       let lcands = retrieve table l in
319       let rcands = retrieve table r in
320       let f b c = 
321         let id, dir, l, r, vl = 
322           match c with
323             | (d, (id,Terms.Equation (l,r,ty,_),vl,_))-> id, d, l, r, vl
324             |_ -> assert false 
325         in 
326         let reverse = (dir = Terms.Left2Right) = b in
327         let l, r, proof_rewrite_dir = if reverse then l,r,Terms.Left2Right
328         else r,l, Terms.Right2Left in
329           (id,proof_rewrite_dir,Terms.Node [ Terms.Leaf B.eqP; ty; l; r ], vl)
330       in
331       let cands1 = List.map (f true) (IDX.ClauseSet.elements lcands) in
332       let cands2 = List.map (f false) (IDX.ClauseSet.elements rcands) in
333       let t = Terms.Node [ Terms.Leaf B.eqP; ty; l; r ] in
334       let locked_vars = if unify then [] else vl in
335       let rec aux = function
336         | [] -> None
337         | (id2,dir,c,vl1)::tl ->
338             try
339               let subst = Unif.unification (vl@vl1) locked_vars c t in
340               Some (id2, dir, subst)
341             with FoUnif.UnificationFailure _ -> aux tl
342       in
343         aux (cands1 @ cands2)
344     ;;
345
346     let is_subsumed ~unify bag maxvar (id, lit, vl, _) table =
347       match lit with
348       | Terms.Predicate _ -> assert false
349       | Terms.Equation (l,r,ty,_) -> 
350           match rewrite_eq ~unify l r ty vl table with
351             | None -> None
352             | Some (id2, dir, subst) ->
353                 let id_t = Terms.Node [ Terms.Leaf B.eqP; ty; r; r ] in
354                   build_new_clause bag maxvar (fun _ -> true)
355                   Terms.Superposition id_t subst id id2 [2] dir 
356     ;;
357     let prof_is_subsumed = HExtlib.profile ~enable "is_subsumed";;
358     let is_subsumed ~unify bag maxvar c x =
359       prof_is_subsumed.HExtlib.profile (is_subsumed ~unify bag maxvar c) x
360     ;;
361     (* id refers to a clause proving contextl l = contextr r *)
362
363     let rec deep_eq ~unify l r ty pos contextl contextr table acc =
364       match acc with 
365       | None -> None
366       | Some(bag,maxvar,(id,lit,vl,p),subst) -> 
367           let l = Subst.apply_subst subst l in 
368           let r = Subst.apply_subst subst r in 
369             try 
370               let subst1 = Unif.unification vl [] l r in
371               let lit = 
372                 match lit with Terms.Predicate _ -> assert false
373                   | Terms.Equation (l,r,ty,o) -> 
374                      Terms.Equation (FoSubst.apply_subst subst1 l,
375                        FoSubst.apply_subst subst1 r, ty, o)
376               in
377                 Some(bag,maxvar,(id,lit,vl,p),Subst.concat subst1 subst)
378             with FoUnif.UnificationFailure _ -> 
379               match rewrite_eq ~unify l r ty vl table with
380               | Some (id2, dir, subst1) ->
381                   let newsubst = Subst.concat subst1 subst in
382                   let id_t = 
383                     FoSubst.apply_subst newsubst
384                       (Terms.Node[Terms.Leaf B.eqP;ty;contextl r;contextr r]) 
385                   in
386                     (match 
387                       build_new_clause bag maxvar (fun _ -> true)
388                         Terms.Superposition id_t 
389                         subst1 id id2 (pos@[2]) dir 
390                     with
391                     | Some ((bag, maxvar), c) -> 
392                         Some(bag,maxvar,c,newsubst)
393                     | None -> assert false)
394               | None ->
395                   match l,r with 
396                   | Terms.Node (a::la), Terms.Node (b::lb) when 
397                       a = b && List.length la = List.length lb ->
398                       let acc,_,_,_ =
399                         List.fold_left2 
400                           (fun (acc,pre,postl,postr) a b -> 
401                              let newcl = 
402                               fun x -> contextl(Terms.Node (pre@(x::postl))) in
403                              let newcr = 
404                               fun x -> contextr(Terms.Node (pre@(x::postr))) in
405                              let newpos = List.length pre::pos in
406                              let footail l =
407                                if l = [] then [] else List.tl l in
408                                (deep_eq ~unify a b ty 
409                                  newpos newcl newcr table acc,pre@[b],
410                                  footail postl, footail postr))
411                           (acc,[a],List.tl la,List.tl lb) la lb
412                       in acc
413                   | _,_ -> None
414     ;;
415     let prof_deep_eq = HExtlib.profile ~enable "deep_eq";;
416     let deep_eq ~unify l r ty pos contextl contextr table x =
417       prof_deep_eq.HExtlib.profile (deep_eq ~unify l r ty pos contextl contextr table) x
418     ;;
419
420     let rec orphan_murder bag acc i =
421       match Terms.get_from_bag i bag with
422         | (_,_,_,Terms.Exact _),discarded,_ -> (discarded,acc)
423         | (_,_,_,Terms.Step (_,i1,i2,_,_,_)),true,_ -> (true,acc)
424         | (_,_,_,Terms.Step (_,i1,i2,_,_,_)),false,_ ->
425             if (List.mem i acc) then (false,acc)
426             else match orphan_murder bag acc i1 with
427               | (true,acc) -> (true,acc)
428               | (false,acc) ->
429                   let (res,acc) = orphan_murder bag acc i2 in
430                   if res then res,acc else res,i::acc
431     ;;
432
433     let orphan_murder bag actives cl =
434       let (id,_,_,_) = cl in
435       let actives = List.map (fun (i,_,_,_) -> i) actives in
436       let (res,_) = orphan_murder bag actives id in
437         if res then debug "Orphan murdered"; res
438     ;;
439     let prof_orphan_murder = HExtlib.profile ~enable "orphan_murder";;
440     let orphan_murder bag actives x =
441       prof_orphan_murder.HExtlib.profile (orphan_murder bag actives) x
442     ;;
443
444     (* demodulate and check for subsumption *)
445     let simplify table maxvar bag clause = 
446       if is_identity_clause ~unify:false clause then bag,None
447       (* else if orphan_murder bag actives clause then bag,None *)
448       else let bag, clause = demodulate bag clause table in
449       if is_identity_clause ~unify:false clause then bag,None
450       else
451         match is_subsumed ~unify:false bag maxvar clause table with
452           | None -> bag, Some clause
453           | Some _ -> bag, None
454     ;;
455
456     let simplify table maxvar bag clause =
457       match simplify table maxvar bag clause with
458         | bag, None ->
459             let (id,_,_,_) = clause in
460             let (_,_,iter) = Terms.get_from_bag id bag in
461             Terms.replace_in_bag (clause,true,iter) bag, None
462         | bag, Some clause -> bag, Some clause
463     (*let (id,_,_,_) = clause in
464             if orphan_murder bag clause then
465               Terms.M.add id (clause,true) bag, Some clause
466             else bag, Some clause*)
467     ;;
468     let prof_simplify = HExtlib.profile ~enable "simplify";;
469     let simplify table maxvar bag x =
470       prof_simplify.HExtlib.profile (simplify table maxvar bag ) x
471     ;;
472
473     let one_pass_simplification new_clause (alist,atable) bag maxvar =
474       match simplify atable maxvar bag new_clause with
475         | bag,None -> bag,None (* new_clause has been discarded *)
476         | bag,(Some clause) ->
477             let ctable = IDX.index_unit_clause IDX.DT.empty clause in
478             let bag, alist, atable = 
479               List.fold_left 
480                 (fun (bag, alist, atable) c ->
481                    match simplify ctable maxvar bag c with
482                      |bag,None -> (bag,alist,atable)
483                         (* an active clause as been discarded *)
484                      |bag,Some c1 ->
485                         bag, c :: alist, IDX.index_unit_clause atable c)
486                 (bag,[],IDX.DT.empty) alist
487             in
488               bag, Some (clause, (alist,atable))
489     ;;
490     let prof_one_pass_simplification = HExtlib.profile ~enable "one_pass_simplification";;
491     let one_pass_simplification new_clause t bag x =
492       prof_one_pass_simplification.HExtlib.profile (one_pass_simplification new_clause t bag ) x
493     ;;
494
495     let simplification_step ~new_cl cl (alist,atable) bag maxvar new_clause =
496       let atable1 =
497         if new_cl then atable else
498         IDX.index_unit_clause atable cl
499       in
500         (* Simplification of new_clause with :      *
501          * - actives and cl if new_clause is not cl *
502          * - only actives otherwise                 *)
503         match
504           simplify atable1 maxvar bag new_clause with
505           | bag,None -> bag,(Some cl, None) (* new_clause has been discarded *)
506           | bag,Some clause ->
507               (* Simplification of each active clause with clause *
508                * which is the simplified form of new_clause       *)
509               let ctable = IDX.index_unit_clause IDX.DT.empty clause in
510               let bag, newa, alist, atable = 
511                 List.fold_left 
512                   (fun (bag, newa, alist, atable) c ->
513                      match simplify ctable maxvar bag c with
514                        |bag,None -> (bag, newa, alist, atable)
515                           (* an active clause as been discarded *)
516                        |bag,Some c1 ->
517                             if (c1 == c) then 
518                               bag, newa, c :: alist,
519                             IDX.index_unit_clause atable c
520                             else
521                               bag, c1 :: newa, alist, atable)                  
522                   (bag,[],[],IDX.DT.empty) alist
523               in
524                 if new_cl then
525                   bag, (Some cl, Some (clause, (alist,atable), newa))
526                 else
527                   (* if new_clause is not cl, we simplify cl with clause *)
528                   match simplify ctable maxvar bag cl with
529                     | bag,None ->
530                         (* cl has been discarded *)
531                         bag,(None, Some (clause, (alist,atable), newa))
532                     | bag,Some cl1 ->
533                         bag,(Some cl1, Some (clause, (alist,atable), newa))
534     ;;
535     let prof_simplification_step = HExtlib.profile ~enable "simplification_step";;
536     let simplification_step ~new_cl cl (alist,atable) bag maxvar x =
537       prof_simplification_step.HExtlib.profile (simplification_step ~new_cl cl (alist,atable) bag maxvar) x
538     ;;
539
540     let keep_simplified cl (alist,atable) bag maxvar =
541       let rec keep_simplified_aux ~new_cl cl (alist,atable) bag newc =
542         if new_cl then
543           match simplification_step ~new_cl cl (alist,atable) bag maxvar cl with
544             | _,(None, _) -> assert false
545             | bag,(Some _, None) -> bag,None
546             | bag,(Some _, Some (clause, (alist,atable), newa)) ->
547                 keep_simplified_aux ~new_cl:(cl!=clause) clause (alist,atable)
548                   bag (newa@newc)
549         else
550           match newc with
551             | [] -> bag, Some (cl, (alist,atable))
552             | hd::tl ->
553                 match simplification_step ~new_cl cl
554                   (alist,atable) bag maxvar hd with
555                   | _,(None,None) -> assert false
556                   | bag,(Some _,None) ->
557                       keep_simplified_aux ~new_cl cl (alist,atable) bag tl
558                   | bag,(None, Some _) -> bag,None
559                   | bag,(Some cl1, Some (clause, (alist,atable), newa)) ->
560                       let alist,atable =
561                         (clause::alist, IDX.index_unit_clause atable clause)
562                       in
563                         keep_simplified_aux ~new_cl:(cl!=cl1) cl1 (alist,atable)
564                           bag (newa@tl)
565       in
566         keep_simplified_aux ~new_cl:true cl (alist,atable) bag []
567     ;;
568     let prof_keep_simplified = HExtlib.profile ~enable "keep_simplified";;
569     let keep_simplified cl t bag x =
570       prof_keep_simplified.HExtlib.profile (keep_simplified cl t bag) x
571     ;;
572
573     (* this is like simplify but raises Success *)
574     let simplify_goal ~no_demod maxvar table bag g_actives clause = 
575       let bag, clause = 
576         if no_demod then bag, clause else demodulate bag clause table 
577       in
578       if List.exists (are_alpha_eq clause) g_actives then None else
579       if (is_identity_clause ~unify:true clause)
580       then raise (Success (bag, maxvar, clause))
581       else   
582         let (id,lit,vl,_) = clause in 
583         if vl = [] then Some (bag,clause)
584         else
585          let l,r,ty = 
586            match lit with
587              | Terms.Equation(l,r,ty,_) -> l,r,ty
588              | _ -> assert false 
589          in
590          match deep_eq ~unify:true l r ty [] (fun x -> x) (fun x -> x) 
591            table (Some(bag,maxvar,clause,Subst.id_subst)) with
592          | None -> Some (bag,clause)
593          | Some (bag,maxvar,cl,subst) -> 
594              prerr_endline "Goal subsumed";
595              raise (Success (bag,maxvar,cl))
596 (*
597       else match is_subsumed ~unify:true bag maxvar clause table with
598         | None -> Some (bag, clause)
599         | Some ((bag,maxvar),c) -> 
600             prerr_endline "Goal subsumed";
601             raise (Success (bag,maxvar,c))
602 *) 
603     ;;
604
605     let prof_simplify_goal = HExtlib.profile ~enable "simplify_goal";;
606     let  simplify_goal ~no_demod maxvar table bag g_actives x =
607       prof_simplify_goal.HExtlib.profile ( simplify_goal ~no_demod maxvar table bag g_actives) x
608     ;;
609
610     (* =================== inference ===================== *)
611
612     (* this is OK for both the sup_left and sup_right inference steps *)
613     let superposition table varlist subterm pos context =
614       let cands = IDX.DT.retrieve_unifiables table subterm in
615       HExtlib.filter_map
616         (fun (dir, (id,lit,vl,_ (*as uc*))) ->
617            match lit with
618            | Terms.Predicate _ -> assert false
619            | Terms.Equation (l,r,_,o) ->
620                let side, newside = if dir=Terms.Left2Right then l,r else r,l in
621                try 
622                  let subst = 
623                    Unif.unification (varlist@vl) [] subterm side 
624                  in 
625                  if o = Terms.Incomparable then
626                    let side = Subst.apply_subst subst side in
627                    let newside = Subst.apply_subst subst newside in
628                    let o = Order.compare_terms side newside in
629                    (* XXX: check Riazanov p. 33 (iii) *)
630                    if o <> Terms.Lt && o <> Terms.Eq then  
631                      Some (context newside, subst, id, pos, dir)
632                    else 
633                      ((*prerr_endline ("Filtering: " ^ 
634                         Pp.pp_foterm side ^ " =(< || =)" ^ 
635                         Pp.pp_foterm newside);*)None)
636                  else
637                    Some (context newside, subst, id, pos, dir)
638                with FoUnif.UnificationFailure _ -> None)
639         (IDX.ClauseSet.elements cands)
640     ;;
641
642     (* Superposes selected equation with equalities in table *)
643     let superposition_with_table bag maxvar (id,selected,vl,_) table =
644       match selected with 
645       | Terms.Predicate _ -> assert false
646       | Terms.Equation (l,r,ty,Terms.Lt) ->
647           fold_build_new_clause bag maxvar id Terms.Superposition
648             (fun _ -> true)
649             (all_positions [3] 
650               (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; l; x ])
651               r (superposition table vl))
652       | Terms.Equation (l,r,ty,Terms.Gt) ->
653           fold_build_new_clause bag maxvar id Terms.Superposition
654             (fun _ -> true)
655             (all_positions [2] 
656               (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; x; r ])
657               l (superposition table vl))
658       | Terms.Equation (l,r,ty,Terms.Incomparable) -> 
659           fold_build_new_clause bag maxvar id Terms.Superposition
660             (function (* Riazanov: p.33 condition (iv) *)
661               | Terms.Node [Terms.Leaf eq; ty; l; r ] when B.eq B.eqP eq -> 
662                   Order.compare_terms l r <> Terms.Eq
663               | _ -> assert false)
664             ((all_positions [3] 
665                (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; l; x ])
666                r (superposition table vl)) @         
667              (all_positions [2] 
668                (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; x; r ])
669                l (superposition table vl)))
670       | _ -> assert false
671     ;;
672
673     (* the current equation is normal w.r.t. demodulation with atable
674      * (and is not the identity) *)
675     let infer_right bag maxvar current (alist,atable) = 
676       (* We demodulate actives clause with current until all *
677        * active clauses are reduced w.r.t each other         *)
678       (* let bag, (alist,atable) = keep_simplified (alist,atable) bag [current] in *)
679       let ctable = IDX.index_unit_clause IDX.DT.empty current in
680       (* let bag, (alist, atable) = 
681         let bag, alist = 
682           HExtlib.filter_map_acc (simplify ctable) bag alist
683         in
684         bag, (alist, List.fold_left IDX.index_unit_clause IDX.DT.empty alist)
685       in*)
686         debug "Simplified active clauses with fact";
687       (* We superpose active clauses with current *)
688       let bag, maxvar, new_clauses =
689         List.fold_left 
690           (fun (bag, maxvar, acc) active ->
691              let bag, maxvar, newc = 
692                superposition_with_table bag maxvar active ctable 
693              in
694              bag, maxvar, newc @ acc)
695           (bag, maxvar, []) alist
696       in
697         debug "First superpositions";
698         (* We add current to active clauses so that it can be *
699          * superposed with itself                             *)
700       let alist, atable = 
701         current :: alist, IDX.index_unit_clause atable current
702       in
703         debug "Indexed";
704       let fresh_current, maxvar = Utils.fresh_unit_clause maxvar current in
705         (* We need to put fresh_current into the bag so that all *
706          * variables clauses refer to are known.                 *)
707       let bag, fresh_current = Terms.add_to_bag fresh_current bag in
708         (* We superpose current with active clauses *)
709       let bag, maxvar, additional_new_clauses =
710         superposition_with_table bag maxvar fresh_current atable 
711       in
712         debug "Another superposition";
713       let new_clauses = new_clauses @ additional_new_clauses in
714         debug (Printf.sprintf "Demodulating %d clauses"
715                  (List.length new_clauses));
716       let bag, new_clauses = 
717         HExtlib.filter_map_monad (simplify atable maxvar) bag new_clauses
718       in
719         debug "Demodulated new clauses";
720       bag, maxvar, (alist, atable), new_clauses
721     ;;
722
723     let prof_ir = HExtlib.profile ~enable "infer_right";;
724     let infer_right bag maxvar current t = 
725       prof_ir.HExtlib.profile (infer_right bag maxvar current) t
726     ;;
727
728     let infer_left bag maxvar goal (_alist, atable) =
729         (* We superpose the goal with active clauses *)
730      if (match goal with (_,_,[],_) -> true | _ -> false) then bag, maxvar, []
731      else
732       let bag, maxvar, new_goals =        
733         superposition_with_table bag maxvar goal atable 
734       in
735         debug "Superposed goal with active clauses";
736         (* We simplify the new goals with active clauses *)
737       let bag, new_goals = 
738         List.fold_left
739          (fun (bag, acc) g -> 
740             match simplify_goal ~no_demod:false maxvar atable bag [] g with
741               | None -> assert false
742               | Some (bag,g) -> bag,g::acc)
743          (bag, []) new_goals
744       in
745         debug "Simplified new goals with active clauses";
746       bag, maxvar, List.rev new_goals
747     ;;
748
749     let prof_il = HExtlib.profile ~enable "infer_left";;
750     let infer_left bag maxvar goal t = 
751       prof_il.HExtlib.profile (infer_left bag maxvar goal) t
752     ;;
753
754   end