@@ -41,25 +41,33 @@ module Constraints = struct
4141 (NotUnifiable (Unchecked. ty_to_string ty1, Unchecked. ty_to_string ty2))
4242
4343 and unify_tm cst tm1 tm2 =
44- match (tm1, tm2) with
45- | Meta_tm _ , Meta_tm _ when tm1 = tm2 -> ()
46- | Meta_tm _ , _ | _ , Meta_tm _ -> Queue. enqueue cst.tm (tm1, tm2)
47- | Var v1 , Var v2 when v1 = v2 -> ()
48- | Coh (coh1 , s1 ), Coh (coh2 , s2 ) -> (
49- try
50- Coh. check_equal coh1 coh2;
51- unify_sub_ps cst s1 s2
52- with Invalid_argument _ ->
53- raise (NotUnifiable (Coh. to_string coh1, Coh. to_string coh2)))
54- | App (t1 , s1 ), App (t2 , s2 ) when t1 == t2 -> unify_sub cst s1 s2
55- | App (t, s), ((App _ | Coh _ | Var _) as tm2)
56- | ((Coh _ | Var _ ) as tm2 ), App (t , s ) ->
57- unify_tm cst (Unchecked. tm_apply_sub (Tm. develop t) s) tm2
58- | Var _ , Coh _ | Coh _ , Var _ | Var _ , Var _ ->
59- raise
60- (NotUnifiable (Unchecked. tm_to_string tm1, Unchecked. tm_to_string tm2))
61- | _ , _ ->
62- Error. fatal " unification not implemented for invertibility structures"
44+ (* Io.debug "unfiying tm %s and %s" *)
45+ (* (Unchecked.tm_to_string tm1) *)
46+ (* (Unchecked.tm_to_string tm2); *)
47+ if tm1 = tm2 then ()
48+ else
49+ match (tm1, tm2) with
50+ | Meta_tm _ , _ | _ , Meta_tm _ -> Queue. enqueue cst.tm (tm1, tm2)
51+ | Var v1 , Var v2 when v1 = v2 -> ()
52+ | Coh (coh1 , s1 ), Coh (coh2 , s2 ) -> (
53+ try
54+ Coh. check_equal coh1 coh2;
55+ unify_sub_ps cst s1 s2
56+ with Invalid_argument _ ->
57+ raise (NotUnifiable (Coh. to_string coh1, Coh. to_string coh2)))
58+ | App (t1 , s1 ), App (t2 , s2 ) when t1 == t2 -> unify_sub cst s1 s2
59+ | App (t, s), ((App _ | Coh _ | Var _) as tm2)
60+ | ((Coh _ | Var _ ) as tm2 ), App (t , s ) ->
61+ unify_tm cst (Unchecked. tm_apply_sub (Tm. develop t) s) tm2
62+ | Var _ , Coh _ | Coh _ , Var _ | Var _ , Var _ ->
63+ raise
64+ (NotUnifiable
65+ (Unchecked. tm_to_string tm1, Unchecked. tm_to_string tm2))
66+ | _ , _ ->
67+ Error. fatal
68+ (Printf. sprintf " trying to unify %s and %s"
69+ (Unchecked. tm_to_string tm1)
70+ (Unchecked. tm_to_string tm2))
6371
6472 and unify_sub cst s1 s2 =
6573 match (s1, s2) with
@@ -111,6 +119,20 @@ module Constraints = struct
111119 List. map
112120 (fun (x , (t , e )) -> (x, (tm_replace_meta_tm (i, tm') t, e)))
113121 s )
122+ | Can (tm , tms ) ->
123+ Can
124+ ( tm_replace_meta_tm (i, tm') tm,
125+ List. map (tm_replace_meta_tm (i, tm')) tms )
126+ | IS (inv , u ) -> IS (inv, tm_replace_meta_tm (i, tm') u)
127+ | Coind (t0 , t1 , t2 , t3 , t4 , t5 , t6 ) ->
128+ Coind
129+ ( tm_replace_meta_tm (i, tm') t0,
130+ tm_replace_meta_tm (i, tm') t1,
131+ tm_replace_meta_tm (i, tm') t2,
132+ tm_replace_meta_tm (i, tm') t3,
133+ tm_replace_meta_tm (i, tm') t4,
134+ tm_replace_meta_tm (i, tm') t5,
135+ tm_replace_meta_tm (i, tm') t6 )
114136 | _ ->
115137 Error. fatal
116138 " resolution of meta_variables in invertibility\n \
@@ -194,8 +216,26 @@ module Constraints = struct
194216 aux c { uty = [] ; utm = [] }
195217end
196218
219+ let inverse_ty ty =
220+ match ty with
221+ | Obj | Inv _ -> assert false
222+ | Arr (ty , u , v ) -> Arr (ty, v, u)
223+ | Meta_ty _ -> Meta. new_ty ()
224+
225+ let lunit_ty tm linv =
226+ let y = Construct. tgt 1 tm in
227+ Construct. arr (Construct. comp linv tm) (Construct. id y)
228+
229+ let runit_ty tm rinv =
230+ let x = Construct. src 1 tm in
231+ Construct. arr (Construct. comp tm rinv) (Construct. id x)
232+
197233module Constraints_typing = struct
198234 let rec tm ctx meta_ctx t cst =
235+ (* Io.debug "constraint typing term %s in ctx %s, meta_ctx %s" *)
236+ (* (Unchecked.tm_to_string t) *)
237+ (* (Unchecked.ctx_to_string ctx) *)
238+ (* (Unchecked.meta_ctx_to_string meta_ctx); *)
199239 Io. info ~v: 5
200240 (lazy
201241 (Printf. sprintf " constraint typing term %s in ctx %s, meta_ctx %s"
@@ -222,6 +262,41 @@ module Constraints_typing = struct
222262 let ty = Ty. forget (Tm. typ t) in
223263 let s = sub ctx meta_ctx s tgt cst in
224264 (App (t, s), Unchecked. ty_apply_sub ty s)
265+ | Can (t , tms ) ->
266+ let t, _ = tm ctx meta_ctx t cst in
267+ let tms = List. map (fun t -> fst (tm ctx meta_ctx t cst)) tms in
268+ (* ignore the constraints on tms : no elaboration *)
269+ (Can (t, tms), Inv t)
270+ | IS (inv , e ) ->
271+ let e, te = tm ctx meta_ctx e cst in
272+ let u = match te with Inv u -> u | _ -> assert false in
273+ let u, tu = tm ctx meta_ctx u cst in
274+ let tui = inverse_ty tu in
275+ let ty =
276+ match inv with
277+ | LInv -> tui
278+ | RInv -> tui
279+ | Lunit -> lunit_ty (u, tu) (IS (LInv , e), tui)
280+ | Runit -> runit_ty (u, tu) (IS (RInv , e), tui)
281+ | Lwit -> Inv (IS (Lunit , e))
282+ | Rwit -> Inv (IS (Runit , e))
283+ in
284+ (IS (inv, e), ty)
285+ | Coind (t0 , t1 , t2 , t3 , t4 , t5 , t6 ) ->
286+ let t0, ty0 = tm ctx meta_ctx t0 cst in
287+ let t1, ty1 = tm ctx meta_ctx t1 cst in
288+ let t2, ty2 = tm ctx meta_ctx t2 cst in
289+ let t3, ty3 = tm ctx meta_ctx t3 cst in
290+ let t4, ty4 = tm ctx meta_ctx t4 cst in
291+ let t5, ty5 = tm ctx meta_ctx t5 cst in
292+ let t6, ty6 = tm ctx meta_ctx t6 cst in
293+ Constraints. unify_ty cst (inverse_ty ty0) ty1;
294+ Constraints. unify_ty cst (inverse_ty ty0) ty2;
295+ Constraints. unify_ty cst (lunit_ty (t0, ty0) (t1, ty1)) ty3;
296+ Constraints. unify_ty cst (runit_ty (t0, ty0) (t2, ty2)) ty4;
297+ Constraints. unify_ty cst (Inv t3) ty5;
298+ Constraints. unify_ty cst (Inv t4) ty6;
299+ (Coind (t0, t1, t2, t3, t4, t5, t6), Inv t0)
225300 | _ -> Error. fatal " invertibility structures in constraint typing"
226301
227302 and sub src meta_ctx s tgt cst =
0 commit comments