1 (******************************************************************************)
5 (* Claudio Sacerdoti Coen <sacerdot@cs.unibo.it> *)
8 (* This module implements a very simple Coq-like pretty printer that, given *)
9 (* an object of cic (internal representation) returns a string describing the *)
10 (* object in a syntax similar to that of coq *)
12 (******************************************************************************)
14 exception CicPpInternalError;;
16 (* Utility functions *)
21 | Cic.Anonimous -> "_"
24 (* get_nth l n returns the nth element of the list l if it exists or raise *)
25 (* a CicPpInternalError if l has less than n elements or n < 1 *)
29 | (n, he::tail) when n > 1 -> get_nth tail (n-1)
30 | (_,_) -> raise CicPpInternalError
34 (* pretty-prints a term t of cic in an environment l where l is a list of *)
35 (* identifier names used to resolve DeBrujin indexes. The head of l is the *)
36 (* name associated to the greatest DeBrujin index in t *)
41 (match get_nth l n with
43 | _ -> raise CicPpInternalError
45 | C.Var uri -> UriManager.name_of_uri uri
46 | C.Meta n -> "?" ^ (string_of_int n)
56 C.Name n -> "(" ^ n ^ ":" ^ pp s l ^ ")" ^ pp t (b::l)
57 | C.Anonimous -> "(" ^ pp s l ^ "->" ^ pp t (b::l) ^ ")"
59 | C.Cast (v,t) -> pp v l
61 "[" ^ string_of_name b ^ ":" ^ pp s l ^ "]" ^ pp t (b::l)
63 "[" ^ string_of_name b ^ ":=" ^ pp s l ^ "]" ^ pp t (b::l)
67 (fun x i -> pp x l ^ (match i with "" -> "" | _ -> " ") ^ i)
70 | C.Const (uri,_) -> UriManager.name_of_uri uri
71 | C.Abst uri -> UriManager.name_of_uri uri
72 | C.MutInd (uri,_,n) ->
73 (match CicCache.get_obj uri with
74 C.InductiveDefinition (dl,_,_) ->
75 let (name,_,_,_) = get_nth dl (n+1) in
77 | _ -> raise CicPpInternalError
79 | C.MutConstruct (uri,_,n1,n2) ->
80 (match CicCache.get_obj uri with
81 C.InductiveDefinition (dl,_,_) ->
82 let (_,_,_,cons) = get_nth dl (n1+1) in
83 let (id,_,_) = get_nth cons n2 in
85 | _ -> raise CicPpInternalError
87 | C.MutCase (uri,_,n1,ty,te,patterns) ->
89 (match CicCache.get_obj uri with
90 C.InductiveDefinition (dl,_,_) ->
91 let (_,_,_,cons) = get_nth dl (n1+1) in
92 List.map (fun (id,_,_) -> id) cons
93 | _ -> raise CicPpInternalError
96 "\n<" ^ pp ty l ^ ">Cases " ^ pp te l ^ " of " ^
97 List.fold_right (fun (x,y) i -> "\n " ^ x ^ " => " ^ pp y l ^ i)
98 (List.combine connames patterns) "" ^
100 | C.Fix (no, funs) ->
101 let snames = List.map (fun (name,_,_,_) -> name) funs in
102 let names = List.rev (List.map (function name -> C.Name name) snames) in
103 "\nFix " ^ get_nth snames (no + 1) ^ " {" ^
105 (fun (name,ind,ty,bo) i -> "\n" ^ name ^ " / " ^ string_of_int ind ^
106 " : " ^ pp ty l ^ " := \n" ^
110 | C.CoFix (no,funs) ->
111 let snames = List.map (fun (name,_,_) -> name) funs in
112 let names = List.rev (List.map (function name -> C.Name name) snames) in
113 "\nCoFix " ^ get_nth snames (no + 1) ^ " {" ^
115 (fun (name,ty,bo) i -> "\n" ^ name ^
116 " : " ^ pp ty l ^ " := \n" ^
122 (* ppinductiveType (typename, inductive, arity, cons) names *)
123 (* pretty-prints a single inductive definition (typename, inductive, arity, *)
124 (* cons) where the cic terms in the inductive definition need to be *)
125 (* evaluated in the environment names that is the list of typenames of the *)
126 (* mutual inductive definitions defined in the block of mutual inductive *)
127 (* definitions to which this one belongs to *)
128 let ppinductiveType (typename, inductive, arity, cons) names =
129 (if inductive then "\nInductive " else "\nCoInductive ") ^ typename ^ ": " ^
130 (*CSC: bug found: was pp arity names ^ " =\n " ^*)
131 pp arity [] ^ " =\n " ^
133 (fun (id,ty,_) i -> id ^ " : " ^ pp ty names ^
134 (if i = "" then "\n" else "\n | ") ^ i)
138 (* ppobj obj returns a string with describing the cic object obj in a syntax *)
139 (* similar to the one used by Coq *)
141 let module C = Cic in
142 let module U = UriManager in
144 C.Definition (id, t1, t2, params) ->
145 "Definition of " ^ id ^
151 U.string_of_uri x ^ match i with "" -> "" | i' -> " " ^ i'
152 ) x "" ^ match i with "" -> "" | i' -> " " ^ i'
154 ":\n" ^ pp t1 [] ^ " : " ^ pp t2 []
155 | C.Axiom (id, ty, params) ->
156 "Axiom " ^ id ^ "(" ^
161 U.string_of_uri x ^ match i with "" -> "" | i' -> " " ^ i'
162 ) x "" ^ match i with "" -> "" | i' -> " " ^ i'
165 | C.Variable (name, bo, ty) ->
166 "Variable " ^ name ^ ":\n" ^ pp ty [] ^ "\n" ^
167 (match bo with None -> "" | Some bo -> ":= " ^ pp bo [])
168 | C.CurrentProof (name, conjectures, value, ty) ->
171 (fun (n, t) i -> "?" ^ (string_of_int n) ^ ": " ^ pp t [] ^ "\n" ^ i)
173 "\n" ^ pp value [] ^ " : " ^ pp ty []
174 | C.InductiveDefinition (l, params, nparams) ->
180 U.string_of_uri x ^ match i with "" -> "" | i' -> " " ^ i'
181 ) x "" ^ match i with "" -> "" | i' -> " " ^ i'
183 "NParams = " ^ string_of_int nparams ^ "\n" ^
184 let names = List.rev (List.map (fun (n,_,_,_) -> C.Name n) l) in
185 List.fold_right (fun x i -> ppinductiveType x names ^ i) l ""