]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/library/cicRecord.ml
Preparing for 0.5.9 release.
[helm.git] / helm / software / components / library / 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 (* $Id$ *)
27
28 let rec_ty uri leftno  = 
29   let rec_ty = Cic.MutInd (uri,0,[]) in
30   if leftno = 0 then rec_ty else
31     Cic.Appl (rec_ty :: (CicUtil.mk_rels leftno 0))
32
33 let generate_one_proj uri params paramsno fields t i =
34  let mk_lambdas l start = 
35   List.fold_right (fun (name,ty) acc -> 
36     Cic.Lambda (Cic.Name name,ty,acc)) l start in
37  let recty = rec_ty uri paramsno in
38  let outtype = Cic.Lambda (Cic.Name "w'", CicSubstitution.lift 1 recty, t) in
39    (mk_lambdas params
40      (Cic.Lambda (Cic.Name "w", recty,
41        Cic.MutCase (uri,0,outtype, Cic.Rel 1, 
42         [mk_lambdas fields (Cic.Rel i)]))))
43
44 let projections_of uri field_names =
45  let buri = UriManager.buri_of_uri uri in
46  let obj,ugraph = CicEnvironment.get_cooked_obj CicUniv.oblivion_ugraph uri in
47   match obj with
48      Cic.InductiveDefinition ([_,_,sort,[_,ty]],params,paramsno,_) ->
49       assert (params = []); (* general case not implemented *)
50       let leftparams,ty =
51        let rec aux =
52         function
53            0,ty -> [],ty
54          | n,Cic.Prod (Cic.Name name,s,t) ->
55             let leftparams,ty = aux (n - 1,t) in
56              (name,s)::leftparams,ty
57          | _,_ -> assert false
58        in
59         aux (paramsno,ty)
60       in
61       let fields =
62        let rec aux =
63         function
64            Cic.MutInd _, []
65          | Cic.Appl _,   [] -> []
66          | Cic.Prod (_,s,t), name::tl -> (name,s)::aux (t,tl)
67          | _,_ -> assert false
68        in
69         aux ((CicSubstitution.lift 1 ty),field_names)
70       in
71        let rec aux i =
72         function
73            Cic.MutInd _, []
74          | Cic.Appl _,   [] -> []
75          | Cic.Prod (_,s,t), name::tl ->
76             let p = generate_one_proj uri leftparams paramsno fields s i in
77             let puri = UriManager.uri_of_string (buri ^ "/" ^ name ^ ".con") in
78              (puri,name,p) ::
79                aux (i - 1)
80                 (CicSubstitution.subst
81                   (Cic.Appl
82                     (Cic.Const (puri,[]) ::
83                       CicUtil.mk_rels paramsno 2 @ [Cic.Rel 1])
84                   ) t, tl)
85          | _,_ -> assert false
86        in
87         aux (List.length fields) (CicSubstitution.lift 2 ty,field_names)
88    | _ -> assert false
89 ;;
90
91 let generate_projections ~add_obj ~add_coercion (uri as orig_uri) obj =
92  match obj with
93   | Cic.InductiveDefinition (inductivefuns,_,_,attrs) ->
94      let rec get_record_attrs =
95        function
96        | [] -> None
97        | (`Class (`Record fields))::_ -> Some fields
98        | _::tl -> get_record_attrs tl
99      in
100       (match get_record_attrs attrs with
101       | None -> []
102       | Some fields ->
103          let uris = ref [] in
104          let projections = 
105            projections_of uri (List.map (fun (x,_,_) -> x) fields) 
106          in
107           List.iter2 
108             (fun (uri, name, bo) (_name, coercion, arity) ->
109              try
110               let ty, _ =
111                 CicTypeChecker.type_of_aux' [] [] bo CicUniv.oblivion_ugraph in
112               let attrs = [`Class `Projection; `Generated] in
113               let obj = Cic.Constant (name,Some bo,ty,[],attrs) in
114               let lemmas = add_obj uri obj in
115               let lemmas1 = 
116                 if not coercion then [] else 
117                  add_coercion uri arity 0 (UriManager.buri_of_uri orig_uri)
118               in
119                uris := lemmas1 @ lemmas @ uri::!uris
120              with
121                 CicTypeChecker.TypeCheckerFailure s ->
122                  HLog.message ("Unable to create projection " ^ name ^
123                   " cause: " ^ Lazy.force s);
124               | CicEnvironment.Object_not_found uri ->
125                  let depend = UriManager.name_of_uri uri in
126                   HLog.message ("Unable to create projection " ^ name ^
127                    " because it requires " ^ depend)
128             ) projections fields;
129           !uris)
130   | _ -> []
131 ;;
132
133
134 let init () = 
135   LibrarySync.add_object_declaration_hook generate_projections;;