2 ||M|| This file is part of HELM, an Hypertextual, Electronic
3 ||A|| Library of Mathematics, developed at the Computer Science
4 ||T|| Department, University of Bologna, Italy.
6 ||T|| HELM is free software; you can redistribute it and/or
7 ||A|| modify it under the terms of the GNU General Public License
8 \ / version 2 or (at your option) any later version.
9 \ / This software is distributed as is, NO WARRANTY.
10 V_______________________________________________________________ *)
12 (* $Id: nCic.ml 9058 2008-10-13 17:42:30Z tassi $ *)
18 "x_" ^ string_of_int !i
22 let id = if id = "_" then fresh_name () else id in
23 CicNotationPt.Ident (id,None)
26 (*CSC: cut&paste from nCicReduction.split_prods, but does not check that
27 the return type is a sort *)
28 let rec my_split_prods ~subst context n te =
29 match (n, NCicReduction.whd ~subst context te) with
30 | (0, _) -> context,te
31 | (n, NCic.Prod (name,so,ta)) ->
32 my_split_prods ~subst ((name,(NCic.Decl so))::context) (n - 1) ta
33 | (n, _) when n <= 0 -> context,te
34 | (_, _) -> raise (Failure "my_split_prods")
41 | CicNotationPt.Appl l1 :: l2 -> CicNotationPt.Appl (l1 @ l2)
42 | l -> CicNotationPt.Appl l
45 let mk_elim uri leftno it (outsort,suffix) pragma =
46 let _,ind_name,ty,cl = it in
47 let srec_name = ind_name ^ "_" ^ suffix in
48 let rec_name = mk_id srec_name in
49 let name_of_k id = mk_id ("H_" ^ id) in
50 let p_name = mk_id "Q_" in
51 let params,ty = NCicReduction.split_prods ~subst:[] [] leftno ty in
52 let params = List.rev_map (function name,_ -> mk_id name) params in
53 let args,sort = NCicReduction.split_prods ~subst:[] [] (-1) ty in
54 let args = List.rev_map (function name,_ -> mk_id name) args in
55 let rec_arg = mk_id (fresh_name ()) in
58 (fun name res -> CicNotationPt.Binder (`Forall,(name,None),res)) args
61 (rec_arg,Some (mk_appl (mk_id ind_name :: params @ args))),
62 CicNotationPt.Sort outsort)) in
63 let args = args @ [rec_arg] in
64 let k_names = List.map (function _,name,_ -> name_of_k name) cl in
66 List.map (function name -> name, None) params @
68 List.map (function name -> name, None) k_names @
69 List.map (function name -> name, None) args in
70 let cty = mk_appl (p_name :: args) in
74 (function (_,name,ty) ->
75 let _,ty = NCicReduction.split_prods ~subst:[] [] leftno ty in
76 let cargs,ty= my_split_prods ~subst:[] [] (-1) ty in
77 let cargs_and_recursive_args =
80 _,NCic.Def _ -> assert false
81 | name,NCic.Decl ty ->
82 let context,ty = my_split_prods ~subst:[] [] (-1) ty in
85 | NCic.Appl (NCic.Const nref::_)
87 let NReference.Ref (uri',_) = nref in
90 let abs = List.rev_map (fun id,_ -> mk_id id) context in
91 let name = mk_id name in
95 CicNotationPt.Binder (`Lambda,(id,None),res))
102 List.map (fun _ -> CicNotationPt.Implicit `JustOne)
104 [mk_appl (name::abs)])))
105 | _ -> mk_id name,None
107 let cargs,recursive_args = List.split cargs_and_recursive_args in
108 let recursive_args = HExtlib.filter_map (fun x -> x) recursive_args in
109 CicNotationPt.Pattern (name,None,List.map (fun x -> x,None) cargs),
110 mk_appl (name_of_k name :: cargs @ recursive_args)
113 let bo = CicNotationPt.Case (rec_arg,Some (ind_name,None),None,branches) in
114 let recno = List.length final_params in
115 let where = recno - 1 in
117 CicNotationPt.LetRec (`Inductive,
118 [final_params, (rec_name,ty), bo, where], rec_name)
122 (BoxPp.render_to_string
123 ~map_unicode_to_tex:false
124 (function x::_ -> x | _ -> assert false)
125 80 (CicNotationPres.render (fun _ -> None)
126 (TermContentPres.pp_ast res)));
127 prerr_endline "#####";
128 let cobj = ("xxx", [], None, `Joint {
129 Content.joint_id = "yyy";
130 joint_kind = `Recursive [recno];
133 Content.def_name = Some srec_name;
139 (fun x t -> CicNotationPt.Binder(`Forall,x,t))
145 let ids_to_nrefs = Hashtbl.create 1 in
146 let boxml = Content2pres.ncontent2pres ~ids_to_nrefs cobj in
148 (BoxPp.render_to_string ~map_unicode_to_tex:false
149 (function x::_ -> x | _ -> assert false) 80
150 (CicNotationPres.mpres_of_box boxml)));
152 CicNotationPt.Theorem
153 (`Definition,srec_name,
154 CicNotationPt.Implicit `JustOne,Some res,pragma)
158 let headrm prefix s =
160 let len_prefix = String.length prefix in
161 assert (String.sub s 0 len_prefix = prefix);
162 String.sub s len_prefix (String.length s - len_prefix)
163 with Invalid_argument _ -> assert false
166 | NCic.Prop -> `Prop,"ind"
167 | NCic.Type [] -> `NType "", "rect_Type"
168 | NCic.Type ((`Type,u) :: _) ->
169 let name = NUri.name_of_uri u in
170 `NType (headrm "Type" name), "rect_" ^ name
171 | NCic.Type ((`CProp,u) :: _) ->
172 let name = NUri.name_of_uri u in
173 `NCProp (headrm "Type" name),
174 "rect_" ^ Str.replace_first (Str.regexp "Type") "CProp" name
178 let mk_elims (uri,_,_,_,obj) =
180 NCic.Inductive (true,leftno,[itl],_) ->
181 List.map (fun s -> mk_elim uri leftno itl (ast_of_sort s) (`Elim s))
183 List.map (fun s -> NCic.Type s) (NCicEnvironment.get_universes ()))
187 (********************* Projections **********************)
193 | l -> CicNotationPt.Appl l
196 let rec count_prods = function NCic.Prod (_,_,t) -> 1 + count_prods t | _ -> 0;;
198 let rec nth_prod projs n ty =
200 NCic.Prod (_,s,_) when n=0 -> projs, s
201 | NCic.Prod (name,_,t) -> nth_prod (name::projs) (n-1) t
205 (* this code should be unified with NTermCicContent.nast_of_cic0,
206 but the two contexts have different types *)
209 NCic.Rel i -> List.nth rels (i - 1)
210 | NCic.Const _ as t ->
212 (NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] t,None)
213 | NCic.Sort s -> CicNotationPt.Sort (fst (ast_of_sort s))
215 | NCic.Implicit _ -> assert false
216 | NCic.Appl l -> CicNotationPt.Appl (List.map (pp rels) l)
217 | NCic.Prod (n,s,t) ->
219 CicNotationPt.Binder (`Pi, (n,Some (pp rels s)), pp (n::rels) t)
220 | NCic.Lambda (n,s,t) ->
222 CicNotationPt.Binder (`Lambda, (n,Some (pp rels s)), pp (n::rels) t)
223 | NCic.LetIn (n,s,ty,t) ->
225 CicNotationPt.LetIn ((n, Some (pp rels ty)), pp rels s, pp (n::rels) t)
226 | NCic.Match (NReference.Ref (uri,_) as r,outty,te,patterns) ->
227 let name = NUri.name_of_uri uri in
228 let case_indty = Some (name, None) in
229 let constructors, leftno =
230 let _,leftno,tys,_,n = NCicEnvironment.get_checked_indtys r in
231 let _,_,_,cl = List.nth tys n in
234 let rec eat_branch n rels ty pat =
236 | NCic.Prod (name, s, t), _ when n > 0 ->
237 eat_branch (pred n) rels t pat
238 | NCic.Prod (_, _, t), NCic.Lambda (name, s, t') ->
239 let cv, rhs = eat_branch 0 ((mk_id name)::rels) t t' in
240 (mk_id name, Some (pp rels s)) :: cv, rhs
241 | _, _ -> [], pp rels pat
246 (fun (_, name, ty) pat ->
247 let capture_variables,rhs = eat_branch leftno rels ty pat in
248 CicNotationPt.Pattern (name, None, capture_variables), rhs
249 ) constructors patterns
250 with Invalid_argument _ -> assert false
252 CicNotationPt.Case (pp rels te, case_indty, Some (pp rels outty), patterns)
255 let mk_projection leftno tyname consname consty (projname,_,_) i =
256 let argsno = count_prods consty - leftno in
257 let rec aux names ty leftno =
260 let arg = mk_id "xxx" in
261 let arg_ty = mk_appl (mk_id tyname :: List.rev names) in
262 let bvar = mk_id "yyy" in
263 let underscore = CicNotationPt.Ident ("_",None),None in
265 HExtlib.mk_list underscore i @ [bvar,None] @
266 HExtlib.mk_list underscore (argsno - i -1) in
267 let branch = CicNotationPt.Pattern (consname,None,bvars), bvar in
268 let projs,outtype = nth_prod [] i ty in
271 (fun name -> mk_appl (mk_id name :: List.rev names @ [arg])) projs
273 let outtype = pp rels outtype in
274 let outtype= CicNotationPt.Binder (`Lambda, (arg, Some arg_ty), outtype) in
275 [arg, Some arg_ty], CicNotationPt.Case (arg,None,Some outtype,[branch])
276 | _,NCic.Prod (name,_,t) ->
277 let name = mk_id name in
278 let params,body = aux (name::names) t (leftno - 1) in
279 (name,None)::params, body
280 | _,_ -> assert false
282 let params,bo = aux [] consty leftno in
283 let pprojname = mk_id projname in
285 CicNotationPt.LetRec (`Inductive,
286 [params, (pprojname,None), bo, leftno], pprojname) in
288 (BoxPp.render_to_string
289 ~map_unicode_to_tex:false
290 (function x::_ -> x | _ -> assert false)
291 80 (CicNotationPres.render (fun _ -> None)
292 (TermContentPres.pp_ast res)));*)
293 CicNotationPt.Theorem
294 (`Definition,projname,CicNotationPt.Implicit `JustOne,Some res,`Projection)
297 let mk_projections (_,_,_,_,obj) =
300 (true,leftno,[_,tyname,_,[_,consname,consty]],(_,`Record fields))
302 HExtlib.list_mapi (mk_projection leftno tyname consname consty) fields