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
32 module Rd = CicReduction
34 module P = ProceduralPreprocess
35 module T = ProceduralTypes
36 module M = ProceduralMode
38 (* helpers ******************************************************************)
40 let cic = D.deannotate_term
42 let get_ind_type uri tyno =
43 match E.get_obj Un.empty_ugraph uri with
44 | C.InductiveDefinition (tys, _, lpsno, _), _ -> lpsno, List.nth tys tyno
47 let get_default_eliminator context uri tyno ty =
48 let _, (name, _, _, _) = get_ind_type uri tyno in
49 let sort, _ = TC.type_of_aux' [] context ty Un.empty_ugraph in
50 let ext = match sort with
51 | C.Sort C.Prop -> "_ind"
52 | C.Sort C.Set -> "_rec"
53 | C.Sort C.CProp -> "_rec"
54 | C.Sort (C.Type _) -> "_rect"
55 | C.Meta (_,_) -> assert false
58 let buri = UM.buri_of_uri uri in
59 let uri = UM.uri_of_string (buri ^ "/" ^ name ^ ext ^ ".con") in
62 let rec list_sub start length = function
63 | _ :: tl when start > 0 -> list_sub (pred start) length tl
64 | 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 ->
83 if m + n > 0 then C.ARel (id, rid, m + n, b) else
85 | C.AConst (id, uri, xnss) -> C.AConst (id, uri, List.map (lift_xns k) xnss)
86 | C.AVar (id, uri, xnss) -> C.AVar (id, uri, List.map (lift_xns k) xnss)
87 | C.AMutInd (id, uri, tyno, xnss) -> C.AMutInd (id, uri, tyno, List.map (lift_xns k) xnss)
88 | C.AMutConstruct (id, uri, tyno, consno, xnss) -> C.AMutConstruct (id, uri,tyno,consno, List.map (lift_xns k) xnss)
89 | C.AMeta (id, i, mss) -> C.AMeta(id, i, List.map (lift_ms k) mss)
90 | C.AAppl (id, ts) -> C.AAppl (id, List.map (lift_term k) ts)
91 | C.ACast (id, te, ty) -> C.ACast (id, lift_term k te, lift_term k ty)
92 | 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)
93 | C.AProd (id, n, s, t) -> C.AProd (id, n, lift_term k s, lift_term (succ k) t)
94 | C.ALambda (id, n, s, t) -> C.ALambda (id, n, lift_term k s, lift_term (succ k) t)
95 | C.ALetIn (id, n, s, t) -> C.ALetIn (id, n, lift_term k s, lift_term (succ k) t)
96 | C.AFix (id, i, fl) -> C.AFix (id, i, List.map (lift_fix (List.length fl) k) fl)
97 | C.ACoFix (id, i, fl) -> C.ACoFix (id, i, List.map (lift_cofix (List.length fl) k) fl)
101 let fake_annotate c =
103 try match List.nth c (pred m) with
104 | Some (C.Name s, _) -> s
107 | Invalid_argument _ -> assert false
109 let mk_decl n v = Some (n, C.Decl v) in
110 let mk_def n v = Some (n, C.Def (v, None)) in
111 let mk_fix (name, _, _, bo) = mk_def (C.Name name) bo in
112 let mk_cofix (name, _, bo) = mk_def (C.Name name) bo in
113 let rec ann_xns c (uri, t) = uri, ann_term c t
114 and ann_ms c = function
116 | Some t -> Some (ann_term c t)
117 and ann_fix newc c (name, i, ty, bo) =
118 "", name, i, ann_term c ty, ann_term (List.rev_append newc c) bo
119 and ann_cofix newc c (name, ty, bo) =
120 "", name, ann_term c ty, ann_term (List.rev_append newc c) bo
121 and ann_term c = function
122 | C.Sort sort -> C.ASort ("", sort)
123 | C.Implicit ann -> C.AImplicit ("", ann)
124 | C.Rel m -> C.ARel ("", "", m, get_binder c m)
125 | C.Const (uri, xnss) -> C.AConst ("", uri, List.map (ann_xns c) xnss)
126 | C.Var (uri, xnss) -> C.AVar ("", uri, List.map (ann_xns c) xnss)
127 | C.MutInd (uri, tyno, xnss) -> C.AMutInd ("", uri, tyno, List.map (ann_xns c) xnss)
128 | C.MutConstruct (uri, tyno, consno, xnss) -> C.AMutConstruct ("", uri,tyno,consno, List.map (ann_xns c) xnss)
129 | C.Meta (i, mss) -> C.AMeta("", i, List.map (ann_ms c) mss)
130 | C.Appl ts -> C.AAppl ("", List.map (ann_term c) ts)
131 | C.Cast (te, ty) -> C.ACast ("", ann_term c te, ann_term c ty)
132 | 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)
133 | C.Prod (n, s, t) -> C.AProd ("", n, ann_term c s, ann_term (mk_decl n s :: c) t)
134 | C.Lambda (n, s, t) -> C.ALambda ("", n, ann_term c s, ann_term (mk_decl n s :: c) t)
135 | C.LetIn (n, s, t) -> C.ALetIn ("", n, ann_term c s, ann_term (mk_def n s :: c) t)
136 | C.Fix (i, fl) -> C.AFix ("", i, List.map (ann_fix (List.rev_map mk_fix fl) c) fl)
137 | C.CoFix (i, fl) -> C.ACoFix ("", i, List.map (ann_cofix (List.rev_map mk_cofix fl) c) fl)
142 let rec aux k n = function
143 | C.AImplicit (_, None) as t -> t
144 | C.ALambda (id, s, v, t) when k > 0 ->
145 C.ALambda (id, s, v, aux (pred k) n t)
146 | C.ALambda (_, _, _, t) when n > 0 ->
147 aux 0 (pred n) (lift 1 (-1) t)
149 Printf.eprintf "CLEAR: %u %s\n" n (CicPp.ppterm (cic t));
155 let mk_ind context id uri tyno outty arg cases =
158 let is_recursive = function
159 | C.MutInd (u, no, _) -> UM.eq u uri && no = tyno
162 let lpsno, (_, _, _, constructors) = get_ind_type uri tyno in
163 let inty, _ = TC.type_of_aux' [] context (cic arg) Un.empty_ugraph in
164 let ps = match Rd.whd ~delta:true context inty with
166 | C.Appl (C.MutInd _ :: args) -> List.map (fake_annotate context) args
169 let lps, rps = T.list_split lpsno ps in
170 let rpsno = List.length rps in
171 let eliminator = get_default_eliminator context uri tyno inty in
172 let eliminator = fake_annotate context eliminator in
173 let predicate = clear_absts rpsno (1 - sort_disp) outty in
174 let map2 case (_, cty) =
175 let map (h, case, k) premise =
176 if h > 0 then pred h, lift k 1 case, k else
177 if is_recursive premise then 0, lift (succ k) 1 case, succ k else
180 let premises, _ = P.split context cty in
181 let _, lifted_case, _ =
182 List.fold_left map (lpsno, case, 1) (List.rev (List.tl premises))
186 let lifted_cases = List.map2 map2 cases constructors in
187 let args = eliminator :: lps @ predicate :: lifted_cases @ rps @ [arg] in
188 Some (C.AAppl (id, args))
189 with Invalid_argument _ -> failwith "PCn.mk_ind"
191 let hole id = C.AImplicit (id, Some `Hole)
193 let meta id = C.AImplicit (id, None)
195 let anon = C.Anonymous
200 | C.AImplicit (_, None) when b -> b
203 List.fold_left map true
205 let rec gen_fix len k (id, name, i, ty, bo) =
206 id, name, i, gen_term k ty, gen_term (k + len) bo
207 and gen_cofix len k (id, name, ty, bo) =
208 id, name, gen_term k ty, gen_term (k + len) bo
209 and gen_term k = function
211 | C.AImplicit (id, _)
212 | C.AConst (id, _, _)
214 | C.AMutInd (id, _, _, _)
215 | C.AMutConstruct (id, _, _, _, _)
216 | C.AMeta (id, _, _) -> meta id
217 | C.ARel (id, _, m, _) ->
218 if m = succ (k - n) then hole id else meta id
219 | C.AAppl (id, ts) ->
220 let ts = List.map (gen_term k) ts in
221 if is_meta ts then meta id else C.AAppl (id, ts)
222 | C.ACast (id, te, ty) ->
223 let te, ty = gen_term k te, gen_term k ty in
224 if is_meta [te; ty] then meta id else C.ACast (id, te, ty)
225 | C.AMutCase (id, sp, i, outty, t, pl) ->
226 let outty, t, pl = gen_term k outty, gen_term k t, List.map (gen_term k) pl in
227 if is_meta (outty :: t :: pl) then meta id else hole id (* C.AMutCase (id, sp, i, outty, t, pl) *)
228 | C.AProd (id, _, s, t) ->
229 let s, t = gen_term k s, gen_term (succ k) t in
230 if is_meta [s; t] then meta id else C.AProd (id, anon, s, t)
231 | C.ALambda (id, _, s, t) ->
232 let s, t = gen_term k s, gen_term (succ k) t in
233 if is_meta [s; t] then meta id else C.ALambda (id, anon, s, t)
234 | C.ALetIn (id, _, s, t) ->
235 let s, t = gen_term k s, gen_term (succ k) t in
236 if is_meta [s; t] then meta id else C.ALetIn (id, anon, s, t)
237 | C.AFix (id, i, fl) -> C.AFix (id, i, List.map (gen_fix (List.length fl) k) fl)
238 | C.ACoFix (id, i, fl) -> C.ACoFix (id, i, List.map (gen_cofix (List.length fl) k) fl)
242 let mk_pattern rps predicate =
244 let rpsno = List.length rps in
245 let body = generalize (rpsno + sort_disp) predicate in
246 clear_absts 0 (rpsno + sort_disp) body