]> matita.cs.unibo.it Git - helm.git/blob - helm/interface/cicPp.ml
932978664684cd8b638262890ea5bda9374032a7
[helm.git] / helm / interface / cicPp.ml
1 (******************************************************************************)
2 (*                                                                            *)
3 (*                               PROJECT HELM                                 *)
4 (*                                                                            *)
5 (*                Claudio Sacerdoti Coen <sacerdot@cs.unibo.it>               *)
6 (*                                 24/01/2000                                 *)
7 (*                                                                            *)
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                                  *)
11 (*                                                                            *)
12 (******************************************************************************)
13
14 exception CicPpInternalError;;
15
16 (* Utility functions *)
17
18 let string_of_name =
19  function
20     Cic.Name s     -> s
21   | Cic.Anonimous  -> "_"
22 ;;
23
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               *)
26 let rec get_nth l n =
27  match (n,l) with
28     (1, he::_) -> he
29   | (n, he::tail) when n > 1 -> get_nth tail (n-1)
30   | (_,_) -> raise CicPpInternalError
31 ;;
32
33 (* pp t l                                                                  *)
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                     *)
37 let rec pp t l =
38  let module C = Cic in
39    match t with
40       C.Rel n ->
41        (match get_nth l n with
42            C.Name s -> s
43          | _        -> raise CicPpInternalError
44        )
45     | C.Var uri -> UriManager.name_of_uri uri
46     | C.Meta n -> "?" ^ (string_of_int n)
47     | C.Sort s ->
48        (match s with
49            C.Prop -> "Prop"
50          | C.Set  -> "Set"
51          | C.Type -> "Type"
52        )
53     | C.Implicit -> "?"
54     | C.Prod (b,s,t) ->
55        (match b with
56           C.Name n -> "(" ^ n ^ ":" ^ pp s l ^ ")" ^ pp t (b::l)
57         | C.Anonimous -> "(" ^ pp s l ^ "->" ^ pp t (b::l) ^ ")"
58        )
59     | C.Cast (v,t) -> pp v l
60     | C.Lambda (b,s,t) ->
61        "[" ^ string_of_name b ^ ":" ^ pp s l ^ "]" ^ pp t (b::l)
62     | C.Appl li ->
63        "(" ^
64        (List.fold_right
65         (fun x i -> pp x l ^ (match i with "" -> "" | _ -> " ") ^ i)
66         li ""
67        ) ^ ")"
68     | C.Const (uri,_) -> UriManager.name_of_uri uri
69     | C.Abst uri -> UriManager.name_of_uri uri
70     | C.MutInd (uri,_,n) ->
71        (match CicCache.get_obj uri with
72            C.InductiveDefinition (dl,_,_) ->
73             let (name,_,_,_) = get_nth dl (n+1) in
74              name
75          | _ -> raise CicPpInternalError
76        )
77     | C.MutConstruct (uri,_,n1,n2) ->
78        (match CicCache.get_obj uri with
79            C.InductiveDefinition (dl,_,_) ->
80             let (_,_,_,cons) = get_nth dl (n1+1) in
81              let (id,_,_) = get_nth cons n2 in
82               id
83          | _ -> raise CicPpInternalError
84        )
85     | C.MutCase (uri,_,n1,ty,te,patterns) ->
86        let connames =
87         (match CicCache.get_obj uri with
88             C.InductiveDefinition (dl,_,_) ->
89              let (_,_,_,cons) = get_nth dl (n1+1) in
90               List.map (fun (id,_,_) -> id) cons
91           | _ -> raise CicPpInternalError
92         )
93        in
94         "\n<" ^ pp ty l ^ ">Cases " ^ pp te l ^ " of " ^
95           List.fold_right (fun (x,y) i -> "\n " ^ x ^ " => " ^ pp y l ^ i)
96            (List.combine connames patterns) "" ^
97           "\nend"
98     | C.Fix (no, funs) ->
99        let snames = List.map (fun (name,_,_,_) -> name) funs in
100         let names = List.rev (List.map (function name -> C.Name name) snames) in
101          "\nFix " ^ get_nth snames (no + 1) ^ " {" ^
102          List.fold_right
103           (fun (name,ind,ty,bo) i -> "\n" ^ name ^ " / " ^ string_of_int ind ^
104             " : " ^ pp ty l ^ " := \n" ^
105             pp bo (names@l) ^ i)
106           funs "" ^
107          "}\n"
108     | C.CoFix (no,funs) ->
109        let snames = List.map (fun (name,_,_) -> name) funs in
110         let names = List.rev (List.map (function name -> C.Name name) snames) in
111          "\nCoFix " ^ get_nth snames (no + 1) ^ " {" ^
112          List.fold_right
113           (fun (name,ty,bo) i -> "\n" ^ name ^ 
114             " : " ^ pp ty l ^ " := \n" ^
115             pp bo (names@l) ^ i)
116           funs "" ^
117          "}\n"
118 ;;
119
120 (* ppinductiveType (typename, inductive, arity, cons) names                 *)
121 (* pretty-prints a single inductive definition (typename, inductive, arity, *)
122 (*  cons) where the cic terms in the inductive definition need to be        *)
123 (*  evaluated in the environment names that is the list of typenames of the *)
124 (*  mutual inductive definitions defined in the block of mutual inductive   *)
125 (*  definitions to which this one belongs to                                *)
126 let ppinductiveType (typename, inductive, arity, cons) names =
127   (if inductive then "\nInductive " else "\nCoInductive ") ^ typename ^ ": " ^
128   (*CSC: bug found: was pp arity names ^ " =\n   " ^*)
129   pp arity [] ^ " =\n   " ^
130   List.fold_right
131    (fun (id,ty,_) i -> id ^ " : " ^ pp ty names ^ 
132     (if i = "" then "\n" else "\n | ") ^ i)
133    cons ""
134 ;;
135
136 (* ppobj obj  returns a string with describing the cic object obj in a syntax *)
137 (* similar to the one used by Coq                                             *)
138 let ppobj obj =
139  let module C = Cic in
140  let module U = UriManager in
141   match obj with
142     C.Definition (id, t1, t2, params) ->
143       "Definition of " ^ id ^
144       "(" ^
145       List.fold_right
146        (fun (_,x) i ->
147          List.fold_right
148           (fun x i ->
149             U.string_of_uri x ^ match i with "" -> "" | i' -> " " ^ i'
150           ) x "" ^ match i with "" -> "" | i' -> " " ^ i'
151        ) params "" ^ ")" ^
152       ":\n" ^ pp t1 [] ^ " : " ^ pp t2 []
153    | C.Axiom (id, ty, params) ->
154       "Axiom " ^ id ^ "(" ^
155       List.fold_right
156        (fun (_,x) i ->
157          List.fold_right
158           (fun x i ->
159             U.string_of_uri x ^ match i with "" -> "" | i' -> " " ^ i'
160           ) x "" ^ match i with "" -> "" | i' -> " " ^ i'
161        ) params "" ^
162       "):\n" ^ pp ty []
163    | C.Variable (name, ty) ->
164       "Variable " ^ name ^ ":\n" ^ pp ty []
165    | C.CurrentProof (name, conjectures, value, ty) ->
166       "Current Proof:\n" ^
167       List.fold_right
168        (fun (n, t) i -> "?" ^ (string_of_int n) ^ ": " ^ pp t [] ^ "\n" ^ i)
169        conjectures "" ^
170       "\n" ^ pp value [] ^ " : " ^ pp ty [] 
171    | C.InductiveDefinition (l, params, nparams) ->
172       "Parameters = " ^
173       List.fold_right
174        (fun (_,x) i ->
175          List.fold_right
176           (fun x i ->
177             U.string_of_uri x ^ match i with "" -> "" | i' -> " " ^ i'
178           ) x "" ^ match i with "" -> "" | i' -> " " ^ i'
179        ) params "" ^ "\n" ^
180       "NParams = " ^ string_of_int nparams ^ "\n" ^
181       let names = List.rev (List.map (fun (n,_,_,_) -> C.Name n) l) in
182        List.fold_right (fun x i -> ppinductiveType x names ^ i) l ""
183 ;;