]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_paramodulation/superposition.ml
Fixed typo
[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 : Orderings.Blob) = 
15   struct
16     module IDX = Index.Index(B)
17     module Unif = FoUnif.FoUnif(B)
18     module Subst = FoSubst 
19     module Order = B
20     module Utils = FoUtils.Utils(B)
21     module Pp = Pp.Pp(B)
22     module Clauses = Clauses.Clauses(B)
23     
24     exception Success of B.t Terms.bag * int * B.t Terms.clause
25
26     let debug s = prerr_endline (Lazy.force s);;
27     let debug _ = ();;
28     let enable = true;;
29
30     let rec list_first f = function
31       | [] -> None
32       | x::tl -> match f x with Some _ as x -> x | _ -> list_first f tl
33     ;;
34
35     let first_position pos ctx t f =
36       let inject_pos pos ctx = function
37         | None -> None
38         | Some (a,b,c,d) -> Some(ctx a,b,c,d,pos)
39       in
40       let rec aux pos ctx = function
41       | Terms.Leaf _ as t -> inject_pos pos ctx (f t)
42       | Terms.Var _ -> None
43       | Terms.Node l as t->
44           match f t with
45           | Some _ as x -> inject_pos pos ctx x
46           | None ->
47               let rec first pre post = function
48                 | [] -> None
49                 | t :: tl -> 
50                      let newctx = fun x -> ctx (Terms.Node (pre@[x]@post)) in
51                      match aux (List.length pre :: pos) newctx t with
52                      | Some _ as x -> x
53                      | None -> 
54                          if post = [] then None (* tl is also empty *)
55                          else first (pre @ [t]) (List.tl post) tl
56               in
57                 first [] (List.tl l) l 
58       in
59         aux pos ctx t
60     ;;
61                                      
62     let all_positions pos ctx t f =
63       let rec aux pos ctx = function
64       | Terms.Leaf _ as t -> f t pos ctx 
65       | Terms.Var _ -> []
66       | Terms.Node l as t-> 
67           let acc, _, _ = 
68             List.fold_left
69             (fun (acc,pre,post) t -> (* Invariant: pre @ [t] @ post = l *)
70                 let newctx = fun x -> ctx (Terms.Node (pre@[x]@post)) in
71                 let acc = aux (List.length pre :: pos) newctx t @ acc in
72                 if post = [] then acc, l, []
73                 else acc, pre @ [t], List.tl post)
74              (f t pos ctx, [], List.tl l) l
75           in
76            acc
77       in
78         aux pos ctx t
79     ;;
80
81     let parallel_positions bag pos ctx id lit t f =
82       let rec aux bag pos ctx id lit = function
83       | Terms.Leaf _ as t -> f bag t pos ctx id lit
84       | Terms.Var _ as t -> bag,t,id,lit
85       | Terms.Node (hd::l) as t->
86           let bag,t,id1,lit = f bag t pos ctx id lit in
87             if id = id1 then
88               let bag, l, _, id, lit = 
89                 List.fold_left
90                   (fun (bag,pre,post,id,lit) t ->
91                      let newctx = fun x -> ctx (Terms.Node (pre@[x]@post)) in
92                      let newpos = (List.length pre)::pos in
93                      let bag,newt,id,lit = aux bag newpos newctx id lit t in
94                        if post = [] then bag, pre@[newt], [], id,lit
95                        else bag, pre @ [newt], List.tl post, id, lit)
96                   (bag, [hd], List.tl l, id,lit) l
97               in
98                 bag, Terms.Node l, id, lit
99             else bag,t,id1,lit
100       | _ -> assert false
101       in
102         aux bag pos ctx id lit t
103     ;;
104
105     let visit bag pos ctx id lit t f =
106       let rec aux bag pos ctx id subst lit = function
107       | Terms.Leaf _ as t -> 
108           let  bag,subst,t,id,lit = f bag t pos ctx id lit
109           in assert (subst=[]); bag,t,id,lit
110       | Terms.Var i as t ->  
111           let t= Subst.apply_subst subst t in
112             bag,t,id,lit
113       | Terms.Node (hd::l) ->
114           let bag, l, _, id,lit = 
115             List.fold_left
116               (fun (bag,pre,post,id,lit) t ->
117                  let newctx = fun x -> ctx (Terms.Node (pre@[x]@post)) in
118                  let newpos = (List.length pre)::pos in
119                  let bag,newt,id,lit = aux bag newpos newctx id subst lit t in
120                    if post = [] then bag, pre@[newt], [], id, lit
121                    else bag, pre @ [newt], List.tl post, id, lit)
122               (bag, [hd], List.map (Subst.apply_subst subst) (List.tl l), id, lit) l
123           in
124           let bag,subst,t,id1,lit = f bag (Terms.Node l) pos ctx id lit
125           in
126             if id1 = id then (assert (subst=[]); bag,t,id,lit)
127             else aux bag pos ctx id1 subst lit t
128       | _ -> assert false
129       in
130         aux bag pos ctx id [] lit t
131     ;;
132
133     let build_clause ~fresh bag maxvar filter rule t subst id id2 pos dir clause_ctx =
134       let proof = Terms.Step(rule,id,id2,dir,pos,subst) in
135       let t = Subst.apply_subst subst t in
136       if filter subst then
137         let literal = 
138           match t with
139           | Terms.Node [ Terms.Leaf eq ; ty; l; r ] when B.eq B.eqP eq ->
140                let o = Order.compare_terms l r in
141                Terms.Equation (l, r, ty, o)
142           | t -> Terms.Predicate t
143         in
144         let nlit,plit = clause_ctx literal in
145         let cl = (0, nlit, plit, [], proof) in 
146         let vl = Clauses.vars_of_clause cl in
147         let cl,maxvar = 
148             if fresh then Clauses.fresh_clause ~subst maxvar (0, nlit, plit, vl, proof) 
149             else (0,nlit,plit,vl,proof),maxvar
150         in
151         let bag, cl = 
152           Terms.add_to_bag cl bag
153         in
154         debug (lazy (Pp.pp_clause cl));
155         Some (bag, maxvar, cl, literal)
156       else
157         ((*prerr_endline ("Filtering: " ^ Pp.pp_foterm t);*)None)
158     ;;
159     let prof_build_clause = HExtlib.profile ~enable "build_clause";;
160     let build_clause ~fresh bag maxvar filter rule t subst id id2 pos x =
161       prof_build_clause.HExtlib.profile
162           (build_clause ~fresh bag maxvar filter rule t subst id id2 pos) x
163     ;;
164       
165     
166     (* ============ simplification ================= *)
167     let prof_demod_u = HExtlib.profile ~enable "demod.unify";;
168     let prof_demod_r = HExtlib.profile ~enable "demod.retrieve_generalizations";;
169     let prof_demod_o = HExtlib.profile ~enable "demod.compare_terms";;
170     let prof_demod_s = HExtlib.profile ~enable "demod.apply_subst";;
171
172     let demod table varlist subterm =
173       let cands = 
174         prof_demod_r.HExtlib.profile 
175          (IDX.DT.retrieve_generalizations table) subterm 
176       in
177       list_first
178         (fun (dir, is_pos, pos, (id,nlit,plit,vl,_)) ->
179           match nlit,plit with
180            | [], [(lit,_)] ->
181               (match lit with
182               | Terms.Predicate _ -> assert false
183               | Terms.Equation (l,r,_,o) ->
184                   let side, newside = if dir=Terms.Left2Right then l,r else r,l in
185                   try 
186                     let subst =
187                       prof_demod_u.HExtlib.profile 
188                         (Unif.unification (* (varlist@vl) *) varlist subterm) side 
189                     in 
190                     let side = 
191                       prof_demod_s.HExtlib.profile 
192                         (Subst.apply_subst subst) side 
193                     in
194                     let newside = 
195                       prof_demod_s.HExtlib.profile 
196                         (Subst.apply_subst subst) newside 
197                     in
198                     if o = Terms.Incomparable || o = Terms.Invertible then
199                       let o = 
200                         prof_demod_o.HExtlib.profile 
201                          (Order.compare_terms newside) side in
202                       (* Riazanov, pp. 45 (ii) *)
203                       if o = Terms.Lt then
204                         Some (newside, subst, id, dir)
205                       else 
206                         ((*prerr_endline ("Filtering: " ^ 
207                            Pp.pp_foterm side ^ " =(< || =)" ^ 
208                            Pp.pp_foterm newside ^ " coming from " ^ 
209                            Pp.pp_clause uc );*)None)
210                     else
211                       Some (newside, subst, id, dir)
212                   with FoUnif.UnificationFailure _ -> None)
213           |  _ -> None)
214               (IDX.ClauseSet.elements cands)
215     ;;
216     let prof_demod = HExtlib.profile ~enable "demod";;
217     let demod table varlist x =
218       prof_demod.HExtlib.profile (demod table varlist) x
219     ;;
220
221     let mydemod table varlist subterm =
222       let cands = 
223         prof_demod_r.HExtlib.profile 
224          (IDX.DT.retrieve_generalizations table) subterm 
225       in
226       list_first
227         (fun (dir, is_pos, pos, (id,nlit,plit,vl,_)) ->
228          match (nlit,plit) with
229           | [],[(lit,_)] ->
230            (match lit with
231            | Terms.Predicate _ -> assert false
232            | Terms.Equation (l,r,_,o) ->
233                let side, newside = if dir=Terms.Left2Right then l,r else r,l in
234                try 
235                  let subst =
236                    prof_demod_u.HExtlib.profile 
237                      (Unif.unification (* (varlist@vl) *) varlist subterm) side 
238                  in 
239                  let iside = 
240                    prof_demod_s.HExtlib.profile 
241                      (Subst.apply_subst subst) side 
242                  in
243                  let inewside = 
244                    prof_demod_s.HExtlib.profile 
245                      (Subst.apply_subst subst) newside 
246                  in
247                  if o = Terms.Incomparable || o = Terms.Invertible then
248                    let o = 
249                      prof_demod_o.HExtlib.profile 
250                       (Order.compare_terms inewside) iside in
251                    (* Riazanov, pp. 45 (ii) *)
252                    if o = Terms.Lt then
253                      Some (newside, subst, id, dir)
254                    else 
255                      ((*prerr_endline ("Filtering: " ^ 
256                         Pp.pp_foterm side ^ " =(< || =)" ^ 
257                         Pp.pp_foterm newside ^ " coming from " ^ 
258                         Pp.pp_unit_clause uc );*)None)
259                  else
260                    Some (newside, subst, id, dir)
261                with FoUnif.UnificationFailure _ -> None)
262           | _ -> assert false)
263         (IDX.ClauseSet.elements cands)
264     ;;
265
266     let ctx_demod table vl clause_ctx bag t pos ctx id lit =
267       match mydemod table vl t with
268         | None -> (bag,[],t,id,lit)
269         | Some (newside, subst, id2, dir) ->
270             let inewside = Subst.apply_subst subst newside in
271             match build_clause ~fresh:false bag 0 (fun _ -> true)
272               Terms.Demodulation (ctx inewside) subst id id2 pos dir clause_ctx
273             with
274               | None -> assert false
275               | Some (bag,_,(id,_,_,_,_),lit) ->
276                     (bag,subst,newside,id,lit)
277     ;;
278       
279     let rec demodulate bag (id,nlit,plit,vl,proof) table =
280       let rec demod_lit ~jump_to_right bag id lit clause_ctx =
281        (match lit with
282       | Terms.Predicate t -> assert false
283       | Terms.Equation (l,r,ty,_) ->
284           let bag,l,id1,lit = 
285             visit bag [2]
286             (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; x; r ]) id lit l
287             (ctx_demod table vl clause_ctx)
288           in 
289           let bag,_,id2,lit = 
290             visit bag [3]
291               (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; l; x ]) id1 lit r
292               (ctx_demod table vl clause_ctx)
293           in
294             bag,id2,lit)
295       in
296      let nlit,_,bag,id = if nlit = [] then nlit,[],bag,id
297        else List.fold_left
298        (fun (pre,post,bag,id) (lit,sel) ->
299           let bag, id, lit =
300               demod_lit ~jump_to_right:false bag id lit (fun l -> pre@[l,sel]@post,plit)
301           in
302              if post=[] then pre@[(lit,sel)],[],bag,id
303              else pre@[(lit,sel)],List.tl post,bag,id)
304        ([],List.tl nlit, bag, id) nlit
305       in
306       let _,_,bag,id = if plit = [] then plit,[],bag,id
307        else List.fold_left
308        (fun (pre,post,bag,id) (lit,sel) ->
309           let bag, id, lit = 
310               demod_lit ~jump_to_right:false bag id lit (fun l -> nlit,pre@[l,sel]@post)
311           in
312              if post=[] then pre@[(lit,sel)],[],bag,id
313              else pre@[(lit,sel)],List.tl post,bag,id)
314        ([],List.tl plit, bag, id) plit
315       in
316        let cl,_,_ = Terms.get_from_bag id bag in
317         bag,cl
318     ;;
319
320     let parallel_demod table vl clause_ctx bag t pos ctx id lit =
321       match demod table vl t with
322         | None -> (bag,t,id,lit)
323         | Some (newside, subst, id2, dir) ->
324             match build_clause ~fresh:false bag 0 (fun _ -> true)
325               Terms.Demodulation (ctx newside) subst id id2 pos dir clause_ctx
326             with
327               | None -> assert false
328               | Some (bag,_,(id,_,_,_,_),lit) ->
329                     (bag,newside,id,lit)
330     ;;
331
332     let demodulate_once ~jump_to_right bag id literal vl table clause_ctx =
333       match literal with
334       | Terms.Predicate t -> assert false
335       | Terms.Equation (l,r,ty,_) as lit ->
336           let bag,l,id1,lit = if jump_to_right then (bag,l,id,lit) else
337             parallel_positions bag [2]
338               (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; x; r ]) id lit l
339               (parallel_demod table vl clause_ctx)
340           in
341           let jump_to_right = id1 = id in
342           let bag,r,id2,lit =
343             parallel_positions bag [3]
344               (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; l; x ]) id1 lit r
345               (parallel_demod table vl clause_ctx)
346           in
347             if id = id2 then None
348             else
349               Some ((bag,id2,lit),jump_to_right)
350     ;;
351
352     let rec demodulate_old bag (id,nlit,plit,vl,proof) table =
353       let rec demod_lit ~jump_to_right bag id lit clause_ctx =
354          match demodulate_once ~jump_to_right bag id lit vl table clause_ctx with
355          | None -> bag, id, lit
356          | Some ((bag, id, lit),jump) ->
357              demod_lit ~jump_to_right:jump bag id lit clause_ctx
358       in
359       (*match nlit,plit with
360       |[],[lit,_] ->
361         let bag, id, lit = demod_lit ~jump_to_right:false bag id lit (fun l -> nlit, [l,true])
362         in
363         let cl,_,_ = Terms.get_from_bag id bag in
364         bag,cl
365       |[lit,_],[] ->
366         let bag, id, lit = demod_lit ~jump_to_right:false bag id lit (fun l -> [l,true],[])
367         in
368         let cl,_,_ = Terms.get_from_bag id bag in
369         bag,cl
370       |_ -> assert false*)
371       let nlit,_,bag,id = if nlit = [] then nlit,[],bag,id
372        else List.fold_left
373        (fun (pre,post,bag,id) (lit,sel) ->
374           let bag, id, lit =
375               demod_lit ~jump_to_right:false bag id lit (fun l -> pre@[l,sel]@post,plit)
376           in
377              if post=[] then pre@[(lit,sel)],[],bag,id
378              else pre@[(lit,sel)],List.tl post,bag,id)
379        ([],List.tl nlit, bag, id) nlit
380       in
381       let _,_,bag,id = if plit = [] then plit,[],bag,id
382        else List.fold_left
383        (fun (pre,post,bag,id) (lit,sel) ->
384           let bag, id, lit = 
385               demod_lit ~jump_to_right:false bag id lit (fun l -> nlit,pre@[l,sel]@post)
386           in
387              if post=[] then pre@[(lit,sel)],[],bag,id
388              else pre@[(lit,sel)],List.tl post,bag,id)
389        ([],List.tl plit, bag, id) plit
390       in
391        let cl,_,_ = Terms.get_from_bag id bag in
392         bag,cl
393     ;;
394
395     let prof_demodulate = HExtlib.profile ~enable "demodulate";;
396     let demodulate bag clause x =
397       prof_demodulate.HExtlib.profile (demodulate bag clause) x
398     ;;
399
400     (* move away *)
401     let is_identity_clause ~unify = function
402       | _, [], [Terms.Equation (_,_,_,Terms.Eq),_], _, _ -> true
403       | _, [], [Terms.Equation (l,r,_,_),_], vl, _ when unify ->
404           (try ignore(Unif.unification (* vl *) [] l r); true
405           with FoUnif.UnificationFailure _ -> false)
406       | _ -> false
407     ;;
408
409     let is_goal_trivial = function
410       | _, [Terms.Equation (_,_,_,Terms.Eq),_], [], _, _ -> true
411       | _, [Terms.Equation (l,r,_,_),_], [], vl, _ ->
412           (try ignore(Unif.unification (* vl *) [] l r); true
413           with FoUnif.UnificationFailure _ -> false)
414       | _ -> false
415
416     let fold_build_new_clause bag maxvar id rule filter res clause_ctx =
417       debug (lazy (string_of_int (List.length res)));
418       let (bag, maxvar), res =
419        HExtlib.filter_map_acc 
420          (fun (bag, maxvar) (t,subst,id2,pos,dir) ->
421            match build_clause ~fresh:true bag maxvar filter 
422                   rule t subst id id2 pos dir clause_ctx with
423             | None -> None
424             | Some (bag,maxvar,res,_) -> Some ((bag,maxvar),res))
425          (bag, maxvar) res
426       in
427        bag, maxvar, res
428     ;;
429
430     (* Tries to rewrite an equality to identity, using unit equalities in table *)    
431     let rewrite_eq ~unify l r ty vl table =
432       let retrieve = if unify then IDX.DT.retrieve_unifiables
433       else IDX.DT.retrieve_generalizations in
434       let lcands = retrieve table l in
435       let rcands = retrieve table r in
436       let f b c = 
437         let id, dir, l, r, vl = 
438           match c with
439             | (d,_,_, (id,[],[Terms.Equation (l,r,ty,_),_],vl,_))-> id, d, l, r, vl
440             | (d,_,_, (id,[Terms.Equation (l,r,ty,_),_],[],vl,_))-> id, d, l, r, vl
441             |_ -> assert false 
442         in 
443         let reverse = (dir = Terms.Left2Right) = b in
444         let l, r, proof_rewrite_dir = if reverse then l,r,Terms.Left2Right
445         else r,l, Terms.Right2Left in
446           (id,proof_rewrite_dir,Terms.Node [ Terms.Leaf B.eqP; ty; l; r ], vl)
447       in
448       let cands1 = List.map (f true) (IDX.ClauseSet.elements lcands) in
449       let cands2 = List.map (f false) (IDX.ClauseSet.elements rcands) in
450       let t = Terms.Node [ Terms.Leaf B.eqP; ty; l; r ] in
451       let locked_vars = if unify then [] else vl in
452       let rec aux = function
453         | [] -> None
454         | (id2,dir,c,vl1)::tl ->
455             try
456               let subst = Unif.unification (* (vl@vl1) *) locked_vars c t in
457               Some (id2, dir, subst)
458             with FoUnif.UnificationFailure _ -> aux tl
459       in
460         aux (cands1 @ cands2)
461     ;;
462
463     let is_subsumed ~unify bag maxvar (id, nlit, plit, vl, _) table =
464       match nlit,plit with
465       |  [Terms.Equation (l,r,ty,_) ,_],[] 
466       | [],[Terms.Equation (l,r,ty,_) ,_]-> 
467           (match rewrite_eq ~unify l r ty vl table with
468             | None -> None
469             | Some (id2, dir, subst) ->
470                 let id_t = Terms.Node [ Terms.Leaf B.eqP; ty; r; r ] in
471                   build_clause ~fresh:true bag maxvar (fun _ -> true)
472                   Terms.Superposition id_t subst id id2 [2] dir (fun l -> [],[l,true]))
473      | _ -> None (* TODO : implement subsumption for clauses *)
474     ;;
475     let prof_is_subsumed = HExtlib.profile ~enable "is_subsumed";;
476     let is_subsumed ~unify bag maxvar c x =
477       prof_is_subsumed.HExtlib.profile (is_subsumed ~unify bag maxvar c) x
478     ;;
479     (* id refers to a clause proving contextl l = contextr r *)
480
481     let rec deep_eq ~unify l r ty pos contextl contextr table acc =
482       match acc with 
483       | None -> None
484       | Some(bag,maxvar,(id,nlit,plit,vl,p as cl),subst) -> 
485           let l = Subst.apply_subst subst l in 
486           let r = Subst.apply_subst subst r in 
487             try 
488               let subst1 = Unif.unification (* vl *) [] l r in
489               let lit = 
490                 match nlit,plit with
491                   | [Terms.Equation (l,r,ty,o),_],[] -> 
492                      Terms.Equation (FoSubst.apply_subst subst1 l,
493                        FoSubst.apply_subst subst1 r, ty, o)
494                   | _ -> debug (lazy (Pp.pp_clause cl));assert false
495               in
496                 Some(bag,maxvar,(id,[lit,true],[],vl,p),Subst.concat subst1 subst)
497             with FoUnif.UnificationFailure _ -> 
498               match rewrite_eq ~unify l r ty vl table with
499               | Some (id2, dir, subst1) ->
500                   let newsubst = Subst.concat subst1 subst in
501                   let id_t = 
502                     FoSubst.apply_subst newsubst
503                       (Terms.Node[Terms.Leaf B.eqP;ty;contextl r;contextr r]) 
504                   in
505                     (match 
506                       build_clause ~fresh:true bag maxvar (fun _ -> true)
507                         Terms.Superposition id_t 
508                         subst1 id id2 (pos@[2]) dir (fun l -> [l,true],[])
509                     with
510                     | Some (bag, maxvar, c, _) -> 
511                         Some(bag,maxvar,c,newsubst)
512                     | None -> assert false)
513               | None ->
514                   match l,r with 
515                   | Terms.Node (a::la), Terms.Node (b::lb) when 
516                       a = b && List.length la = List.length lb ->
517                       let acc,_,_,_ =
518                         List.fold_left2 
519                           (fun (acc,pre,postl,postr) a b -> 
520                              let newcl = 
521                               fun x -> contextl(Terms.Node (pre@(x::postl))) in
522                              let newcr = 
523                               fun x -> contextr(Terms.Node (pre@(x::postr))) in
524                              let newpos = List.length pre::pos in
525                              let footail l =
526                                if l = [] then [] else List.tl l in
527                                (deep_eq ~unify a b ty 
528                                  newpos newcl newcr table acc,pre@[b],
529                                  footail postl, footail postr))
530                           (acc,[a],List.tl la,List.tl lb) la lb
531                       in acc
532                   | _,_ -> None
533     ;;
534     let prof_deep_eq = HExtlib.profile ~enable "deep_eq";;
535     let deep_eq ~unify l r ty pos contextl contextr table x =
536       prof_deep_eq.HExtlib.profile (deep_eq ~unify l r ty pos contextl contextr table) x
537     ;;
538
539     let rec orphan_murder bag acc i =
540       match Terms.get_from_bag i bag with
541         | (_,_,_,_,Terms.Exact _),discarded,_ -> (discarded,acc)
542         | (_,_,_,_,Terms.Step (_,i1,i2,_,_,_)),true,_ -> (true,acc)
543         | (_,_,_,_,Terms.Step (_,i1,i2,_,_,_)),false,_ ->
544             if (List.mem i acc) then (false,acc)
545             else match orphan_murder bag acc i1 with
546               | (true,acc) -> (true,acc)
547               | (false,acc) ->
548                   let (res,acc) = orphan_murder bag acc i2 in
549                   if res then res,acc else res,i::acc
550     ;;
551
552     let orphan_murder bag actives cl =
553       let (id,_,_,_,_) = cl in
554       let actives = List.map (fun (i,_,_,_,_) -> i) actives in
555       let (res,_) = orphan_murder bag actives id in
556         if res then debug (lazy "Orphan murdered"); res
557     ;;
558     let prof_orphan_murder = HExtlib.profile ~enable "orphan_murder";;
559     let orphan_murder bag actives x =
560       prof_orphan_murder.HExtlib.profile (orphan_murder bag actives) x
561     ;;
562
563     (* demodulate and check for subsumption *)
564     let simplify table maxvar bag clause = 
565       if is_identity_clause ~unify:false clause then bag,None
566       (* else if orphan_murder bag actives clause then bag,None *)
567       else let bag, clause = demodulate bag clause table in
568       if is_identity_clause ~unify:false clause then bag,None
569       else
570         match is_subsumed ~unify:false bag maxvar clause table with
571           | None -> bag, Some clause
572           | Some _ -> bag, None
573     ;;
574
575     let simplify table maxvar bag clause =
576       match simplify table maxvar bag clause with
577         | bag, None ->
578             let (id,_,_,_,_) = clause in
579             let (_,_,iter) = Terms.get_from_bag id bag in
580             Terms.replace_in_bag (clause,true,iter) bag, None
581         | bag, Some clause -> bag, Some clause
582     (*let (id,_,_,_) = clause in
583             if orphan_murder bag clause then
584               Terms.M.add id (clause,true) bag, Some clause
585             else bag, Some clause*)
586     ;;
587     let prof_simplify = HExtlib.profile ~enable "simplify";;
588     let simplify table maxvar bag x =
589       prof_simplify.HExtlib.profile (simplify table maxvar bag ) x
590     ;;
591
592     let one_pass_simplification new_clause (alist,atable) bag maxvar =
593       match simplify atable maxvar bag new_clause with
594         | bag,None -> bag,None (* new_clause has been discarded *)
595         | bag,(Some clause) ->
596             let ctable = IDX.index_clause IDX.DT.empty clause in
597             let bag, alist, atable = 
598               List.fold_left 
599                 (fun (bag, alist, atable) c ->
600                    match simplify ctable maxvar bag c with
601                      |bag,None -> (bag,alist,atable)
602                         (* an active clause as been discarded *)
603                      |bag,Some c1 ->
604                         bag, c :: alist, IDX.index_clause atable c)
605                 (bag,[],IDX.DT.empty) alist
606             in
607               bag, Some (clause, (alist,atable))
608     ;;
609     let prof_one_pass_simplification = HExtlib.profile ~enable "one_pass_simplification";;
610     let one_pass_simplification new_clause t bag x =
611       prof_one_pass_simplification.HExtlib.profile (one_pass_simplification new_clause t bag ) x
612     ;;
613
614     let simplification_step ~new_cl cl (alist,atable) bag maxvar new_clause =
615       let atable1 =
616         if new_cl then atable else
617         IDX.index_clause atable cl
618       in
619         (* Simplification of new_clause with :      *
620          * - actives and cl if new_clause is not cl *
621          * - only actives otherwise                 *)
622         match
623           simplify atable1 maxvar bag new_clause with
624           | bag,None -> bag,(Some cl, None) (* new_clause has been discarded *)
625           | bag,Some clause ->
626               (* Simplification of each active clause with clause *
627                * which is the simplified form of new_clause       *)
628               let ctable = IDX.index_clause IDX.DT.empty clause in
629               let bag, newa, alist, atable = 
630                 List.fold_left 
631                   (fun (bag, newa, alist, atable) c ->
632                      match simplify ctable maxvar bag c with
633                        |bag,None -> (bag, newa, alist, atable)
634                           (* an active clause as been discarded *)
635                        |bag,Some c1 ->
636                             if (c1 == c) then 
637                               bag, newa, c :: alist,
638                             IDX.index_clause atable c
639                             else
640                               bag, c1 :: newa, alist, atable)                  
641                   (bag,[],[],IDX.DT.empty) alist
642               in
643                 if new_cl then
644                   bag, (Some cl, Some (clause, (alist,atable), newa))
645                 else
646                   (* if new_clause is not cl, we simplify cl with clause *)
647                   match simplify ctable maxvar bag cl with
648                     | bag,None ->
649                         (* cl has been discarded *)
650                         bag,(None, Some (clause, (alist,atable), newa))
651                     | bag,Some cl1 ->
652                         bag,(Some cl1, Some (clause, (alist,atable), newa))
653     ;;
654     let prof_simplification_step = HExtlib.profile ~enable "simplification_step";;
655     let simplification_step ~new_cl cl (alist,atable) bag maxvar x =
656       prof_simplification_step.HExtlib.profile (simplification_step ~new_cl cl (alist,atable) bag maxvar) x
657     ;;
658
659     let keep_simplified cl (alist,atable) bag maxvar =
660       let rec keep_simplified_aux ~new_cl cl (alist,atable) bag newc =
661         if new_cl then
662           match simplification_step ~new_cl cl (alist,atable) bag maxvar cl with
663             | _,(None, _) -> assert false
664             | bag,(Some _, None) -> bag,None
665             | bag,(Some _, Some (clause, (alist,atable), newa)) ->
666                 keep_simplified_aux ~new_cl:(cl!=clause) clause (alist,atable)
667                   bag (newa@newc)
668         else
669           match newc with
670             | [] -> bag, Some (cl, (alist,atable))
671             | hd::tl ->
672                 match simplification_step ~new_cl cl
673                   (alist,atable) bag maxvar hd with
674                   | _,(None,None) -> assert false
675                   | bag,(Some _,None) ->
676                       keep_simplified_aux ~new_cl cl (alist,atable) bag tl
677                   | bag,(None, Some _) -> bag,None
678                   | bag,(Some cl1, Some (clause, (alist,atable), newa)) ->
679                       let alist,atable =
680                         (clause::alist, IDX.index_clause atable clause)
681                       in
682                         keep_simplified_aux ~new_cl:(cl!=cl1) cl1 (alist,atable)
683                           bag (newa@tl)
684       in
685         keep_simplified_aux ~new_cl:true cl (alist,atable) bag []
686     ;;
687     let prof_keep_simplified = HExtlib.profile ~enable "keep_simplified";;
688     let keep_simplified cl t bag x =
689       prof_keep_simplified.HExtlib.profile (keep_simplified cl t bag) x
690     ;;
691
692     (* this is like simplify but raises Success *)
693     let simplify_goal ~no_demod maxvar table bag g_actives clause = 
694       let bag, clause = 
695         if no_demod then bag, clause else demodulate bag clause table 
696       in
697       if List.exists (Clauses.are_alpha_eq_cl clause) g_actives then None else
698       if (is_goal_trivial clause)
699       then raise (Success (bag, maxvar, clause))
700       else   
701         let (id,nlit,plit,vl,_) = clause in 
702         if vl = [] then Some (bag,clause)
703         else
704          let l,r,ty = 
705            match nlit,plit with
706              | [Terms.Equation(l,r,ty,_),_],[] -> l,r,ty
707              | _ -> assert false 
708          in
709          match deep_eq ~unify:true l r ty [] (fun x -> x) (fun x -> x) 
710            table (Some(bag,maxvar,clause,Subst.id_subst)) with
711          | None -> Some (bag,clause)
712          | Some (bag,maxvar,cl,subst) -> 
713              prerr_endline "Goal subsumed";
714              raise (Success (bag,maxvar,cl))
715 (*
716       else match is_subsumed ~unify:true bag maxvar clause table with
717         | None -> Some (bag, clause)
718         | Some ((bag,maxvar),c) -> 
719             prerr_endline "Goal subsumed";
720             raise (Success (bag,maxvar,c))
721 *) 
722     ;;
723
724     let prof_simplify_goal = HExtlib.profile ~enable "simplify_goal";;
725     let  simplify_goal ~no_demod maxvar table bag g_actives x =
726       prof_simplify_goal.HExtlib.profile ( simplify_goal ~no_demod maxvar table bag g_actives) x
727     ;;
728
729     (* =================== inference ===================== *)
730
731     (* this is OK for both the sup_left and sup_right inference steps *)
732     let superposition table varlist is_pos subterm pos context =
733       let cands = IDX.DT.retrieve_unifiables table subterm in
734       HExtlib.filter_map
735         (fun (dir, is_cand_pos, _, (id,nlit,plit,vl,_ (*as uc*))) ->
736            match nlit,plit with
737            | [],[Terms.Equation (l,r,_,o),_] ->
738                (let side, newside = if dir=Terms.Left2Right then l,r else r,l in
739                debug (lazy (Pp.pp_foterm subterm));
740                debug (lazy (Pp.pp_foterm side));
741                try 
742                  let subst = 
743                    Unif.unification (* (varlist@vl)*)  [] subterm side 
744                  in 
745                  if o = Terms.Incomparable || o = Terms.Invertible then
746                    let side = Subst.apply_subst subst side in
747                    let newside = Subst.apply_subst subst newside in
748                    let o = Order.compare_terms side newside in
749                    (* XXX: check Riazanov p. 33 (iii) *)
750                    if o <> Terms.Lt && o <> Terms.Eq then  
751                      Some (context newside, subst, id, pos, dir)
752                    else 
753                      (debug (lazy "Filtering out..."); None)
754                  else
755                    Some (context newside, subst, id, pos, dir)
756                with FoUnif.UnificationFailure _ -> None)
757             | _ -> assert false)
758         (IDX.ClauseSet.elements cands)
759     ;;
760
761     (* Superposes selected equation with equalities in table *)
762     let superposition_with_table bag maxvar id vl lit is_pos clause_ctx table =
763       match lit with 
764       | Terms.Equation (l,r,ty,Terms.Lt) ->
765           fold_build_new_clause bag maxvar id Terms.Superposition
766             (fun _ -> true)
767             (all_positions [3] 
768               (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; l; x ])
769               r (superposition table vl is_pos)) clause_ctx
770       | Terms.Equation (l,r,ty,Terms.Invertible)
771       | Terms.Equation (l,r,ty,Terms.Gt) ->
772           fold_build_new_clause bag maxvar id Terms.Superposition
773             (fun _ -> true)
774             (all_positions [2] 
775               (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; x; r ])
776               l (superposition table vl is_pos)) clause_ctx
777       | Terms.Equation (l,r,ty,Terms.Incomparable) ->
778           let filtering avoid subst = (* Riazanov: p.33 condition (iv) *)
779             let l = Subst.apply_subst subst l in
780             let r = Subst.apply_subst subst r in
781             let o = Order.compare_terms l r in
782             o <> avoid && o <> Terms.Eq
783           in
784           let bag, maxvar,r_terms =
785             fold_build_new_clause bag maxvar id Terms.Superposition
786               (filtering Terms.Gt)
787               (all_positions [3] 
788                  (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; l; x ])
789                  r (superposition table vl is_pos)) clause_ctx
790           in
791           let bag, maxvar, l_terms =
792             fold_build_new_clause bag maxvar id Terms.Superposition
793               (filtering Terms.Lt)
794               (all_positions [2] 
795                  (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; x; r ])
796                  l (superposition table vl is_pos)) clause_ctx
797           in
798             bag, maxvar, r_terms @ l_terms
799       | _ -> assert false
800     ;;
801
802     let superpose_literal id vl table is_pos (bag,maxvar,pre,post,acc) (lit,sel) =
803       let clause_ctx =
804          if is_pos then fun l -> [],pre@[l,true]@post
805          else fun l -> pre@[l,true]@post,[]
806       in
807       let bag, maxvar, newc =
808         superposition_with_table bag maxvar id vl lit is_pos clause_ctx table
809       in
810          if post = [] then bag,maxvar,pre@[lit,sel],[],newc@acc
811          else bag,maxvar,pre@[lit,sel],List.tl post,newc@acc
812     ;;
813
814
815     (* the current equation is normal w.r.t. demodulation with atable
816      * (and is not the identity) *)
817     let infer_right bag maxvar current (alist,atable) = 
818       (* We demodulate actives clause with current until all *
819        * active clauses are reduced w.r.t each other         *)
820       (* let bag, (alist,atable) = keep_simplified (alist,atable) bag [current] in *)
821       let ctable = IDX.index_clause IDX.DT.empty current in
822       (* let bag, (alist, atable) = 
823         let bag, alist = 
824           HExtlib.filter_map_acc (simplify ctable) bag alist
825         in
826         bag, (alist, List.fold_left IDX.index_clause IDX.DT.empty alist)
827       in*)
828         debug (lazy "Simplified active clauses with fact");
829       (* We superpose active clauses with current *)
830       let bag, maxvar, new_clauses =
831         List.fold_left 
832           (fun (bag, maxvar, acc) (id,nlit,plit,vl,_) ->
833              let bag, maxvar, _, _, acc =
834               if nlit = [] then bag,maxvar,[],[],acc
835               else List.fold_left
836                  (superpose_literal id vl ctable false) (bag,maxvar,[],List.tl nlit,acc) nlit
837              in
838               let bag, maxvar, _, _, acc =
839                if plit = [] then bag,maxvar,[],[],acc
840                else List.fold_left
841                  (superpose_literal id vl ctable true) (bag,maxvar,[],List.tl plit,acc) plit
842              in            
843              bag, maxvar, acc)
844           (bag, maxvar, []) alist
845       in
846         debug (lazy "First superpositions");
847         (* We add current to active clauses so that it can be *
848          * superposed with itself                             *)
849       let alist, atable = 
850         current :: alist, IDX.index_clause atable current
851       in
852         debug (lazy "Indexed");
853       let fresh_current, maxvar = Clauses.fresh_clause maxvar current in
854         (* We need to put fresh_current into the bag so that all *
855          * variables clauses refer to are known.                 *)
856       let bag, (id,nlit,plit,vl,_) = Terms.add_to_bag fresh_current bag in
857         (* We superpose current with active clauses *)
858       let bag, maxvar, _, _, additional_new_clauses =
859          if nlit = [] then bag,maxvar,[],[],[]
860          else List.fold_left
861          (superpose_literal id vl atable false) (bag,maxvar,[],List.tl nlit,[]) nlit
862       in
863       let bag, maxvar, _, _, additional_new_clauses =
864          if plit = [] then bag,maxvar,[],[],[]
865          else List.fold_left
866           (superpose_literal id vl atable true) (bag,maxvar,[],List.tl plit,additional_new_clauses) plit
867       in 
868         debug (lazy "Another superposition");
869         let new_clauses = new_clauses@additional_new_clauses in
870         debug (lazy (Printf.sprintf "Demodulating %d clauses"
871                  (List.length new_clauses)));
872       let bag, new_clauses = 
873         HExtlib.filter_map_monad (simplify atable maxvar) bag new_clauses
874       in
875         debug (lazy "Demodulated new clauses");
876       bag, maxvar, (alist, atable), new_clauses
877     ;;
878
879     let prof_ir = HExtlib.profile ~enable "infer_right";;
880     let infer_right bag maxvar current t = 
881       prof_ir.HExtlib.profile (infer_right bag maxvar current) t
882     ;;
883
884     let infer_left bag maxvar goal (_alist, atable) =
885         (* We superpose the goal with active clauses *)
886      if (match goal with (_,_,_,[],_) -> true | _ -> false) then bag, maxvar, []
887      else
888       let (id,nlit,vl) =
889          match goal with
890            | (id,nlit,[],vl,_) -> id,nlit,vl
891            | _ -> assert false
892       in
893       let bag, maxvar, _, _, new_goals = 
894         List.fold_left (superpose_literal id vl atable false)
895             (bag,maxvar,[],List.tl nlit,[]) nlit
896       in
897         debug (lazy (string_of_int (List.length new_goals) ^ " new goals"));
898         (* We simplify the new goals with active clauses *)
899       let bag, new_goals = 
900         List.fold_left
901          (fun (bag, acc) g -> 
902             match simplify_goal ~no_demod:false maxvar atable bag [] g with
903               | None -> assert false
904               | Some (bag,g) -> bag,g::acc)
905          (bag, []) new_goals
906       in
907         debug (lazy "Simplified new goals with active clauses");
908       bag, maxvar, List.rev new_goals
909     ;;
910
911     let prof_il = HExtlib.profile ~enable "infer_left";;
912     let infer_left bag maxvar goal t = 
913       prof_il.HExtlib.profile (infer_left bag maxvar goal) t
914     ;;
915
916   end