(* Copyright (C) 2000, HELM Team. * * This file is part of HELM, an Hypertextual, Electronic * Library of Mathematics, developed at the Computer Science * Department, University of Bologna, Italy. * * HELM is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * HELM is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HELM; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, * MA 02111-1307, USA. * * For details, see the HELM World-Wide-Web page, * http://cs.unibo.it/helm/. *) (******************************************************************************) (* *) (* PROJECT HELM *) (* *) (* Claudio Sacerdoti Coen *) (* 24/01/2000 *) (* *) (* This module implements a very simple Coq-like pretty printer that, given *) (* an object of cic (internal representation) returns a string describing the *) (* object in a syntax similar to that of coq *) (* *) (******************************************************************************) exception CicPpInternalError;; exception NotEnoughElements;; (* Utility functions *) let ppname = function Cic.Name s -> s | Cic.Anonymous -> "_" ;; (* get_nth l n returns the nth element of the list l if it exists or *) (* raises NotEnoughElements if l has less than n elements *) let rec get_nth l n = match (n,l) with (1, he::_) -> he | (n, he::tail) when n > 1 -> get_nth tail (n-1) | (_,_) -> raise NotEnoughElements ;; (* pp t l *) (* pretty-prints a term t of cic in an environment l where l is a list of *) (* identifier names used to resolve DeBrujin indexes. The head of l is the *) (* name associated to the greatest DeBrujin index in t *) let rec pp t l = let module C = Cic in match t with C.Rel n -> begin try (match get_nth l n with Some (C.Name s) -> s | Some C.Anonymous -> "__" ^ string_of_int n | None -> "_hidden_" ^ string_of_int n ) with NotEnoughElements -> string_of_int (List.length l - n) end | C.Var (uri,exp_named_subst) -> UriManager.string_of_uri (*UriManager.name_of_uri*) uri ^ pp_exp_named_subst exp_named_subst l | C.Meta (n,l1) -> "?" ^ (string_of_int n) ^ "[" ^ String.concat " ; " (List.rev_map (function None -> "_" | Some t -> pp t l) l1) ^ "]" | C.Sort s -> (match s with C.Prop -> "Prop" | C.Set -> "Set" | C.Type -> "Type" | C.CProp -> "CProp" ) | C.Implicit -> "?" | C.Prod (b,s,t) -> (match b with C.Name n -> "(" ^ n ^ ":" ^ pp s l ^ ")" ^ pp t ((Some b)::l) | C.Anonymous -> "(" ^ pp s l ^ "->" ^ pp t ((Some b)::l) ^ ")" ) | C.Cast (v,t) -> pp v l | C.Lambda (b,s,t) -> "[" ^ ppname b ^ ":" ^ pp s l ^ "]" ^ pp t ((Some b)::l) | C.LetIn (b,s,t) -> "[" ^ ppname b ^ ":=" ^ pp s l ^ "]" ^ pp t ((Some b)::l) | C.Appl li -> "(" ^ (List.fold_right (fun x i -> pp x l ^ (match i with "" -> "" | _ -> " ") ^ i) li "" ) ^ ")" | C.Const (uri,exp_named_subst) -> UriManager.name_of_uri uri ^ pp_exp_named_subst exp_named_subst l | C.MutInd (uri,n,exp_named_subst) -> (try match CicEnvironment.get_obj uri with C.InductiveDefinition (dl,_,_) -> let (name,_,_,_) = get_nth dl (n+1) in name ^ pp_exp_named_subst exp_named_subst l | _ -> raise CicPpInternalError with _ -> UriManager.string_of_uri uri ^ "#1/" ^ string_of_int (n + 1) ) | C.MutConstruct (uri,n1,n2,exp_named_subst) -> (try match CicEnvironment.get_obj uri with C.InductiveDefinition (dl,_,_) -> let (_,_,_,cons) = get_nth dl (n1+1) in let (id,_) = get_nth cons n2 in id ^ pp_exp_named_subst exp_named_subst l | _ -> raise CicPpInternalError with _ -> UriManager.string_of_uri uri ^ "#1/" ^ string_of_int (n1 + 1) ^ "/" ^ string_of_int n2 ) | C.MutCase (uri,n1,ty,te,patterns) -> let connames = (match CicEnvironment.get_obj uri with C.InductiveDefinition (dl,_,_) -> let (_,_,_,cons) = get_nth dl (n1+1) in List.map (fun (id,_) -> id) cons | _ -> raise CicPpInternalError ) in "\n<" ^ pp ty l ^ ">Cases " ^ pp te l ^ " of " ^ List.fold_right (fun (x,y) i -> "\n " ^ x ^ " => " ^ pp y l ^ i) (List.combine connames patterns) "" ^ "\nend" | C.Fix (no, funs) -> let snames = List.map (fun (name,_,_,_) -> name) funs in let names = List.rev (List.map (function name -> Some (C.Name name)) snames) in "\nFix " ^ get_nth snames (no + 1) ^ " {" ^ List.fold_right (fun (name,ind,ty,bo) i -> "\n" ^ name ^ " / " ^ string_of_int ind ^ " : " ^ pp ty l ^ " := \n" ^ pp bo (names@l) ^ i) funs "" ^ "}\n" | C.CoFix (no,funs) -> let snames = List.map (fun (name,_,_) -> name) funs in let names = List.rev (List.map (function name -> Some (C.Name name)) snames) in "\nCoFix " ^ get_nth snames (no + 1) ^ " {" ^ List.fold_right (fun (name,ty,bo) i -> "\n" ^ name ^ " : " ^ pp ty l ^ " := \n" ^ pp bo (names@l) ^ i) funs "" ^ "}\n" and pp_exp_named_subst exp_named_subst l = if exp_named_subst = [] then "" else "{" ^ String.concat " ; " ( List.map (function (uri,t) -> UriManager.name_of_uri uri ^ ":=" ^ pp t l) exp_named_subst ) ^ "}" ;; let ppterm t = pp t [] ;; (* ppinductiveType (typename, inductive, arity, cons) *) (* pretty-prints a single inductive definition *) (* (typename, inductive, arity, cons) *) let ppinductiveType (typename, inductive, arity, cons) = (if inductive then "\nInductive " else "\nCoInductive ") ^ typename ^ ": " ^ pp arity [] ^ " =\n " ^ List.fold_right (fun (id,ty) i -> id ^ " : " ^ pp ty [] ^ (if i = "" then "\n" else "\n | ") ^ i) cons "" ;; (* ppobj obj returns a string with describing the cic object obj in a syntax *) (* similar to the one used by Coq *) let ppobj obj = let module C = Cic in let module U = UriManager in match obj with C.Constant (name, Some t1, t2, params) -> "Definition of " ^ name ^ "(" ^ String.concat ";" (List.map UriManager.string_of_uri params) ^ ")" ^ ":\n" ^ pp t1 [] ^ " : " ^ pp t2 [] | C.Constant (name, None, ty, params) -> "Axiom " ^ name ^ "(" ^ String.concat ";" (List.map UriManager.string_of_uri params) ^ "):\n" ^ pp ty [] | C.Variable (name, bo, ty, params) -> "Variable " ^ name ^ "(" ^ String.concat ";" (List.map UriManager.string_of_uri params) ^ ")" ^ ":\n" ^ pp ty [] ^ "\n" ^ (match bo with None -> "" | Some bo -> ":= " ^ pp bo []) | C.CurrentProof (name, conjectures, value, ty, params) -> "Current Proof of " ^ name ^ "(" ^ String.concat ";" (List.map UriManager.string_of_uri params) ^ ")" ^ ":\n" ^ let separate s = if s = "" then "" else s ^ " ; " in List.fold_right (fun (n, context, t) i -> let conjectures',name_context = List.fold_right (fun context_entry (i,name_context) -> (match context_entry with Some (n,C.Decl at) -> (separate i) ^ ppname n ^ ":" ^ pp at name_context ^ " ", (Some n)::name_context | Some (n,C.Def (at,None)) -> (separate i) ^ ppname n ^ ":= " ^ pp at name_context ^ " ", (Some n)::name_context | None -> (separate i) ^ "_ :? _ ", None::name_context | _ -> assert false) ) context ("",[]) in conjectures' ^ " |- " ^ "?" ^ (string_of_int n) ^ ": " ^ pp t name_context ^ "\n" ^ i ) conjectures "" ^ "\n" ^ pp value [] ^ " : " ^ pp ty [] | C.InductiveDefinition (l, params, nparams) -> "Parameters = " ^ String.concat ";" (List.map UriManager.string_of_uri params) ^ "\n" ^ "NParams = " ^ string_of_int nparams ^ "\n" ^ List.fold_right (fun x i -> ppinductiveType x ^ i) l "" ;;