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
33 module PEH = ProofEngineHelpers
34 module PT = PrimitiveTactics
36 module DTI = DoubleTypeInference
38 (* helpers ******************************************************************)
40 let cic = D.deannotate_term
42 let rec list_sub start length = function
43 | _ :: tl when start > 0 -> list_sub (pred start) length tl
44 | hd :: tl when length > 0 -> hd :: list_sub start (pred length) tl
47 (* proof construction *******************************************************)
50 let rec lift_xns k (uri, t) = uri, lift_term k t
51 and lift_ms k = function
53 | Some t -> Some (lift_term k t)
54 and lift_fix len k (id, name, i, ty, bo) =
55 id, name, i, lift_term k ty, lift_term (k + len) bo
56 and lift_cofix len k (id, name, ty, bo) =
57 id, name, lift_term k ty, lift_term (k + len) bo
58 and lift_term k = function
60 | C.AImplicit _ as t -> t
61 | C.ARel (id, rid, m, b) as t ->
63 if m + n > 0 then C.ARel (id, rid, m + n, b) else
65 | C.AConst (id, uri, xnss) -> C.AConst (id, uri, List.map (lift_xns k) xnss)
66 | C.AVar (id, uri, xnss) -> C.AVar (id, uri, List.map (lift_xns k) xnss)
67 | C.AMutInd (id, uri, tyno, xnss) -> C.AMutInd (id, uri, tyno, List.map (lift_xns k) xnss)
68 | C.AMutConstruct (id, uri, tyno, consno, xnss) -> C.AMutConstruct (id, uri,tyno,consno, List.map (lift_xns k) xnss)
69 | C.AMeta (id, i, mss) -> C.AMeta(id, i, List.map (lift_ms k) mss)
70 | C.AAppl (id, ts) -> C.AAppl (id, List.map (lift_term k) ts)
71 | C.ACast (id, te, ty) -> C.ACast (id, lift_term k te, lift_term k ty)
72 | 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)
73 | C.AProd (id, n, s, t) -> C.AProd (id, n, lift_term k s, lift_term (succ k) t)
74 | C.ALambda (id, n, s, t) -> C.ALambda (id, n, lift_term k s, lift_term (succ k) t)
75 | C.ALetIn (id, n, ty, s, t) -> C.ALetIn (id, n, lift_term k s, lift_term k ty, lift_term (succ k) t)
76 | C.AFix (id, i, fl) -> C.AFix (id, i, List.map (lift_fix (List.length fl) k) fl)
77 | C.ACoFix (id, i, fl) -> C.ACoFix (id, i, List.map (lift_cofix (List.length fl) k) fl)
81 let fake_annotate id c =
83 try match List.nth c (pred m) with
84 | Some (C.Name s, _) -> s
87 | Invalid_argument _ -> assert false
89 let mk_decl n v = Some (n, C.Decl v) in
90 let mk_def n v ty = Some (n, C.Def (v, ty)) in
91 let mk_fix (name, _, ty, bo) = mk_def (C.Name name) bo ty in
92 let mk_cofix (name, ty, bo) = mk_def (C.Name name) bo ty in
93 let rec ann_xns c (uri, t) = uri, ann_term c t
94 and ann_ms c = function
96 | Some t -> Some (ann_term c t)
97 and ann_fix newc c (name, i, ty, bo) =
98 id, name, i, ann_term c ty, ann_term (List.rev_append newc c) bo
99 and ann_cofix newc c (name, ty, bo) =
100 id, name, ann_term c ty, ann_term (List.rev_append newc c) bo
101 and ann_term c = function
102 | C.Sort sort -> C.ASort (id, sort)
103 | C.Implicit ann -> C.AImplicit (id, ann)
104 | C.Rel m -> C.ARel (id, id, m, get_binder c m)
105 | C.Const (uri, xnss) -> C.AConst (id, uri, List.map (ann_xns c) xnss)
106 | C.Var (uri, xnss) -> C.AVar (id, uri, List.map (ann_xns c) xnss)
107 | C.MutInd (uri, tyno, xnss) -> C.AMutInd (id, uri, tyno, List.map (ann_xns c) xnss)
108 | C.MutConstruct (uri, tyno, consno, xnss) -> C.AMutConstruct (id, uri,tyno,consno, List.map (ann_xns c) xnss)
109 | C.Meta (i, mss) -> C.AMeta(id, i, List.map (ann_ms c) mss)
110 | C.Appl ts -> C.AAppl (id, List.map (ann_term c) ts)
111 | C.Cast (te, ty) -> C.ACast (id, ann_term c te, ann_term c ty)
112 | C.MutCase (sp, i, outty, t, pl) -> C.AMutCase (id, sp, i, ann_term c outty, ann_term c t, List.map (ann_term c) pl)
113 | C.Prod (n, s, t) -> C.AProd (id, n, ann_term c s, ann_term (mk_decl n s :: c) t)
114 | C.Lambda (n, s, t) -> C.ALambda (id, n, ann_term c s, ann_term (mk_decl n s :: c) t)
115 | C.LetIn (n, s, ty, t) -> C.ALetIn (id, n, ann_term c s, ann_term c ty, ann_term (mk_def n s ty :: c) t)
116 | C.Fix (i, fl) -> C.AFix (id, i, List.map (ann_fix (List.rev_map mk_fix fl) c) fl)
117 | C.CoFix (i, fl) -> C.ACoFix (id, i, List.map (ann_cofix (List.rev_map mk_cofix fl) c) fl)
122 let rec aux k n = function
123 | C.AImplicit (_, None) as t -> t
124 | C.ALambda (id, s, v, t) when k > 0 ->
125 C.ALambda (id, s, v, aux (pred k) n t)
126 | C.ALambda (_, _, _, t) when n > 0 ->
127 aux 0 (pred n) (lift 1 (-1) t)
129 Printf.eprintf "CLEAR: %u %s\n" n (CicPp.ppterm (cic t));
135 let hole id = C.AImplicit (id, Some `Hole)
137 let meta id = C.AImplicit (id, None)
139 let anon = C.Anonymous
144 | C.AImplicit (_, None) when b -> b
147 List.fold_left map true
149 let rec gen_fix len k (id, name, i, ty, bo) =
150 id, name, i, gen_term k ty, gen_term (k + len) bo
151 and gen_cofix len k (id, name, ty, bo) =
152 id, name, gen_term k ty, gen_term (k + len) bo
153 and gen_term k = function
155 | C.AImplicit (id, _)
156 | C.AConst (id, _, _)
158 | C.AMutInd (id, _, _, _)
159 | C.AMutConstruct (id, _, _, _, _)
160 | C.AMeta (id, _, _) -> meta id
161 | C.ARel (id, _, m, _) ->
162 if succ (k - n) <= m && m <= k then hole id else meta id
163 | C.AAppl (id, ts) ->
164 let ts = List.map (gen_term k) ts in
165 if is_meta ts then meta id else C.AAppl (id, ts)
166 | C.ACast (id, te, ty) ->
167 let te, ty = gen_term k te, gen_term k ty in
168 if is_meta [te; ty] then meta id else C.ACast (id, te, ty)
169 | C.AMutCase (id, sp, i, outty, t, pl) ->
170 let outty, t, pl = gen_term k outty, gen_term k t, List.map (gen_term k) pl in
171 if is_meta (outty :: t :: pl) then meta id else hole id (* C.AMutCase (id, sp, i, outty, t, pl) *)
172 | C.AProd (id, _, s, t) ->
173 let s, t = gen_term k s, gen_term (succ k) t in
174 if is_meta [s; t] then meta id else C.AProd (id, anon, s, t)
175 | C.ALambda (id, _, s, t) ->
176 let s, t = gen_term k s, gen_term (succ k) t in
177 if is_meta [s; t] then meta id else C.ALambda (id, anon, s, t)
178 | C.ALetIn (id, _, s, ty, t) ->
179 let s, ty, t = gen_term k s, gen_term k ty, gen_term (succ k) t in
180 if is_meta [s; t] then meta id else C.ALetIn (id, anon, s, ty, t)
181 | C.AFix (id, i, fl) -> C.AFix (id, i, List.map (gen_fix (List.length fl) k) fl)
182 | C.ACoFix (id, i, fl) -> C.ACoFix (id, i, List.map (gen_cofix (List.length fl) k) fl)
186 let mk_pattern psno predicate =
187 let body = generalize psno predicate in
188 clear_absts 0 psno body
190 let get_clears c p xtypes =
191 let meta = C.Implicit None in
192 let rec aux c names p it et = function
194 List.rev c, List.rev names
195 | Some (C.Name name as n, C.Decl v) as hd :: tl ->
197 if DTI.does_not_occur 1 p && DTI.does_not_occur 1 it && DTI.does_not_occur 1 et then
198 Some (C.Anonymous, C.Decl v), name :: names, meta
202 let p = C.Lambda (n, v, p) in
203 let it = C.Prod (n, v, it) in
204 let et = C.Prod (n, v, et) in
205 aux (hd :: c) names p it et tl
206 | Some (C.Name name as n, C.Def (v, x)) as hd :: tl ->
208 if DTI.does_not_occur 1 p && DTI.does_not_occur 1 it && DTI.does_not_occur 1 et then
209 Some (C.Anonymous, C.Def (v, x)), name :: names, meta
213 let p = C.LetIn (n, v, x, p) in
214 let it = C.LetIn (n, v, x, it) in
215 let et = C.LetIn (n, v, x, et) in
216 aux (hd :: c) names p it et tl
217 | Some (C.Anonymous as n, C.Decl v) as hd :: tl ->
218 let p = C.Lambda (n, meta, p) in
219 let it = C.Lambda (n, meta, it) in
220 let et = C.Lambda (n, meta, et) in
221 aux (hd :: c) names p it et tl
222 | Some (C.Anonymous as n, C.Def (v, _)) as hd :: tl ->
223 let p = C.LetIn (n, meta, meta, p) in
224 let it = C.LetIn (n, meta, meta, it) in
225 let et = C.LetIn (n, meta, meta, et) in
226 aux (hd :: c) names p it et tl
227 | None :: tl -> assert false
230 | Some (it, et) -> aux [] [] p it et c
234 let rec aux c = function
236 | Some (C.Name name, entry) :: tail when name = hyp ->
237 aux (Some (C.Anonymous, entry) :: c) tail
238 | entry :: tail -> aux (entry :: c) tail
242 let elim_inferred_type context goal arg using cpattern =
243 let metasenv, ugraph = [], Un.oblivion_ugraph in
244 let ety, _ugraph = TC.type_of_aux' metasenv context using ugraph in
245 let _splits, args_no = PEH.split_with_whd (context, ety) in
246 let _metasenv, predicate, _arg, actual_args = PT.mk_predicate_for_elim
247 ~context ~metasenv ~ugraph ~goal ~arg ~using ~cpattern ~args_no
249 let ty = C.Appl (predicate :: actual_args) in
250 let upto = List.length actual_args in
251 Rd.head_beta_reduce ~delta:false ~upto ty