(* ||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 L = Log module P = Marks module G = Options module E = Entity module Z = Bag module ZO = BagOutput module ZE = BagEnvironment module ZS = BagSubstitution IFDEF TYPE THEN type machine = { i: int; c: Z.lenv; s: Z.term list } type whd_result = | Sort_ of int | LRef_ of P.mark * Z.term option | GRef_ of Z.entity | Bind_ of Z.b_attrs * P.mark * Z.term * Z.term type ho_whd_result = | Sort of int | Abst of Z.term (* Internal functions *******************************************************) let level = 5 let term_of_whdr = function | Sort_ h -> Z.Sort h | LRef_ (i, _) -> Z.LRef i | GRef_ (_, _, uri, _) -> Z.GRef uri | Bind_ (a, l, w, t) -> Z.bind_abst a l w t let log1 st s c t = let s1, s2 = s ^ " in the environment", "the term" in L.log st ZO.specs (pred level) (L.et_items1 s1 c s2 t) let log2 st s cu u ct t = let s1, s2, s3 = s ^ " in the environment", "the term", "and in the environment" in L.log st ZO.specs (pred level) (L.et_items2 s1 cu s2 u ~sc2:s3 ~c2:ct s2 t) let empty_machine = {i = 0; c = Z.empty_lenv; s = []} let inc m = {m with i = succ m.i} let unwind_to_term f m t = let map f t (y, l, b) = f (Z.Bind (y, l, b, t)) in let f mc = C.list_fold_left f map t mc in Z.contents f m.c let unwind_stack f m = let map f v = unwind_to_term f m v in C.list_map f map m.s let get f c m i = let f _ b = f b in let f c = Z.get C.err f c i in Z.append f c m.c let push msg f c m a l w = assert (m.s = []); let f w = Z.push msg f c a l (Z.Abst w) in unwind_to_term f m w (* to share *) let rec whd f c m x = (* L.warn "entering R.whd"; *) match x with | Z.Sort h -> f m (Sort_ h) | Z.GRef uri -> let f entry = f m (GRef_ entry) in ZE.get_entity f uri | Z.LRef i -> let f = function | Z.Void -> f m (LRef_ (i, None)) | Z.Abst t -> f m (LRef_ (i, Some t)) | Z.Abbr t -> whd f c m t in get f c m i | Z.Cast (_, t) -> whd f c m t | Z.Appl (v, t) -> whd f c {m with s = v :: m.s} t | Z.Bind (y, l, Z.Abst w, t) -> begin match m.s with | [] -> f m (Bind_ (y, l, w, t)) | v :: tl -> let nl = P.new_mark () in let f mc = ZS.subst (whd f c {m with c = mc; s = tl}) nl l t in Z.push "!" f m.c y nl (Z.Abbr (Z.Cast (w, v))) end | Z.Bind (y, l, b, t) -> let nl = P.new_mark () in let f mc = ZS.subst (whd f c {m with c = mc}) nl l t in Z.push "!" f m.c y nl b (* Interface functions ******************************************************) let rec ho_whd f c m x = (* L.warn "entering R.ho_whd"; *) let aux m = function | Sort_ h -> f (Sort h) | Bind_ (_, _, w, _) -> let f w = f (Abst w) in unwind_to_term f m w | LRef_ (_, Some w) -> ho_whd f c m w | GRef_ (_, _, _, E.Abst (_, w)) -> ho_whd f c m w | GRef_ (_, _, _, E.Abbr (_, v)) -> ho_whd f c m v | LRef_ (_, None) -> assert false | GRef_ (_, _, _, E.Void) -> assert false in whd aux c m x let ho_whd f st c t = IFDEF TRACE THEN if !G.ct >= level then log1 st "Now scanning" c t ELSE () END; ho_whd f c empty_machine t let rec are_convertible f st a c m1 t1 m2 t2 = (* L.warn "entering R.are_convertible"; *) let rec aux m1 r1 m2 r2 = (* L.warn "entering R.are_convertible_aux"; *) IFDEF TRACE THEN let u, t = term_of_whdr r1, term_of_whdr r2 in if !G.ct >= level then log2 st "Now really converting" c u c t ELSE () END; match r1, r2 with | Sort_ k1, Sort_ k2 -> if k1 = k2 then f a else f false | LRef_ (i1, _), LRef_ (i2, _) -> if i1 = i2 then are_convertible_stacks f st a c m1 m2 else f false | GRef_ (_, {E.n_apix = a1}, _, E.Abst _), GRef_ (_, {E.n_apix = a2}, _, E.Abst _) -> if a1 = a2 then are_convertible_stacks f st a c m1 m2 else f false | GRef_ (_, {E.n_apix = a1}, _, E.Abbr (_, v1)), GRef_ (_, {E.n_apix = a2}, _, E.Abbr (_, v2)) -> if a1 = a2 then let f a = if a then f a else are_convertible f st true c m1 v1 m2 v2 in are_convertible_stacks f st a c m1 m2 else if a1 < a2 then whd (aux m1 r1) c m2 v2 else whd (aux_rev m2 r2) c m1 v1 | _, GRef_ (_, _, _, E.Abbr (_, v2)) -> whd (aux m1 r1) c m2 v2 | GRef_ (_, _, _, E.Abbr (_, v1)), _ -> whd (aux_rev m2 r2) c m1 v1 | Bind_ (y1, l1, w1, t1), Bind_ (_, l2, w2, t2) -> let l = P.new_mark () in let h c = let m1, m2 = inc m1, inc m2 in let f t1 = ZS.subst (are_convertible f st a c m1 t1 m2) l l2 t2 in ZS.subst f l l1 t1 in let f r = if r then push "!" h c m1 y1 l w1 else f false in are_convertible f st a c m1 w1 m2 w2 (* we detect the AUT-QE reduction rule for type/prop inclusion *) | Sort_ _, Bind_ (y2, l2, w2, t2) when !G.si -> let m1, m2 = inc m1, inc m2 in let f c = are_convertible f st a c m1 (term_of_whdr r1) m2 t2 in push "nsi" f c m2 y2 l2 w2 | _ -> f false and aux_rev m2 r2 m1 r1 = aux m1 r1 m2 r2 in let g m1 r1 = whd (aux m1 r1) c m2 t2 in if a = false then f false else whd g c m1 t1 and are_convertible_stacks f st a c m1 m2 = (* L.warn "entering R.are_convertible_stacks"; *) let mm1, mm2 = {m1 with s = []}, {m2 with s = []} in let map f a v1 v2 = are_convertible f st a c mm1 v1 mm2 v2 in if List.length m1.s <> List.length m2.s then begin (* L.warn (Printf.sprintf "Different lengths: %u %u" (List.length m1.s) (List.length m2.s) ); *) f false end else C.list_fold_left2 f map a m1.s m2.s let are_convertible f st c u t = IFDEF TRACE THEN if !G.ct >= level then log2 st "Now converting" c u c t ELSE () END; are_convertible f st true c empty_machine u empty_machine t END