1 (* Copyright (C) 2003-2005, HELM Team.
3 * This file is part of HELM, an Hypertextual, Electronic
4 * Library of Mathematics, developed at the Computer Science
5 * Department, University of Bologna, Italy.
7 * HELM is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * HELM is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with HELM; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22 * For details, see the HELM World-Wide-Web page,
23 * http://cs.unibo.it/helm/.
27 module E = CicEnvironment
29 module TC = CicTypeChecker
31 module UM = UriManager
33 module T = ProceduralTypes
34 module Cl = ProceduralClassify
35 module M = ProceduralMode
37 (* helpers ******************************************************************)
39 let cic = D.deannotate_term
41 let get_ind_type uri tyno =
42 match E.get_obj Un.empty_ugraph uri with
43 | C.InductiveDefinition (tys, _, lpsno, _), _ -> lpsno, List.nth tys tyno
46 let get_default_eliminator context uri tyno ty =
47 let _, (name, _, _, _) = get_ind_type uri tyno in
48 let sort, _ = TC.type_of_aux' [] context ty Un.empty_ugraph in
49 let ext = match sort with
50 | C.Sort C.Prop -> "_ind"
51 | C.Sort C.Set -> "_rec"
52 | C.Sort C.CProp -> "_rec"
53 | C.Sort (C.Type _) -> "_rect"
54 | C.Meta (_,_) -> assert false
57 let buri = UM.buri_of_uri uri in
58 let uri = UM.uri_of_string (buri ^ "/" ^ name ^ ext ^ ".con") in
61 let rec list_sub start length = function
62 | _ :: tl when start > 0 -> list_sub (pred start) length tl
63 | hd :: tl when length > 0 -> hd :: list_sub start (pred length) tl
67 (* proof construction *******************************************************)
70 let rec lift_xns k (uri, t) = uri, lift_term k t
71 and lift_ms k = function
73 | Some t -> Some (lift_term k t)
74 and lift_fix len k (id, name, i, ty, bo) =
75 id, name, i, lift_term k ty, lift_term (k + len) bo
76 and lift_cofix len k (id, name, ty, bo) =
77 id, name, lift_term k ty, lift_term (k + len) bo
78 and lift_term k = function
80 | C.AImplicit _ as t -> t
81 | C.ARel (id, rid, m, b) as t -> if m < k then t else C.ARel (id, rid, m + n, b)
82 | C.AConst (id, uri, xnss) -> C.AConst (id, uri, List.map (lift_xns k) xnss)
83 | C.AVar (id, uri, xnss) -> C.AVar (id, uri, List.map (lift_xns k) xnss)
84 | C.AMutInd (id, uri, tyno, xnss) -> C.AMutInd (id, uri, tyno, List.map (lift_xns k) xnss)
85 | C.AMutConstruct (id, uri, tyno, consno, xnss) -> C.AMutConstruct (id, uri,tyno,consno, List.map (lift_xns k) xnss)
86 | C.AMeta (id, i, mss) -> C.AMeta(id, i, List.map (lift_ms k) mss)
87 | C.AAppl (id, ts) -> C.AAppl (id, List.map (lift_term k) ts)
88 | C.ACast (id, te, ty) -> C.ACast (id, lift_term k te, lift_term k ty)
89 | 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)
90 | C.AProd (id, n, s, t) -> C.AProd (id, n, lift_term k s, lift_term (succ k) t)
91 | C.ALambda (id, n, s, t) -> C.ALambda (id, n, lift_term k s, lift_term (succ k) t)
92 | C.ALetIn (id, n, s, t) -> C.ALetIn (id, n, lift_term k s, lift_term (succ k) t)
93 | C.AFix (id, i, fl) -> C.AFix (id, i, List.map (lift_fix (List.length fl) k) fl)
94 | C.ACoFix (id, i, fl) -> C.ACoFix (id, i, List.map (lift_cofix (List.length fl) k) fl)
100 try match List.nth c (pred m) with
101 | Some (C.Name s, _) -> s
104 | Invalid_argument _ -> assert false
106 let mk_decl n v = Some (n, C.Decl v) in
107 let mk_def n v = Some (n, C.Def (v, None)) in
108 let mk_fix (name, _, _, bo) = mk_def (C.Name name) bo in
109 let mk_cofix (name, _, bo) = mk_def (C.Name name) bo in
110 let rec ann_xns c (uri, t) = uri, ann_term c t
111 and ann_ms c = function
113 | Some t -> Some (ann_term c t)
114 and ann_fix newc c (name, i, ty, bo) =
115 "", name, i, ann_term c ty, ann_term (List.rev_append newc c) bo
116 and ann_cofix newc c (name, ty, bo) =
117 "", name, ann_term c ty, ann_term (List.rev_append newc c) bo
118 and ann_term c = function
119 | C.Sort sort -> C.ASort ("", sort)
120 | C.Implicit ann -> C.AImplicit ("", ann)
121 | C.Rel m -> C.ARel ("", "", m, get_binder c m)
122 | C.Const (uri, xnss) -> C.AConst ("", uri, List.map (ann_xns c) xnss)
123 | C.Var (uri, xnss) -> C.AVar ("", uri, List.map (ann_xns c) xnss)
124 | C.MutInd (uri, tyno, xnss) -> C.AMutInd ("", uri, tyno, List.map (ann_xns c) xnss)
125 | C.MutConstruct (uri, tyno, consno, xnss) -> C.AMutConstruct ("", uri,tyno,consno, List.map (ann_xns c) xnss)
126 | C.Meta (i, mss) -> C.AMeta("", i, List.map (ann_ms c) mss)
127 | C.Appl ts -> C.AAppl ("", List.map (ann_term c) ts)
128 | C.Cast (te, ty) -> C.ACast ("", ann_term c te, ann_term c ty)
129 | 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)
130 | C.Prod (n, s, t) -> C.AProd ("", n, ann_term c s, ann_term (mk_decl n s :: c) t)
131 | C.Lambda (n, s, t) -> C.ALambda ("", n, ann_term c s, ann_term (mk_decl n s :: c) t)
132 | C.LetIn (n, s, t) -> C.ALetIn ("", n, ann_term c s, ann_term (mk_def n s :: c) t)
133 | C.Fix (i, fl) -> C.AFix ("", i, List.map (ann_fix (List.rev_map mk_fix fl) c) fl)
134 | C.CoFix (i, fl) -> C.ACoFix ("", i, List.map (ann_cofix (List.rev_map mk_cofix fl) c) fl)
138 let rec add_abst n t =
139 if n <= 0 then t else
140 let t = C.ALambda ("", C.Name "foo", C.AImplicit ("", None), lift 0 1 t) in
143 let mk_ind context id uri tyno outty arg cases =
145 let is_recursive = function
146 | C.MutInd (u, no, _) -> UM.eq u uri && no = tyno
149 let lpsno, (_, _, _, constructors) = get_ind_type uri tyno in
150 let inty, _ = TC.type_of_aux' [] context (cic arg) Un.empty_ugraph in
151 let ps = match inty with
153 | C.Appl (C.MutInd _ :: args) -> List.map (fake_annotate context) args
156 let lps, rps = T.list_split lpsno ps in
157 let eliminator = get_default_eliminator context uri tyno inty in
158 let eliminator = fake_annotate context eliminator in
159 let arg_ref = T.mk_arel 0 "foo" in
160 let body = C.AMutCase (id, uri, tyno, outty, arg_ref, cases) in
161 let predicate = add_abst (succ (List.length rps)) body in
162 let map2 case (_, cty) =
163 let map (h, case, k) premise =
164 if h > 0 then pred h, lift k 1 case, k else
165 if is_recursive premise then 0, lift (succ k) 1 case, succ k else
168 let premises, _ = Cl.split context cty in
169 let _, lifted_case, _ =
170 List.fold_left map (lpsno, case, 1) (List.rev (List.tl premises))
174 let lifted_cases = List.map2 map2 cases constructors in
175 let args = eliminator :: lps @ predicate :: lifted_cases @ rps @ [arg] in
176 Some (C.AAppl (id, args))
177 with Invalid_argument _ -> failwith "PCn.mk_ind"
179 let apply_substs substs =
180 let length = List.length substs in
181 let rec apply_xns k (uri, t) = uri, apply_term k t
182 and apply_ms k = function
184 | Some t -> Some (apply_term k t)
185 and apply_fix len k (id, name, i, ty, bo) =
186 id, name, i, apply_term k ty, apply_term (k + len) bo
187 and apply_cofix len k (id, name, ty, bo) =
188 id, name, apply_term k ty, apply_term (k + len) bo
189 and apply_term k = function
190 | C.ASort _ as t -> t
191 | C.AImplicit _ as t -> t
192 | C.ARel (id, rid, m, b) as t ->
193 if m < k || m >= length + k then t
194 else lift 1 k (List.nth substs (m - k))
195 | C.AConst (id, uri, xnss) -> C.AConst (id, uri, List.map (apply_xns k) xnss)
196 | C.AVar (id, uri, xnss) -> C.AVar (id, uri, List.map (apply_xns k) xnss)
197 | C.AMutInd (id, uri, tyno, xnss) -> C.AMutInd (id, uri, tyno, List.map (apply_xns k) xnss)
198 | C.AMutConstruct (id, uri, tyno, consno, xnss) -> C.AMutConstruct (id, uri,tyno,consno, List.map (apply_xns k) xnss)
199 | C.AMeta (id, i, mss) -> C.AMeta(id, i, List.map (apply_ms k) mss)
200 | C.AAppl (id, ts) -> C.AAppl (id, List.map (apply_term k) ts)
201 | C.ACast (id, te, ty) -> C.ACast (id, apply_term k te, apply_term k ty)
202 | C.AMutCase (id, sp, i, outty, t, pl) -> C.AMutCase (id, sp, i, apply_term k outty, apply_term k t, List.map (apply_term k) pl)
203 | C.AProd (id, n, s, t) -> C.AProd (id, n, apply_term k s, apply_term (succ k) t)
204 | C.ALambda (id, n, s, t) -> C.ALambda (id, n, apply_term k s, apply_term (succ k) t)
205 | C.ALetIn (id, n, s, t) -> C.ALetIn (id, n, apply_term k s, apply_term (succ k) t)
206 | C.AFix (id, i, fl) -> C.AFix (id, i, List.map (apply_fix (List.length fl) k) fl)
207 | C.ACoFix (id, i, fl) -> C.ACoFix (id, i, List.map (apply_cofix (List.length fl) k) fl)
211 let hole = C.AImplicit ("", Some `Hole)
213 let mk_pattern rps predicate = hole
214 (* let rec clear_absts n = function
215 | C.ALambda (_, _, _, t) when n > 0 -> clear_absts (pred n) t
216 (* | t when n > 0 -> assert false *)
219 let substs = hole :: List.rev rps in
220 let body = clear_absts (succ (List.length rps)) predicate in
221 if M.is_appl true (cic body) then apply_substs substs body else hole