Debian Codegraph @

plugins/firstorder/unify.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 open Util
12 open Constr
13 open EConstr
14 open Vars
15 open Termops
16 open Reductionops
17
18 exception UFAIL
19
20 (*
21 RIGID-only Martelli-Montanari style unification for CLOSED terms
22 I repeat : t1 and t2 must NOT have ANY free deBruijn
23 sigma is kept normal with respect to itself but is lazily applied
24 to the equation set. Raises UFAIL with a pair of terms
25 *)
26
27 let pop t = Vars.lift (-1) t
28 let subst_meta subst t =
29 let subst = List.map (fun (m, c) -> (m, EConstr.Unsafe.to_constr c)) subst in
30 EConstr.of_constr (subst_meta subst (EConstr.Unsafe.to_constr t))
31
32 let unif env evd t1 t2=
33 let bige=Queue.create ()
34 and sigma=ref [] in
35 let bind i t=
36 sigma:=(i,t)::
37 (List.map (function (n,tn)->(n,subst_meta [i,t] tn)) !sigma) in
38 let rec head_reduce t=
39 (* forbids non-sigma-normal meta in head position*)
40 match EConstr.kind evd t with
41 Meta i->
42 (try
43 head_reduce (Int.List.assoc i !sigma)
44 with Not_found->t)
45 | _->t in
46 Queue.add (t1,t2) bige;
47 try while true do
48 let t1,t2=Queue.take bige in
49 let nt1=head_reduce (whd_betaiotazeta env evd t1)
50 and nt2=head_reduce (whd_betaiotazeta env evd t2) in
51 match (EConstr.kind evd nt1),(EConstr.kind evd nt2) with
52 Meta i,Meta j->
53 if not (Int.equal i j) then
54 if i<j then bind j nt1
55 else bind i nt2
56 | Meta i,_ ->
57 let t=subst_meta !sigma nt2 in
58 if Int.Set.is_empty (free_rels evd t) &&
59 not (occur_metavariable evd i t) then
60 bind i t else raise UFAIL
61 | _,Meta i ->
62 let t=subst_meta !sigma nt1 in
63 if Int.Set.is_empty (free_rels evd t) &&
64 not (occur_metavariable evd i t) then
65 bind i t else raise UFAIL
66 | Cast(_,_,_),_->Queue.add (strip_outer_cast evd nt1,nt2) bige
67 | _,Cast(_,_,_)->Queue.add (nt1,strip_outer_cast evd nt2) bige
68 | (Prod(_,a,b),Prod(_,c,d))|(Lambda(_,a,b),Lambda(_,c,d))->
69 Queue.add (a,c) bige;Queue.add (pop b,pop d) bige
70 | Case (cia,ua,pmsa,pa,iva,ca,va),Case (cib,ub,pmsb,pb,ivb,cb,vb)->
71 let env = Global.env () in
72 let (cia,(pa,_),iva,ca,va) = EConstr.expand_case env evd (cia,ua,pmsa,pa,iva,ca,va) in
73 let (cib,(pb,_),iva,cb,vb) = EConstr.expand_case env evd (cib,ub,pmsb,pb,ivb,cb,vb) in
74 Queue.add (pa,pb) bige;
75 Queue.add (ca,cb) bige;
76 let l=Array.length va in
77 if not (Int.equal l (Array.length vb)) then
78 raise UFAIL
79 else
80 for i=0 to l-1 do
81 Queue.add (va.(i),vb.(i)) bige
82 done
83 | App(ha,va),App(hb,vb)->
84 Queue.add (ha,hb) bige;
85 let l=Array.length va in
86 if not (Int.equal l (Array.length vb)) then
87 raise UFAIL
88 else
89 for i=0 to l-1 do
90 Queue.add (va.(i),vb.(i)) bige
91 done
92 | _->if not (eq_constr_nounivs evd nt1 nt2) then raise UFAIL
93 done;
94 assert false
95 (* this place is unreachable but needed for the sake of typing *)
96 with Queue.Empty-> !sigma
97
98 let value evd i t=
99 let add x y=
100 if x<0 then y else if y<0 then x else x+y in
101 let rec vaux term=
102 if isMeta evd term && Int.equal (destMeta evd term) i then 0 else
103 let f v t=add v (vaux t) in
104 let vr=EConstr.fold evd f (-1) term in
105 if vr<0 then -1 else vr+1 in
106 vaux t
107
108 module Item =
109 struct
110
111 type t = int * constr
112
113 let repr i = i
114
115 let compare (i1, c1) (i2, c2) =
116 let c = Int.compare i1 i2 in
117 if c = 0 then Constr.compare (EConstr.Unsafe.to_constr c1) (EConstr.Unsafe.to_constr c2) else c
118
119 let is_ground (m, _) = Int.equal m 0
120
121 end
122
123 type instance=
124 Real of (int*constr)*int
125 | Phantom of constr
126
127 let mk_rel_inst evd t=
128 let new_rel=ref 1 in
129 let rel_env=ref [] in
130 let rec renum_rec d t=
131 match EConstr.kind evd t with
132 Meta n->
133 (try
134 mkRel (d+(Int.List.assoc n !rel_env))
135 with Not_found->
136 let m= !new_rel in
137 incr new_rel;
138 rel_env:=(n,m) :: !rel_env;
139 mkRel (m+d))
140 | _ -> EConstr.map_with_binders evd succ renum_rec d t
141 in
142 let nt=renum_rec 0 t in (!new_rel - 1,nt)
143
144 let unif_atoms ~check state env evd i dom t1 t2 =
145 try
146 (* Fast path: the meta is definitely not in any of these atoms *)
147 let () = if not check && not (Formula.meta_in_atom i t1) && not (Formula.meta_in_atom i t2) then raise UFAIL in
148 let t1 = Formula.repr_atom state t1 in
149 let t=Int.List.assoc i (unif env evd t1 (Formula.repr_atom state t2)) in
150 if isMeta evd t then Some (Phantom dom)
151 else Some (Real(mk_rel_inst evd t,value evd i t1))
152 with
153 | UFAIL -> None
154 | Not_found ->Some (Phantom dom)
155
156 let renum_metas_from k n t= (* requires n = max (free_rels t) *)
157 let l=List.init n (fun i->mkMeta (k+i)) in
158 substl l t
159
160 let more_general env evd (m1,t1) (m2,t2)=
161 m1 > m2 &&
162 let mt1=renum_metas_from 0 m1 t1
163 and mt2=renum_metas_from m1 m2 t2 in
164 try
165 let sigma=unif env evd mt1 mt2 in
166 let p (n,t)= n<m1 || isMeta evd t in
167 List.for_all p sigma
168 with UFAIL -> false