]> matita.cs.unibo.it Git - helm.git/blob - helm/interface/cicPp.ml
First very partial implementation of LetIn and bodyed Variables
[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.LetIn (b,s,t) ->
63        "[" ^ string_of_name b ^ ":=" ^ pp s l ^ "]" ^ pp t (b::l)
64     | C.Appl li ->
65        "(" ^
66        (List.fold_right
67         (fun x i -> pp x l ^ (match i with "" -> "" | _ -> " ") ^ i)
68         li ""
69        ) ^ ")"
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
76              name
77          | _ -> raise CicPpInternalError
78        )
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
84               id
85          | _ -> raise CicPpInternalError
86        )
87     | C.MutCase (uri,_,n1,ty,te,patterns) ->
88        let connames =
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
94         )
95        in
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) "" ^
99           "\nend"
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) ^ " {" ^
104          List.fold_right
105           (fun (name,ind,ty,bo) i -> "\n" ^ name ^ " / " ^ string_of_int ind ^
106             " : " ^ pp ty l ^ " := \n" ^
107             pp bo (names@l) ^ i)
108           funs "" ^
109          "}\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) ^ " {" ^
114          List.fold_right
115           (fun (name,ty,bo) i -> "\n" ^ name ^ 
116             " : " ^ pp ty l ^ " := \n" ^
117             pp bo (names@l) ^ i)
118           funs "" ^
119          "}\n"
120 ;;
121
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   " ^
132   List.fold_right
133    (fun (id,ty,_) i -> id ^ " : " ^ pp ty names ^ 
134     (if i = "" then "\n" else "\n | ") ^ i)
135    cons ""
136 ;;
137
138 (* ppobj obj  returns a string with describing the cic object obj in a syntax *)
139 (* similar to the one used by Coq                                             *)
140 let ppobj obj =
141  let module C = Cic in
142  let module U = UriManager in
143   match obj with
144     C.Definition (id, t1, t2, params) ->
145       "Definition of " ^ id ^
146       "(" ^
147       List.fold_right
148        (fun (_,x) i ->
149          List.fold_right
150           (fun x i ->
151             U.string_of_uri x ^ match i with "" -> "" | i' -> " " ^ i'
152           ) x "" ^ match i with "" -> "" | i' -> " " ^ i'
153        ) params "" ^ ")" ^
154       ":\n" ^ pp t1 [] ^ " : " ^ pp t2 []
155    | C.Axiom (id, ty, params) ->
156       "Axiom " ^ id ^ "(" ^
157       List.fold_right
158        (fun (_,x) i ->
159          List.fold_right
160           (fun x i ->
161             U.string_of_uri x ^ match i with "" -> "" | i' -> " " ^ i'
162           ) x "" ^ match i with "" -> "" | i' -> " " ^ i'
163        ) params "" ^
164       "):\n" ^ pp ty []
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) ->
169       "Current Proof:\n" ^
170       List.fold_right
171        (fun (n, t) i -> "?" ^ (string_of_int n) ^ ": " ^ pp t [] ^ "\n" ^ i)
172        conjectures "" ^
173       "\n" ^ pp value [] ^ " : " ^ pp ty [] 
174    | C.InductiveDefinition (l, params, nparams) ->
175       "Parameters = " ^
176       List.fold_right
177        (fun (_,x) i ->
178          List.fold_right
179           (fun x i ->
180             U.string_of_uri x ^ match i with "" -> "" | i' -> " " ^ i'
181           ) x "" ^ match i with "" -> "" | i' -> " " ^ i'
182        ) params "" ^ "\n" ^
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 ""
186 ;;