]> matita.cs.unibo.it Git - helm.git/blob - components/cic/deannotate.ml
tagged 0.5.0-rc1
[helm.git] / components / cic / deannotate.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 (* $Id$ *)
27
28 (* converts annotated terms into cic terms (forgetting ids and names) *)
29 let rec deannotate_term =
30  let module C = Cic in
31   function
32      C.ARel (_,_,n,_) -> C.Rel n
33    | C.AVar (_,uri,exp_named_subst) ->
34       let deann_exp_named_subst =
35        List.map (function (uri,t) -> uri,deannotate_term t) exp_named_subst
36       in
37        C.Var (uri, deann_exp_named_subst)
38    | C.AMeta (_,n, l) ->
39       let l' =
40        List.map
41         (function
42             None -> None
43           | Some at -> Some (deannotate_term at)
44         ) l
45       in
46        C.Meta (n, l')
47    | C.ASort (_,s) -> C.Sort s
48    | C.AImplicit (_, annotation) -> C.Implicit annotation
49    | C.ACast (_,va,ty) -> C.Cast (deannotate_term va, deannotate_term ty)
50    | C.AProd (_,name,so,ta) ->
51       C.Prod (name, deannotate_term so, deannotate_term ta)
52    | C.ALambda (_,name,so,ta) ->
53       C.Lambda (name, deannotate_term so, deannotate_term ta)
54    | C.ALetIn (_,name,so,ty,ta) ->
55       C.LetIn (name, deannotate_term so, deannotate_term ty, deannotate_term ta)
56    | C.AAppl (_,l) -> C.Appl (List.map deannotate_term l)
57    | C.AConst (_,uri,exp_named_subst) ->
58       let deann_exp_named_subst =
59        List.map (function (uri,t) -> uri,deannotate_term t) exp_named_subst
60       in
61        C.Const (uri, deann_exp_named_subst)
62    | C.AMutInd (_,uri,i,exp_named_subst) ->
63       let deann_exp_named_subst =
64        List.map (function (uri,t) -> uri,deannotate_term t) exp_named_subst
65       in
66        C.MutInd (uri,i,deann_exp_named_subst)
67    | C.AMutConstruct (_,uri,i,j,exp_named_subst) ->
68       let deann_exp_named_subst =
69        List.map (function (uri,t) -> uri,deannotate_term t) exp_named_subst
70       in
71        C.MutConstruct (uri,i,j,deann_exp_named_subst)
72    | C.AMutCase (_,uri,i,outtype,te,pl) ->
73       C.MutCase (uri,i,deannotate_term outtype,
74        deannotate_term te, List.map deannotate_term pl)
75    | C.AFix (_,funno,ifl) ->
76       C.Fix (funno, List.map deannotate_inductiveFun ifl)
77    | C.ACoFix (_,funno,ifl) ->
78       C.CoFix (funno, List.map deannotate_coinductiveFun ifl)
79
80 and deannotate_inductiveFun (_,name,index,ty,bo) =
81  (name, index, deannotate_term ty, deannotate_term bo)
82
83 and deannotate_coinductiveFun (_,name,ty,bo) =
84  (name, deannotate_term ty, deannotate_term bo)
85 ;;
86
87 let deannotate_inductiveType (_, name, isinductive, arity, cons) =
88  (name, isinductive, deannotate_term arity,
89   List.map (fun (id,ty) -> (id,deannotate_term ty)) cons)
90 ;;
91
92 let deannotate_conjectures =
93  let module C = Cic in
94   List.map
95    (function 
96      (_,id,acontext,con) -> 
97       let context = 
98        List.map 
99         (function 
100           | _,Some (n,(C.ADef (ate,aty))) ->
101              Some(n,(C.Def(deannotate_term ate,deannotate_term aty)))
102           | _,Some (n,(C.ADecl at)) -> Some (n,(C.Decl (deannotate_term at)))
103           | _,None -> None)
104         acontext  
105       in
106        (id,context,deannotate_term con))
107 ;;
108
109 let type_of_aux' = ref (fun _ _ -> assert false);;
110 let lift  = ref (fun _ _ -> assert false);;
111
112 let rec compute_letin_type context te =
113  let module C = Cic in
114    match te with
115       C.Rel _
116     | C.Sort _ -> te
117     | C.Implicit _ -> assert false
118     | C.Meta (n,l) ->
119        C.Meta (n,
120         List.map
121         (fun x ->
122           match x with
123              None -> None
124            | Some x -> Some (compute_letin_type context x)) l)
125     | C.Cast (te,ty) ->
126        C.Cast
127         (compute_letin_type context te,
128          compute_letin_type context ty)
129     | C.Prod (name,so,dest) ->
130        let so = compute_letin_type context so in
131        C.Prod (name, so,
132         compute_letin_type ((Some (name,(C.Decl so)))::context) dest)
133     | C.Lambda (name,so,dest) ->
134        let so = compute_letin_type context so in
135        C.Lambda (name, so,
136         compute_letin_type ((Some (name,(C.Decl so)))::context) dest)
137     | C.LetIn (name,so,C.Implicit _,dest) ->
138        let so = compute_letin_type context so in
139        let ty = Unshare.unshare ~fresh_univs:true (!type_of_aux' context so) in
140         C.LetIn (name, so, ty,
141          compute_letin_type ((Some (name,(C.Def (so,ty))))::context) dest)
142     | C.LetIn (name,so,ty,dest) ->
143        let so = compute_letin_type context so in
144        let ty = compute_letin_type context ty in
145        C.LetIn (name, so, ty,
146         compute_letin_type ((Some (name,(C.Def (so,ty))))::context) dest)
147     | C.Appl l ->
148        C.Appl (List.map (fun x -> compute_letin_type context x) l)
149     | C.Var (uri,exp_named_subst) -> 
150        C.Var (uri,
151         List.map (fun (u,x) -> u,compute_letin_type context x) exp_named_subst)
152     | C.Const (uri,exp_named_subst) ->
153        C.Const (uri,
154         List.map (fun (u,x) -> u,compute_letin_type context x) exp_named_subst)
155     | C.MutInd (uri,i,exp_named_subst) ->
156        C.MutInd (uri,i,
157         List.map (fun (u,x) -> u,compute_letin_type context x) exp_named_subst)
158     | C.MutConstruct (uri,i,j,exp_named_subst) ->
159        C.MutConstruct (uri,i,j,
160         List.map (fun (u,x) -> u,compute_letin_type context x) exp_named_subst)
161     | C.MutCase (uri,i,out,te,pl) ->
162        C.MutCase (uri,i,
163         compute_letin_type context out,
164         compute_letin_type context te,
165         List.map (fun x -> compute_letin_type context x) pl)
166     | C.Fix (fno,fl) ->
167         let fl =
168          List.map
169           (function (name,recno,ty,bo) ->
170             name,recno,compute_letin_type context ty, bo) fl in
171         let tys,_ =
172          List.fold_left
173           (fun (types,len) (n,_,ty,_) ->
174              (Some (C.Name n,(C.Decl (!lift len ty)))::types,
175               len+1)
176           ) ([],0) fl
177         in
178          C.Fix (fno,
179           List.map
180            (fun (name,recno,ty,bo) ->
181              name, recno, ty, compute_letin_type (tys @ context) bo
182            ) fl)
183     | C.CoFix (fno,fl) ->
184         let fl =
185          List.map
186           (function (name,ty,bo) ->
187             name, compute_letin_type context ty, bo) fl in
188         let tys,_ =
189          List.fold_left
190           (fun (types,len) (n,ty,_) ->
191              (Some (C.Name n,(C.Decl (!lift len ty)))::types,
192               len+1)
193           ) ([],0) fl
194         in
195          C.CoFix (fno,
196           List.map
197            (fun (name,ty,bo) ->
198              name, ty, compute_letin_type (tys @ context) bo
199            ) fl)
200 ;;
201
202 let deannotate_obj =
203  let deannotate_term t =
204    compute_letin_type [] (deannotate_term t)
205  in
206  let module C = Cic in
207   function
208      C.AConstant (_, _, id, bo, ty, params, attrs) ->
209       C.Constant (id,
210        (match bo with None -> None | Some bo -> Some (deannotate_term bo)),
211        deannotate_term ty, params, attrs)
212    | C.AVariable (_, name, bo, ty, params, attrs) ->
213       C.Variable (name,
214        (match bo with None -> None | Some bo -> Some (deannotate_term bo)),
215        deannotate_term ty, params, attrs)
216    | C.ACurrentProof (_, _, name, conjs, bo, ty, params, attrs) ->
217       C.CurrentProof (
218        name,
219        deannotate_conjectures conjs,
220        deannotate_term bo,deannotate_term ty, params, attrs
221       )
222    | C.AInductiveDefinition (_, tys, params, parno, attrs) ->
223       C.InductiveDefinition (List.map deannotate_inductiveType tys,
224        params, parno, attrs)
225 ;;