(* Copyright (C) 2003-2005, HELM Team. * * This file is part of HELM, an Hypertextual, Electronic * Library of Mathematics, developed at the Computer Science * Department, University of Bologna, Italy. * * HELM is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * HELM is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HELM; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, * MA 02111-1307, USA. * * For details, see the HELM World-Wide-Web page, * http://cs.unibo.it/helm/. *) module C = Cic module E = CicEnvironment module Un = CicUniv module TC = CicTypeChecker module D = Deannotate module UM = UriManager module T = ProceduralTypes module Cl = ProceduralClassify (* helpers ******************************************************************) let cic = D.deannotate_term let get_ind_type uri tyno = match E.get_obj Un.empty_ugraph uri with | C.InductiveDefinition (tys, _, lpsno, _), _ -> lpsno, List.nth tys tyno | _ -> assert false let get_default_eliminator context uri tyno ty = let _, (name, _, _, _) = get_ind_type uri tyno in let sort, _ = TC.type_of_aux' [] context ty Un.empty_ugraph in let ext = match sort with | C.Sort C.Prop -> "_ind" | C.Sort C.Set -> "_rec" | C.Sort C.CProp -> "_rec" | C.Sort (C.Type _) -> "_rect" | C.Meta (_,_) -> assert false | _ -> assert false in let buri = UM.buri_of_uri uri in let uri = UM.uri_of_string (buri ^ "/" ^ name ^ ext ^ ".con") in C.Const (uri, []) let rec list_sub start length = function | _ :: tl when start > 0 -> list_sub (pred start) length tl | hd :: tl when length > 0 -> hd :: list_sub start (pred length) tl | _ -> [] (* proof construction *******************************************************) let rec need_whd i = function | C.ACast (_, t, _) -> need_whd i t | C.AProd (_, _, _, t) when i > 0 -> need_whd (pred i) t | _ when i > 0 -> true | _ -> false let lift k n = let rec lift_xns k (uri, t) = uri, lift_term k t and lift_ms k = function | None -> None | Some t -> Some (lift_term k t) and lift_fix len k (id, name, i, ty, bo) = id, name, i, lift_term k ty, lift_term (k + len) bo and lift_cofix len k (id, name, ty, bo) = id, name, lift_term k ty, lift_term (k + len) bo and lift_term k = function | C.ASort _ as t -> t | C.AImplicit _ as t -> t | C.ARel (id, rid, m, b) as t -> if m < k then t else C.ARel (id, rid, m + n, b) | C.AConst (id, uri, xnss) -> C.AConst (id, uri, List.map (lift_xns k) xnss) | C.AVar (id, uri, xnss) -> C.AVar (id, uri, List.map (lift_xns k) xnss) | C.AMutInd (id, uri, tyno, xnss) -> C.AMutInd (id, uri, tyno, List.map (lift_xns k) xnss) | C.AMutConstruct (id, uri, tyno, consno, xnss) -> C.AMutConstruct (id, uri,tyno,consno, List.map (lift_xns k) xnss) | C.AMeta (id, i, mss) -> C.AMeta(id, i, List.map (lift_ms k) mss) | C.AAppl (id, ts) -> C.AAppl (id, List.map (lift_term k) ts) | C.ACast (id, te, ty) -> C.ACast (id, lift_term k te, lift_term k ty) | C.AMutCase (id, sp, i, outty, t, pl) -> C.AMutCase (id, sp, i, lift_term k outty, lift_term k t, List.map (lift_term k) pl) | C.AProd (id, n, s, t) -> C.AProd (id, n, lift_term k s, lift_term (succ k) t) | C.ALambda (id, n, s, t) -> C.ALambda (id, n, lift_term k s, lift_term (succ k) t) | C.ALetIn (id, n, s, t) -> C.ALetIn (id, n, lift_term k s, lift_term (succ k) t) | C.AFix (id, i, fl) -> C.AFix (id, i, List.map (lift_fix (List.length fl) k) fl) | C.ACoFix (id, i, fl) -> C.ACoFix (id, i, List.map (lift_cofix (List.length fl) k) fl) in lift_term k let fake_annotate c = let get_binder c m = try match List.nth c (pred m) with | Some (C.Name s, _) -> s | _ -> assert false with | Invalid_argument _ -> assert false in let mk_decl n v = Some (n, C.Decl v) in let mk_def n v = Some (n, C.Def (v, None)) in let mk_fix (name, _, _, bo) = mk_def (C.Name name) bo in let mk_cofix (name, _, bo) = mk_def (C.Name name) bo in let rec ann_xns c (uri, t) = uri, ann_term c t and ann_ms c = function | None -> None | Some t -> Some (ann_term c t) and ann_fix newc c (name, i, ty, bo) = "", name, i, ann_term c ty, ann_term (List.rev_append newc c) bo and ann_cofix newc c (name, ty, bo) = "", name, ann_term c ty, ann_term (List.rev_append newc c) bo and ann_term c = function | C.Sort sort -> C.ASort ("", sort) | C.Implicit ann -> C.AImplicit ("", ann) | C.Rel m -> C.ARel ("", "", m, get_binder c m) | C.Const (uri, xnss) -> C.AConst ("", uri, List.map (ann_xns c) xnss) | C.Var (uri, xnss) -> C.AVar ("", uri, List.map (ann_xns c) xnss) | C.MutInd (uri, tyno, xnss) -> C.AMutInd ("", uri, tyno, List.map (ann_xns c) xnss) | C.MutConstruct (uri, tyno, consno, xnss) -> C.AMutConstruct ("", uri,tyno,consno, List.map (ann_xns c) xnss) | C.Meta (i, mss) -> C.AMeta("", i, List.map (ann_ms c) mss) | C.Appl ts -> C.AAppl ("", List.map (ann_term c) ts) | C.Cast (te, ty) -> C.ACast ("", ann_term c te, ann_term c ty) | C.MutCase (sp, i, outty, t, pl) -> C.AMutCase ("", sp, i, ann_term c outty, ann_term c t, List.map (ann_term c) pl) | C.Prod (n, s, t) -> C.AProd ("", n, ann_term c s, ann_term (mk_decl n s :: c) t) | C.Lambda (n, s, t) -> C.ALambda ("", n, ann_term c s, ann_term (mk_decl n s :: c) t) | C.LetIn (n, s, t) -> C.ALetIn ("", n, ann_term c s, ann_term (mk_def n s :: c) t) | C.Fix (i, fl) -> C.AFix ("", i, List.map (ann_fix (List.rev_map mk_fix fl) c) fl) | C.CoFix (i, fl) -> C.ACoFix ("", i, List.map (ann_cofix (List.rev_map mk_cofix fl) c) fl) in ann_term c let rec add_abst n t = if n <= 0 then t else let t = C.ALambda ("", C.Name "foo", C.AImplicit ("", None), lift 0 1 t) in add_abst (pred n) t let mk_ind context id uri tyno outty arg cases = try let is_recursive = function | C.MutInd (u, no, _) -> UM.eq u uri && no = tyno | _ -> false in let lpsno, (_, _, _, constructors) = get_ind_type uri tyno in let inty, _ = TC.type_of_aux' [] context (cic arg) Un.empty_ugraph in let ps = match inty with | C.MutInd _ -> [] | C.Appl (C.MutInd _ :: args) -> List.map (fake_annotate context) args | _ -> assert false in let lps, rps = T.list_split lpsno ps in let eliminator = get_default_eliminator context uri tyno inty in let eliminator = fake_annotate context eliminator in let arg_ref = T.mk_arel 0 "foo" in let body = C.AMutCase (id, uri, tyno, outty, arg_ref, cases) in let predicate = add_abst (succ (List.length rps)) body in let map2 case (_, cty) = let map (h, case, k) premise = if h > 0 then pred h, lift k 1 case, k else if is_recursive premise then 0, lift (succ k) 1 case, succ k else 0, case, succ k in let premises, _ = Cl.split context cty in let _, lifted_case, _ = List.fold_left map (lpsno, case, 1) (List.rev (List.tl premises)) in lifted_case in let lifted_cases = List.map2 map2 cases constructors in let args = eliminator :: lps @ predicate :: lifted_cases @ rps @ [arg] in Some (C.AAppl (id, args)) with Invalid_argument _ -> failwith "PCn.mk_ind"