(* ||M|| This file is part of HELM, an Hypertextual, Electronic ||A|| Library of Mathematics, developed at the Computer Science ||T|| Department, University of Bologna, Italy. ||I|| ||T|| HELM is free software; you can redistribute it and/or ||A|| modify it under the terms of the GNU General Public License \ / version 2 or (at your option) any later version. \ / This software is distributed as is, NO WARRANTY. V_______________________________________________________________ *) module U = NUri module C = Cps module S = Share module L = Log module H = Hierarchy module I = AutItem module B = Bag module O = BagOutput module E = BagEnvironment module R = BagReduction exception TypeError of B.message (* Internal functions *******************************************************) let level = 4 let log1 s c t = let sc, st = s ^ " in the context", "the term" in L.log O.specs level (L.ct_items1 sc c st t) let error1 st c t = let sc = "In the context" in raise (TypeError (L.ct_items1 sc c st t)) let error3 c t1 t2 t3 = let sc, st1, st2, st3 = "In the context", "the term", "is of type", "but must be of type" in raise (TypeError (L.ct_items3 sc c st1 t1 st2 t2 st3 t3)) let mk_gref u l = let map t v = B.Appl (v, t) in List.fold_left map (B.GRef u) l let add_coercion f t (i, uri, vs) = let rec add f x = match x with | B.Sort _ | B.LRef _ | B.GRef _ -> f x | B.Cast (u, t) -> let f uu = let f tt = f (S.sh2 u uu t tt x B.cast) in add f t in add f u | B.Appl (v, t) -> let f vv = let f tt = f (S.sh2 v vv t tt x B.appl) in add f t in add f v | B.Bind (l, _, _, _) when i = l -> if U.eq uri I.imp then f (mk_gref I.mt (vs @ [x])) else if U.eq uri I.all then f (mk_gref I.alli (vs @ [x])) else assert false | B.Bind (l, id, B.Abst w, t) -> let f ww = let f tt = f (S.sh2 w ww t tt x (B.bind_abst l id)) in add f t in add f w | B.Bind (l, id, B.Abbr v, t) -> let f vv = let f tt = f (S.sh2 v vv t tt x (B.bind_abbr l id)) in add f t in add f v | B.Bind (l, id, B.Void, t) -> let f tt = f (S.sh1 t tt x (B.bind l id B.Void)) in add f t in add f t let add_coercions f = C.list_fold_left f add_coercion (* Interface functions ******************************************************) let rec b_type_of f g c x = log1 "Now checking" c x; match x with | B.Sort h -> let f h = f x (B.Sort h) in H.apply f g h | B.LRef i -> let f = function | Some (_, B.Abst w) -> f x w | Some (_, B.Abbr (B.Cast (w, v))) -> f x w | Some (_, B.Abbr _) -> assert false | Some (_, B.Void) -> error1 "reference to excluded variable" c x | None -> error1 "variable not found" c x in B.get f c i | B.GRef uri -> let f = function | _, _, B.Abst w -> f x w | _, _, B.Abbr (B.Cast (w, v)) -> f x w | _, _, B.Abbr _ -> assert false | _, _, B.Void -> error1 "reference to excluded object" c x in E.get_obj f uri | B.Bind (l, id, B.Abbr v, t) -> let f xv xt tt = f (S.sh2 v xv t xt x (B.bind_abbr l id)) (B.bind_abbr l id xv tt) in let f xv cc = b_type_of (f xv) g cc t in let f xv = B.push (f xv) c l id (B.Abbr xv) in let f xv vv = match xv with | B.Cast _ -> f xv | _ -> f (B.Cast (vv, xv)) in type_of f g c v | B.Bind (l, id, B.Abst u, t) -> let f xu xt tt = f (S.sh2 u xu t xt x (B.bind_abst l id)) (B.bind_abst l id xu tt) in let f xu cc = b_type_of (f xu) g cc t in let f xu _ = B.push (f xu) c l id (B.Abst xu) in type_of f g c u | B.Bind (l, id, B.Void, t) -> let f xt tt = f (S.sh1 t xt x (B.bind l id B.Void)) (B.bind l id B.Void tt) in let f cc = b_type_of f g cc t in B.push f c l id B.Void | B.Appl (v, t) -> let f xv vv xt tt = function | R.Abst w -> L.box (succ level); L.log O.specs (succ level) (L.t_items1 "Just scanned" c w); L.unbox (succ level); let f = function | Some [] -> f (S.sh2 v xv t xt x B.appl) (B.appl xv tt) | Some l -> L.log O.specs level (L.items1 "Rechecking coerced term"); let f xv = b_type_of f g c (S.sh2 v xv t xt x B.appl) in add_coercions f xv l | None -> error3 c xv vv w in R.are_convertible f c w vv (* inserting a missing "modus ponens" *) | R.GRef (uri, vs) when U.eq uri I.imp -> L.log O.specs level (L.items1 "Rechecking coerced term"); b_type_of f g c (mk_gref I.mp (vs @ [xv; xt])) | R.GRef (uri, vs) when U.eq uri I.all -> L.log O.specs level (L.items1 "Rechecking coerced term"); b_type_of f g c (mk_gref I.alle (vs @ [xt; xv])) | _ -> error1 "not a function" c xt in let f xv vv xt tt = R.ho_whd (f xv vv xt tt) c tt in let f xv vv = b_type_of (f xv vv) g c t in type_of f g c v | B.Cast (u, t) -> let f xu xt tt = function | Some [] -> f (S.sh2 u xu t xt x B.cast) xu | Some l -> L.log O.specs level (L.items1 "Rechecking coerced term"); let f xt = b_type_of f g c (S.sh2 u xu t xt x B.cast) in add_coercions f xt l | None -> error3 c xt tt xu in let f xu xt tt = R.are_convertible (f xu xt tt) c xu tt in let f xu _ = b_type_of (f xu) g c t in type_of f g c u and type_of f g c x = let f t u = L.unbox level; f t u in L.box level; b_type_of f g c x