Debian Codegraph @

apps/tc/elpi/unif.elpi

raw
1 namespace tc{
2 namespace unif{
3 shorten std.{rev, append, ignore-failure!, mem, map2, split-at, map, assert!}.
4
5 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 %%%%% Unification %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
7 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
8 pred unify-eq i:term, i:term.
9 pred unify-list-eq i:list term, i:list term.
10 pred unify-leq i:term, i:term.
11
12 % invariant: hd-beta terms
13 % we start with ff, tt to handle symmetric cases
14 % NOTE: rec-calls with unify (ensured hd-beta + ff) , symmetric rules are typically !
15 % NOTE: asymmetric rules are not ! otherwise the flip rule is killed
16 % NOTE: whd are !
17 % names: unif X C T D M
18 kind cumul type.
19 type eq cumul.
20 type leq cumul.
21
22 macro @tail-cut-if Option Hd Hyps :- (
23 (Hd :- get-option Option tt, Hyps, !),
24 (Hd :- not(get-option Option tt), Hyps )
25 ).
26
27 pred unif i:term, i:stack, i:term, i:stack, i:bool, i:cumul.
28 :if "DBG:unif"
29 unif X CX Y CY D M :-
30 coq.say {counter "run"} "unif" X CX "==" Y CY "(flipped?" D "cumul:" M ")", fail.
31
32 func swap bool, (func A, A -> ), A, A -> .
33 swap tt F A B :- F B A.
34 swap ff F A B :- F A B.
35
36 % flexible cases
37 unif (uvar V L) [] T D _ _ :- get-option "unif:greedy" tt, !, bind-list L {unwind T D} V, !.
38 unif (uvar V L) [] T D _ _ :- !, bind-list L {unwind T D} V.
39 unif X C (uvar V L) [] _ _ :- get-option "unif:greedy" tt, !, bind-list L {unwind X C} V, !.
40 unif X C (uvar V L) [] _ _ :- !, bind-list L {unwind X C} V.
41
42 unif (sort prop) [] (sort (uvar as Y)) [] _ _ :- !, Y = prop.
43 unif X [] (sort (uvar as Y)) [] M U :- !,
44 coq.univ.new Lvl,
45 Y = typ Lvl,
46 unif X [] (sort Y) [] M U.
47 unif (sort (uvar as X)) [] Y [] M U :- !,
48 coq.univ.new Lvl,
49 X = typ Lvl,
50 unif (sort X) [] Y [] M U.
51
52 unif (sort S1) [] (sort S2) [] M eq :- !, swap M coq.sort.eq S1 S2.
53 unif (sort S1) [] (sort S2) [] M leq :- !, swap M coq.sort.leq S1 S2.
54
55 unif (primitive X) [] (primitive X) [] ff _ :- !.
56
57 unif (global (indt GR1)) C (global (indt GR2)) D _ _ :- !, GR1 = GR2, unify-ctxs C D.
58 unif (global (indc GR1)) C (global (indc GR2)) D _ _ :- !, GR1 = GR2, unify-ctxs C D.
59 unif (pglobal (indt GR1) I1) C (pglobal (indt GR2) I2) D _ eq :- !,
60 GR1 = GR2,
61 coq.univ-instance.unify-eq (indt GR1) I1 I2 ok,
62 unify-ctxs C D.
63 unif (pglobal (indt GR1) I1) C (pglobal (indt GR2) I2) D _ leq :- !,
64 GR1 = GR2,
65 coq.univ-instance.unify-leq (indt GR1) I1 I2 ok,
66 unify-ctxs C D.
67
68 % fast path for stuck term on the right
69 unif X C (global (indt _) as T) D ff U :- !, unif T D {whd X C} tt U. % TODO:1
70 unif X C (global (indc _) as T) D ff U :- !, unif T D {whd X C} tt U. % TODO:1
71 unif X C (pglobal (indt _) _ as T) D ff U :- !, unif T D {whd X C} tt U. % TODO:1
72 unif X C (pglobal (indc _) _ as T) D ff U :- !, unif T D {whd X C} tt U. % TODO:1
73
74 % congruence rules TODO: is the of assumption really needed?
75 unif (fun N T1 F1) [] (fun M T2 F2) [] _ _ :- !, ignore-failure! (N = M),
76 unify T1 T2 eq,
77 pi x\ (decl x N T1) => unify (F1 x) (F2 x) eq.
78 unif (prod N T1 F1) [] (prod M T2 F2) [] _ U :- !, ignore-failure! (N = M),
79 unify T1 T2 eq,
80 pi x\ (decl x N T1) => unify (F1 x) (F2 x) U.
81 unif (fix N Rno Ty1 F1) B1 (fix M Rno Ty2 F2) B2 _ _ :- !, ignore-failure! (N = M),
82 unify Ty1 Ty2 eq,
83 (pi f\ (decl f N Ty1) => unify (F1 f) (F2 f) eq),
84 unify-ctxs B1 B2.
85 unif (match A1 R1 L1) B1 (match A2 R2 L2) B2 _ _ :- !,
86 unify A1 A2 eq, unify R1 R2 eq, unify-list L1 L2, unify-ctxs B1 B2.
87
88 % congruence heuristic (same maybe-non-normal head)
89 unif (let N T1 B1 F1) C1 (let M T2 B2 F2) C2 _ _ :- ignore-failure! (N = M),
90 unify T1 T2 eq, unify B1 B2 eq,
91 (@pi-def N T1 B1 x\ unify (F1 x) (F2 x) eq),
92 unify-ctxs C1 C2, !.
93 unif (global (const GR)) C (global (const GR)) D _ _ :- unify-ctxs C D, !.
94 unif (pglobal (const GR) I1) C (pglobal (const GR) I2) D _ eq :-
95 coq.univ-instance.unify-eq (const GR) I1 I2 ok,
96 unify-ctxs C D, !.
97 unif (pglobal (const GR) I1) C (pglobal (const GR) I2) D _ leq :-
98 coq.univ-instance.unify-leq (const GR) I1 I2 ok,
99 unify-ctxs C D, !.
100 unif X C T D _ _ :- name X, name T, X = T, unify-ctxs C D.
101
102 % 1 step reduction TODO:1
103 unif (global (const GR)) C T D M U :- unfold GR none C X1 C1, !, unif X1 C1 T D M U.
104 unif (pglobal (const GR) I) C T D M U :- unfold GR (some I) C X1 C1, !, unif X1 C1 T D M U.
105 unif (let N TB B F) C1 T C2 M U :- !,
106 @pi-def N TB B x\ unif {hd-beta (F x) C1} T C2 M U.
107 unif (match A _ L) C T D M U :- whd-indc A GR KA, !,
108 unif {match-red GR KA L C} T D M U.
109 unif (fix _ N _ F as X) C T D M U :- nth-stack N C LA A RA, whd-indc A GR KA, !,
110 unif {fix-red F X LA GR KA RA} T D M U.
111 unif X C T D M U :- name X, def X _ _ V, !, unif {hd-beta V C} T D M U.
112 % TODO we could use _VN if nonflex
113 % TODO:1 turn into (if reducible then reduce1 else fully-reduce2 tt)
114
115 % symmetry
116 unif X C T D ff U :- !, unif T D X C tt U.
117
118 % error
119 % unif X C1 Y C2 _tt :- !,
120 % print "Error: " {coq.term->string {unwind X C1}} "vs" {coq.term->string {unwind Y C2}}. %, halt.
121
122 % Contexts happens to be lists, so we just reuse the code
123 pred unify-list i:list term, i:list term.
124 unify-list L1 L2 :- unify-ctxs L1 L2.
125
126 % the entry points of rec calls: unify unify-ctxs
127 pred unify-ctxs i:list term, i:list term.
128 unify-ctxs [] [].
129 unify-ctxs [X|XS] [Y|YS] :- unify X Y eq, !, unify-ctxs XS YS.
130
131 % Note lists are reversed + the 2nd length should be leq then the 1st
132 pred unify-lists i:list term, i:list term.
133 unify-lists [X] [Y] :- !, X = Y.
134 unify-lists L [X] :- !, app {std.rev L} = X.
135 unify-lists [X|Xs] [Y|Ys] :- X = Y, unify-lists Xs Ys.
136
137 pred unify i:term, i:term, i:cumul.
138 unify T1 (app [HD|TL]) _ :- not (var T1), var HD, !, T1 = app L1,
139 unify-lists {std.rev L1} {std.rev [HD|TL]}.
140 unify A B C :- unif {hd-beta A []} {hd-beta B []} ff C.
141
142 %%%% Flexible case %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
143 % Binding a list of terms (delift in Matita, invert subst in Coq)
144
145 % We use a keyd discipline, i.e. we only bind terms with a rigid head
146 pred key i:term.
147 key (global _) :- !.
148 key (pglobal _ _) :- !.
149 key C :- name C, !.
150 key (primitive _) :- !.
151
152 pred bind-list i:list term, i:term, o:any.
153 bind-list [] T T' :- bind T T1, T1 = T'.
154 bind-list [app [C| AS] |VS] T R :- key C, !,
155 pi x\
156 (pi L X\ bind (app[C|L]) X :- get-option "unif:greedy" tt, unify-list-eq L AS, X = x, !) =>
157 (pi L X\ bind (app[C|L]) X :- not (get-option "unif:greedy" tt),unify-list-eq L AS, X = x) =>
158 bind-list VS T (R x).
159 bind-list [C|VS] T R :- key C, def C _ _ V, key V, !,
160 pi x\ @tail-cut-if "unif:greedy" (bind C x) true =>
161 @tail-cut-if "unif:greedy" (bind V x) true =>
162 bind-list VS T (R x).
163 bind-list [C|VS] T R :- key C, !,
164 pi x\ @tail-cut-if "unif:greedy" (bind C x) true => bind-list VS T (R x).
165 bind-list [ _ |VS] T R :- !, pi x\ bind-list VS T (R x).
166
167 % CAVEAT: (app FLEX), (match _ _ FLEX) are not terms!
168 pred bind i:term, o:term.
169 bind X Y :- name X, X = Y, !.
170 bind X Y :- name X, def X _ _ T, !, bind T Y.
171 bind (global _ as C) C :- !.
172 bind (pglobal _ _ as C) C :- !.
173 bind (sort _ as C) C :- !.
174 bind (fix N Rno Ty F) (fix N Rno Ty1 F1) :- !,
175 bind Ty Ty1, pi x\ decl x N Ty => bind (F x) (F1 x).
176 bind (match T Rty B) X :- !,
177 bind T T1, bind Rty Rty1, map B bind B1, X = (match T1 Rty1 B1).
178 bind (app L) X :- !, map L bind L1, X = app L1.
179 bind (fun N T F) (fun N T1 F1) :- !,
180 bind T T1, pi x\ decl x N T => bind (F x) (F1 x).
181 bind (let N T B F) (let N T1 B1 F1) :- !,
182 bind T T1, bind B B1, @pi-def N T B x\ bind (F x) (F1 x).
183 bind (prod N T F) X :- !,
184 bind T T1, (@pi-decl N T x\ bind (F x) (F1 x)), X = (prod N T1 F1).
185 bind (uvar M L) W :- map L bind L1, coq.mk-app-uvar M L1 W.
186
187 unify-eq X Y :- unify X Y eq.
188 unify-leq X Y :- unify X Y leq.
189 }
190 }
191
192 pred tc.unif-aux i:(pred i:A, i:A), i:A, i:A.
193 tc.unif-aux P A B :-
194 (pi K A AS X C TA F\
195 hd-beta (uvar as K) [A|AS] X C :- !, % auto-intro
196 std.assert-ok! (coq.typecheck A TA) "already typed",
197 K = (fun `hd_beta_auto` TA F),
198 hd-beta (F A) AS X C) =>
199 (pi K A AS X C TA F\
200 hd-beta-zeta (uvar as K) [A|AS] X C :- !, % auto-intro
201 std.assert-ok! (coq.typecheck A TA) "already typed",
202 K = (fun `hd_beta_zeta_auto` TA F),
203 hd-beta-zeta (F A) AS X C) =>
204 P A B.
205
206 pred tc.unify-eq i:term, i:term.
207 pred tc.unify-leq i:term, i:term.
208 pred tc.unify-list-eq i:list term, i:list term.
209
210 tc.unify-eq X Y :- tc.unif-aux tc.unif.unify-eq X Y.
211 tc.unify-leq X Y :- tc.unif-aux tc.unif.unify-leq X Y.
212 tc.unify-list-eq X Y :- tc.unif-aux tc.unif.unify-list X Y.