]> matita.cs.unibo.it Git - helm.git/blob - helm/gTopLevel/cic2acic.ml
Conjectures and Hypotheses inside every conjecture and in the sequents now
[helm.git] / helm / gTopLevel / cic2acic.ml
1 (* Copyright (C) 2000, HELM Team.
2  * 
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.
6  * 
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.
11  * 
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.
16  *
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,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://cs.unibo.it/helm/.
24  *)
25
26 exception NotImplemented;;
27
28 let fresh_id seed ids_to_terms ids_to_father_ids =
29  fun father t ->
30   let res = "i" ^ string_of_int !seed in
31    incr seed ;
32    Hashtbl.add ids_to_father_ids res father ;
33    Hashtbl.add ids_to_terms res t ;
34    res
35 ;;
36
37 exception NotEnoughElements;;
38 exception NameExpected;;
39
40 (*CSC: cut&paste da cicPp.ml *)
41 (* get_nth l n   returns the nth element of the list l if it exists or *)
42 (* raises NotEnoughElements if l has less than n elements             *)
43 let rec get_nth l n =
44  match (n,l) with
45     (1, he::_) -> he
46   | (n, he::tail) when n > 1 -> get_nth tail (n-1)
47   | (_,_) -> raise NotEnoughElements
48 ;;
49
50 let acic_of_cic_context' seed ids_to_terms ids_to_father_ids ids_to_inner_sorts
51      ids_to_inner_types metasenv context t
52 =
53  let module T = CicTypeChecker in
54  let module C = Cic in
55   let fresh_id' = fresh_id seed ids_to_terms ids_to_father_ids in
56    let rec aux computeinnertypes father context tt =
57     let fresh_id'' = fresh_id' father tt in
58     let aux' = aux true (Some fresh_id'') in
59      (* First of all we compute the inner type and the inner sort *)
60      (* of the term. They may be useful in what follows.          *)
61      (*CSC: This is a very inefficient way of computing inner types *)
62      (*CSC: and inner sorts: very deep terms have their types/sorts *)
63      (*CSC: computed again and again.                               *)
64      let string_of_sort =
65       function 
66          C.Sort C.Prop -> "Prop"
67        | C.Sort C.Set  -> "Set"
68        | C.Sort C.Type -> "Type"
69        | _ -> assert false
70      in
71       let ainnertype,innertype,innersort =
72 (*CSC: Here we need the algorithm for Coscoy's double type-inference  *)
73 (*CSC: (expected type + inferred type). Just for now we use the usual *)
74 (*CSC: type-inference, but the result is very poort. As a very weak   *)
75 (*CSC: patch, I apply whd to the computed type. Full beta             *)
76 (*CSC: reduction would be a much better option.                       *)
77         let innertype =
78          CicReduction.whd context (T.type_of_aux' metasenv context tt)
79         in
80          let innersort = T.type_of_aux' metasenv context innertype in
81           let ainnertype =
82            if computeinnertypes then
83             Some (aux false (Some fresh_id'') context innertype)
84            else
85             None
86           in
87            ainnertype, innertype, string_of_sort innersort
88       in
89       let add_inner_type id =
90        match ainnertype with
91           None -> ()
92         | Some ainnertype -> Hashtbl.add ids_to_inner_types id ainnertype
93       in
94        match tt with
95           C.Rel n ->
96            let id =
97             match get_nth context n with
98                (Some (C.Name s,_)) -> s
99              | _ -> raise NameExpected
100            in
101             Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
102             C.ARel (fresh_id'', n, id)
103         | C.Var uri ->
104            Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
105            C.AVar (fresh_id'', uri)
106         | C.Meta (n,l) ->
107            Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
108            C.AMeta (fresh_id'', n,
109             (List.map
110               (function None -> None | Some t -> Some (aux' context t)) l))
111         | C.Sort s -> C.ASort (fresh_id'', s)
112         | C.Implicit -> C.AImplicit (fresh_id'')
113         | C.Cast (v,t) ->
114            Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
115            if innersort = "Prop" then
116             add_inner_type fresh_id'' ;
117            C.ACast (fresh_id'', aux' context v, aux' context t)
118         | C.Prod (n,s,t) ->
119             Hashtbl.add ids_to_inner_sorts fresh_id''
120              (string_of_sort innertype) ;
121             C.AProd
122              (fresh_id'', n, aux' context s,
123               aux' ((Some (n, C.Decl s))::context) t)
124         | C.Lambda (n,s,t) ->
125            Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
126            if innersort = "Prop" then
127             begin
128              let father_is_lambda =
129               match father with
130                  None -> false
131                | Some father' ->
132                   match Hashtbl.find ids_to_terms father' with
133                      C.Lambda _ -> true
134                    | _ -> false
135              in
136               if not father_is_lambda then
137                add_inner_type fresh_id''
138             end ;
139            C.ALambda
140             (fresh_id'',n, aux' context s,
141              aux' ((Some (n, C.Decl s)::context)) t)
142         | C.LetIn (n,s,t) ->
143           Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
144           C.ALetIn
145            (fresh_id'', n, aux' context s,
146             aux' ((Some (n, C.Def s))::context) t)
147         | C.Appl l ->
148            Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
149            if innersort = "Prop" then
150             add_inner_type fresh_id'' ;
151            C.AAppl (fresh_id'', List.map (aux' context) l)
152         | C.Const (uri,cn) ->
153            Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
154            C.AConst (fresh_id'', uri, cn)
155         | C.Abst _ -> raise NotImplemented
156         | C.MutInd (uri,cn,tyno) -> C.AMutInd (fresh_id'', uri, cn, tyno)
157         | C.MutConstruct (uri,cn,tyno,consno) ->
158            Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
159            C.AMutConstruct (fresh_id'', uri, cn, tyno, consno)
160         | C.MutCase (uri, cn, tyno, outty, term, patterns) ->
161            Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
162            if innersort = "Prop" then
163             add_inner_type fresh_id'' ;
164            C.AMutCase (fresh_id'', uri, cn, tyno, aux' context outty,
165             aux' context term, List.map (aux' context) patterns)
166         | C.Fix (funno, funs) ->
167            let tys =
168             List.map (fun (name,_,ty,_) -> Some (C.Name name, C.Decl ty)) funs
169            in
170             Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
171             if innersort = "Prop" then
172              add_inner_type fresh_id'' ;
173             C.AFix (fresh_id'', funno,
174              List.map
175               (fun (name, indidx, ty, bo) ->
176                 (name, indidx, aux' context ty, aux' (tys@context) bo)
177               ) funs
178            )
179         | C.CoFix (funno, funs) ->
180            let tys =
181             List.map (fun (name,ty,_) -> Some (C.Name name, C.Decl ty)) funs in
182             Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
183             if innersort = "Prop" then
184              add_inner_type fresh_id'' ;
185             C.ACoFix (fresh_id'', funno,
186              List.map
187               (fun (name, ty, bo) ->
188                 (name, aux' context ty, aux' (tys@context) bo)
189               ) funs
190             )
191       in
192        aux true None context t
193 ;;
194
195 let acic_of_cic_context metasenv context t =
196  let ids_to_terms = Hashtbl.create 503 in
197  let ids_to_father_ids = Hashtbl.create 503 in
198  let ids_to_inner_sorts = Hashtbl.create 503 in
199  let ids_to_inner_types = Hashtbl.create 503 in
200  let seed = ref 0 in
201    acic_of_cic_context' seed ids_to_terms ids_to_father_ids ids_to_inner_sorts
202     ids_to_inner_types metasenv context t,
203    ids_to_terms, ids_to_father_ids, ids_to_inner_sorts, ids_to_inner_types
204 ;;
205
206 exception Found of (Cic.name * Cic.context_entry) list;;
207
208 let acic_object_of_cic_object obj =
209  let module C = Cic in
210   let ids_to_terms = Hashtbl.create 503 in
211   let ids_to_father_ids = Hashtbl.create 503 in
212   let ids_to_inner_sorts = Hashtbl.create 503 in
213   let ids_to_inner_types = Hashtbl.create 503 in
214   let ids_to_conjectures = Hashtbl.create 11 in
215   let ids_to_hypotheses = Hashtbl.create 127 in
216   let hypotheses_seed = ref 0 in
217   let conjectures_seed = ref 0 in
218   let seed = ref 0 in
219   let acic_term_of_cic_term_context' =
220    acic_of_cic_context' seed ids_to_terms ids_to_father_ids ids_to_inner_sorts
221     ids_to_inner_types in
222   let acic_term_of_cic_term' = acic_term_of_cic_term_context' [] [] in
223    let aobj =
224     match obj with
225       C.Definition (id,bo,ty,params) ->
226        let abo = acic_term_of_cic_term' bo in
227        let aty = acic_term_of_cic_term' ty
228        in
229         C.ADefinition ("mettereaposto",id,abo,aty,(Cic.Actual params))
230     | C.Axiom (id,ty,params) -> raise NotImplemented
231     | C.Variable (id,bo,ty) -> raise NotImplemented
232     | C.CurrentProof (id,conjectures,bo,ty) ->
233        let aconjectures =
234         List.map
235          (function (i,canonical_context,term) as conjecture ->
236            let cid = "c" ^ string_of_int !conjectures_seed in
237             Hashtbl.add ids_to_conjectures cid conjecture ;
238             incr conjectures_seed ;
239             let acanonical_context =
240              let rec aux =
241               function
242                  [] -> []
243                | hyp::tl ->
244                   let hid = "h" ^ string_of_int !hypotheses_seed in
245                    Hashtbl.add ids_to_hypotheses hid hyp ;
246                    incr hypotheses_seed ;
247                    match hyp with
248                       (Some (n,C.Decl t)) ->
249                         let at =
250                          acic_term_of_cic_term_context' conjectures tl t
251                         in
252                          (hid,Some (n,C.ADecl at))::(aux tl)
253                     | (Some (n,C.Def t)) ->
254                         let at =
255                          acic_term_of_cic_term_context' conjectures tl t
256                         in
257                          (hid,Some (n,C.ADef at))::(aux tl)
258                     | None -> (hid,None)::(aux tl)
259              in
260               aux canonical_context
261             in
262              let aterm =
263               acic_term_of_cic_term_context' conjectures canonical_context term
264              in
265               (cid,i,acanonical_context,aterm)
266          ) conjectures in
267        let abo = acic_term_of_cic_term_context' conjectures [] bo in
268        let aty = acic_term_of_cic_term_context' conjectures [] ty in
269         C.ACurrentProof ("mettereaposto",id,aconjectures,abo,aty)
270     | C.InductiveDefinition (tys,params,paramsno) -> raise NotImplemented
271    in
272     aobj,ids_to_terms,ids_to_father_ids,ids_to_inner_sorts,ids_to_inner_types,
273      ids_to_conjectures,ids_to_hypotheses
274 ;;