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/.
28 module S = CicSubstitution
29 module Rd = CicReduction
30 module TC = CicTypeChecker
31 module DTI = DoubleTypeInference
34 (* helper functions *********************************************************)
38 let comp f g x = f (g x)
41 let ty, _ = TC.type_of_aux' [] c t Un.empty_ugraph in ty
44 match Rd.whd ~delta:true c (get_type c (get_type c t)) with
45 | C.Sort C.Prop -> true
48 let is_not_atomic = function
54 | C.MutConstruct _ -> false
58 let add s v c = Some (s, C.Decl v) :: c in
59 let rec aux whd a n c = function
60 | C.Prod (s, v, t) -> aux false (v :: a) (succ n) (add s v c) t
61 | v when whd -> v :: a, n
62 | v -> aux true a n c (Rd.whd ~delta:true c v)
66 let add g htbl t proof decurry =
67 if proof then C.CicHash.add htbl t decurry;
72 let decurry = C.CicHash.find htbl t in g t true decurry
73 with Not_found -> g t false 0
75 (* term preprocessing *******************************************************)
77 let expanded_premise = "EXPANDED"
79 let defined_premise = "DEFINED"
81 let eta_expand tys t =
82 let n = List.length tys in
83 let name i = Printf.sprintf "%s%u" expanded_premise i in
84 let lambda i ty t = C.Lambda (C.Name (name i), ty, t) in
85 let arg i n = C.Rel (n - i) in
86 let rec aux i f a = function
88 | ty :: tl -> aux (succ i) (comp f (lambda i ty)) (arg i n :: a) tl
90 let absts, args = aux 0 identity [] tys in
91 absts (C.Appl (S.lift n t :: args))
93 let get_tys c decurry t =
94 let tys, _ = split c (get_type c t) in
95 let tys, _ = HEL.split_nth decurry (List.tl tys) in
98 let eta_fix c t proof decurry =
99 if proof && decurry > 0 then eta_expand (get_tys c decurry t) t else t
101 let rec pp_cast g ht es c t v =
102 if true then pp_proof g ht es c t else find g ht t
104 and pp_lambda g ht es c name v t =
105 let name = if DTI.does_not_occur 1 t then C.Anonymous else name in
106 let entry = Some (name, C.Decl v) in
108 let t = eta_fix (entry :: c) t true decurry in
109 g (C.Lambda (name, v, t)) true 0 in
110 if true then pp_proof g ht es (entry :: c) t else find g ht t
112 and pp_letin g ht es c name v t =
113 let entry = Some (name, C.Def (v, None)) in
115 if DTI.does_not_occur 1 t then g (S.lift (-1) t) true decurry else
116 let g v proof d = match v with
117 | C.LetIn (mame, w, u) when proof ->
118 let x = C.LetIn (mame, w, C.LetIn (name, u, S.lift_from 2 1 t)) in
119 pp_proof g ht false c x
121 let v = eta_fix c v proof d in
122 (* let t = eta_fix (entry :: c) t true decurry in *)
123 g (C.LetIn (name, v, t)) true decurry
125 if true then pp_term g ht es c v else find g ht v
127 if true then pp_proof g ht es (entry :: c) t else find g ht t
129 and pp_appl_one g ht es c t v =
133 | t, C.LetIn (mame, w, u) when proof ->
134 let x = C.LetIn (mame, w, C.Appl [S.lift 1 t; u]) in
135 pp_proof g ht false c x
136 | C.LetIn (mame, w, u), v ->
137 let x = C.LetIn (mame, w, C.Appl [u; S.lift 1 v]) in
138 pp_proof g ht false c x
139 | C.Appl ts, v when decurry > 0 ->
140 let v = eta_fix c v proof d in
141 g (C.Appl (List.append ts [v])) true (pred decurry)
142 | t, v when is_not_atomic t ->
143 let mame = C.Name defined_premise in
144 let x = C.LetIn (mame, t, C.Appl [C.Rel 1; S.lift 1 v]) in
145 pp_proof g ht false c x
147 let _, premsno = split c (get_type c t) in
148 let v = eta_fix c v proof d in
149 g (C.Appl [t; v]) true (pred premsno)
151 if true then pp_term g ht es c v else find g ht v
153 if true then pp_proof g ht es c t else find g ht t
155 and pp_appl g ht es c t = function
156 | [] -> pp_proof g ht es c t
157 | [v] -> pp_appl_one g ht es c t v
159 let x = C.Appl (C.Appl [t; v1] :: v2 :: vs) in
162 and pp_proof g ht es c t =
163 (* let g t proof decurry = add g ht t proof decurry in *)
165 | C.Cast (t, v) -> pp_cast g ht es c t v
166 | C.Lambda (name, v, t) -> pp_lambda g ht es c name v t
167 | C.LetIn (name, v, t) -> pp_letin g ht es c name v t
168 | C.Appl (t :: vs) -> pp_appl g ht es c t vs
171 and pp_term g ht es c t =
172 if is_proof c t then pp_proof g ht es c t else g t false 0
174 (* object preprocessing *****************************************************)
176 let pp_obj = function
177 | C.Constant (name, Some bo, ty, pars, attrs) ->
178 let g bo proof decurry =
179 let bo = eta_fix [] bo proof decurry in
180 C.Constant (name, Some bo, ty, pars, attrs)
182 let ht = C.CicHash.create 1 in
183 begin try pp_term g ht true [] bo
184 with e -> failwith ("PPP: " ^ Printexc.to_string e) end