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/.
26 module UM = UriManager
31 module E = CicEnvironment
32 module S = CicSubstitution
33 module TC = CicTypeChecker
35 module DTI = DoubleTypeInference
37 module PEH = ProofEngineHelpers
39 (* helper functions *********************************************************)
43 let comp f g x = f (g x)
46 try let ty, _ = TC.type_of_aux' [] c t Un.empty_ugraph in ty
48 Printf.eprintf "TC: context: %s\n" (Pp.ppcontext c);
49 Printf.eprintf "TC: term : %s\n" (Pp.ppterm t);
53 try let t, _, _, _ = Rf.type_of_aux' [] c t Un.empty_ugraph in t
55 Printf.eprintf "REFINE EROR: %s\n" (Printexc.to_string e);
56 Printf.eprintf "Ref: context: %s\n" (Pp.ppcontext c);
57 Printf.eprintf "Ref: term : %s\n" (Pp.ppterm t);
61 match PEH.split_with_whd (c, t) with
62 | (_, hd) :: _, _ -> hd
66 match get_tail c (get_type c (get_type c t)) with
67 | C.Sort C.Prop -> true
71 let is_not_atomic = function
77 | C.MutConstruct _ -> false
81 let rec aux k n = function
82 | C.Lambda (s, v, t) when k > 0 ->
83 C.Lambda (s, v, aux (pred k) n t)
84 | C.Lambda (_, _, t) when n > 0 ->
85 aux 0 (pred n) (S.lift (-1) t)
87 Printf.eprintf "CicPPP clear_absts: %u %s\n" n (Pp.ppterm t);
93 let rec add_abst k = function
94 | C.Lambda (s, v, t) when k > 0 -> C.Lambda (s, v, add_abst (pred k) t)
95 | t when k > 0 -> assert false
96 | t -> C.Lambda (C.Anonymous, C.Implicit None, S.lift 1 t)
98 let get_ind_type uri tyno =
99 match E.get_obj Un.empty_ugraph uri with
100 | C.InductiveDefinition (tys, _, lpsno, _), _ -> lpsno, List.nth tys tyno
103 let get_ind_parameters c t =
104 let ty = get_type c t in
105 let ps = match get_tail c ty with
107 | C.Appl (C.MutInd _ :: args) -> args
110 let disp = match get_tail c (get_type c ty) with
117 let get_default_eliminator context uri tyno ty =
118 let _, (name, _, _, _) = get_ind_type uri tyno in
119 let ext = match get_tail context (get_type context ty) with
120 | C.Sort C.Prop -> "_ind"
121 | C.Sort C.Set -> "_rec"
122 | C.Sort C.CProp -> "_rec"
123 | C.Sort (C.Type _) -> "_rect"
125 Printf.eprintf "CicPPP get_default_eliminator: %s\n" (Pp.ppterm t);
128 let buri = UM.buri_of_uri uri in
129 let uri = UM.uri_of_string (buri ^ "/" ^ name ^ ext ^ ".con") in
132 let add g htbl t proof decurry =
133 if proof then C.CicHash.add htbl t decurry;
138 let decurry = C.CicHash.find htbl t in g t true decurry
139 with Not_found -> g t false 0
141 (* term preprocessing *******************************************************)
143 let expanded_premise = "EXPANDED"
145 let defined_premise = "DEFINED"
147 let eta_expand g tys t =
149 let name i = Printf.sprintf "%s%u" expanded_premise i in
150 let lambda i ty t = C.Lambda (C.Name (name i), ty, t) in
151 let arg i = C.Rel (succ i) in
152 let rec aux i f a = function
154 | (_, ty) :: tl -> aux (succ i) (comp f (lambda i ty)) (arg i :: a) tl
156 let n = List.length tys in
157 let absts, args = aux 0 identity [] tys in
158 let t = match S.lift n t with
159 | C.Appl ts -> C.Appl (ts @ args)
160 | t -> C.Appl (t :: args)
164 let get_tys c decurry =
165 let rec aux n = function
166 (* | C.Appl (hd :: tl) -> aux (n + List.length tl) hd *)
168 let tys, _ = PEH.split_with_whd (c, get_type c t) in
169 let _, tys = HEL.split_nth n (List.rev tys) in
170 let tys, _ = HEL.split_nth decurry tys in
175 let eta_fix c t proof decurry =
176 let rec aux g c = function
177 | C.LetIn (name, v, t) ->
178 let g t = g (C.LetIn (name, v, t)) in
179 let entry = Some (name, C.Def (v, None)) in
181 | t -> eta_expand g (get_tys c decurry t) t
183 if proof && decurry > 0 then aux identity c t else t
185 let rec pp_cast g ht es c t v =
186 if true then pp_proof g ht es c t else find g ht t
188 and pp_lambda g ht es c name v t =
189 let name = if DTI.does_not_occur 1 t then C.Anonymous else name in
190 let entry = Some (name, C.Decl v) in
192 let t = eta_fix (entry :: c) t true decurry in
193 g (C.Lambda (name, v, t)) true 0 in
194 if true then pp_proof g ht es (entry :: c) t else find g ht t
196 and pp_letin g ht es c name v t =
197 let entry = Some (name, C.Def (v, None)) in
199 if DTI.does_not_occur 1 t then g (S.lift (-1) t) true decurry else
200 let g v proof d = match v with
201 | C.LetIn (mame, w, u) when proof ->
202 let x = C.LetIn (mame, w, C.LetIn (name, u, S.lift_from 2 1 t)) in
203 pp_proof g ht false c x
205 let v = eta_fix c v proof d in
206 g (C.LetIn (name, v, t)) true decurry
208 if true then pp_term g ht es c v else find g ht v
210 if true then pp_proof g ht es (entry :: c) t else find g ht t
212 and pp_appl_one g ht es c t v =
216 | t, C.LetIn (mame, w, u) when proof ->
217 let x = C.LetIn (mame, w, C.Appl [S.lift 1 t; u]) in
218 pp_proof g ht false c x
219 | C.LetIn (mame, w, u), v ->
220 let x = C.LetIn (mame, w, C.Appl [u; S.lift 1 v]) in
221 pp_proof g ht false c x
222 | C.Appl ts, v when decurry > 0 ->
223 let v = eta_fix c v proof d in
224 g (C.Appl (List.append ts [v])) true (pred decurry)
225 | t, v when is_not_atomic t ->
226 let mame = C.Name defined_premise in
227 let x = C.LetIn (mame, t, C.Appl [C.Rel 1; S.lift 1 v]) in
228 pp_proof g ht false c x
230 let v = eta_fix c v proof d in
231 g (C.Appl [t; v]) true (pred decurry)
233 if true then pp_term g ht es c v else find g ht v
235 if true then pp_proof g ht es c t else find g ht t
237 and pp_appl g ht es c t = function
238 | [] -> pp_proof g ht es c t
239 | [v] -> pp_appl_one g ht es c t v
241 let x = C.Appl (C.Appl [t; v1] :: v2 :: vs) in
244 and pp_atomic g ht es c t =
245 let _, premsno = PEH.split_with_whd (c, get_type c t) in
248 and pp_mutcase g ht es c uri tyno outty arg cases =
249 let eliminator = get_default_eliminator c uri tyno outty in
250 let lpsno, (_, _, _, constructors) = get_ind_type uri tyno in
251 let ps, sort_disp = get_ind_parameters c arg in
252 let lps, rps = HEL.split_nth lpsno ps in
253 let rpsno = List.length rps in
254 let predicate = clear_absts rpsno (1 - sort_disp) outty in
256 I.S.mem tyno (I.get_mutinds_of_uri uri t)
258 let map2 case (_, cty) =
259 let map (h, case, k) (_, premise) =
260 if h > 0 then pred h, case, k else
261 if is_recursive premise then
262 0, add_abst k case, k + 2
266 let premises, _ = PEH.split_with_whd (c, cty) in
267 let _, lifted_case, _ =
268 List.fold_left map (lpsno, case, 1) (List.rev (List.tl premises))
272 let lifted_cases = List.map2 map2 cases constructors in
273 let args = eliminator :: lps @ predicate :: lifted_cases @ rps @ [arg] in
274 let x = refine c (C.Appl args) in
277 and pp_proof g ht es c t =
278 (* Printf.eprintf "IN: |- %s\n" (*CicPp.ppcontext c*) (CicPp.ppterm t);
279 let g t proof decurry =
280 Printf.eprintf "OUT: %b %u |- %s\n" proof decurry (CicPp.ppterm t);
283 (* let g t proof decurry = add g ht t proof decurry in *)
285 | C.Cast (t, v) -> pp_cast g ht es c t v
286 | C.Lambda (name, v, t) -> pp_lambda g ht es c name v t
287 | C.LetIn (name, v, t) -> pp_letin g ht es c name v t
288 | C.Appl (t :: vs) -> pp_appl g ht es c t vs
289 | C.MutCase (u, n, t, v, ws) -> pp_mutcase g ht es c u n t v ws
290 | t -> pp_atomic g ht es c t
292 and pp_term g ht es c t =
293 if is_proof c t then pp_proof g ht es c t else g t false 0
295 (* object preprocessing *****************************************************)
297 let pp_obj = function
298 | C.Constant (name, Some bo, ty, pars, attrs) ->
299 let g bo proof decurry =
300 let bo = eta_fix [] bo proof decurry in
301 C.Constant (name, Some bo, ty, pars, attrs)
303 let ht = C.CicHash.create 1 in
304 Printf.eprintf "BEGIN: %s\n" name;
305 begin try pp_term g ht true [] bo
306 with e -> failwith ("PPP: " ^ Printexc.to_string e) end