1 (* Copyright (C) 2000, HELM Team.
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.
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.
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.
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,
22 * For details, see the HELM World-Wide-Web page,
23 * http://cs.unibo.it/helm/.
26 (* $Id: cicPp.ml 7413 2007-05-29 15:30:53Z tassi $ *)
29 let apply_subst subst t = assert (subst=[]); t;;
31 type typformerreference = NReference.reference
32 type reference = NReference.reference
36 | KArrow of kind * kind
37 | KSkip of kind (* dropped abstraction *)
42 | TConst of typformerreference
45 | Forall of string * kind * typ
51 | Lambda of string * (* typ **) term
53 | LetIn of string * (* typ **) term * term
54 | Match of reference * term * term list
55 | TLambda of (* string **) term
56 | Inst of (*typ_former **) term
59 NCic.Const (NReference.reference_of_spec (NUri.uri_of_string "cic:/matita/basics/types/unit.ind") (NReference.Ind (true,0,0)));;
61 (* None = dropped abstraction *)
62 type typ_context = (string * kind) option list
63 type term_context = (string * [`OfKind of kind | `OfType of typ]) option list
65 type typ_former_decl = typ_context * kind
66 type typ_former_def = typ_former_decl * typ
68 type term_former_decl = term_context * typ
69 type term_former_def = term_former_decl * term
72 TypeDeclaration of typ_former_decl
73 | TypeDefinition of typ_former_def
74 | TermDeclaration of term_former_decl
75 | TermDefinition of term_former_def
76 | LetRec of (string * typ * term) list
77 (* inductive and records missing *)
79 type obj = NUri.uri * obj_kind
83 let rec classify_not_term status no_dep_prods context t =
84 match NCicReduction.whd status ~subst:[] context t with
88 | NCic.Type [`CProp,_] -> `PropKind
89 | NCic.Type [`Type,_] ->
90 if no_dep_prods then `Kind
92 raise NotInFOmega (* ?? *)
93 | NCic.Type _ -> assert false)
94 | NCic.Prod (b,s,t) ->
95 (*CSC: using invariant on "_" *)
96 classify_not_term status (no_dep_prods && b.[0] = '_')
97 ((b,NCic.Decl s)::context) t
101 | NCic.Const (NReference.Ref (_,NReference.CoFix _))
102 | NCic.Appl [] -> assert false (* NOT POSSIBLE *)
104 | NCic.Const (NReference.Ref (_,NReference.Fix _)) ->
105 (* be aware: we can be the head of an application *)
106 assert false (* TODO *)
107 | NCic.Meta _ -> assert false (* TODO *)
108 | NCic.Appl (he::_) -> classify_not_term status no_dep_prods context he
109 | NCic.Rel _ -> `KindOrType
110 | NCic.Const (NReference.Ref (_,NReference.Decl _) as ref) ->
111 let _,_,ty,_,_ = NCicEnvironment.get_checked_decl status ref in
112 (match classify_not_term status true [] ty with
114 | `Type -> assert false (* IMPOSSIBLE *)
116 | `KindOrType -> `KindOrType
117 | `PropKind -> `Proposition)
118 | NCic.Const (NReference.Ref (_,NReference.Ind _) as ref) ->
119 let _,_,ityl,_,i = NCicEnvironment.get_checked_indtys status ref in
120 let _,_,arity,_ = List.nth ityl i in
121 (match classify_not_term status true [] arity with
124 | `KindOrType -> assert false (* IMPOSSIBLE *)
126 | `PropKind -> `Proposition)
127 | NCic.Const (NReference.Ref (_,NReference.Con _))
128 | NCic.Const (NReference.Ref (_,NReference.Def _)) ->
129 assert false (* NOT POSSIBLE *)
132 type not_term = [`Kind | `KindOrType | `PropKind | `Proposition | `Type];;
134 let classify status ~metasenv context t =
135 match NCicTypeChecker.typeof status ~metasenv ~subst:[] context t with
137 (classify_not_term status true context t : not_term :> [> not_term])
139 let ty = fix_sorts ty in
140 prerr_endline ("XXX: " ^ status#ppterm ~metasenv:[] ~subst:[] ~context:[] ty);
142 (match classify_not_term status true context ty with
143 | `Proposition -> `Proof
145 | `KindOrType -> `TypeFormerOrTerm
146 | `Kind -> `TypeFormer
147 | `PropKind -> `PropFormer)
151 let rec kind_of status ~metasenv context k =
152 match NCicReduction.whd status ~subst:[] context k with
153 | NCic.Sort _ -> Type
154 | NCic.Prod (b,s,t) ->
155 (* CSC: non-invariant assumed here about "_" *)
156 (match classify status ~metasenv context s with
158 | `KindOrType -> (* KindOrType OK?? *)
159 KArrow (kind_of status ~metasenv context s,
160 kind_of ~metasenv status ((b,NCic.Decl s)::context) t)
164 KSkip (kind_of status ~metasenv ((b,NCic.Decl s)::context) t)
165 | `Term _ -> assert false (* IMPOSSIBLE *))
167 | NCic.LetIn _ -> assert false (* IMPOSSIBLE *)
170 | NCic.Const _ -> assert false (* NOT A KIND *)
171 | NCic.Appl _ -> assert false (* TODO: when head is a match/let rec;
172 otherwise NOT A KIND *)
174 | NCic.Match (_,_,_,_) -> assert false (* TODO *)
177 let rec skip_args status ~metasenv context =
180 | [],_ -> assert false (* IMPOSSIBLE *)
181 | None::tl1,_::tl2 -> skip_args status ~metasenv context (tl1,tl2)
183 match classify status ~metasenv context arg with
186 | `Term `TypeFormer -> arg::skip_args status ~metasenv context (tl1,tl2)
189 | `PropKind -> unitty::skip_args status ~metasenv context (tl1,tl2)
190 | `Term _ -> assert false (* IMPOSSIBLE *)
193 module ReferenceMap = Map.Make(struct type t = NReference.reference let compare = NReference.compare end)
195 type db = typ_context ReferenceMap.t
197 class type g_status =
199 method extraction_db: db
202 class virtual status =
205 val extraction_db = ReferenceMap.empty
206 method extraction_db = extraction_db
207 method set_extraction_db v = {< extraction_db = v >}
208 method set_extraction_status
209 : 'status. #g_status as 'status -> 'self
210 = fun o -> {< extraction_db = o#extraction_db >}
213 let rec split_kind_prods context =
215 | KArrow (so,ta)-> split_kind_prods (Some ("_",so)::context) ta
216 | KSkip ta -> split_kind_prods (None::context) ta
217 | Type -> context,Type
220 let rec split_typ_prods context =
222 | Arrow (so,ta)-> split_typ_prods (Some ("_",`OfType so)::context) ta
223 | Forall (name,so,ta)-> split_typ_prods (Some (name,`OfKind so)::context) ta
224 | Skip ta -> split_typ_prods (None::context) ta
225 | _ as t -> context,t
228 let rec split_typ_lambdas status n ~metasenv context typ =
229 if n = 0 then context,typ
231 match NCicReduction.whd status ~delta:max_int ~subst:[] context typ with
232 | NCic.Lambda (name,s,t) ->
233 split_typ_lambdas status (n-1) ~metasenv ((name,NCic.Decl s)::context) t
235 (* eta-expansion required *)
236 let ty = NCicTypeChecker.typeof status ~metasenv ~subst:[] context t in
237 match NCicReduction.whd status ~delta:max_int ~subst:[] context ty with
238 | NCic.Prod (name,typ,_) ->
239 split_typ_lambdas status (n-1) ~metasenv
240 ((name,NCic.Decl typ)::context)
241 (NCicUntrusted.mk_appl t [NCic.Rel 1])
242 | _ -> assert false (* IMPOSSIBLE *)
246 let context_of_typformer status ~metasenv context =
248 NCic.Const (NReference.Ref (_,NReference.Ind _) as ref)
249 | NCic.Const (NReference.Ref (_,NReference.Def _) as ref)
250 | NCic.Const (NReference.Ref (_,NReference.Decl _) as ref)
251 | NCic.Const (NReference.Ref (_,NReference.Fix _) as ref) ->
252 (try ReferenceMap.find ref status#extraction_db
254 Not_found -> assert false (* IMPOSSIBLE *))
255 | NCic.Match _ -> assert false (* TODO ???? *)
258 match List.nth context (n-1) with
259 _,NCic.Decl typ -> typ
260 | _,NCic.Def _ -> assert false (* IMPOSSIBLE *) in
261 let typ_ctx = snd (HExtlib.split_nth n context) in
262 let typ = kind_of status ~metasenv typ_ctx typ in
263 fst (split_kind_prods [] typ)
264 | NCic.Meta _ -> assert false (* TODO *)
265 | NCic.Const (NReference.Ref (_,NReference.Con _))
266 | NCic.Const (NReference.Ref (_,NReference.CoFix _))
267 | NCic.Sort _ | NCic.Implicit _ | NCic.Lambda _ | NCic.LetIn _
268 | NCic.Appl _ | NCic.Prod _ ->
269 assert false (* IMPOSSIBLE *)
271 let rec typ_of status ~metasenv context k =
272 match NCicReduction.whd status ~delta:max_int ~subst:[] context k with
273 | NCic.Prod (b,s,t) ->
274 (* CSC: non-invariant assumed here about "_" *)
275 (match classify status ~metasenv context s with
277 Forall (b, kind_of status ~metasenv context s,
278 typ_of ~metasenv status ((b,NCic.Decl s)::context) t)
280 | `KindOrType -> (* ??? *)
281 Arrow (typ_of status ~metasenv context s,
282 typ_of status ~metasenv ((b,NCic.Decl s)::context) t)
285 Skip (typ_of status ~metasenv ((b,NCic.Decl s)::context) t)
286 | `Term _ -> assert false (* IMPOSSIBLE *))
289 | NCic.LetIn _ -> assert false (* IMPOSSIBLE *)
290 | NCic.Lambda _ -> assert false (* NOT A TYPE *)
291 | NCic.Rel n -> Var n
292 | NCic.Const ref -> TConst ref
293 | NCic.Appl (he::args) ->
294 let he_context = context_of_typformer status ~metasenv context he in
295 TAppl (typ_of status ~metasenv context he ::
296 List.map (typ_of status ~metasenv context)
297 (skip_args status ~metasenv context (List.rev he_context,args)))
298 | NCic.Appl _ -> assert false (* TODO: when head is a match/let rec;
299 otherwise NOT A TYPE *)
301 | NCic.Match (_,_,_,_) -> assert false (* TODO *)
304 let rec term_of status ~metasenv context =
308 | NCic.Prod _ -> assert false (* IMPOSSIBLE *)
309 | NCic.Lambda (b,ty,bo) ->
310 (* CSC: non-invariant assumed here about "_" *)
311 (match classify status ~metasenv context ty with
313 TLambda (term_of status ~metasenv ((b,NCic.Decl ty)::context) bo)
314 | `KindOrType (* ??? *)
316 Lambda (b, term_of status ~metasenv ((b,NCic.Decl ty)::context) bo)
320 term_of status ~metasenv ((b,NCic.Decl ty)::context) bo
321 | `Term _ -> assert false (* IMPOSSIBLE *))
322 | NCic.LetIn (b,ty,t,bo) ->
323 (match classify status ~metasenv context t with
325 LetIn (b,term_of status ~metasenv context t,
326 term_of status ~metasenv ((b,NCic.Def (t,ty))::context) bo)
334 | `Term `TypeFormerOrTerm
335 | `Term `Proof -> assert false (* EXPAND IT ??? *))
336 | NCic.Rel n -> Rel n
337 | NCic.Const ref -> Const ref
338 | NCic.Appl (he::args) ->
340 let he_context = context_of_typformer status ~metasenv context he in
341 TAppl (typ_of status ~metasenv context he ::
342 List.map (typ_of status ~metasenv context)
343 (skip_args status ~metasenv context (List.rev he_context,args)))*)
344 | NCic.Appl _ -> assert false (* TODO: when head is a match/let rec;
345 otherwise NOT A TYPE *)
346 | NCic.Meta _ -> assert false (* TODO *)
347 | NCic.Match (ref,_,t,pl) ->
348 Match (ref,term_of status ~metasenv context t,
349 List.map (term_of status ~metasenv context) pl)
352 let obj_of status (uri,height,metasenv,subst,obj_kind) =
353 let obj_kind = apply_subst subst obj_kind in
356 | NCic.Constant (_,_,None,ty,_) ->
357 (match classify status ~metasenv [] ty with
359 let ty = kind_of status ~metasenv [] ty in
360 let ctx,_ as res = split_kind_prods [] ty in
361 let ref = NReference.reference_of_spec uri NReference.Decl in
362 status#set_extraction_db
363 (ReferenceMap.add ref ctx status#extraction_db),
364 Some (uri, TypeDeclaration res)
365 | `KindOrType -> assert false (* TODO ??? *)
367 | `Proposition -> status, None
369 let ty = typ_of status ~metasenv [] ty in
371 Some (uri, TermDeclaration (split_typ_prods [] ty))
372 | `Term _ -> assert false (* IMPOSSIBLE *))
373 | NCic.Constant (_,_,Some bo,ty,_) ->
374 (match classify status ~metasenv [] ty with
376 let ty = kind_of status ~metasenv [] ty in
377 let ctx0,_ as res = split_kind_prods [] ty in
379 split_typ_lambdas status ~metasenv (List.length ctx0) [] bo in
380 (match classify status ~metasenv ctx bo with
382 | `KindOrType -> (* ?? no kind formers in System F_omega *)
384 NReference.reference_of_spec uri (NReference.Def height) in
385 status#set_extraction_db
386 (ReferenceMap.add ref ctx0 status#extraction_db),
387 Some (uri,TypeDefinition(res,typ_of status ~metasenv ctx bo))
388 | `Kind -> status, None
390 | `Proposition -> status, None
391 | `Term _ -> assert false (* IMPOSSIBLE *))
393 | `Proposition -> status, None
394 | `KindOrType (* ??? *)
396 (* CSC: TO BE FINISHED, REF NON REGISTERED *)
397 let ty = typ_of status ~metasenv [] ty in
399 Some (uri, TermDefinition (split_typ_prods [] ty,
400 term_of status ~metasenv [](* BAD! *) bo))
401 | `Term _ -> assert false (* IMPOSSIBLE *))
404 prerr_endline "NOT IN F_omega";
407 (************************ HASKELL *************************)
409 let pp_ref status = NCicPp.r2s status false
411 (* cons avoid duplicates *)
412 let rec (@::) name l =
413 if name.[0] = '_' then "a" @:: l
414 else if List.mem name l then (name ^ "'") @:: l
422 | KArrow (k1,k2) -> "(" ^ pp_kind k1 ^ ") -> " ^ pp_kind k2
423 | KSkip k -> pp_kind k
425 let rec pp_typ status ctx =
427 Var n -> List.nth ctx (n-1)
428 | Top -> assert false (* ??? *)
429 | TConst ref -> pp_ref status ref
430 | Arrow (t1,t2) -> "(" ^ pp_typ status ctx t1 ^ ") -> " ^ pp_typ status ("_"::ctx) t2
431 | Skip t -> pp_typ status ("_"::ctx) t
432 | Forall (name,_,t) -> "(forall " ^ name ^ ". " ^ pp_typ status (name@::ctx) t ^")"
433 | TAppl tl -> "(" ^ String.concat " " (List.map (pp_typ status ctx) tl) ^ ")"
435 let rec pp_term status ctx =
437 Rel n -> List.nth ctx (n-1)
438 | Const ref -> pp_ref status ref
439 | Lambda (name,t) -> "(\\" ^ name ^ " -> " ^ pp_term status (name@::ctx) t ^ ")"
440 | Appl tl -> "(" ^ String.concat " " (List.map (pp_term status ctx) tl) ^ ")"
441 | LetIn (name,s,t) ->
442 "(let " ^ name ^ " = " ^ pp_term status ctx s ^ " in " ^ pp_term status (name@::ctx) t ^
444 | Match _ -> assert false (* TODO of reference * term * term list *)
445 | TLambda t -> pp_term status ctx t
446 | Inst t -> pp_term status ctx t
449 type term_context = (string * [`OfKind of kind | `OfType of typ]) option list
451 type term_former_def = term_context * term * typ
452 type term_former_decl = term_context * typ
455 let pp_obj status (uri,obj_kind) =
457 String.concat " " (List.rev
458 (List.fold_right (fun (x,_) l -> x@::l)
459 (HExtlib.filter_map (fun x -> x) ctx) [])) in
460 let namectx_of_ctx ctx =
461 List.fold_right (@::)
462 (List.map (function None -> "_" | Some (x,_) -> x) ctx) [] in
464 TypeDeclaration (ctx,_) ->
465 (* data?? unsure semantics *)
466 "data " ^ NUri.name_of_uri uri ^ " " ^ printctx ctx
467 | TypeDefinition ((ctx,_),ty) ->
468 let namectx = namectx_of_ctx ctx in
469 "type " ^ NUri.name_of_uri uri ^ " " ^ printctx ctx ^ " = " ^
470 pp_typ status namectx ty
471 | TermDeclaration (ctx,ty) ->
472 (*CSC: Ask Dominic about the syntax *)
473 let namectx = namectx_of_ctx ctx in
474 "let " ^ NUri.name_of_uri uri ^ " " ^ printctx ctx ^
475 " : " ^ pp_typ status namectx ty
476 | TermDefinition ((ctx,ty),bo) ->
477 (*CSC: Ask Dominic about the syntax *)
478 let namectx = namectx_of_ctx ctx in
479 "let " ^ NUri.name_of_uri uri ^ " " ^ printctx ctx ^
480 " : " ^ pp_typ status namectx ty ^ " = " ^
481 pp_term status namectx bo
482 | LetRec _ -> assert false (* TODO
483 (* inductive and records missing *)*)
485 let haskell_of_obj status obj =
486 let status, obj = obj_of status obj in
487 status,HExtlib.map_option (pp_obj status) obj
490 let rec typ_of context =
496 (match get_nth context n with
497 Some (C.Name s,_) -> ppid s
498 | Some (C.Anonymous,_) -> "__" ^ string_of_int n
499 | None -> "_hidden_" ^ string_of_int n
502 NotEnoughElements -> string_of_int (List.length context - n)
507 "?" ^ (string_of_int n) ^ "[" ^
512 | Some t -> pp ~in_type:false t context) l1) ^
516 let _,context,_ = CicUtil.lookup_meta n metasenv in
517 "?" ^ (string_of_int n) ^ "[" ^
525 | Some _, Some t -> pp ~in_type:false t context
529 CicUtil.Meta_not_found _
530 | Invalid_argument _ ->
531 "???" ^ (string_of_int n) ^ "[" ^
533 (List.rev_map (function None -> "_" | Some t ->
534 pp ~in_type:false t context) l1) ^
542 (*| C.Type u -> ("Type" ^ CicUniv.string_of_universe u)*)
543 | C.CProp _ -> "CProp"
545 | C.Implicit (Some `Hole) -> "%"
546 | C.Implicit _ -> "?"
548 (match b, is_term s with
549 _, true -> typ_of (None::context) t
550 | "_",_ -> Arrow (typ_of context s) (typ_of (Some b::context) t)
551 | _,_ -> Forall (b,typ_of (Some b::context) t)
552 | C.Lambda (b,s,t) ->
553 (match analyze_type context s with
555 | `Statement -> pp ~in_type t ((Some (b,Cic.Decl s))::context)
556 | `Optimize -> prerr_endline "XXX lambda";assert false
558 "(function " ^ ppname b ^ " -> " ^
559 pp ~in_type t ((Some (b,Cic.Decl s))::context) ^ ")")
560 | C.LetIn (b,s,ty,t) ->
561 (match analyze_term context s with
563 | `Proof -> pp ~in_type t ((Some (b,Cic.Def (s,ty)))::context)
566 "(let " ^ ppname b ^ (*" : " ^ pp ~in_type:true ty context ^*)
567 " = " ^ pp ~in_type:false s context ^ " in " ^
568 pp ~in_type t ((Some (b,Cic.Def (s,ty)))::context) ^ ")")
569 | C.Appl (he::tl) when in_type ->
570 let hes = pp ~in_type he context in
571 let stl = String.concat "," (clean_args_for_ty context tl) in
572 (if stl = "" then "" else "(" ^ stl ^ ") ") ^ hes
573 | C.Appl (C.MutInd _ as he::tl) ->
574 let hes = pp ~in_type he context in
575 let stl = String.concat "," (clean_args_for_ty context tl) in
576 (if stl = "" then "" else "(" ^ stl ^ ") ") ^ hes
577 | C.Appl (C.MutConstruct (uri,n,_,_) as he::tl) ->
579 match fst(CicEnvironment.get_obj CicUniv.oblivion_ugraph uri) with
580 C.InductiveDefinition (_,_,nparams,_) -> nparams
581 | _ -> assert false in
582 let hes = pp ~in_type he context in
583 let stl = String.concat "," (clean_args_for_constr nparams context tl) in
584 "(" ^ hes ^ (if stl = "" then "" else "(" ^ stl ^ ")") ^ ")"
586 "(" ^ String.concat " " (clean_args context li) ^ ")"
587 | C.Const (uri,exp_named_subst) ->
588 qualified_name_of_uri current_module_uri uri ^
589 pp_exp_named_subst exp_named_subst context
590 | C.MutInd (uri,n,exp_named_subst) ->
592 match fst(CicEnvironment.get_obj CicUniv.oblivion_ugraph uri) with
593 C.InductiveDefinition (dl,_,_,_) ->
594 let (name,_,_,_) = get_nth dl (n+1) in
595 qualified_name_of_uri current_module_uri
596 (UriManager.uri_of_string
597 (UriManager.buri_of_uri uri ^ "/" ^ name ^ ".con")) ^
598 pp_exp_named_subst exp_named_subst context
599 | _ -> raise CicExportationInternalError
601 Sys.Break as exn -> raise exn
602 | _ -> UriManager.string_of_uri uri ^ "#1/" ^ string_of_int (n + 1)
604 | C.MutConstruct (uri,n1,n2,exp_named_subst) ->
606 match fst(CicEnvironment.get_obj CicUniv.oblivion_ugraph uri) with
607 C.InductiveDefinition (dl,_,_,_) ->
608 let _,_,_,cons = get_nth dl (n1+1) in
609 let id,_ = get_nth cons n2 in
610 qualified_name_of_uri current_module_uri ~capitalize:true
611 (UriManager.uri_of_string
612 (UriManager.buri_of_uri uri ^ "/" ^ id ^ ".con")) ^
613 pp_exp_named_subst exp_named_subst context
614 | _ -> raise CicExportationInternalError
616 Sys.Break as exn -> raise exn
618 UriManager.string_of_uri uri ^ "#1/" ^ string_of_int (n1 + 1) ^ "/" ^
621 | C.MutCase (uri,n1,ty,te,patterns) ->
623 "unit (* TOO POLYMORPHIC TYPE *)"
625 let rec needs_obj_magic ty =
626 match CicReduction.whd context ty with
627 | Cic.Lambda (_,_,(Cic.Lambda(_,_,_) as t)) -> needs_obj_magic t
628 | Cic.Lambda (_,_,t) -> not (DoubleTypeInference.does_not_occur 1 t)
629 | _ -> false (* it can be a Rel, e.g. in *_rec *)
631 let needs_obj_magic = needs_obj_magic ty in
632 (match analyze_term context te with
633 `Type -> assert false
636 [] -> "assert false" (* empty type elimination *)
638 pp ~in_type:false he context (* singleton elimination *)
642 if patterns = [] then "assert false"
644 (let connames_and_argsno, go_up, go_pu, go_down, go_nwod =
645 (match fst(CicEnvironment.get_obj CicUniv.oblivion_ugraph uri) with
646 C.InductiveDefinition (dl,_,paramsno,_) ->
647 let (_,_,_,cons) = get_nth dl (n1+1) in
651 (* this is just an approximation since we do not have
653 let rec count_prods toskip =
655 C.Prod (_,_,bo) when toskip > 0 ->
656 count_prods (toskip - 1) bo
657 | C.Prod (_,_,bo) -> 1 + count_prods 0 bo
660 qualified_name_of_uri current_module_uri
662 (UriManager.uri_of_string
663 (UriManager.buri_of_uri uri ^ "/" ^ id ^ ".con")),
664 count_prods paramsno ty
667 if not (is_mcu_type uri) then rc, "","","",""
668 else rc, !current_go_up, "))", "( .< (", " ) >.)"
669 | _ -> raise CicExportationInternalError
672 let connames_and_argsno_and_patterns =
676 | (x,no)::tlx,y::tly -> (x,no,y)::(combine (tlx,tly))
677 | _,_ -> assert false
679 combine (connames_and_argsno,patterns)
682 "\n(match " ^ pp ~in_type:false te context ^ " with \n " ^
683 (String.concat "\n | "
686 let rec aux argsno context =
688 Cic.Lambda (name,ty,bo) when argsno > 0 ->
691 Cic.Anonymous -> Cic.Anonymous
692 | Cic.Name n -> Cic.Name (ppid n) in
694 aux (argsno - 1) (Some (name,Cic.Decl ty)::context)
697 (match analyze_type context ty with
698 | `Optimize -> prerr_endline "XXX contructor with l2 arg"; assert false
700 | `Sort _ -> args,res
704 | C.Name s -> s)::args,res)
705 | t when argsno = 0 -> [],pp ~in_type:false t context
707 ["{" ^ string_of_int argsno ^ " args missing}"],
708 pp ~in_type:false t context
711 if argsno = 0 then x,pp ~in_type:false y context
713 let args,body = aux argsno context y in
714 let sargs = String.concat "," args in
715 x ^ (if sargs = "" then "" else "(" ^ sargs^ ")"),
718 pattern ^ " -> " ^ go_down ^
719 (if needs_obj_magic then
720 "Obj.magic (" ^ body ^ ")"
723 ) connames_and_argsno_and_patterns)) ^
725 | C.Fix (no, funs) ->
728 (fun (types,len) (n,_,ty,_) ->
729 (Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::types,
735 (fun (name,ind,ty,bo) i -> name ^ " = \n" ^
736 pp ~in_type:false bo (names@context) ^ i)
739 (match get_nth names (no + 1) with
740 Some (Cic.Name n,_) -> n
742 | C.CoFix (no,funs) ->
745 (fun (types,len) (n,ty,_) ->
746 (Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::types,
752 (fun (name,ty,bo) i -> "\n" ^ name ^
753 " : " ^ pp ~in_type:true ty context ^ " := \n" ^
754 pp ~in_type:false bo (names@context) ^ i)
760 exception CicExportationInternalError;;
761 exception NotEnoughElements;;
766 UriManager.eq (UriManager.uri_of_string
767 "cic:/matita/freescale/opcode/mcu_type.ind") u
770 (* Utility functions *)
772 let analyze_term context t =
773 match fst(CicTypeChecker.type_of_aux' [] context t CicUniv.oblivion_ugraph)with
774 | Cic.Sort _ -> `Type
775 | Cic.MutInd (u,0,_) when is_mcu_type u -> `Optimize
778 fst (CicTypeChecker.type_of_aux' [] context ty CicUniv.oblivion_ugraph)
780 | Cic.Sort Cic.Prop -> `Proof
784 let analyze_type context t =
787 Cic.Sort s -> `Sort s
788 | Cic.MutInd (u,0,_) when is_mcu_type u -> `Optimize
789 | Cic.Prod (_,_,t) -> aux t
790 | _ -> `SomethingElse
793 `Sort _ | `Optimize as res -> res
796 fst(CicTypeChecker.type_of_aux' [] context t CicUniv.oblivion_ugraph)
798 Cic.Sort Cic.Prop -> `Statement
812 let n = String.uncapitalize n in
813 if List.mem n reserved then n ^ "_" else n
819 | Cic.Anonymous -> "_"
822 (* get_nth l n returns the nth element of the list l if it exists or *)
823 (* raises NotEnoughElements if l has less than n elements *)
824 let rec get_nth l n =
827 | (n, he::tail) when n > 1 -> get_nth tail (n-1)
828 | (_,_) -> raise NotEnoughElements
831 let qualified_name_of_uri current_module_uri ?(capitalize=false) uri =
834 String.capitalize (UriManager.name_of_uri uri)
836 ppid (UriManager.name_of_uri uri) in
838 let suri = UriManager.buri_of_uri uri in
839 let s = String.sub suri 5 (String.length suri - 5) in
840 let s = Pcre.replace ~pat:"/" ~templ:"_" s in
841 String.uncapitalize s in
842 if current_module_uri = UriManager.buri_of_uri uri then
845 String.capitalize filename ^ "." ^ name
848 let current_go_up = ref "(.!(";;
851 current_go_up := "(.~(";
853 current_go_up := "(.!(";
856 current_go_up := "(.!(";
860 let pp current_module_uri ?metasenv ~in_type =
861 let rec pp ~in_type t context =
862 let module C = Cic in
867 (match get_nth context n with
868 Some (C.Name s,_) -> ppid s
869 | Some (C.Anonymous,_) -> "__" ^ string_of_int n
870 | None -> "_hidden_" ^ string_of_int n
873 NotEnoughElements -> string_of_int (List.length context - n)
875 | C.Var (uri,exp_named_subst) ->
876 qualified_name_of_uri current_module_uri uri ^
877 pp_exp_named_subst exp_named_subst context
881 "?" ^ (string_of_int n) ^ "[" ^
886 | Some t -> pp ~in_type:false t context) l1) ^
890 let _,context,_ = CicUtil.lookup_meta n metasenv in
891 "?" ^ (string_of_int n) ^ "[" ^
899 | Some _, Some t -> pp ~in_type:false t context
903 CicUtil.Meta_not_found _
904 | Invalid_argument _ ->
905 "???" ^ (string_of_int n) ^ "[" ^
907 (List.rev_map (function None -> "_" | Some t ->
908 pp ~in_type:false t context) l1) ^
916 (*| C.Type u -> ("Type" ^ CicUniv.string_of_universe u)*)
917 | C.CProp _ -> "CProp"
919 | C.Implicit (Some `Hole) -> "%"
920 | C.Implicit _ -> "?"
924 let n = "'" ^ String.uncapitalize n in
925 "(" ^ pp ~in_type:true s context ^ " -> " ^
926 pp ~in_type:true t ((Some (Cic.Name n,Cic.Decl s))::context) ^ ")"
928 "(" ^ pp ~in_type:true s context ^ " -> " ^
929 pp ~in_type:true t ((Some (b,Cic.Decl s))::context) ^ ")")
930 | C.Cast (v,t) -> pp ~in_type v context
931 | C.Lambda (b,s,t) ->
932 (match analyze_type context s with
934 | `Statement -> pp ~in_type t ((Some (b,Cic.Decl s))::context)
935 | `Optimize -> prerr_endline "XXX lambda";assert false
937 "(function " ^ ppname b ^ " -> " ^
938 pp ~in_type t ((Some (b,Cic.Decl s))::context) ^ ")")
939 | C.LetIn (b,s,ty,t) ->
940 (match analyze_term context s with
942 | `Proof -> pp ~in_type t ((Some (b,Cic.Def (s,ty)))::context)
945 "(let " ^ ppname b ^ (*" : " ^ pp ~in_type:true ty context ^*)
946 " = " ^ pp ~in_type:false s context ^ " in " ^
947 pp ~in_type t ((Some (b,Cic.Def (s,ty)))::context) ^ ")")
948 | C.Appl (he::tl) when in_type ->
949 let hes = pp ~in_type he context in
950 let stl = String.concat "," (clean_args_for_ty context tl) in
951 (if stl = "" then "" else "(" ^ stl ^ ") ") ^ hes
952 | C.Appl (C.MutInd _ as he::tl) ->
953 let hes = pp ~in_type he context in
954 let stl = String.concat "," (clean_args_for_ty context tl) in
955 (if stl = "" then "" else "(" ^ stl ^ ") ") ^ hes
956 | C.Appl (C.MutConstruct (uri,n,_,_) as he::tl) ->
958 match fst(CicEnvironment.get_obj CicUniv.oblivion_ugraph uri) with
959 C.InductiveDefinition (_,_,nparams,_) -> nparams
960 | _ -> assert false in
961 let hes = pp ~in_type he context in
962 let stl = String.concat "," (clean_args_for_constr nparams context tl) in
963 "(" ^ hes ^ (if stl = "" then "" else "(" ^ stl ^ ")") ^ ")"
965 "(" ^ String.concat " " (clean_args context li) ^ ")"
966 | C.Const (uri,exp_named_subst) ->
967 qualified_name_of_uri current_module_uri uri ^
968 pp_exp_named_subst exp_named_subst context
969 | C.MutInd (uri,n,exp_named_subst) ->
971 match fst(CicEnvironment.get_obj CicUniv.oblivion_ugraph uri) with
972 C.InductiveDefinition (dl,_,_,_) ->
973 let (name,_,_,_) = get_nth dl (n+1) in
974 qualified_name_of_uri current_module_uri
975 (UriManager.uri_of_string
976 (UriManager.buri_of_uri uri ^ "/" ^ name ^ ".con")) ^
977 pp_exp_named_subst exp_named_subst context
978 | _ -> raise CicExportationInternalError
980 Sys.Break as exn -> raise exn
981 | _ -> UriManager.string_of_uri uri ^ "#1/" ^ string_of_int (n + 1)
983 | C.MutConstruct (uri,n1,n2,exp_named_subst) ->
985 match fst(CicEnvironment.get_obj CicUniv.oblivion_ugraph uri) with
986 C.InductiveDefinition (dl,_,_,_) ->
987 let _,_,_,cons = get_nth dl (n1+1) in
988 let id,_ = get_nth cons n2 in
989 qualified_name_of_uri current_module_uri ~capitalize:true
990 (UriManager.uri_of_string
991 (UriManager.buri_of_uri uri ^ "/" ^ id ^ ".con")) ^
992 pp_exp_named_subst exp_named_subst context
993 | _ -> raise CicExportationInternalError
995 Sys.Break as exn -> raise exn
997 UriManager.string_of_uri uri ^ "#1/" ^ string_of_int (n1 + 1) ^ "/" ^
1000 | C.MutCase (uri,n1,ty,te,patterns) ->
1002 "unit (* TOO POLYMORPHIC TYPE *)"
1004 let rec needs_obj_magic ty =
1005 match CicReduction.whd context ty with
1006 | Cic.Lambda (_,_,(Cic.Lambda(_,_,_) as t)) -> needs_obj_magic t
1007 | Cic.Lambda (_,_,t) -> not (DoubleTypeInference.does_not_occur 1 t)
1008 | _ -> false (* it can be a Rel, e.g. in *_rec *)
1010 let needs_obj_magic = needs_obj_magic ty in
1011 (match analyze_term context te with
1012 `Type -> assert false
1014 (match patterns with
1015 [] -> "assert false" (* empty type elimination *)
1017 pp ~in_type:false he context (* singleton elimination *)
1018 | _ -> assert false)
1021 if patterns = [] then "assert false"
1023 (let connames_and_argsno, go_up, go_pu, go_down, go_nwod =
1024 (match fst(CicEnvironment.get_obj CicUniv.oblivion_ugraph uri) with
1025 C.InductiveDefinition (dl,_,paramsno,_) ->
1026 let (_,_,_,cons) = get_nth dl (n1+1) in
1030 (* this is just an approximation since we do not have
1032 let rec count_prods toskip =
1034 C.Prod (_,_,bo) when toskip > 0 ->
1035 count_prods (toskip - 1) bo
1036 | C.Prod (_,_,bo) -> 1 + count_prods 0 bo
1039 qualified_name_of_uri current_module_uri
1041 (UriManager.uri_of_string
1042 (UriManager.buri_of_uri uri ^ "/" ^ id ^ ".con")),
1043 count_prods paramsno ty
1046 if not (is_mcu_type uri) then rc, "","","",""
1047 else rc, !current_go_up, "))", "( .< (", " ) >.)"
1048 | _ -> raise CicExportationInternalError
1051 let connames_and_argsno_and_patterns =
1055 | (x,no)::tlx,y::tly -> (x,no,y)::(combine (tlx,tly))
1056 | _,_ -> assert false
1058 combine (connames_and_argsno,patterns)
1061 "\n(match " ^ pp ~in_type:false te context ^ " with \n " ^
1062 (String.concat "\n | "
1064 (fun (x,argsno,y) ->
1065 let rec aux argsno context =
1067 Cic.Lambda (name,ty,bo) when argsno > 0 ->
1070 Cic.Anonymous -> Cic.Anonymous
1071 | Cic.Name n -> Cic.Name (ppid n) in
1073 aux (argsno - 1) (Some (name,Cic.Decl ty)::context)
1076 (match analyze_type context ty with
1077 | `Optimize -> prerr_endline "XXX contructor with l2 arg"; assert false
1079 | `Sort _ -> args,res
1083 | C.Name s -> s)::args,res)
1084 | t when argsno = 0 -> [],pp ~in_type:false t context
1086 ["{" ^ string_of_int argsno ^ " args missing}"],
1087 pp ~in_type:false t context
1090 if argsno = 0 then x,pp ~in_type:false y context
1092 let args,body = aux argsno context y in
1093 let sargs = String.concat "," args in
1094 x ^ (if sargs = "" then "" else "(" ^ sargs^ ")"),
1097 pattern ^ " -> " ^ go_down ^
1098 (if needs_obj_magic then
1099 "Obj.magic (" ^ body ^ ")"
1102 ) connames_and_argsno_and_patterns)) ^
1104 | C.Fix (no, funs) ->
1107 (fun (types,len) (n,_,ty,_) ->
1108 (Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::types,
1114 (fun (name,ind,ty,bo) i -> name ^ " = \n" ^
1115 pp ~in_type:false bo (names@context) ^ i)
1118 (match get_nth names (no + 1) with
1119 Some (Cic.Name n,_) -> n
1120 | _ -> assert false)
1121 | C.CoFix (no,funs) ->
1124 (fun (types,len) (n,ty,_) ->
1125 (Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::types,
1131 (fun (name,ty,bo) i -> "\n" ^ name ^
1132 " : " ^ pp ~in_type:true ty context ^ " := \n" ^
1133 pp ~in_type:false bo (names@context) ^ i)
1136 and pp_exp_named_subst exp_named_subst context =
1137 if exp_named_subst = [] then "" else
1139 String.concat " ; " (
1141 (function (uri,t) -> UriManager.name_of_uri uri ^ " \\Assign " ^ pp ~in_type:false t context)
1144 and clean_args_for_constr nparams context =
1145 let nparams = ref nparams in
1149 match analyze_term context t with
1150 `Term when !nparams < 0 -> Some (pp ~in_type:false t context)
1155 and clean_args context =
1157 | [] | [_] -> assert false
1158 | he::arg1::tl as l ->
1159 let head_arg1, rest =
1160 match analyze_term context arg1 with
1162 !current_go_up :: pp ~in_type:false he context ::
1163 pp ~in_type:false arg1 context :: ["))"], tl
1169 match analyze_term context t with
1170 | `Term -> Some (pp ~in_type:false t context)
1172 prerr_endline "XXX function taking twice (or not as first) a l2 term"; assert false
1174 | `Proof -> None) rest
1175 and clean_args_for_ty context =
1178 match analyze_term context t with
1179 `Type -> Some (pp ~in_type:true t context)
1187 let ppty current_module_uri =
1188 (* nparams is the number of left arguments
1189 left arguments should either become parameters or be skipped altogether *)
1190 let rec args nparams context =
1195 Cic.Anonymous -> Cic.Anonymous
1196 | Cic.Name n -> Cic.Name (String.uncapitalize n)
1198 (match analyze_type context s with
1202 args (nparams - 1) ((Some (n,Cic.Decl s))::context) t
1203 | `Type when nparams > 0 ->
1204 args (nparams - 1) ((Some (n,Cic.Decl s))::context) t
1207 args (nparams - 1) ((Some (n,Cic.Decl s))::context) t in
1208 abstr,pp ~in_type:true current_module_uri s context::args
1209 | `Sort _ when nparams <= 0 ->
1210 let n = Cic.Name "unit (* EXISTENTIAL TYPE *)" in
1211 args (nparams - 1) ((Some (n,Cic.Decl s))::context) t
1215 Cic.Anonymous -> Cic.Anonymous
1216 | Cic.Name name -> Cic.Name ("'" ^ name) in
1218 args (nparams - 1) ((Some (n,Cic.Decl s))::context) t
1221 Cic.Anonymous -> abstr
1222 | Cic.Name name -> name::abstr),
1229 exception DoNotExtract;;
1231 let pp_abstracted_ty current_module_uri =
1232 let rec args context =
1234 Cic.Lambda (n,s,t) ->
1237 Cic.Anonymous -> Cic.Anonymous
1238 | Cic.Name n -> Cic.Name (String.uncapitalize n)
1240 (match analyze_type context s with
1245 args ((Some (n,Cic.Decl s))::context) t
1249 Cic.Anonymous -> Cic.Anonymous
1250 | Cic.Name name -> Cic.Name ("'" ^ name) in
1252 args ((Some (n,Cic.Decl s))::context) t
1255 Cic.Anonymous -> abstr
1256 | Cic.Name name -> name::abstr),
1259 match analyze_type context ty with
1261 prerr_endline "XXX abstracted l2 ty"; assert false
1263 | `Statement -> raise DoNotExtract
1265 (* BUG HERE: this can be a real System F type *)
1266 let head = pp ~in_type:true current_module_uri ty context in
1273 (* ppinductiveType (typename, inductive, arity, cons) *)
1274 (* pretty-prints a single inductive definition *)
1275 (* (typename, inductive, arity, cons) *)
1276 let ppinductiveType current_module_uri nparams (typename, inductive, arity, cons)
1278 match analyze_type [] arity with
1279 `Sort Cic.Prop -> ""
1282 | `Type -> assert false
1285 "type " ^ String.uncapitalize typename ^ " = unit (* empty type *)\n"
1289 (fun (id,ty) (_abstr,i) -> (* we should verify _abstr = abstr' *)
1290 let abstr',sargs = ppty current_module_uri nparams [] ty in
1291 let sargs = String.concat " * " sargs in
1293 String.capitalize id ^
1294 (if sargs = "" then "" else " of " ^ sargs) ^
1295 (if i = "" then "" else "\n | ") ^ i)
1299 let s = String.concat "," abstr in
1300 if s = "" then "" else "(" ^ s ^ ") "
1302 "type " ^ abstr ^ String.uncapitalize typename ^ " =\n" ^ scons ^ "\n")
1305 let ppobj current_module_uri obj =
1306 let module C = Cic in
1307 let module U = UriManager in
1308 let pp ~in_type = pp ~in_type current_module_uri in
1310 C.Constant (name, Some t1, t2, params, _) ->
1311 (match analyze_type [] t2 with
1317 | Cic.Lambda (Cic.Name arg, s, t) ->
1318 (match analyze_type [] s with
1321 "let " ^ ppid name ^ "__1 = function " ^ ppid arg
1323 at_level2 (pp ~in_type:false t) [Some (Cic.Name arg, Cic.Decl s)]
1325 ^ "let " ^ ppid name ^ "__2 = ref ([] : (unit list*unit list) list);;\n"
1326 ^ "let " ^ ppid name ^ " = function " ^ ppid arg
1327 ^ " -> (try ignore (List.assoc "^ppid arg^" (Obj.magic !"^ppid name
1328 ^"__2)) with Not_found -> "^ppid name^"__2 := (Obj.magic ("
1329 ^ ppid arg^",.! ("^ppid name^"__1 "^ppid arg^")))::!"
1330 ^ppid name^"__2); .< List.assoc "^ppid arg^" (Obj.magic (!"
1331 ^ppid name^"__2)) >.\n;;\n"
1332 ^" let xxx = prerr_endline \""^ppid name^"\"; .!("^ppid
1333 name^" Matita_freescale_opcode.HCS08)"
1335 "let " ^ ppid name ^ " =\n" ^ pp ~in_type:false t1 [] ^ "\n")
1336 | _ -> "let " ^ ppid name ^ " =\n" ^ pp ~in_type:false t1 [] ^ "\n")
1338 match analyze_type [] t1 with
1339 `Sort Cic.Prop -> ""
1340 | `Optimize -> prerr_endline "XXX aliasing l2 type"; assert false
1343 let abstr,res = pp_abstracted_ty current_module_uri [] t1 in
1345 let s = String.concat "," abstr in
1346 if s = "" then "" else "(" ^ s ^ ") "
1348 "type " ^ abstr ^ ppid name ^ " = " ^ res ^ "\n"
1350 DoNotExtract -> ""))
1351 | C.Constant (name, None, ty, params, _) ->
1352 (match analyze_type [] ty with
1354 | `Optimize -> prerr_endline "XXX axiom l2"; assert false
1356 | `Sort _ -> "type " ^ ppid name ^ "\n"
1357 | `Type -> "let " ^ ppid name ^ " = assert false\n")
1358 | C.Variable (name, bo, ty, params, _) ->
1359 "Variable " ^ name ^
1360 "(" ^ String.concat ";" (List.map UriManager.string_of_uri params) ^
1362 pp ~in_type:true ty [] ^ "\n" ^
1363 (match bo with None -> "" | Some bo -> ":= " ^ pp ~in_type:false bo [])
1364 | C.CurrentProof (name, conjectures, value, ty, params, _) ->
1365 "Current Proof of " ^ name ^
1366 "(" ^ String.concat ";" (List.map UriManager.string_of_uri params) ^
1368 let separate s = if s = "" then "" else s ^ " ; " in
1370 (fun (n, context, t) i ->
1371 let conjectures',name_context =
1373 (fun context_entry (i,name_context) ->
1374 (match context_entry with
1375 Some (n,C.Decl at) ->
1378 pp ~in_type:true ~metasenv:conjectures
1379 at name_context ^ " ",
1380 context_entry::name_context
1381 | Some (n,C.Def (at,aty)) ->
1384 pp ~in_type:true ~metasenv:conjectures
1386 ":= " ^ pp ~in_type:false
1387 ~metasenv:conjectures at name_context ^ " ",
1388 context_entry::name_context
1390 (separate i) ^ "_ :? _ ", context_entry::name_context)
1393 conjectures' ^ " |- " ^ "?" ^ (string_of_int n) ^ ": " ^
1394 pp ~in_type:true ~metasenv:conjectures t name_context ^ "\n" ^ i
1396 "\n" ^ pp ~in_type:false ~metasenv:conjectures value [] ^ " : " ^
1397 pp ~in_type:true ~metasenv:conjectures ty []
1398 | C.InductiveDefinition (l, params, nparams, _) ->
1400 (fun x i -> ppinductiveType current_module_uri nparams x ^ i) l ""
1403 let ppobj current_module_uri obj =
1404 let res = ppobj current_module_uri obj in
1405 if res = "" then "" else res ^ ";;\n\n"