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 *)
39 let rec size_of_kind =
42 | KArrow (l, r) -> 1 + size_of_kind l + size_of_kind r
43 | KSkip k -> size_of_kind k
46 let bracket size_of pp o =
53 let rec pretty_print_kind =
56 | KArrow (l, r) -> bracket size_of_kind pretty_print_kind l ^ " -> " ^ pretty_print_kind r
57 | KSkip k -> pretty_print_kind k
64 | TConst of typformerreference
67 | Forall of string * kind * typ
70 let rec size_of_type =
77 | Skip t -> size_of_type t
86 | Lambda of string * (* typ **) term
88 | LetIn of string * (* typ **) term * term
89 | Match of reference * term * term list
90 | TLambda of (* string **) term
91 | Inst of (*typ_former **) term
94 let rec size_of_term =
99 | Lambda (name, body) -> 1 + size_of_term body
100 | Appl l -> List.length l
101 | LetIn (name, def, body) -> 1 + size_of_term def + size_of_term body
102 | Match (name, case, pats) -> 1 + size_of_term case + List.length pats
103 | TLambda t -> size_of_term t
104 | Inst t -> size_of_term t
107 NCic.Const (NReference.reference_of_spec (NUri.uri_of_string "cic:/matita/basics/types/unit.ind") (NReference.Ind (true,0,0)));;
109 (* None = dropped abstraction *)
110 type typ_context = (string * kind) option list
111 type term_context = (string * [`OfKind of kind | `OfType of typ]) option list
113 type typ_former_decl = typ_context * kind
114 type typ_former_def = typ_former_decl * typ
116 type term_former_decl = term_context * typ
117 type term_former_def = term_former_decl * term
120 TypeDeclaration of typ_former_decl
121 | TypeDefinition of typ_former_def
122 | TermDeclaration of term_former_decl
123 | TermDefinition of term_former_def
124 | LetRec of obj_kind list
125 (* name, left, right, constructors *)
126 | Algebraic of (string * typ_context * typ_context * (string * typ) list) list
128 type obj = NUri.uri * obj_kind
130 let rec classify_not_term status context t =
131 match NCicReduction.whd status ~subst:[] context t with
135 | NCic.Type [`CProp,_] -> `PropKind
136 | NCic.Type [`Type,_] -> `Kind
137 | NCic.Type _ -> assert false)
138 | NCic.Prod (b,s,t) ->
139 (*CSC: using invariant on "_" *)
140 classify_not_term status ((b,NCic.Decl s)::context) t
144 | NCic.Const (NReference.Ref (_,NReference.CoFix _))
145 | NCic.Appl [] -> assert false (* NOT POSSIBLE *)
147 | NCic.Const (NReference.Ref (_,NReference.Fix _)) ->
148 (* be aware: we can be the head of an application *)
149 assert false (* TODO *)
150 | NCic.Meta _ -> assert false (* TODO *)
151 | NCic.Appl (he::_) -> classify_not_term status context he
153 let rec find_sort typ =
154 match NCicReduction.whd status ~subst:[] context (NCicSubstitution.lift status n typ) with
155 NCic.Sort NCic.Prop -> `Proposition
156 | NCic.Sort (NCic.Type [`CProp,_]) -> `Proposition
157 | NCic.Sort (NCic.Type [`Type,_]) ->
158 (*CSC: we could be more precise distinguishing the user provided
159 minimal elements of the hierarchies and classify these
162 | NCic.Sort (NCic.Type _) -> assert false (* ALGEBRAIC *)
163 | NCic.Prod (_,_,t) ->
164 (* we skipped arguments of applications, so here we need to skip
167 | _ -> assert false (* NOT A SORT *)
169 (match List.nth context (n-1) with
170 _,NCic.Decl typ -> find_sort typ
171 | _,NCic.Def _ -> assert false (* IMPOSSIBLE *))
172 | NCic.Const (NReference.Ref (_,NReference.Decl) as ref) ->
173 let _,_,ty,_,_ = NCicEnvironment.get_checked_decl status ref in
174 (match classify_not_term status [] ty with
176 | `Type -> assert false (* IMPOSSIBLE *)
178 | `KindOrType -> `Type
179 | `PropKind -> `Proposition)
180 | NCic.Const (NReference.Ref (_,NReference.Ind _) as ref) ->
181 let _,_,ityl,_,i = NCicEnvironment.get_checked_indtys status ref in
182 let _,_,arity,_ = List.nth ityl i in
183 (match classify_not_term status [] arity with
186 | `KindOrType -> assert false (* IMPOSSIBLE *)
188 | `PropKind -> `Proposition)
189 | NCic.Const (NReference.Ref (_,NReference.Con _))
190 | NCic.Const (NReference.Ref (_,NReference.Def _)) ->
191 assert false (* IMPOSSIBLE *)
194 type not_term = [`Kind | `KindOrType | `PropKind | `Proposition | `Type];;
196 let classify status ~metasenv context t =
197 match NCicTypeChecker.typeof status ~metasenv ~subst:[] context t with
199 (classify_not_term status context t : not_term :> [> not_term])
201 let ty = fix_sorts ty in
203 (match classify_not_term status context ty with
204 | `Proposition -> `Proof
206 | `KindOrType -> `TypeFormerOrTerm
207 | `Kind -> `TypeFormer
208 | `PropKind -> `PropFormer)
212 let rec kind_of status ~metasenv context k =
213 match NCicReduction.whd status ~subst:[] context k with
214 | NCic.Sort NCic.Type _ -> Type
215 | NCic.Sort _ -> assert false (* NOT A KIND *)
216 | NCic.Prod (b,s,t) ->
217 (match classify status ~metasenv context s with
219 KArrow (kind_of status ~metasenv context s,
220 kind_of ~metasenv status ((b,NCic.Decl s)::context) t)
225 KSkip (kind_of status ~metasenv ((b,NCic.Decl s)::context) t)
226 | `Term _ -> assert false (* IMPOSSIBLE *))
228 | NCic.LetIn _ -> assert false (* IMPOSSIBLE *)
231 | NCic.Const _ -> assert false (* NOT A KIND *)
232 | NCic.Appl _ -> assert false (* TODO: when head is a match/let rec;
233 otherwise NOT A KIND *)
235 | NCic.Match (_,_,_,_) -> assert false (* TODO *)
238 let rec skip_args status ~metasenv context =
241 | [],_ -> assert false (* IMPOSSIBLE *)
242 | None::tl1,_::tl2 -> skip_args status ~metasenv context (tl1,tl2)
244 match classify status ~metasenv context arg with
247 | `Term `TypeFormer ->
248 Some arg::skip_args status ~metasenv context (tl1,tl2)
251 | `PropKind -> None::skip_args status ~metasenv context (tl1,tl2)
252 | `Term _ -> assert false (* IMPOSSIBLE *)
255 module ReferenceMap = Map.Make(struct type t = NReference.reference let compare = NReference.compare end)
257 type db = (typ_context * typ option) ReferenceMap.t
259 class type g_status =
261 method extraction_db: db
264 class virtual status =
267 val extraction_db = ReferenceMap.empty
268 method extraction_db = extraction_db
269 method set_extraction_db v = {< extraction_db = v >}
270 method set_extraction_status
271 : 'status. #g_status as 'status -> 'self
272 = fun o -> {< extraction_db = o#extraction_db >}
275 let rec split_kind_prods context =
277 | KArrow (so,ta)-> split_kind_prods (Some ("_",so)::context) ta
278 | KSkip ta -> split_kind_prods (None::context) ta
279 | Type -> context,Type
282 let rec split_typ_prods context =
284 | Arrow (so,ta)-> split_typ_prods (Some ("_",`OfType so)::context) ta
285 | Forall (name,so,ta)-> split_typ_prods (Some (name,`OfKind so)::context) ta
286 | Skip ta -> split_typ_prods (None::context) ta
287 | _ as t -> context,t
290 let rec glue_ctx_typ ctx typ =
293 | Some (_,`OfType so)::ctx -> glue_ctx_typ ctx (Arrow (so,typ))
294 | Some (name,`OfKind so)::ctx -> glue_ctx_typ ctx (Forall (name,so,typ))
295 | None::ctx -> glue_ctx_typ ctx (Skip typ)
298 let rec split_typ_lambdas status n ~metasenv context typ =
299 if n = 0 then context,typ
301 match NCicReduction.whd status ~delta:max_int ~subst:[] context typ with
302 | NCic.Lambda (name,s,t) ->
303 split_typ_lambdas status (n-1) ~metasenv ((name,NCic.Decl s)::context) t
305 (* eta-expansion required *)
306 let ty = NCicTypeChecker.typeof status ~metasenv ~subst:[] context t in
307 match NCicReduction.whd status ~delta:max_int ~subst:[] context ty with
308 | NCic.Prod (name,typ,_) ->
309 split_typ_lambdas status (n-1) ~metasenv
310 ((name,NCic.Decl typ)::context)
311 (NCicUntrusted.mk_appl t [NCic.Rel 1])
312 | _ -> assert false (* IMPOSSIBLE *)
316 let context_of_typformer status ~metasenv context =
318 NCic.Const (NReference.Ref (_,NReference.Ind _) as ref)
319 | NCic.Const (NReference.Ref (_,NReference.Def _) as ref)
320 | NCic.Const (NReference.Ref (_,NReference.Decl) as ref)
321 | NCic.Const (NReference.Ref (_,NReference.Fix _) as ref) ->
322 (try fst (ReferenceMap.find ref status#extraction_db)
324 Not_found -> assert false (* IMPOSSIBLE *))
325 | NCic.Match _ -> assert false (* TODO ???? *)
328 match List.nth context (n-1) with
329 _,NCic.Decl typ -> typ
330 | _,NCic.Def _ -> assert false (* IMPOSSIBLE *) in
331 let typ_ctx = snd (HExtlib.split_nth n context) in
332 let typ = kind_of status ~metasenv typ_ctx typ in
333 fst (split_kind_prods [] typ)
334 | NCic.Meta _ -> assert false (* TODO *)
335 | NCic.Const (NReference.Ref (_,NReference.Con _))
336 | NCic.Const (NReference.Ref (_,NReference.CoFix _))
337 | NCic.Sort _ | NCic.Implicit _ | NCic.Lambda _ | NCic.LetIn _
338 | NCic.Appl _ | NCic.Prod _ ->
339 assert false (* IMPOSSIBLE *)
341 let rec typ_of status ~metasenv context k =
342 match NCicReduction.whd status ~delta:max_int ~subst:[] context k with
343 | NCic.Prod (b,s,t) ->
344 (* CSC: non-invariant assumed here about "_" *)
345 (match classify status ~metasenv context s with
347 Forall (b, kind_of status ~metasenv context s,
348 typ_of ~metasenv status ((b,NCic.Decl s)::context) t)
350 | `KindOrType -> (* ??? *)
351 Arrow (typ_of status ~metasenv context s,
352 typ_of status ~metasenv ((b,NCic.Decl s)::context) t)
355 Skip (typ_of status ~metasenv ((b,NCic.Decl s)::context) t)
356 | `Term _ -> assert false (* IMPOSSIBLE *))
359 | NCic.LetIn _ -> assert false (* IMPOSSIBLE *)
360 | NCic.Lambda _ -> assert false (* LAMBDA-LIFT INNER DECLARATION *)
361 | NCic.Rel n -> Var n
362 | NCic.Const ref -> TConst ref
363 | NCic.Appl (he::args) ->
364 let he_context = context_of_typformer status ~metasenv context he in
365 TAppl (typ_of status ~metasenv context he ::
367 (function None -> Unit | Some ty -> typ_of status ~metasenv context ty)
368 (skip_args status ~metasenv context (List.rev he_context,args)))
369 | NCic.Appl _ -> assert false (* TODO: when head is a match/let rec;
370 otherwise NOT A TYPE *)
372 | NCic.Match (_,_,_,_) -> assert false (* TODO *)
375 let rec fomega_subst k t1 =
379 else if n < k then Var n
382 | TConst ref -> TConst ref
384 | Arrow (ty1,ty2) -> Arrow (fomega_subst k t1 ty1, fomega_subst (k+1) t1 ty2)
385 | Skip t -> Skip (fomega_subst (k+1) t1 t)
386 | Forall (n,kind,t) -> Forall (n,kind,fomega_subst (k+1) t1 t)
387 | TAppl args -> TAppl (List.map (fomega_subst k t1) args)
389 let fomega_lookup status ref = snd (ReferenceMap.find ref status#extraction_db)
391 let rec fomega_whd status ty =
394 (match fomega_lookup status r with
396 | Some ty -> fomega_whd status ty)
397 | TAppl (TConst r::args) ->
398 (match fomega_lookup status r with
400 | Some ty -> fomega_whd status (List.fold_right (fomega_subst 1) args ty))
403 let rec term_of status ~metasenv context =
407 | NCic.Prod _ -> assert false (* IMPOSSIBLE *)
408 | NCic.Lambda (b,ty,bo) ->
409 (* CSC: non-invariant assumed here about "_" *)
410 (match classify status ~metasenv context ty with
412 TLambda (term_of status ~metasenv ((b,NCic.Decl ty)::context) bo)
413 | `KindOrType (* ??? *)
415 Lambda (b, term_of status ~metasenv ((b,NCic.Decl ty)::context) bo)
419 term_of status ~metasenv ((b,NCic.Decl ty)::context) bo
420 | `Term _ -> assert false (* IMPOSSIBLE *))
421 | NCic.LetIn (b,ty,t,bo) ->
422 (match classify status ~metasenv context t with
423 | `Term `TypeFormerOrTerm (* ???? *)
425 LetIn (b,term_of status ~metasenv context t,
426 term_of status ~metasenv ((b,NCic.Def (t,ty))::context) bo)
435 (* not in programming languages, we expand it *)
436 term_of status ~metasenv context
437 (NCicSubstitution.subst status ~avoid_beta_redexes:true t bo))
438 | NCic.Rel n -> Rel n
439 | NCic.Const ref -> Const ref
440 | NCic.Appl (he::args) ->
441 eat_args status metasenv
442 (term_of status ~metasenv context he) context
443 (typ_of status ~metasenv context
444 (NCicTypeChecker.typeof status ~metasenv ~subst:[] context he))
447 let he_context = context_of_typformer status ~metasenv context he in
448 let process_args he =
451 | `Inst tl -> Inst (process_args he tl)
452 | `Appl (arg,tl) -> process_args (Appl (he,... arg)) tl
454 Appl (typ_of status ~metasenv context he ::
455 process_args (typ_of status ~metasenv context he)
456 (skip_term_args status ~metasenv context (List.rev he_context,args))
458 | NCic.Appl _ -> assert false (* TODO: when head is a match/let rec;
459 otherwise NOT A TYPE *)
460 | NCic.Meta _ -> assert false (* TODO *)
461 | NCic.Match (ref,_,t,pl) ->
462 Match (ref,term_of status ~metasenv context t,
463 List.map (term_of status ~metasenv context) pl)
464 and eat_args status metasenv acc context tyhe =
470 Appl args -> Appl (args@[x])
473 match fomega_whd status tyhe with
478 | _ -> term_of status ~metasenv context arg in
479 eat_args status metasenv (mk_appl acc arg) context t tl
481 eat_args status metasenv (Inst acc)
482 context (fomega_subst 1 (typ_of status ~metasenv context arg) t) tl
484 eat_args status metasenv acc context t tl
485 | Top -> assert false (*TODO: HOW??*)
486 | Unit | Var _ | TConst _ | TAppl _ -> assert false (* NOT A PRODUCT *)
496 let object_of_constant status ~metasenv uri height bo ty =
497 match classify status ~metasenv [] ty with
499 let ty = kind_of status ~metasenv [] ty in
500 let ctx0,res = split_kind_prods [] ty in
502 split_typ_lambdas status ~metasenv (List.length ctx0) [] bo in
503 (match classify status ~metasenv ctx bo with
505 | `KindOrType -> (* ?? no kind formers in System F_omega *)
509 HExtlib.map_option (fun (_,k) ->
510 (*CSC: BUG here, clashes*)
511 String.uncapitalize (fst n),k) p1)
514 (* BUG here: for mutual type definitions the spec is not good *)
516 NReference.reference_of_spec uri (NReference.Def height) in
517 let bo = typ_of status ~metasenv ctx bo in
518 status#set_extraction_db
519 (ReferenceMap.add ref (nicectx,Some bo)
520 status#extraction_db),
521 Success (uri,TypeDefinition((nicectx,res),bo))
522 | `Kind -> status, Erased (* DPM: but not really, more a failure! *)
524 | `Proposition -> status, Erased
525 | `Term _ -> status, Failure "Body of type lambda classified as a term. This is a bug.")
527 | `Proposition -> status, Erased
528 | `KindOrType (* ??? *)
530 (* CSC: TO BE FINISHED, REF NON REGISTERED *)
531 let ty = typ_of status ~metasenv [] ty in
533 Success (uri, TermDefinition (split_typ_prods [] ty, term_of status ~metasenv [] bo))
534 | `Term _ -> status, Failure "Non-term classified as a term. This is a bug."
537 let object_of_inductive status ~metasenv uri ind leftno il =
538 let status,_,rev_tyl =
540 (fun (status,i,res) (_,name,arity,cl) ->
541 match classify_not_term status [] arity with
544 | `Type -> assert false (* IMPOSSIBLE *)
545 | `PropKind -> status,i+1,res
547 let arity = kind_of status ~metasenv [] arity in
548 let ctx,_ = split_kind_prods [] arity in
549 let right,left = HExtlib.split_nth (List.length ctx - leftno) ctx in
551 NReference.reference_of_spec uri (NReference.Ind (ind,i,leftno)) in
553 status#set_extraction_db
554 (ReferenceMap.add ref (ctx,None) status#extraction_db) in
559 NCicReduction.split_prods status ~subst:[] [] leftno ty in
560 let ty = typ_of status ~metasenv ctx ty in
564 status,i+1,(name,left,right,cl)::res
569 | _ -> status, Success (uri, Algebraic (List.rev rev_tyl))
572 let object_of status (uri,height,metasenv,subst,obj_kind) =
573 let obj_kind = apply_subst subst obj_kind in
575 | NCic.Constant (_,_,None,ty,_) ->
576 (match classify status ~metasenv [] ty with
578 let ty = kind_of status ~metasenv [] ty in
579 let ctx,_ as res = split_kind_prods [] ty in
580 let ref = NReference.reference_of_spec uri NReference.Decl in
581 status#set_extraction_db
582 (ReferenceMap.add ref (ctx,None) status#extraction_db), Success (uri, TypeDeclaration res)
584 | `Proposition -> status, Erased
586 | `KindOrType (*???*) ->
587 let ty = typ_of status ~metasenv [] ty in
588 status, Success (uri, TermDeclaration (split_typ_prods [] ty))
589 | `Term _ -> status, Failure "Type classified as a term. This is a bug.")
590 | NCic.Constant (_,_,Some bo,ty,_) ->
591 object_of_constant status ~metasenv uri height bo ty
592 | NCic.Fixpoint (_fix_or_cofix,fs,_) ->
595 (fun (_,_name,_,ty,bo) (status,res) ->
596 let status,obj = object_of_constant ~metasenv status uri height bo ty in
598 | Success (_uri,obj) -> status, obj::res
599 | _ -> status, res) fs (status,[])
601 status, Success (uri,LetRec objs)
602 | NCic.Inductive (ind,leftno,il,_) ->
603 object_of_inductive status ~metasenv uri ind leftno il
605 (************************ HASKELL *************************)
607 (* -----------------------------------------------------------------------------
608 * Helper functions I can't seem to find anywhere in the OCaml stdlib?
609 * -----------------------------------------------------------------------------
619 let uncurry f (x, y) =
623 let rec char_list_of_string s =
624 let l = String.length s in
625 let rec aux buffer s =
628 | m -> aux (s.[m - 1]::buffer) s (m - 1)
633 let string_of_char_list s =
637 | x::xs -> aux (String.make 1 x ^ buffer) xs
642 (* ----------------------------------------------------------------------------
643 * Haskell name management: prettyfying valid and idiomatic Haskell identifiers
644 * and type variable names.
645 * ----------------------------------------------------------------------------
648 let remove_underscores_and_mark =
649 let rec aux char_list_buffer positions_buffer position =
651 | [] -> (string_of_char_list char_list_buffer, positions_buffer)
654 aux char_list_buffer (position::positions_buffer) position xs
656 aux (x::char_list_buffer) positions_buffer (position + 1) xs
661 let rec capitalize_marked_positions s =
665 if x < String.length s then
666 let c = Char.uppercase (String.get s x) in
667 let _ = String.set s x c in
668 capitalize_marked_positions s xs
670 capitalize_marked_positions s xs
673 let contract_underscores_and_capitalise =
674 char_list_of_string |>
675 remove_underscores_and_mark |>
676 uncurry capitalize_marked_positions
679 let idiomatic_haskell_type_name_of_string =
680 contract_underscores_and_capitalise |>
684 let idiomatic_haskell_term_name_of_string =
685 contract_underscores_and_capitalise |>
689 (*CSC: code to be changed soon when we implement constructors and
690 we fix the code for term application *)
691 let classify_reference status ref =
692 if ReferenceMap.mem ref status#extraction_db then
698 let capitalize classification name =
699 match classification with
701 | `TypeName -> idiomatic_haskell_type_name_of_string name
702 | `FunctionName -> idiomatic_haskell_term_name_of_string name
705 let pp_ref status ref =
706 capitalize (classify_reference status ref)
707 (NCicPp.r2s status false ref)
709 let name_of_uri classification uri =
710 capitalize classification (NUri.name_of_uri uri)
712 (* cons avoid duplicates *)
713 let rec (@:::) name l =
714 if name <> "" (* propositional things *) && name.[0] = '_' then
715 let name = String.sub name 1 (String.length name - 1) in
716 let name = if name = "" then "a" else name in
718 else if List.mem name l then (name ^ "'") @::: l
719 else if name="" then "[erased]",l else name,l
722 let (@::) x l = let x,l = x @::: l in x::l;;
724 let rec pretty_print_type status ctxt =
726 | Var n -> List.nth ctxt (n-1)
728 | Top -> assert false (* ??? *)
729 | TConst ref -> pp_ref status ref
731 bracket size_of_type (pretty_print_type status ctxt) t1 ^ " -> " ^
732 pretty_print_type status ("_"::ctxt) t2
733 | Skip t -> pretty_print_type status ("_"::ctxt) t
734 | Forall (name, kind, t) ->
735 (*CSC: BUG HERE: avoid clashes due to uncapitalisation*)
736 let name = String.uncapitalize name in
737 if size_of_kind kind > 1 then
738 "forall (" ^ name ^ " :: " ^ pretty_print_kind kind ^ "). " ^ pretty_print_type status (name@::ctxt) t
740 "forall " ^ name ^ ". " ^ pretty_print_type status (name@::ctxt) t
741 | TAppl tl -> String.concat " " (List.map (pretty_print_type status ctxt) tl)
743 let rec pretty_print_term status ctxt =
745 | Rel n -> List.nth ctxt (n-1)
747 | Const ref -> pp_ref status ref
748 | Lambda (name,t) -> "\\" ^ name ^ " -> " ^ pretty_print_term status (name@::ctxt) t
749 | Appl tl -> String.concat " " (List.map (bracket size_of_term (pretty_print_term status ctxt)) tl)
750 | LetIn (name,s,t) ->
751 "let " ^ name ^ " = " ^ pretty_print_term status ctxt s ^ " in " ^ pretty_print_term status (name@::ctxt) t
752 | Match (r,matched,pl) ->
754 "error \"Case analysis over empty type\""
756 let constructors, leftno =
757 let _,leftno,tys,_,n = NCicEnvironment.get_checked_indtys status r in
758 let _,_,_,cl = List.nth tys n in
761 let rec eat_branch n ty pat =
763 | NCic.Prod (_, _, t), _ when n > 0 -> eat_branch (pred n) t pat
764 | NCic.Prod (_, _, t), Lambda (name, t') ->
765 (*CSC: BUG HERE; WHAT IF SOME ARGUMENTS ARE DELETED?*)
766 let cv, rhs = eat_branch 0 t t' in
774 (fun (_, name, ty) pat -> incr j; name, eat_branch leftno ty pat) constructors pl
775 with Invalid_argument _ -> assert false
777 "case " ^ pretty_print_term status ctxt matched ^ " of\n" ^
780 (fun (name,(bound_names,rhs)) ->
782 (*CSC: BUG avoid name clashes *)
783 String.concat " " (String.capitalize name::bound_names),
784 pretty_print_term status ((List.rev bound_names)@ctxt) rhs
786 " " ^ pattern ^ " -> " ^ body
788 | TLambda t -> pretty_print_term status ctxt t
789 | Inst t -> pretty_print_term status ctxt t
793 type term_context = (string * [`OfKind of kind | `OfType of typ]) option list
795 type term_former_def = term_context * term * typ
796 type term_former_decl = term_context * typ
799 let rec pp_obj status (uri,obj_kind) =
800 let pretty_print_context ctx =
801 String.concat " " (List.rev (snd
803 (fun (x,kind) (l,res) ->
805 if size_of_kind kind > 1 then
806 x::l,("(" ^ x ^ " :: " ^ pretty_print_kind kind ^ ")")::res
809 (HExtlib.filter_map (fun x -> x) ctx) ([],[]))))
811 let namectx_of_ctx ctx =
812 List.fold_right (@::)
813 (List.map (function None -> "" | Some (x,_) -> x) ctx) [] in
815 TypeDeclaration (ctx,_) ->
816 (* data?? unsure semantics: inductive type without constructor, but
817 not matchable apparently *)
818 if List.length ctx = 0 then
819 "data " ^ name_of_uri `TypeName uri
821 "data " ^ name_of_uri `TypeName uri ^ " " ^ pretty_print_context ctx
822 | TypeDefinition ((ctx, _),ty) ->
823 let namectx = namectx_of_ctx ctx in
824 if List.length ctx = 0 then
825 "type " ^ name_of_uri `TypeName uri ^ " = " ^ pretty_print_type status namectx ty
827 "type " ^ name_of_uri `TypeName uri ^ " " ^ pretty_print_context ctx ^ " = " ^ pretty_print_type status namectx ty
828 | TermDeclaration (ctx,ty) ->
829 let name = name_of_uri `FunctionName uri in
830 name ^ " :: " ^ pretty_print_type status [] (glue_ctx_typ ctx ty) ^ "\n" ^
831 name ^ " = error \"The declaration `" ^ name ^ "' has yet to be defined.\""
832 | TermDefinition ((ctx,ty),bo) ->
833 let name = name_of_uri `FunctionName uri in
834 let namectx = namectx_of_ctx ctx in
836 name ^ " :: " ^ pretty_print_type status namectx (glue_ctx_typ ctx ty) ^ "\n" ^
837 name ^ " = " ^ pretty_print_term status namectx bo
839 (*CSC: BUG always uses the name of the URI *)
840 String.concat "\n" (List.map (fun obj -> pp_obj status (uri,obj)) l)
844 (fun _name,left,right,cl ->
845 (*CSC: BUG always uses the name of the URI *)
846 "data " ^ name_of_uri `TypeName uri ^ " " ^
847 pretty_print_context (right@left) ^ " where\n " ^
848 String.concat "\n " (List.map
850 let namectx = namectx_of_ctx left in
851 capitalize `Constructor name ^ " :: " ^
852 pretty_print_type status namectx tys
855 (* inductive and records missing *)
857 let haskell_of_obj status (uri,_,_,_,_ as obj) =
858 let status, obj = object_of status obj in
861 Erased -> "-- [?] " ^ NUri.name_of_uri uri ^ " erased due to term being propositionally irrelevant.\n"
862 | OutsideTheory -> "-- [?] " ^ NUri.name_of_uri uri ^ " erased due to image of term under extraction residing outside Fω.\n"
863 | Failure msg -> "-- [?] " ^ NUri.name_of_uri uri ^ " FAILURE: " ^ msg ^ "\n"
864 | Success o -> pp_obj status o ^ "\n"