]> matita.cs.unibo.it Git - helm.git/blob - helm/gTopLevel/cic2acic.ml
First commit of our future proof-assistant/proof-improver (???)
[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 ids_to_terms ids_to_father_ids =
29  let id = ref 0 in
30   fun father t ->
31    let res = "i" ^ string_of_int !id in
32     incr id ;
33     Hashtbl.add ids_to_father_ids res father ;
34     Hashtbl.add ids_to_terms res t ;
35     res
36 ;;
37
38 exception NotEnoughElements;;
39 exception NameExpected;;
40
41 (*CSC: cut&paste da cicPp.ml *)
42 (* get_nth l n   returns the nth element of the list l if it exists or *)
43 (* raises NotEnoughElements if l has less than n elements             *)
44 let rec get_nth l n =
45  match (n,l) with
46     (1, he::_) -> he
47   | (n, he::tail) when n > 1 -> get_nth tail (n-1)
48   | (_,_) -> raise NotEnoughElements
49 ;;
50
51 let acic_of_cic_env env t =
52  let module C = Cic in
53   let ids_to_terms = Hashtbl.create 503 in
54   let ids_to_father_ids = Hashtbl.create 503 in
55   let fresh_id' = fresh_id ids_to_terms ids_to_father_ids in
56    let rec aux father bs tt =
57     let fresh_id'' = fresh_id' father tt in
58     let aux' = aux (Some fresh_id'') in
59      match tt with
60         C.Rel n ->
61          let id =
62           match get_nth bs n with
63              C.Name s -> s
64            | _ -> raise NameExpected
65          in
66           C.ARel (fresh_id'', n, id)
67       | C.Var uri -> C.AVar (fresh_id'', uri)
68       | C.Meta n -> C.AMeta (fresh_id'', n)
69       | C.Sort s -> C.ASort (fresh_id'', s)
70       | C.Implicit -> C.AImplicit (fresh_id'')
71       | C.Cast (v,t) ->
72          C.ACast (fresh_id'', aux' bs v, aux' bs t)
73       | C.Prod (n,s,t) ->
74          C.AProd (fresh_id'', n, aux' bs s, aux' (n::bs) t)
75       | C.Lambda (n,s,t) ->
76          C.ALambda (fresh_id'',n, aux' bs s, aux' (n::bs) t)
77       | C.LetIn (n,s,t) ->
78          C.ALetIn (fresh_id'', n, aux' bs s, aux' (n::bs) t)
79       | C.Appl l -> C.AAppl (fresh_id'', List.map (aux' bs) l)
80       | C.Const (uri,cn) -> C.AConst (fresh_id'', uri, cn)
81       | C.Abst _ -> raise NotImplemented
82       | C.MutInd (uri,cn,tyno) -> C.AMutInd (fresh_id'', uri, cn, tyno)
83       | C.MutConstruct (uri,cn,tyno,consno) ->
84          C.AMutConstruct (fresh_id'', uri, cn, tyno, consno)
85       | C.MutCase (uri, cn, tyno, outty, term, patterns) ->
86          C.AMutCase (fresh_id'', uri, cn, tyno, aux' bs outty,
87           aux' bs term, List.map (aux' bs) patterns)
88       | C.Fix (funno, funs) ->
89          let names = List.map (fun (name,_,_,_) -> C.Name name) funs in
90           C.AFix (fresh_id'', funno,
91            List.map
92             (fun (name, indidx, ty, bo) ->
93               (name, indidx, aux' bs ty, aux' (names@bs) bo)
94             ) funs
95          )
96       | C.CoFix (funno, funs) ->
97          let names = List.map (fun (name,_,_) -> C.Name name) funs in
98           C.ACoFix (fresh_id'', funno,
99            List.map
100             (fun (name, ty, bo) ->
101               (name, aux' bs ty, aux' (names@bs) bo)
102             ) funs
103           )
104     in
105      aux None env t, ids_to_terms, ids_to_father_ids
106 ;;
107
108 let acic_of_cic = acic_of_cic_env [];;