]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_proof_checking/cicRecord.ml
added record generation module
[helm.git] / helm / ocaml / cic_proof_checking / cicRecord.ml
1 (* Copyright (C) 2004-2005, 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://helm.cs.unibo.it/
24  *)
25
26 type record_spec = 
27   string * (string * Cic.term) list * Cic.term * (string * Cic.term) list
28
29 let rec_ty uri leftno  = 
30   let rec_ty = Cic.MutInd (uri,0,[]) in
31   if leftno = 0 then rec_ty else
32     Cic.Appl (rec_ty :: (CicUtil.mk_rels leftno 0))
33
34 let inductive_of_record (suri,params,ty,fields) =
35   let uri = UriManager.uri_of_string suri in
36   let name = UriManager.name_of_uri uri in
37   let leftno = List.length params in
38   let constructor_ty = 
39     List.fold_right (
40       fun (name, ty) acc -> 
41         Cic.Prod (Cic.Name name, ty, acc)) 
42     (params @ fields) (CicSubstitution.lift (List.length fields) 
43     (rec_ty uri leftno))
44   in
45   let ty = 
46     List.fold_right (
47       fun (name,ty) t ->
48         Cic.Prod(Cic.Name name,ty, t)
49     ) params ty
50   in
51   let types = [name,true,ty,["mk_" ^ name,constructor_ty]] in
52   let obj = 
53     Cic.InductiveDefinition (types, [],leftno,[`Class `Record]) 
54   in
55   let ugraph =
56     CicTypeChecker.typecheck_mutual_inductive_defs uri
57       (types, [], leftno) CicUniv.empty_ugraph
58   in
59   types, leftno, obj, ugraph
60  
61 let projections_of (suri,params,ty,fields) =
62   let uri = UriManager.uri_of_string suri in
63   let buri = UriManager.buri_of_uri uri in
64   let name = UriManager.name_of_uri uri in
65   let leftno = List.length params in
66   let ty = 
67     List.fold_right (
68       fun (name,ty) t ->
69         Cic.Prod(Cic.Name name,ty, t)
70     ) params ty
71   in
72   let mk_lambdas l start = 
73     List.fold_right (fun (name,ty) acc -> 
74       Cic.Lambda (Cic.Name name,ty,acc)) l start
75   in
76   let recty = rec_ty uri leftno in
77   let _,projections,_ = 
78     let qualify name = buri ^ "/" ^ name ^ ".con" in
79     let mk_oty toapp typ = 
80       Cic.Lambda (Cic.Name "w", recty, (
81         let l,_ = 
82           List.fold_right (fun (name,_) (acc,i) -> 
83           let u = UriManager.uri_of_string (qualify name) in
84           (Cic.Appl ([Cic.Const (u,[])] @ 
85             CicUtil.mk_rels leftno i @ [Cic.Rel i]))
86           :: acc, i+1
87           ) toapp ([],1)
88         in
89         List.fold_left (
90           fun res t -> CicSubstitution.subst t res
91         ) (CicSubstitution.lift_from (List.length toapp + 1) 1 typ) l))
92     in
93     let toapp = try List.tl (List.rev fields) with Failure _-> [] in
94     List.fold_right (
95       fun (name, typ) (i, acc, toapp) -> 
96         let start = 
97           Cic.Lambda(Cic.Name "x", recty,
98             Cic.MutCase (uri,0,CicSubstitution.lift 1 (mk_oty toapp typ),
99             Cic.Rel 1, 
100               [CicSubstitution.lift 1 
101                  (mk_lambdas fields (Cic.Rel i))]))
102         in
103         i+1, ((qualify name, name, mk_lambdas params start) :: acc) , 
104                   if toapp <> [] then List.tl toapp else []
105       )
106     fields (1, [], toapp)
107   in
108     projections
109     
110     
111
112