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 *)
43 | TConst of typformerreference
46 | Forall of string * kind * typ
53 | Lambda of string * (* typ **) term
55 | LetIn of string * (* typ **) term * term
56 | Match of reference * term * term list
57 | TLambda of (* string **) term
58 | Inst of (*typ_former **) term
61 NCic.Const (NReference.reference_of_spec (NUri.uri_of_string "cic:/matita/basics/types/unit.ind") (NReference.Ind (true,0,0)));;
63 (* None = dropped abstraction *)
64 type typ_context = (string * kind) option list
65 type term_context = (string * [`OfKind of kind | `OfType of typ]) option list
67 type typ_former_decl = typ_context * kind
68 type typ_former_def = typ_former_decl * typ
70 type term_former_decl = term_context * typ
71 type term_former_def = term_former_decl * term
74 TypeDeclaration of typ_former_decl
75 | TypeDefinition of typ_former_def
76 | TermDeclaration of term_former_decl
77 | TermDefinition of term_former_def
78 | LetRec of obj_kind list
79 (* inductive and records missing *)
81 type obj = NUri.uri * obj_kind
85 let rec classify_not_term status no_dep_prods context t =
86 match NCicReduction.whd status ~subst:[] context t with
90 | NCic.Type [`CProp,_] -> `PropKind
91 | NCic.Type [`Type,_] ->
92 if no_dep_prods then `Kind
94 raise NotInFOmega (* ?? *)
95 | NCic.Type _ -> assert false)
96 | NCic.Prod (b,s,t) ->
97 (*CSC: using invariant on "_" *)
98 classify_not_term status (no_dep_prods && b.[0] = '_')
99 ((b,NCic.Decl s)::context) t
103 | NCic.Const (NReference.Ref (_,NReference.CoFix _))
104 | NCic.Appl [] -> assert false (* NOT POSSIBLE *)
106 | NCic.Const (NReference.Ref (_,NReference.Fix _)) ->
107 (* be aware: we can be the head of an application *)
108 assert false (* TODO *)
109 | NCic.Meta _ -> assert false (* TODO *)
110 | NCic.Appl (he::_) -> classify_not_term status no_dep_prods context he
112 let rec find_sort typ =
113 match NCicReduction.whd status ~subst:[] context (NCicSubstitution.lift status n typ) with
114 NCic.Sort NCic.Prop -> `Proposition
115 | NCic.Sort (NCic.Type [`CProp,_]) -> `Proposition
116 | NCic.Sort (NCic.Type [`Type,_]) ->
117 (*CSC: we could be more precise distinguishing the user provided
118 minimal elements of the hierarchies and classify these
121 | NCic.Sort (NCic.Type _) -> assert false (* ALGEBRAIC *)
122 | NCic.Prod (_,_,t) ->
123 (* we skipped arguments of applications, so here we need to skip
126 | _ -> assert false (* NOT A SORT *)
128 (match List.nth context (n-1) with
129 _,NCic.Decl typ -> find_sort typ
130 | _,NCic.Def _ -> assert false (* IMPOSSIBLE *))
131 | NCic.Const (NReference.Ref (_,NReference.Decl) as ref) ->
132 let _,_,ty,_,_ = NCicEnvironment.get_checked_decl status ref in
133 (match classify_not_term status true [] ty with
135 | `Type -> assert false (* IMPOSSIBLE *)
137 | `KindOrType -> `Type
138 | `PropKind -> `Proposition)
139 | NCic.Const (NReference.Ref (_,NReference.Ind _) as ref) ->
140 let _,_,ityl,_,i = NCicEnvironment.get_checked_indtys status ref in
141 let _,_,arity,_ = List.nth ityl i in
142 (match classify_not_term status true [] arity with
145 | `KindOrType -> assert false (* IMPOSSIBLE *)
147 | `PropKind -> `Proposition)
148 | NCic.Const (NReference.Ref (_,NReference.Con _))
149 | NCic.Const (NReference.Ref (_,NReference.Def _)) ->
150 assert false (* IMPOSSIBLE *)
153 type not_term = [`Kind | `KindOrType | `PropKind | `Proposition | `Type];;
155 let classify status ~metasenv context t =
156 match NCicTypeChecker.typeof status ~metasenv ~subst:[] context t with
158 (classify_not_term status true context t : not_term :> [> not_term])
160 let ty = fix_sorts ty in
162 (match classify_not_term status true context ty with
163 | `Proposition -> `Proof
165 | `KindOrType -> `TypeFormerOrTerm
166 | `Kind -> `TypeFormer
167 | `PropKind -> `PropFormer)
171 let rec kind_of status ~metasenv context k =
172 match NCicReduction.whd status ~subst:[] context k with
173 | NCic.Sort NCic.Type _ -> Type
174 | NCic.Sort _ -> assert false (* NOT A KIND *)
175 | NCic.Prod (b,s,t) ->
176 (* CSC: non-invariant assumed here about "_" *)
177 (match classify status ~metasenv context s with
179 | `KindOrType -> (* KindOrType OK?? *)
180 KArrow (kind_of status ~metasenv context s,
181 kind_of ~metasenv status ((b,NCic.Decl s)::context) t)
185 KSkip (kind_of status ~metasenv ((b,NCic.Decl s)::context) t)
186 | `Term _ -> assert false (* IMPOSSIBLE *))
188 | NCic.LetIn _ -> assert false (* IMPOSSIBLE *)
191 | NCic.Const _ -> assert false (* NOT A KIND *)
192 | NCic.Appl _ -> assert false (* TODO: when head is a match/let rec;
193 otherwise NOT A KIND *)
195 | NCic.Match (_,_,_,_) -> assert false (* TODO *)
198 let rec skip_args status ~metasenv context =
201 | [],_ -> assert false (* IMPOSSIBLE *)
202 | None::tl1,_::tl2 -> skip_args status ~metasenv context (tl1,tl2)
204 match classify status ~metasenv context arg with
207 | `Term `TypeFormer ->
208 Some arg::skip_args status ~metasenv context (tl1,tl2)
211 | `PropKind -> None::skip_args status ~metasenv context (tl1,tl2)
212 | `Term _ -> assert false (* IMPOSSIBLE *)
215 module ReferenceMap = Map.Make(struct type t = NReference.reference let compare = NReference.compare end)
217 type db = (typ_context * typ option) ReferenceMap.t
219 class type g_status =
221 method extraction_db: db
224 class virtual status =
227 val extraction_db = ReferenceMap.empty
228 method extraction_db = extraction_db
229 method set_extraction_db v = {< extraction_db = v >}
230 method set_extraction_status
231 : 'status. #g_status as 'status -> 'self
232 = fun o -> {< extraction_db = o#extraction_db >}
235 let rec split_kind_prods context =
237 | KArrow (so,ta)-> split_kind_prods (Some ("_",so)::context) ta
238 | KSkip ta -> split_kind_prods (None::context) ta
239 | Type -> context,Type
242 let rec split_typ_prods context =
244 | Arrow (so,ta)-> split_typ_prods (Some ("_",`OfType so)::context) ta
245 | Forall (name,so,ta)-> split_typ_prods (Some (name,`OfKind so)::context) ta
246 | Skip ta -> split_typ_prods (None::context) ta
247 | _ as t -> context,t
250 let rec glue_ctx_typ ctx typ =
253 | Some (_,`OfType so)::ctx -> glue_ctx_typ ctx (Arrow (so,typ))
254 | Some (name,`OfKind so)::ctx -> glue_ctx_typ ctx (Forall (name,so,typ))
255 | None::ctx -> glue_ctx_typ ctx (Skip typ)
258 let rec split_typ_lambdas status n ~metasenv context typ =
259 if n = 0 then context,typ
261 match NCicReduction.whd status ~delta:max_int ~subst:[] context typ with
262 | NCic.Lambda (name,s,t) ->
263 split_typ_lambdas status (n-1) ~metasenv ((name,NCic.Decl s)::context) t
265 (* eta-expansion required *)
266 let ty = NCicTypeChecker.typeof status ~metasenv ~subst:[] context t in
267 match NCicReduction.whd status ~delta:max_int ~subst:[] context ty with
268 | NCic.Prod (name,typ,_) ->
269 split_typ_lambdas status (n-1) ~metasenv
270 ((name,NCic.Decl typ)::context)
271 (NCicUntrusted.mk_appl t [NCic.Rel 1])
272 | _ -> assert false (* IMPOSSIBLE *)
276 let context_of_typformer status ~metasenv context =
278 NCic.Const (NReference.Ref (_,NReference.Ind _) as ref)
279 | NCic.Const (NReference.Ref (_,NReference.Def _) as ref)
280 | NCic.Const (NReference.Ref (_,NReference.Decl) as ref)
281 | NCic.Const (NReference.Ref (_,NReference.Fix _) as ref) ->
282 (try fst (ReferenceMap.find ref status#extraction_db)
284 Not_found -> assert false (* IMPOSSIBLE *))
285 | NCic.Match _ -> assert false (* TODO ???? *)
288 match List.nth context (n-1) with
289 _,NCic.Decl typ -> typ
290 | _,NCic.Def _ -> assert false (* IMPOSSIBLE *) in
291 let typ_ctx = snd (HExtlib.split_nth n context) in
292 let typ = kind_of status ~metasenv typ_ctx typ in
293 fst (split_kind_prods [] typ)
294 | NCic.Meta _ -> assert false (* TODO *)
295 | NCic.Const (NReference.Ref (_,NReference.Con _))
296 | NCic.Const (NReference.Ref (_,NReference.CoFix _))
297 | NCic.Sort _ | NCic.Implicit _ | NCic.Lambda _ | NCic.LetIn _
298 | NCic.Appl _ | NCic.Prod _ ->
299 assert false (* IMPOSSIBLE *)
301 let rec typ_of status ~metasenv context k =
302 match NCicReduction.whd status ~delta:max_int ~subst:[] context k with
303 | NCic.Prod (b,s,t) ->
304 (* CSC: non-invariant assumed here about "_" *)
305 (match classify status ~metasenv context s with
307 Forall (b, kind_of status ~metasenv context s,
308 typ_of ~metasenv status ((b,NCic.Decl s)::context) t)
310 | `KindOrType -> (* ??? *)
311 Arrow (typ_of status ~metasenv context s,
312 typ_of status ~metasenv ((b,NCic.Decl s)::context) t)
315 Skip (typ_of status ~metasenv ((b,NCic.Decl s)::context) t)
316 | `Term _ -> assert false (* IMPOSSIBLE *))
319 | NCic.LetIn _ -> assert false (* IMPOSSIBLE *)
320 | NCic.Lambda _ -> assert false (* NOT A TYPE *)
321 | NCic.Rel n -> Var n
322 | NCic.Const ref -> TConst ref
323 | NCic.Appl (he::args) ->
324 let he_context = context_of_typformer status ~metasenv context he in
325 TAppl (typ_of status ~metasenv context he ::
327 (function None -> Unit | Some ty -> typ_of status ~metasenv context ty)
328 (skip_args status ~metasenv context (List.rev he_context,args)))
329 | NCic.Appl _ -> assert false (* TODO: when head is a match/let rec;
330 otherwise NOT A TYPE *)
332 | NCic.Match (_,_,_,_) -> assert false (* TODO *)
335 let rec fomega_subst k t1 =
339 else if n < k then Var n
342 | TConst ref -> TConst ref
344 | Arrow (ty1,ty2) -> Arrow (fomega_subst k t1 ty1, fomega_subst (k+1) t1 ty2)
345 | Skip t -> Skip (fomega_subst (k+1) t1 t)
346 | Forall (n,kind,t) -> Forall (n,kind,fomega_subst (k+1) t1 t)
347 | TAppl args -> TAppl (List.map (fomega_subst k t1) args)
349 let fomega_lookup status ref = snd (ReferenceMap.find ref status#extraction_db)
351 let rec fomega_whd status ty =
354 (match fomega_lookup status r with
356 | Some ty -> fomega_whd status ty)
357 | TAppl (TConst r::args) ->
358 (match fomega_lookup status r with
360 | Some ty -> fomega_whd status (List.fold_right (fomega_subst 1) args ty))
363 let rec term_of status ~metasenv context =
367 | NCic.Prod _ -> assert false (* IMPOSSIBLE *)
368 | NCic.Lambda (b,ty,bo) ->
369 (* CSC: non-invariant assumed here about "_" *)
370 (match classify status ~metasenv context ty with
372 TLambda (term_of status ~metasenv ((b,NCic.Decl ty)::context) bo)
373 | `KindOrType (* ??? *)
375 Lambda (b, term_of status ~metasenv ((b,NCic.Decl ty)::context) bo)
379 term_of status ~metasenv ((b,NCic.Decl ty)::context) bo
380 | `Term _ -> assert false (* IMPOSSIBLE *))
381 | NCic.LetIn (b,ty,t,bo) ->
382 (match classify status ~metasenv context t with
383 | `Term `TypeFormerOrTerm (* ???? *)
385 LetIn (b,term_of status ~metasenv context t,
386 term_of status ~metasenv ((b,NCic.Def (t,ty))::context) bo)
395 (* not in programming languages, we expand it *)
396 term_of status ~metasenv context
397 (NCicSubstitution.subst status ~avoid_beta_redexes:true t bo))
398 | NCic.Rel n -> Rel n
399 | NCic.Const ref -> Const ref
400 | NCic.Appl (he::args) ->
401 eat_args status metasenv
402 (term_of status ~metasenv context he) context
403 (typ_of status ~metasenv context
404 (NCicTypeChecker.typeof status ~metasenv ~subst:[] context he))
407 let he_context = context_of_typformer status ~metasenv context he in
408 let process_args he =
411 | `Inst tl -> Inst (process_args he tl)
412 | `Appl (arg,tl) -> process_args (Appl (he,... arg)) tl
414 Appl (typ_of status ~metasenv context he ::
415 process_args (typ_of status ~metasenv context he)
416 (skip_term_args status ~metasenv context (List.rev he_context,args))
418 | NCic.Appl _ -> assert false (* TODO: when head is a match/let rec;
419 otherwise NOT A TYPE *)
420 | NCic.Meta _ -> assert false (* TODO *)
421 | NCic.Match (ref,_,t,pl) ->
422 Match (ref,term_of status ~metasenv context t,
423 List.map (term_of status ~metasenv context) pl)
424 and eat_args status metasenv acc context tyhe =
430 Appl args -> Appl (args@[x])
433 match fomega_whd status tyhe with
438 | _ -> term_of status ~metasenv context arg in
439 eat_args status metasenv (mk_appl acc arg) context t tl
441 eat_args status metasenv (Inst acc)
442 context (fomega_subst 1 (typ_of status ~metasenv context arg) t) tl
444 eat_args status metasenv acc context t tl
445 | Top -> assert false (*TODO: HOW??*)
446 | Unit | Var _ | TConst _ | TAppl _ -> assert false (* NOT A PRODUCT *)
449 let obj_of_constant status ~metasenv uri height bo ty =
450 match classify status ~metasenv [] ty with
452 let ty = kind_of status ~metasenv [] ty in
453 let ctx0,res = split_kind_prods [] ty in
455 split_typ_lambdas status ~metasenv (List.length ctx0) [] bo in
456 (match classify status ~metasenv ctx bo with
458 | `KindOrType -> (* ?? no kind formers in System F_omega *)
462 HExtlib.map_option (fun (_,k) ->
463 (*CSC: BUG here, clashes*)
464 String.uncapitalize (fst n),k) p1)
467 (* BUG here: for mutual type definitions the spec is not good *)
469 NReference.reference_of_spec uri (NReference.Def height) in
470 let bo = typ_of status ~metasenv ctx bo in
471 status#set_extraction_db
472 (ReferenceMap.add ref (nicectx,Some bo)
473 status#extraction_db),
474 Some (uri,TypeDefinition((nicectx,res),bo))
475 | `Kind -> status, None
477 | `Proposition -> status, None
478 | `Term _ -> assert false (* IMPOSSIBLE *))
480 | `Proposition -> status, None
481 | `KindOrType (* ??? *)
483 (* CSC: TO BE FINISHED, REF NON REGISTERED *)
484 let ty = typ_of status ~metasenv [] ty in
486 Some (uri, TermDefinition (split_typ_prods [] ty,
487 term_of status ~metasenv [] bo))
488 | `Term _ -> assert false (* IMPOSSIBLE *)
491 let obj_of status (uri,height,metasenv,subst,obj_kind) =
492 let obj_kind = apply_subst subst obj_kind in
495 | NCic.Constant (_,_,None,ty,_) ->
496 (match classify status ~metasenv [] ty with
498 let ty = kind_of status ~metasenv [] ty in
499 let ctx,_ as res = split_kind_prods [] ty in
500 let ref = NReference.reference_of_spec uri NReference.Decl in
501 status#set_extraction_db
502 (ReferenceMap.add ref (ctx,None) status#extraction_db),
503 Some (uri, TypeDeclaration res)
505 | `Proposition -> status, None
507 | `KindOrType (*???*) ->
508 let ty = typ_of status ~metasenv [] ty in
510 Some (uri, TermDeclaration (split_typ_prods [] ty))
511 | `Term _ -> assert false (* IMPOSSIBLE *))
512 | NCic.Constant (_,_,Some bo,ty,_) ->
513 obj_of_constant status ~metasenv uri height bo ty
514 | NCic.Fixpoint (_fix_or_cofix,fs,_) ->
517 (fun (_,_name,_,ty,bo) (status,res) ->
518 let status,obj = obj_of_constant ~metasenv status uri height bo ty in
520 None -> status,res (*CSC: PRETTY PRINT SOMETHING ERASED*)
521 | Some (_uri,obj) -> status,obj::res)
524 status, Some (uri,LetRec objs)
525 | NCic.Inductive _ -> status,None (*CSC: TO BE IMPLEMENTED*)
528 prerr_endline "-- NOT IN F_omega";
531 (************************ HASKELL *************************)
533 (*CSC: code to be changed soon when we implement constructors and
534 we fix the code for term application *)
535 let classify_reference status ref =
536 if ReferenceMap.mem ref status#extraction_db then
541 let capitalize classification name =
542 match classification with
544 | `TypeName -> String.capitalize name
545 | `FunctionName -> String.uncapitalize name
547 let pp_ref status ref =
548 capitalize (classify_reference status ref)
549 (NCicPp.r2s status false ref)
551 let name_of_uri classification uri =
552 capitalize classification (NUri.name_of_uri uri)
554 (* cons avoid duplicates *)
555 let rec (@::) name l =
556 if name <> "" (* propositional things *) && name.[0] = '_' then
557 let name = String.sub name 1 (String.length name - 1) in
558 let name = if name = "" then "a" else name in
560 else if List.mem name l then (name ^ "'") @:: l
567 | KArrow (k1,k2) -> "(" ^ pp_kind k1 ^ ") -> " ^ pp_kind k2
568 | KSkip k -> pp_kind k
570 let rec pp_typ status ctx =
572 Var n -> List.nth ctx (n-1)
574 | Top -> assert false (* ??? *)
575 | TConst ref -> pp_ref status ref
576 | Arrow (t1,t2) -> "(" ^ pp_typ status ctx t1 ^ ") -> " ^ pp_typ status ("_"::ctx) t2
577 | Skip t -> pp_typ status ("_"::ctx) t
578 | Forall (name,_,t) ->
579 (*CSC: BUG HERE: avoid clashes due to uncapitalisation*)
580 let name = String.uncapitalize name in
581 "(forall " ^ name ^ ". " ^ pp_typ status (name@::ctx) t ^")"
582 | TAppl tl -> "(" ^ String.concat " " (List.map (pp_typ status ctx) tl) ^ ")"
584 let rec pp_term status ctx =
586 Rel n -> List.nth ctx (n-1)
588 | Const ref -> pp_ref status ref
589 | Lambda (name,t) -> "(\\" ^ name ^ " -> " ^ pp_term status (name@::ctx) t ^ ")"
590 | Appl tl -> "(" ^ String.concat " " (List.map (pp_term status ctx) tl) ^ ")"
591 | LetIn (name,s,t) ->
592 "(let " ^ name ^ " = " ^ pp_term status ctx s ^ " in " ^ pp_term status (name@::ctx) t ^
594 | Match (r,matched,pl) ->
595 let constructors, leftno =
596 let _,leftno,tys,_,n = NCicEnvironment.get_checked_indtys status r in
597 let _,_,_,cl = List.nth tys n in
600 let rec eat_branch n ty pat =
602 | NCic.Prod (_, _, t), _ when n > 0 ->
603 eat_branch (pred n) t pat
604 | NCic.Prod (_, _, t), Lambda (name, t') ->
605 (*CSC: BUG HERE; WHAT IF SOME ARGUMENTS ARE DELETED?*)
606 let cv, rhs = eat_branch 0 t t' in
614 (fun (_, name, ty) pat ->
616 name, eat_branch leftno ty pat
618 with Invalid_argument _ -> assert false
620 "case " ^ pp_term status ctx matched ^ " of\n" ^
623 (fun (name,(bound_names,rhs)) ->
625 (*CSC: BUG avoid name clashes *)
626 String.concat " " (String.capitalize name::bound_names),
627 pp_term status (bound_names@ctx) rhs
629 " " ^ pattern ^ " -> " ^ body
631 | TLambda t -> pp_term status ctx t
632 | Inst t -> pp_term status ctx t
635 type term_context = (string * [`OfKind of kind | `OfType of typ]) option list
637 type term_former_def = term_context * term * typ
638 type term_former_decl = term_context * typ
641 let rec pp_obj status (uri,obj_kind) =
643 String.concat " " (List.rev
644 (List.fold_right (fun (x,_) l -> x@::l)
645 (HExtlib.filter_map (fun x -> x) ctx) [])) in
646 let namectx_of_ctx ctx =
647 List.fold_right (@::)
648 (List.map (function None -> "" | Some (x,_) -> x) ctx) [] in
650 TypeDeclaration (ctx,_) ->
651 (* data?? unsure semantics: inductive type without constructor, but
652 not matchable apparently *)
653 "data " ^ name_of_uri `TypeName uri ^ " " ^ pp_ctx ctx
654 | TypeDefinition ((ctx,_),ty) ->
655 let namectx = namectx_of_ctx ctx in
656 "type " ^ name_of_uri `TypeName uri ^ " " ^ pp_ctx ctx ^ " = " ^
657 pp_typ status namectx ty
658 | TermDeclaration (ctx,ty) ->
659 (* Implemented with undefined, the best we can do *)
660 let name = name_of_uri `FunctionName uri in
661 name ^ " :: " ^ pp_typ status [] (glue_ctx_typ ctx ty) ^ "\n" ^
662 name ^ " = undefined"
663 | TermDefinition ((ctx,ty),bo) ->
664 let name = name_of_uri `FunctionName uri in
665 let namectx = namectx_of_ctx ctx in
666 name ^ " :: " ^ pp_typ status ["a";"b";"c"] (glue_ctx_typ ctx ty) ^ "\n" ^
667 name ^ " = " ^ pp_term status namectx bo
669 (*CSC: BUG always uses the name of the URI *)
670 String.concat "\n" (List.map (fun obj -> pp_obj status (uri,obj)) l)
671 (* inductive and records missing *)
673 let haskell_of_obj status obj =
674 let status, obj = obj_of status obj in
677 None -> "-- ERASED\n"
678 | Some obj -> pp_obj status obj ^ "\n"
681 let rec typ_of context =
687 (match get_nth context n with
688 Some (C.Name s,_) -> ppid s
689 | Some (C.Anonymous,_) -> "__" ^ string_of_int n
690 | None -> "_hidden_" ^ string_of_int n
693 NotEnoughElements -> string_of_int (List.length context - n)
698 "?" ^ (string_of_int n) ^ "[" ^
703 | Some t -> pp ~in_type:false t context) l1) ^
707 let _,context,_ = CicUtil.lookup_meta n metasenv in
708 "?" ^ (string_of_int n) ^ "[" ^
716 | Some _, Some t -> pp ~in_type:false t context
720 CicUtil.Meta_not_found _
721 | Invalid_argument _ ->
722 "???" ^ (string_of_int n) ^ "[" ^
724 (List.rev_map (function None -> "_" | Some t ->
725 pp ~in_type:false t context) l1) ^
733 (*| C.Type u -> ("Type" ^ CicUniv.string_of_universe u)*)
734 | C.CProp _ -> "CProp"
736 | C.Implicit (Some `Hole) -> "%"
737 | C.Implicit _ -> "?"
739 (match b, is_term s with
740 _, true -> typ_of (None::context) t
741 | "_",_ -> Arrow (typ_of context s) (typ_of (Some b::context) t)
742 | _,_ -> Forall (b,typ_of (Some b::context) t)
743 | C.Lambda (b,s,t) ->
744 (match analyze_type context s with
746 | `Statement -> pp ~in_type t ((Some (b,Cic.Decl s))::context)
747 | `Optimize -> prerr_endline "XXX lambda";assert false
749 "(function " ^ ppname b ^ " -> " ^
750 pp ~in_type t ((Some (b,Cic.Decl s))::context) ^ ")")
751 | C.LetIn (b,s,ty,t) ->
752 (match analyze_term context s with
754 | `Proof -> pp ~in_type t ((Some (b,Cic.Def (s,ty)))::context)
757 "(let " ^ ppname b ^ (*" : " ^ pp ~in_type:true ty context ^*)
758 " = " ^ pp ~in_type:false s context ^ " in " ^
759 pp ~in_type t ((Some (b,Cic.Def (s,ty)))::context) ^ ")")
760 | C.Appl (he::tl) when in_type ->
761 let hes = pp ~in_type he context in
762 let stl = String.concat "," (clean_args_for_ty context tl) in
763 (if stl = "" then "" else "(" ^ stl ^ ") ") ^ hes
764 | C.Appl (C.MutInd _ as he::tl) ->
765 let hes = pp ~in_type he context in
766 let stl = String.concat "," (clean_args_for_ty context tl) in
767 (if stl = "" then "" else "(" ^ stl ^ ") ") ^ hes
768 | C.Appl (C.MutConstruct (uri,n,_,_) as he::tl) ->
770 match fst(CicEnvironment.get_obj CicUniv.oblivion_ugraph uri) with
771 C.InductiveDefinition (_,_,nparams,_) -> nparams
772 | _ -> assert false in
773 let hes = pp ~in_type he context in
774 let stl = String.concat "," (clean_args_for_constr nparams context tl) in
775 "(" ^ hes ^ (if stl = "" then "" else "(" ^ stl ^ ")") ^ ")"
777 "(" ^ String.concat " " (clean_args context li) ^ ")"
778 | C.Const (uri,exp_named_subst) ->
779 qualified_name_of_uri status current_module_uri uri ^
780 pp_exp_named_subst exp_named_subst context
781 | C.MutInd (uri,n,exp_named_subst) ->
783 match fst(CicEnvironment.get_obj CicUniv.oblivion_ugraph uri) with
784 C.InductiveDefinition (dl,_,_,_) ->
785 let (name,_,_,_) = get_nth dl (n+1) in
786 qualified_name_of_uri status current_module_uri
787 (UriManager.uri_of_string
788 (UriManager.buri_of_uri uri ^ "/" ^ name ^ ".con")) ^
789 pp_exp_named_subst exp_named_subst context
790 | _ -> raise CicExportationInternalError
792 Sys.Break as exn -> raise exn
793 | _ -> UriManager.string_of_uri uri ^ "#1/" ^ string_of_int (n + 1)
795 | C.MutConstruct (uri,n1,n2,exp_named_subst) ->
797 match fst(CicEnvironment.get_obj CicUniv.oblivion_ugraph uri) with
798 C.InductiveDefinition (dl,_,_,_) ->
799 let _,_,_,cons = get_nth dl (n1+1) in
800 let id,_ = get_nth cons n2 in
801 qualified_name_of_uri status current_module_uri ~capitalize:true
802 (UriManager.uri_of_string
803 (UriManager.buri_of_uri uri ^ "/" ^ id ^ ".con")) ^
804 pp_exp_named_subst exp_named_subst context
805 | _ -> raise CicExportationInternalError
807 Sys.Break as exn -> raise exn
809 UriManager.string_of_uri uri ^ "#1/" ^ string_of_int (n1 + 1) ^ "/" ^
812 | C.MutCase (uri,n1,ty,te,patterns) ->
814 "unit (* TOO POLYMORPHIC TYPE *)"
816 let rec needs_obj_magic ty =
817 match CicReduction.whd context ty with
818 | Cic.Lambda (_,_,(Cic.Lambda(_,_,_) as t)) -> needs_obj_magic t
819 | Cic.Lambda (_,_,t) -> not (DoubleTypeInference.does_not_occur 1 t)
820 | _ -> false (* it can be a Rel, e.g. in *_rec *)
822 let needs_obj_magic = needs_obj_magic ty in
823 (match analyze_term context te with
824 `Type -> assert false
827 [] -> "assert false" (* empty type elimination *)
829 pp ~in_type:false he context (* singleton elimination *)
833 if patterns = [] then "assert false"
835 (let connames_and_argsno, go_up, go_pu, go_down, go_nwod =
836 (match fst(CicEnvironment.get_obj CicUniv.oblivion_ugraph uri) with
837 C.InductiveDefinition (dl,_,paramsno,_) ->
838 let (_,_,_,cons) = get_nth dl (n1+1) in
842 (* this is just an approximation since we do not have
844 let rec count_prods toskip =
846 C.Prod (_,_,bo) when toskip > 0 ->
847 count_prods (toskip - 1) bo
848 | C.Prod (_,_,bo) -> 1 + count_prods 0 bo
851 qualified_name_of_uri status current_module_uri
853 (UriManager.uri_of_string
854 (UriManager.buri_of_uri uri ^ "/" ^ id ^ ".con")),
855 count_prods paramsno ty
858 if not (is_mcu_type uri) then rc, "","","",""
859 else rc, !current_go_up, "))", "( .< (", " ) >.)"
860 | _ -> raise CicExportationInternalError
863 let connames_and_argsno_and_patterns =
867 | (x,no)::tlx,y::tly -> (x,no,y)::(combine (tlx,tly))
868 | _,_ -> assert false
870 combine (connames_and_argsno,patterns)
873 "\n(match " ^ pp ~in_type:false te context ^ " with \n " ^
874 (String.concat "\n | "
877 let rec aux argsno context =
879 Cic.Lambda (name,ty,bo) when argsno > 0 ->
882 Cic.Anonymous -> Cic.Anonymous
883 | Cic.Name n -> Cic.Name (ppid n) in
885 aux (argsno - 1) (Some (name,Cic.Decl ty)::context)
888 (match analyze_type context ty with
889 | `Optimize -> prerr_endline "XXX contructor with l2 arg"; assert false
891 | `Sort _ -> args,res
895 | C.Name s -> s)::args,res)
896 | t when argsno = 0 -> [],pp ~in_type:false t context
898 ["{" ^ string_of_int argsno ^ " args missing}"],
899 pp ~in_type:false t context
902 if argsno = 0 then x,pp ~in_type:false y context
904 let args,body = aux argsno context y in
905 let sargs = String.concat "," args in
906 x ^ (if sargs = "" then "" else "(" ^ sargs^ ")"),
909 pattern ^ " -> " ^ go_down ^
910 (if needs_obj_magic then
911 "Obj.magic (" ^ body ^ ")"
914 ) connames_and_argsno_and_patterns)) ^
916 | C.Fix (no, funs) ->
919 (fun (types,len) (n,_,ty,_) ->
920 (Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::types,
926 (fun (name,ind,ty,bo) i -> name ^ " = \n" ^
927 pp ~in_type:false bo (names@context) ^ i)
930 (match get_nth names (no + 1) with
931 Some (Cic.Name n,_) -> n
933 | C.CoFix (no,funs) ->
936 (fun (types,len) (n,ty,_) ->
937 (Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::types,
943 (fun (name,ty,bo) i -> "\n" ^ name ^
944 " : " ^ pp ~in_type:true ty context ^ " := \n" ^
945 pp ~in_type:false bo (names@context) ^ i)
951 exception CicExportationInternalError;;
952 exception NotEnoughElements;;
957 UriManager.eq (UriManager.uri_of_string
958 "cic:/matita/freescale/opcode/mcu_type.ind") u
961 (* Utility functions *)
963 let analyze_term context t =
964 match fst(CicTypeChecker.type_of_aux' [] context t CicUniv.oblivion_ugraph)with
965 | Cic.Sort _ -> `Type
966 | Cic.MutInd (u,0,_) when is_mcu_type u -> `Optimize
969 fst (CicTypeChecker.type_of_aux' [] context ty CicUniv.oblivion_ugraph)
971 | Cic.Sort Cic.Prop -> `Proof
975 let analyze_type context t =
978 Cic.Sort s -> `Sort s
979 | Cic.MutInd (u,0,_) when is_mcu_type u -> `Optimize
980 | Cic.Prod (_,_,t) -> aux t
981 | _ -> `SomethingElse
984 `Sort _ | `Optimize as res -> res
987 fst(CicTypeChecker.type_of_aux' [] context t CicUniv.oblivion_ugraph)
989 Cic.Sort Cic.Prop -> `Statement
1003 let n = String.uncapitalize n in
1004 if List.mem n reserved then n ^ "_" else n
1009 Cic.Name s -> ppid s
1010 | Cic.Anonymous -> "_"
1013 (* get_nth l n returns the nth element of the list l if it exists or *)
1014 (* raises NotEnoughElements if l has less than n elements *)
1015 let rec get_nth l n =
1018 | (n, he::tail) when n > 1 -> get_nth tail (n-1)
1019 | (_,_) -> raise NotEnoughElements
1022 let qualified_name_of_uri status current_module_uri ?(capitalize=false) uri =
1025 String.capitalize (UriManager.name_of_uri status uri)
1027 ppid (UriManager.name_of_uri status uri) in
1029 let suri = UriManager.buri_of_uri uri in
1030 let s = String.sub suri 5 (String.length suri - 5) in
1031 let s = Pcre.replace ~pat:"/" ~templ:"_" s in
1032 String.uncapitalize s in
1033 if current_module_uri = UriManager.buri_of_uri uri then
1036 String.capitalize filename ^ "." ^ name
1039 let current_go_up = ref "(.!(";;
1042 current_go_up := "(.~(";
1044 current_go_up := "(.!(";
1047 current_go_up := "(.!(";
1051 let pp current_module_uri ?metasenv ~in_type =
1052 let rec pp ~in_type t context =
1053 let module C = Cic in
1058 (match get_nth context n with
1059 Some (C.Name s,_) -> ppid s
1060 | Some (C.Anonymous,_) -> "__" ^ string_of_int n
1061 | None -> "_hidden_" ^ string_of_int n
1064 NotEnoughElements -> string_of_int (List.length context - n)
1066 | C.Var (uri,exp_named_subst) ->
1067 qualified_name_of_uri status current_module_uri uri ^
1068 pp_exp_named_subst exp_named_subst context
1070 (match metasenv with
1072 "?" ^ (string_of_int n) ^ "[" ^
1077 | Some t -> pp ~in_type:false t context) l1) ^
1081 let _,context,_ = CicUtil.lookup_meta n metasenv in
1082 "?" ^ (string_of_int n) ^ "[" ^
1090 | Some _, Some t -> pp ~in_type:false t context
1094 CicUtil.Meta_not_found _
1095 | Invalid_argument _ ->
1096 "???" ^ (string_of_int n) ^ "[" ^
1098 (List.rev_map (function None -> "_" | Some t ->
1099 pp ~in_type:false t context) l1) ^
1106 | C.Type _ -> "Type"
1107 (*| C.Type u -> ("Type" ^ CicUniv.string_of_universe u)*)
1108 | C.CProp _ -> "CProp"
1110 | C.Implicit (Some `Hole) -> "%"
1111 | C.Implicit _ -> "?"
1115 let n = "'" ^ String.uncapitalize n in
1116 "(" ^ pp ~in_type:true s context ^ " -> " ^
1117 pp ~in_type:true t ((Some (Cic.Name n,Cic.Decl s))::context) ^ ")"
1119 "(" ^ pp ~in_type:true s context ^ " -> " ^
1120 pp ~in_type:true t ((Some (b,Cic.Decl s))::context) ^ ")")
1121 | C.Cast (v,t) -> pp ~in_type v context
1122 | C.Lambda (b,s,t) ->
1123 (match analyze_type context s with
1125 | `Statement -> pp ~in_type t ((Some (b,Cic.Decl s))::context)
1126 | `Optimize -> prerr_endline "XXX lambda";assert false
1128 "(function " ^ ppname b ^ " -> " ^
1129 pp ~in_type t ((Some (b,Cic.Decl s))::context) ^ ")")
1130 | C.LetIn (b,s,ty,t) ->
1131 (match analyze_term context s with
1133 | `Proof -> pp ~in_type t ((Some (b,Cic.Def (s,ty)))::context)
1136 "(let " ^ ppname b ^ (*" : " ^ pp ~in_type:true ty context ^*)
1137 " = " ^ pp ~in_type:false s context ^ " in " ^
1138 pp ~in_type t ((Some (b,Cic.Def (s,ty)))::context) ^ ")")
1139 | C.Appl (he::tl) when in_type ->
1140 let hes = pp ~in_type he context in
1141 let stl = String.concat "," (clean_args_for_ty context tl) in
1142 (if stl = "" then "" else "(" ^ stl ^ ") ") ^ hes
1143 | C.Appl (C.MutInd _ as he::tl) ->
1144 let hes = pp ~in_type he context in
1145 let stl = String.concat "," (clean_args_for_ty context tl) in
1146 (if stl = "" then "" else "(" ^ stl ^ ") ") ^ hes
1147 | C.Appl (C.MutConstruct (uri,n,_,_) as he::tl) ->
1149 match fst(CicEnvironment.get_obj CicUniv.oblivion_ugraph uri) with
1150 C.InductiveDefinition (_,_,nparams,_) -> nparams
1151 | _ -> assert false in
1152 let hes = pp ~in_type he context in
1153 let stl = String.concat "," (clean_args_for_constr nparams context tl) in
1154 "(" ^ hes ^ (if stl = "" then "" else "(" ^ stl ^ ")") ^ ")"
1156 "(" ^ String.concat " " (clean_args context li) ^ ")"
1157 | C.Const (uri,exp_named_subst) ->
1158 qualified_name_of_uri status current_module_uri uri ^
1159 pp_exp_named_subst exp_named_subst context
1160 | C.MutInd (uri,n,exp_named_subst) ->
1162 match fst(CicEnvironment.get_obj CicUniv.oblivion_ugraph uri) with
1163 C.InductiveDefinition (dl,_,_,_) ->
1164 let (name,_,_,_) = get_nth dl (n+1) in
1165 qualified_name_of_uri status current_module_uri
1166 (UriManager.uri_of_string
1167 (UriManager.buri_of_uri uri ^ "/" ^ name ^ ".con")) ^
1168 pp_exp_named_subst exp_named_subst context
1169 | _ -> raise CicExportationInternalError
1171 Sys.Break as exn -> raise exn
1172 | _ -> UriManager.string_of_uri uri ^ "#1/" ^ string_of_int (n + 1)
1174 | C.MutConstruct (uri,n1,n2,exp_named_subst) ->
1176 match fst(CicEnvironment.get_obj CicUniv.oblivion_ugraph uri) with
1177 C.InductiveDefinition (dl,_,_,_) ->
1178 let _,_,_,cons = get_nth dl (n1+1) in
1179 let id,_ = get_nth cons n2 in
1180 qualified_name_of_uri status current_module_uri ~capitalize:true
1181 (UriManager.uri_of_string
1182 (UriManager.buri_of_uri uri ^ "/" ^ id ^ ".con")) ^
1183 pp_exp_named_subst exp_named_subst context
1184 | _ -> raise CicExportationInternalError
1186 Sys.Break as exn -> raise exn
1188 UriManager.string_of_uri uri ^ "#1/" ^ string_of_int (n1 + 1) ^ "/" ^
1191 | C.MutCase (uri,n1,ty,te,patterns) ->
1193 "unit (* TOO POLYMORPHIC TYPE *)"
1195 let rec needs_obj_magic ty =
1196 match CicReduction.whd context ty with
1197 | Cic.Lambda (_,_,(Cic.Lambda(_,_,_) as t)) -> needs_obj_magic t
1198 | Cic.Lambda (_,_,t) -> not (DoubleTypeInference.does_not_occur 1 t)
1199 | _ -> false (* it can be a Rel, e.g. in *_rec *)
1201 let needs_obj_magic = needs_obj_magic ty in
1202 (match analyze_term context te with
1203 `Type -> assert false
1205 (match patterns with
1206 [] -> "assert false" (* empty type elimination *)
1208 pp ~in_type:false he context (* singleton elimination *)
1209 | _ -> assert false)
1212 if patterns = [] then "assert false"
1214 (let connames_and_argsno, go_up, go_pu, go_down, go_nwod =
1215 (match fst(CicEnvironment.get_obj CicUniv.oblivion_ugraph uri) with
1216 C.InductiveDefinition (dl,_,paramsno,_) ->
1217 let (_,_,_,cons) = get_nth dl (n1+1) in
1221 (* this is just an approximation since we do not have
1223 let rec count_prods toskip =
1225 C.Prod (_,_,bo) when toskip > 0 ->
1226 count_prods (toskip - 1) bo
1227 | C.Prod (_,_,bo) -> 1 + count_prods 0 bo
1230 qualified_name_of_uri status current_module_uri
1232 (UriManager.uri_of_string
1233 (UriManager.buri_of_uri uri ^ "/" ^ id ^ ".con")),
1234 count_prods paramsno ty
1237 if not (is_mcu_type uri) then rc, "","","",""
1238 else rc, !current_go_up, "))", "( .< (", " ) >.)"
1239 | _ -> raise CicExportationInternalError
1242 let connames_and_argsno_and_patterns =
1246 | (x,no)::tlx,y::tly -> (x,no,y)::(combine (tlx,tly))
1247 | _,_ -> assert false
1249 combine (connames_and_argsno,patterns)
1252 "\n(match " ^ pp ~in_type:false te context ^ " with \n " ^
1253 (String.concat "\n | "
1255 (fun (x,argsno,y) ->
1256 let rec aux argsno context =
1258 Cic.Lambda (name,ty,bo) when argsno > 0 ->
1261 Cic.Anonymous -> Cic.Anonymous
1262 | Cic.Name n -> Cic.Name (ppid n) in
1264 aux (argsno - 1) (Some (name,Cic.Decl ty)::context)
1267 (match analyze_type context ty with
1268 | `Optimize -> prerr_endline "XXX contructor with l2 arg"; assert false
1270 | `Sort _ -> args,res
1274 | C.Name s -> s)::args,res)
1275 | t when argsno = 0 -> [],pp ~in_type:false t context
1277 ["{" ^ string_of_int argsno ^ " args missing}"],
1278 pp ~in_type:false t context
1281 if argsno = 0 then x,pp ~in_type:false y context
1283 let args,body = aux argsno context y in
1284 let sargs = String.concat "," args in
1285 x ^ (if sargs = "" then "" else "(" ^ sargs^ ")"),
1288 pattern ^ " -> " ^ go_down ^
1289 (if needs_obj_magic then
1290 "Obj.magic (" ^ body ^ ")"
1293 ) connames_and_argsno_and_patterns)) ^
1295 | C.Fix (no, funs) ->
1298 (fun (types,len) (n,_,ty,_) ->
1299 (Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::types,
1305 (fun (name,ind,ty,bo) i -> name ^ " = \n" ^
1306 pp ~in_type:false bo (names@context) ^ i)
1309 (match get_nth names (no + 1) with
1310 Some (Cic.Name n,_) -> n
1311 | _ -> assert false)
1312 | C.CoFix (no,funs) ->
1315 (fun (types,len) (n,ty,_) ->
1316 (Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::types,
1322 (fun (name,ty,bo) i -> "\n" ^ name ^
1323 " : " ^ pp ~in_type:true ty context ^ " := \n" ^
1324 pp ~in_type:false bo (names@context) ^ i)
1327 and pp_exp_named_subst exp_named_subst context =
1328 if exp_named_subst = [] then "" else
1330 String.concat " ; " (
1332 (function (uri,t) -> UriManager.name_of_uri status uri ^ " \\Assign " ^ pp ~in_type:false t context)
1335 and clean_args_for_constr nparams context =
1336 let nparams = ref nparams in
1340 match analyze_term context t with
1341 `Term when !nparams < 0 -> Some (pp ~in_type:false t context)
1346 and clean_args context =
1348 | [] | [_] -> assert false
1349 | he::arg1::tl as l ->
1350 let head_arg1, rest =
1351 match analyze_term context arg1 with
1353 !current_go_up :: pp ~in_type:false he context ::
1354 pp ~in_type:false arg1 context :: ["))"], tl
1360 match analyze_term context t with
1361 | `Term -> Some (pp ~in_type:false t context)
1363 prerr_endline "XXX function taking twice (or not as first) a l2 term"; assert false
1365 | `Proof -> None) rest
1366 and clean_args_for_ty context =
1369 match analyze_term context t with
1370 `Type -> Some (pp ~in_type:true t context)
1378 let ppty current_module_uri =
1379 (* nparams is the number of left arguments
1380 left arguments should either become parameters or be skipped altogether *)
1381 let rec args nparams context =
1386 Cic.Anonymous -> Cic.Anonymous
1387 | Cic.Name n -> Cic.Name (String.uncapitalize n)
1389 (match analyze_type context s with
1393 args (nparams - 1) ((Some (n,Cic.Decl s))::context) t
1394 | `Type when nparams > 0 ->
1395 args (nparams - 1) ((Some (n,Cic.Decl s))::context) t
1398 args (nparams - 1) ((Some (n,Cic.Decl s))::context) t in
1399 abstr,pp ~in_type:true current_module_uri s context::args
1400 | `Sort _ when nparams <= 0 ->
1401 let n = Cic.Name "unit (* EXISTENTIAL TYPE *)" in
1402 args (nparams - 1) ((Some (n,Cic.Decl s))::context) t
1406 Cic.Anonymous -> Cic.Anonymous
1407 | Cic.Name name -> Cic.Name ("'" ^ name) in
1409 args (nparams - 1) ((Some (n,Cic.Decl s))::context) t
1412 Cic.Anonymous -> abstr
1413 | Cic.Name name -> name::abstr),
1420 exception DoNotExtract;;
1422 let pp_abstracted_ty current_module_uri =
1423 let rec args context =
1425 Cic.Lambda (n,s,t) ->
1428 Cic.Anonymous -> Cic.Anonymous
1429 | Cic.Name n -> Cic.Name (String.uncapitalize n)
1431 (match analyze_type context s with
1436 args ((Some (n,Cic.Decl s))::context) t
1440 Cic.Anonymous -> Cic.Anonymous
1441 | Cic.Name name -> Cic.Name ("'" ^ name) in
1443 args ((Some (n,Cic.Decl s))::context) t
1446 Cic.Anonymous -> abstr
1447 | Cic.Name name -> name::abstr),
1450 match analyze_type context ty with
1452 prerr_endline "XXX abstracted l2 ty"; assert false
1454 | `Statement -> raise DoNotExtract
1456 (* BUG HERE: this can be a real System F type *)
1457 let head = pp ~in_type:true current_module_uri ty context in
1464 (* ppinductiveType (typename, inductive, arity, cons) *)
1465 (* pretty-prints a single inductive definition *)
1466 (* (typename, inductive, arity, cons) *)
1467 let ppinductiveType current_module_uri nparams (typename, inductive, arity, cons)
1469 match analyze_type [] arity with
1470 `Sort Cic.Prop -> ""
1473 | `Type -> assert false
1476 "type " ^ String.uncapitalize typename ^ " = unit (* empty type *)\n"
1480 (fun (id,ty) (_abstr,i) -> (* we should verify _abstr = abstr' *)
1481 let abstr',sargs = ppty current_module_uri nparams [] ty in
1482 let sargs = String.concat " * " sargs in
1484 String.capitalize id ^
1485 (if sargs = "" then "" else " of " ^ sargs) ^
1486 (if i = "" then "" else "\n | ") ^ i)
1490 let s = String.concat "," abstr in
1491 if s = "" then "" else "(" ^ s ^ ") "
1493 "type " ^ abstr ^ String.uncapitalize typename ^ " =\n" ^ scons ^ "\n")
1496 let ppobj current_module_uri obj =
1497 let module C = Cic in
1498 let module U = UriManager in
1499 let pp ~in_type = pp ~in_type current_module_uri in
1501 C.Constant (name, Some t1, t2, params, _) ->
1502 (match analyze_type [] t2 with
1508 | Cic.Lambda (Cic.Name arg, s, t) ->
1509 (match analyze_type [] s with
1512 "let " ^ ppid name ^ "__1 = function " ^ ppid arg
1514 at_level2 (pp ~in_type:false t) [Some (Cic.Name arg, Cic.Decl s)]
1516 ^ "let " ^ ppid name ^ "__2 = ref ([] : (unit list*unit list) list);;\n"
1517 ^ "let " ^ ppid name ^ " = function " ^ ppid arg
1518 ^ " -> (try ignore (List.assoc "^ppid arg^" (Obj.magic !"^ppid name
1519 ^"__2)) with Not_found -> "^ppid name^"__2 := (Obj.magic ("
1520 ^ ppid arg^",.! ("^ppid name^"__1 "^ppid arg^")))::!"
1521 ^ppid name^"__2); .< List.assoc "^ppid arg^" (Obj.magic (!"
1522 ^ppid name^"__2)) >.\n;;\n"
1523 ^" let xxx = prerr_endline \""^ppid name^"\"; .!("^ppid
1524 name^" Matita_freescale_opcode.HCS08)"
1526 "let " ^ ppid name ^ " =\n" ^ pp ~in_type:false t1 [] ^ "\n")
1527 | _ -> "let " ^ ppid name ^ " =\n" ^ pp ~in_type:false t1 [] ^ "\n")
1529 match analyze_type [] t1 with
1530 `Sort Cic.Prop -> ""
1531 | `Optimize -> prerr_endline "XXX aliasing l2 type"; assert false
1534 let abstr,res = pp_abstracted_ty current_module_uri [] t1 in
1536 let s = String.concat "," abstr in
1537 if s = "" then "" else "(" ^ s ^ ") "
1539 "type " ^ abstr ^ ppid name ^ " = " ^ res ^ "\n"
1541 DoNotExtract -> ""))
1542 | C.Constant (name, None, ty, params, _) ->
1543 (match analyze_type [] ty with
1545 | `Optimize -> prerr_endline "XXX axiom l2"; assert false
1547 | `Sort _ -> "type " ^ ppid name ^ "\n"
1548 | `Type -> "let " ^ ppid name ^ " = assert false\n")
1549 | C.Variable (name, bo, ty, params, _) ->
1550 "Variable " ^ name ^
1551 "(" ^ String.concat ";" (List.map UriManager.string_of_uri params) ^
1553 pp ~in_type:true ty [] ^ "\n" ^
1554 (match bo with None -> "" | Some bo -> ":= " ^ pp ~in_type:false bo [])
1555 | C.CurrentProof (name, conjectures, value, ty, params, _) ->
1556 "Current Proof of " ^ name ^
1557 "(" ^ String.concat ";" (List.map UriManager.string_of_uri params) ^
1559 let separate s = if s = "" then "" else s ^ " ; " in
1561 (fun (n, context, t) i ->
1562 let conjectures',name_context =
1564 (fun context_entry (i,name_context) ->
1565 (match context_entry with
1566 Some (n,C.Decl at) ->
1569 pp ~in_type:true ~metasenv:conjectures
1570 at name_context ^ " ",
1571 context_entry::name_context
1572 | Some (n,C.Def (at,aty)) ->
1575 pp ~in_type:true ~metasenv:conjectures
1577 ":= " ^ pp ~in_type:false
1578 ~metasenv:conjectures at name_context ^ " ",
1579 context_entry::name_context
1581 (separate i) ^ "_ :? _ ", context_entry::name_context)
1584 conjectures' ^ " |- " ^ "?" ^ (string_of_int n) ^ ": " ^
1585 pp ~in_type:true ~metasenv:conjectures t name_context ^ "\n" ^ i
1587 "\n" ^ pp ~in_type:false ~metasenv:conjectures value [] ^ " : " ^
1588 pp ~in_type:true ~metasenv:conjectures ty []
1589 | C.InductiveDefinition (l, params, nparams, _) ->
1591 (fun x i -> ppinductiveType current_module_uri nparams x ^ i) l ""
1594 let ppobj current_module_uri obj =
1595 let res = ppobj current_module_uri obj in
1596 if res = "" then "" else res ^ ";;\n\n"