| 1 |
/* Type inference and unification */ |
| 2 |
/* license: GNU Lesser General Public License Version 2.1 or later */ |
| 3 |
/* ------------------------------------------------------------------------- */ |
| 4 |
|
| 5 |
shorten std.{rev, append, ignore-failure!, mem, map2, split-at, map, assert!}. |
| 6 |
|
| 7 |
% Entry points |
| 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 |
pred of i:term, o:term, o:term. % of Term Type(i/o) RefinedTerm |
| 12 |
|
| 13 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 14 |
%%%%% Reduction %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 15 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 16 |
|
| 17 |
:before "hd-beta:end" |
| 18 |
hd-beta (uvar as K) [A|AS] X C :- !, % auto-intro |
| 19 |
assert! (of A TA _) "already typed", |
| 20 |
unify-eq K (fun `hd_beta_auto` TA F), !, |
| 21 |
hd-beta (F A) AS X C. |
| 22 |
|
| 23 |
:before "hd-beta-zeta:end" |
| 24 |
hd-beta-zeta (uvar as K) [A|AS] X C :- !, % auto-intro |
| 25 |
assert! (of A TA _) "already typed", |
| 26 |
unify-eq K (fun `hd_beta_zeta_auto` TA F), !, |
| 27 |
hd-beta-zeta (F A) AS X C. |
| 28 |
|
| 29 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 30 |
%%%%% Unification %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 31 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 32 |
|
| 33 |
% invariant: hd-beta terms |
| 34 |
% we start with ff, tt to handle symmetric cases |
| 35 |
% NOTE: rec-calls with unify (ensured hd-beta + ff) , symmetric rules are typically ! |
| 36 |
% NOTE: asymmetric rules are not ! otherwise the flip rule is killed |
| 37 |
% NOTE: whd are ! |
| 38 |
% names: unif X C T D M |
| 39 |
kind cumul type. |
| 40 |
type eq cumul. |
| 41 |
type leq cumul. |
| 42 |
|
| 43 |
macro @tail-cut-if Option Hd Hyps :- ( |
| 44 |
(Hd :- get-option Option tt, Hyps, !), |
| 45 |
(Hd :- not(get-option Option tt), Hyps ) |
| 46 |
). |
| 47 |
|
| 48 |
pred unif i:term, i:stack, i:term, i:stack, i:bool, i:cumul. |
| 49 |
:if "DBG:unif" |
| 50 |
unif X CX Y CY D M :- |
| 51 |
coq.say {counter "run"} "unif" X CX "==" Y CY "(flipped?" D "cumul:" M ")", fail. |
| 52 |
|
| 53 |
func swap bool, (func A, A -> ), A, A -> . |
| 54 |
swap tt F A B :- F B A. |
| 55 |
swap ff F A B :- F A B. |
| 56 |
|
| 57 |
% flexible cases |
| 58 |
unif (uvar V L) [] T D _ _ :- get-option "unif:greedy" tt, !, bind-list L {unwind T D} V, !. |
| 59 |
unif (uvar V L) [] T D _ _ :- !, bind-list L {unwind T D} V. |
| 60 |
unif X C (uvar V L) [] _ _ :- get-option "unif:greedy" tt, !, bind-list L {unwind X C} V, !. |
| 61 |
unif X C (uvar V L) [] _ _ :- !, bind-list L {unwind X C} V. |
| 62 |
|
| 63 |
unif (sort prop) [] (sort (uvar as Y)) [] _ _ :- !, Y = prop. |
| 64 |
unif X [] (sort (uvar as Y)) [] M U :- !, |
| 65 |
coq.univ.new Lvl, |
| 66 |
Y = typ Lvl, |
| 67 |
unif X [] (sort Y) [] M U. |
| 68 |
unif (sort (uvar as X)) [] Y [] M U :- !, |
| 69 |
coq.univ.new Lvl, |
| 70 |
X = typ Lvl, |
| 71 |
unif (sort X) [] Y [] M U. |
| 72 |
|
| 73 |
unif (sort S1) [] (sort S2) [] M eq :- !, swap M coq.sort.eq S1 S2. |
| 74 |
unif (sort S1) [] (sort S2) [] M leq :- !, swap M coq.sort.leq S1 S2. |
| 75 |
|
| 76 |
unif (primitive X) [] (primitive X) [] ff _ :- !. |
| 77 |
|
| 78 |
unif (global (indt GR1)) C (global (indt GR2)) D _ _ :- !, GR1 = GR2, unify-ctxs C D. |
| 79 |
unif (global (indc GR1)) C (global (indc GR2)) D _ _ :- !, GR1 = GR2, unify-ctxs C D. |
| 80 |
unif (pglobal (indt GR1) I1) C (pglobal (indt GR2) I2) D _ eq :- !, |
| 81 |
GR1 = GR2, |
| 82 |
coq.univ-instance.unify-eq (indt GR1) I1 I2 ok, |
| 83 |
unify-ctxs C D. |
| 84 |
unif (pglobal (indt GR1) I1) C (pglobal (indt GR2) I2) D _ leq :- !, |
| 85 |
GR1 = GR2, |
| 86 |
coq.univ-instance.unify-leq (indt GR1) I1 I2 ok, |
| 87 |
unify-ctxs C D. |
| 88 |
|
| 89 |
% fast path for stuck term on the right |
| 90 |
unif X C (global (indt _) as T) D ff U :- !, unif T D {whd X C} tt U. % TODO:1 |
| 91 |
unif X C (global (indc _) as T) D ff U :- !, unif T D {whd X C} tt U. % TODO:1 |
| 92 |
unif X C (pglobal (indt _) _ as T) D ff U :- !, unif T D {whd X C} tt U. % TODO:1 |
| 93 |
unif X C (pglobal (indc _) _ as T) D ff U :- !, unif T D {whd X C} tt U. % TODO:1 |
| 94 |
|
| 95 |
% congruence rules TODO: is the of assumption really needed? |
| 96 |
unif (fun N T1 F1) [] (fun M T2 F2) [] _ _ :- !, ignore-failure! (N = M), |
| 97 |
unify T1 T2 eq, |
| 98 |
pi x\ (decl x N T1) => unify (F1 x) (F2 x) eq. |
| 99 |
unif (prod N T1 F1) [] (prod M T2 F2) [] _ U :- !, ignore-failure! (N = M), |
| 100 |
unify T1 T2 eq, |
| 101 |
pi x\ (decl x N T1) => unify (F1 x) (F2 x) U. |
| 102 |
unif (fix N Rno Ty1 F1) B1 (fix M Rno Ty2 F2) B2 _ _ :- !, ignore-failure! (N = M), |
| 103 |
unify Ty1 Ty2 eq, |
| 104 |
(pi f\ (decl f N Ty1) => unify (F1 f) (F2 f) eq), |
| 105 |
unify-ctxs B1 B2. |
| 106 |
unif (match A1 R1 L1) B1 (match A2 R2 L2) B2 _ _ :- !, |
| 107 |
unify A1 A2 eq, unify R1 R2 eq, unify-list L1 L2, unify-ctxs B1 B2. |
| 108 |
|
| 109 |
% congruence heuristic (same maybe-non-normal head) |
| 110 |
unif (let N T1 B1 F1) C1 (let M T2 B2 F2) C2 _ _ :- ignore-failure! (N = M), |
| 111 |
unify T1 T2 eq, unify B1 B2 eq, |
| 112 |
(@pi-def N T1 B1 x\ unify (F1 x) (F2 x) eq), |
| 113 |
unify-ctxs C1 C2, !. |
| 114 |
unif (global (const GR)) C (global (const GR)) D _ _ :- unify-ctxs C D, !. |
| 115 |
unif (pglobal (const GR) I1) C (pglobal (const GR) I2) D _ eq :- |
| 116 |
coq.univ-instance.unify-eq (const GR) I1 I2 ok, |
| 117 |
unify-ctxs C D, !. |
| 118 |
unif (pglobal (const GR) I1) C (pglobal (const GR) I2) D _ leq :- |
| 119 |
coq.univ-instance.unify-leq (const GR) I1 I2 ok, |
| 120 |
unify-ctxs C D, !. |
| 121 |
unif X C T D _ _ :- name X, name T, X = T, unify-ctxs C D. |
| 122 |
|
| 123 |
% 1 step reduction TODO:1 |
| 124 |
unif (global (const GR)) C T D M U :- unfold GR none C X1 C1, !, unif X1 C1 T D M U. |
| 125 |
unif (pglobal (const GR) I) C T D M U :- unfold GR (some I) C X1 C1, !, unif X1 C1 T D M U. |
| 126 |
unif (let N TB B F) C1 T C2 M U :- !, |
| 127 |
@pi-def N TB B x\ unif {hd-beta (F x) C1} T C2 M U. |
| 128 |
unif (match A _ L) C T D M U :- whd-indc A GR KA, !, |
| 129 |
unif {match-red GR KA L C} T D M U. |
| 130 |
unif (fix _ N _ F as X) C T D M U :- nth-stack N C LA A RA, whd-indc A GR KA, !, |
| 131 |
unif {fix-red F X LA GR KA RA} T D M U. |
| 132 |
unif X C T D M U :- name X, def X _ _ V, !, unif {hd-beta V C} T D M U. |
| 133 |
% TODO we could use _VN if nonflex |
| 134 |
% TODO:1 turn into (if reducible then reduce1 else fully-reduce2 tt) |
| 135 |
|
| 136 |
% symmetry |
| 137 |
unif X C T D ff U :- !, unif T D X C tt U. |
| 138 |
|
| 139 |
% error |
| 140 |
% unif X C1 Y C2 _tt :- !, |
| 141 |
% print "Error: " {coq.term->string {unwind X C1}} "vs" {coq.term->string {unwind Y C2}}. %, halt. |
| 142 |
|
| 143 |
% Contexts happens to be lists, so we just reuse the code |
| 144 |
pred unify-list i:list term, i:list term. |
| 145 |
unify-list L1 L2 :- unify-ctxs L1 L2. |
| 146 |
|
| 147 |
% the entry points of rec calls: unify unify-ctxs |
| 148 |
pred unify-ctxs i:list term, i:list term. |
| 149 |
unify-ctxs [] []. |
| 150 |
unify-ctxs [X|XS] [Y|YS] :- unify X Y eq, !, unify-ctxs XS YS. |
| 151 |
|
| 152 |
pred unify i:term, i:term, i:cumul. |
| 153 |
unify A B C :- unif {hd-beta A []} {hd-beta B []} ff C. |
| 154 |
|
| 155 |
%%%%%% entry points for clients %%%%%%% |
| 156 |
unify-eq X Y :- unify X Y eq. |
| 157 |
unify-leq X Y :- unify X Y leq. |
| 158 |
unify-list-eq L1 L2 :- unify-list L1 L2. |
| 159 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 160 |
|
| 161 |
%%%% Flexible case %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 162 |
% Binding a list of terms (delift in Matita, invert subst in Coq) |
| 163 |
|
| 164 |
% We use a keyd discipline, i.e. we only bind terms with a rigid head |
| 165 |
pred key i:term. |
| 166 |
key (global _) :- !. |
| 167 |
key (pglobal _ _) :- !. |
| 168 |
key C :- name C, !. |
| 169 |
key (primitive _) :- !. |
| 170 |
|
| 171 |
pred bind-list i:list term, i:term, o:any. |
| 172 |
bind-list [] T T' :- bind T T1, T1 = T'. |
| 173 |
bind-list [app [C| AS] |VS] T R :- key C, !, |
| 174 |
pi x\ |
| 175 |
(pi L X\ bind (app[C|L]) X :- get-option "unif:greedy" tt, unify-list-eq L AS, X = x, !) => |
| 176 |
(pi L X\ bind (app[C|L]) X :- not (get-option "unif:greedy" tt),unify-list-eq L AS, X = x) => |
| 177 |
bind-list VS T (R x). |
| 178 |
bind-list [C|VS] T R :- key C, def C _ _ V, key V, !, |
| 179 |
pi x\ @tail-cut-if "unif:greedy" (bind C x) true => |
| 180 |
@tail-cut-if "unif:greedy" (bind V x) true => |
| 181 |
bind-list VS T (R x). |
| 182 |
bind-list [C|VS] T R :- key C, !, |
| 183 |
pi x\ @tail-cut-if "unif:greedy" (bind C x) true => bind-list VS T (R x). |
| 184 |
bind-list [ _ |VS] T R :- !, pi x\ bind-list VS T (R x). |
| 185 |
|
| 186 |
% CAVEAT: (app FLEX), (match _ _ FLEX) are not terms! |
| 187 |
pred bind i:term, o:term. |
| 188 |
bind X Y :- name X, X = Y, !. |
| 189 |
bind X Y :- name X, def X _ _ T, !, bind T Y. |
| 190 |
bind (global _ as C) C :- !. |
| 191 |
bind (pglobal _ _ as C) C :- !. |
| 192 |
bind (sort _ as C) C :- !. |
| 193 |
bind (fix N Rno Ty F) (fix N Rno Ty1 F1) :- !, |
| 194 |
bind Ty Ty1, pi x\ decl x N Ty => bind (F x) (F1 x). |
| 195 |
bind (match T Rty B) X :- !, |
| 196 |
bind T T1, bind Rty Rty1, map B bind B1, X = (match T1 Rty1 B1). |
| 197 |
bind (app L) X :- !, map L bind L1, X = app L1. |
| 198 |
bind (fun N T F) (fun N T1 F1) :- !, |
| 199 |
bind T T1, pi x\ decl x N T => bind (F x) (F1 x). |
| 200 |
bind (let N T B F) (let N T1 B1 F1) :- !, |
| 201 |
bind T T1, bind B B1, @pi-def N T B x\ bind (F x) (F1 x). |
| 202 |
bind (prod N T F) X :- !, |
| 203 |
bind T T1, (@pi-decl N T x\ bind (F x) (F1 x)), X = (prod N T1 F1). |
| 204 |
bind (uvar M L) W :- map L bind L1, coq.mk-app-uvar M L1 W. |
| 205 |
|
| 206 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 207 |
%%%% Type checking %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 208 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 209 |
|
| 210 |
%%%%% eat-prod head head-ty args-done todo-args refined-app refined-ty %%%%%%%% |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
pred bidir-app i:term, i:term, i:list term, o:term. |
| 215 |
:name "of:bidirectional-app" |
| 216 |
bidir-app _ _ _ _. |
| 217 |
|
| 218 |
pred saturate-dummy i:term, o:term. |
| 219 |
saturate-dummy (prod _ _ F) R :- pi x\ saturate-dummy (F x) R. |
| 220 |
saturate-dummy X X. |
| 221 |
|
| 222 |
pred ensure-prod.aux i:list A, i:term, o:term, o:bool. |
| 223 |
ensure-prod.aux [] X X _. |
| 224 |
ensure-prod.aux [_|Args] (prod N S T) (prod N S T1) NU :- !, |
| 225 |
pi x\ ensure-prod.aux Args (T x) (T1 x) NU. |
| 226 |
ensure-prod.aux [_|Args] uvar (prod Name_ Src_ R) tt :- !, |
| 227 |
pi x\ ensure-prod.aux Args (R x) (R x) tt. |
| 228 |
ensure-prod.aux Args X R NU :- |
| 229 |
whd1 X Y, ensure-prod.aux Args Y R NU. |
| 230 |
|
| 231 |
% TODO: do not fail if whd1 fails and the term is not flexible since it |
| 232 |
% may just need to be passed a concrete argument |
| 233 |
|
| 234 |
pred ensure-prod i:list A, i:term. |
| 235 |
ensure-prod Args Ty :- |
| 236 |
ensure-prod.aux Args Ty R NeedsUnif, |
| 237 |
if (var NeedsUnif) true (of R _ R1, unify-eq Ty R1). |
| 238 |
|
| 239 |
|
| 240 |
pred eat-prod i:list term, i:term, i:term, o:list term, o:term, o:term. |
| 241 |
:if "DBG:of" eat-prod [] Hd Prod Adone Res ResTy :- |
| 242 |
coq.say "eat-prod" Hd {rev Adone} "==" Res ";" Prod "=<=" ResTy, fail. |
| 243 |
|
| 244 |
eat-prod [] Hd Prod Adone Res ResTy :- !, |
| 245 |
rev Adone Args, |
| 246 |
unify-eq Res {coq.mk-app Hd Args}, |
| 247 |
unify-leq Prod ResTy. |
| 248 |
|
| 249 |
% XXX why not unif? |
| 250 |
eat-prod [A|AS] Hd (prod _ Src Tgt as Prod) Adone Res ResTy :- |
| 251 |
bidir-app Hd Prod Adone ResTy, |
| 252 |
of A Src ResA, |
| 253 |
eat-prod AS Hd (Tgt ResA) [ResA|Adone] Res ResTy. |
| 254 |
% TODO: add a whd1 eg in case of a n-ary function |
| 255 |
|
| 256 |
:if "DBG:of" |
| 257 |
of X Tx Rx :- coq.say {counter "run"} "of" X Tx Rx, fail. |
| 258 |
|
| 259 |
of X Tx R :- name X, (decl X _ T ; def X _ T _), unify-leq T Tx, R = X. |
| 260 |
|
| 261 |
of (fun N S F) LamTy (fun M S2 F2) :- |
| 262 |
of (prod N S _) (sort _U) (prod M S2 T), |
| 263 |
unify-leq (prod M S2 T) LamTy, |
| 264 |
pi x\ decl x M S2 => of (F x) (T x) (F2 x). |
| 265 |
|
| 266 |
of (app [X]) Ty R :- !, of X Ty R. |
| 267 |
|
| 268 |
of (app [Hd|Args]) TyApp App :- |
| 269 |
of Hd Prod Hd1, |
| 270 |
ensure-prod Args Prod, |
| 271 |
eat-prod Args Hd1 Prod [] App TyApp. |
| 272 |
|
| 273 |
of (prod N S F) ProdTy (prod N ResS ResF) :- |
| 274 |
closed_term U1, closed_term U2, closed_term U, |
| 275 |
of S (sort U1) ResS, |
| 276 |
(pi x\ decl x N ResS => of (F x) (sort U2) (ResF x)), |
| 277 |
pts U1 U2 U, |
| 278 |
unify-leq (sort U) ProdTy. |
| 279 |
|
| 280 |
of (match T Rty Bs) ResRtyInst (match ResT ResRty ResBs) :- |
| 281 |
of T TyT ResT, |
| 282 |
% T : TyT = (indt GR) LArgs RArgs, and (indt GR) : Ty |
| 283 |
coq.safe-dest-app TyT (global (indt GR)) Args, |
| 284 |
coq.env.indt GR _IsInd Lno _Luno Ty Kn Ks, % TODO LUno |
| 285 |
split-at Lno Args LArgs RArgs, % TODO: not a failure, just type err |
| 286 |
% fix LArgs on ind ty and constructors ty |
| 287 |
coq.subst-prod LArgs Ty TyLArgs, |
| 288 |
map Ks (coq.subst-prod LArgs) KsLArgs, |
| 289 |
% Rty skeleton (uknown ending) = fun rargs, fun e : indt largs rargs, ? |
| 290 |
mk-rty [] {coq.mk-app (global (indt GR)) LArgs} TyLArgs ResRtyRaw, |
| 291 |
of ResRtyRaw _ ResRty, unify-eq Rty ResRty, |
| 292 |
% Rty must type each branch |
| 293 |
map2 KsLArgs Kn (mk-bty Rty Lno) BsTy, |
| 294 |
map2R Bs BsTy of ResBs, |
| 295 |
% Outside type |
| 296 |
unify-leq {coq.mk-app ResRty {append RArgs [ResT]}} ResRtyInst. |
| 297 |
|
| 298 |
pred map2R i:list A, o:list B, i:(pred i:A, o:B, o:C), o:list C. |
| 299 |
map2R [] [] _ []. |
| 300 |
map2R [X|XS] [Y|YS] F [Z|ZS] :- F X Y Z, map2R XS YS F ZS. |
| 301 |
|
| 302 |
of (let N Ty Bo F) TyFx (let N ResTy ResBo ResF) :- |
| 303 |
of Ty (sort _) ResTy, of Bo ResTy ResBo, |
| 304 |
pi x\ def x N ResTy ResBo => cache x T_ => of (F x) TyFx (ResF x). |
| 305 |
|
| 306 |
of (fix N Rno Ty BoF) ResTy (fix N Rno RTy ResBoF) :- |
| 307 |
of Ty (sort _) RTy, |
| 308 |
unify-leq RTy ResTy, |
| 309 |
pi f\ decl f N RTy => of (BoF f) ResTy (ResBoF f). |
| 310 |
|
| 311 |
of (sort S) (sort S1) (sort S) :- coq.sort.sup S S1. |
| 312 |
|
| 313 |
of (global GR as X) T X :- coq.env.typeof GR T1, unify-leq T1 T. |
| 314 |
of (pglobal GR I as X) T X :- |
| 315 |
(@uinstance! I => coq.env.typeof GR T1), unify-leq T1 T. |
| 316 |
|
| 317 |
of (primitive (uint63 _) as X) T X :- unify-leq {{ lib:num.int63.type }} T. |
| 318 |
of (primitive (float64 _) as X) T X :- unify-leq {{ lib:num.float.type }} T. |
| 319 |
of (primitive (pstring _) as X) T X :- unify-leq {{ lib:elpi.pstring }} T. |
| 320 |
|
| 321 |
of (uvar as X) T Y :- !, evar X T Y. |
| 322 |
|
| 323 |
:if "OVERRIDE_COQ_ELABORATOR" |
| 324 |
:name "refiner-assign-evar" |
| 325 |
:before "default-assign-evar" |
| 326 |
evar X Ty S :- !, of X Ty S, !. |
| 327 |
|
| 328 |
pred coerce o:term, o:term, o:term, o:term. |
| 329 |
pred coerced i:term, i:term, i:term, o:term. |
| 330 |
pred coerceible i:term, o:term, i:term, o:term. |
| 331 |
of X T R :- get-option "of:coerce" tt, not (var T), of X XT Y, coerced XT T Y R. |
| 332 |
|
| 333 |
:if "DBG:of" |
| 334 |
of X Tx Rx :- coq.say {counter "run"} "of [FAIL]" X Tx Rx, fail. |
| 335 |
|
| 336 |
func utc % Uniqueness of typing |
| 337 |
list term, % names (canonical) |
| 338 |
term, % type living in names |
| 339 |
list term, % values (explicit subst on names) |
| 340 |
term % type living in values |
| 341 |
-> (func). % goal checking compatibility of the two types |
| 342 |
utc [] T1 [] T2 (std.once(unify-eq T1V T2)) :- !, copy T1 T1V. |
| 343 |
utc [N|NS] T1 [V|VS] T2 C :- !, (copy N V :- !) => utc NS T1 VS T2 C. |
| 344 |
utc [] T1 VS T2 C :- !, utc [] {coq.subst-prod VS T1} [] T2 C. % FIXME: reduction |
| 345 |
utc [_|NS] (prod _ _ F) [] T2 C :- !, % FIXME: reduction |
| 346 |
assert! (pi x\ F x = F1) "restriction bug", utc NS F1 [] T2 C. |
| 347 |
|
| 348 |
% This could be done in ML |
| 349 |
func canonical? list term. |
| 350 |
canonical? []. |
| 351 |
canonical? [N|NS] :- name N, not(mem NS N), canonical? NS. |
| 352 |
|
| 353 |
constraint declare-evar evar decl def cache rm-evar { |
| 354 |
rule (E1 :> G1 ?- evar _ T1 (uvar K L1)) % canonical |
| 355 |
\ (E2 :> G2 ?- evar _ T2 (uvar K L2)) % actual |
| 356 |
| (canonical? L1, utc L1 T1 L2 T2 Condition, |
| 357 |
coq.say "CHR: Uniqueness of typing of" K "+" L1 "<->" L2, |
| 358 |
coq.say E1 "|>" G1 "|-" K L1 ":" T1, |
| 359 |
coq.say E2 "|>" G2 "|-" K L2 ":" T2, |
| 360 |
coq.say E2 "|>" G2 "|-" Condition "\n" |
| 361 |
) |
| 362 |
<=> (E2 :> G2 ?- Condition). |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
% typing match %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 367 |
type mk-rty list term -> term -> term -> term -> prop. |
| 368 |
mk-rty ARGS HD (prod N S T) (fun N S F) :- !, |
| 369 |
pi x\ mk-rty [x|ARGS] HD (T x) (F x). |
| 370 |
mk-rty ARGS HD _ (fun _ IndApp _FRESH) :- |
| 371 |
coq.mk-app HD {rev ARGS} IndApp. |
| 372 |
|
| 373 |
pred mk-bty i:term, i:int, i:term, i:constructor, o:term. |
| 374 |
mk-bty Rty Lno (prod N S T) Ki (prod N S B) :- !, |
| 375 |
pi x\ mk-bty Rty Lno (T x) Ki (B x). |
| 376 |
mk-bty Rty Lno T Ki AppRtyNorm :- |
| 377 |
coq.safe-dest-app T (global (indt _)) Args, |
| 378 |
split-at Lno Args LArgs RArgs, |
| 379 |
coq.mk-app Rty {append RArgs [{coq.mk-app (global (indc Ki)) {append LArgs RArgs}}]} AppRty, |
| 380 |
hd-beta-zeta-reduce AppRty AppRtyNorm. |
| 381 |
mk-bty Rty Lno T Ki AppRtyNorm :- |
| 382 |
coq.safe-dest-app T (pglobal (indt _) I) Args, |
| 383 |
split-at Lno Args LArgs RArgs, |
| 384 |
coq.mk-app Rty {append RArgs [{coq.mk-app (pglobal (indc Ki) I) {append LArgs RArgs}}]} AppRty, |
| 385 |
hd-beta-zeta-reduce AppRty AppRtyNorm. |
| 386 |
|
| 387 |
% PTS sorts %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 388 |
|
| 389 |
pred pts i:sort, i:sort, o:sort. |
| 390 |
pts X Y U :- coq.sort.pts-triple X Y U. |