]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/ocaml/cic_notation/grafiteAstPp.ml
merged cic_notation with disambiguation: good luck!
[helm.git] / helm / ocaml / cic_notation / grafiteAstPp.ml
diff --git a/helm/ocaml/cic_notation/grafiteAstPp.ml b/helm/ocaml/cic_notation/grafiteAstPp.ml
new file mode 100644 (file)
index 0000000..db7a725
--- /dev/null
@@ -0,0 +1,283 @@
+(* Copyright (C) 2004, 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://helm.cs.unibo.it/
+ *)
+
+open Printf
+
+open GrafiteAst
+
+let tactical_terminator = "."
+let tactic_terminator = tactical_terminator
+let command_terminator = tactical_terminator
+
+let pp_term_ast term = CicNotationPp.pp_term term
+let pp_term_cic term = CicPp.ppterm term
+
+let pp_idents idents = "[" ^ String.concat "; " idents ^ "]"
+
+let pp_terms_ast terms = String.concat ", " (List.map pp_term_ast terms)
+
+let pp_reduction_kind = function
+  | `Reduce -> "reduce"
+  | `Simpl -> "simplify"
+  | `Whd -> "whd"
+  | `Normalize -> "normalize"
+  
+let pp_pattern (t, hyp, goal) = 
+  let pp_hyp_pattern l =
+    String.concat "; "
+      (List.map (fun (name, p) -> sprintf "%s : %s" name (pp_term_ast p)) l) in
+  let pp_t t =
+   match t with
+      None -> ""
+    | Some t -> pp_term_ast t
+  in
+   pp_t t ^ " in " ^ pp_hyp_pattern hyp ^ " \\vdash " ^ pp_term_ast goal
+
+let pp_intros_specs = function
+   | None, []         -> ""
+   | Some num, []     -> Printf.sprintf " names %i" num
+   | None, idents     -> Printf.sprintf " names %s" (pp_idents idents)
+   | Some num, idents -> Printf.sprintf " names %i %s" num (pp_idents idents)
+
+let rec pp_tactic = function
+  | Absurd (_, term) -> "absurd" ^ pp_term_ast term
+  | Apply (_, term) -> "apply " ^ pp_term_ast term
+  | Auto _ -> "auto"
+  | Assumption _ -> "assumption"
+  | Change (_, where, with_what) ->
+      sprintf "change %s with %s" (pp_pattern where) (pp_term_ast with_what)
+  | Clear (_,id) -> sprintf "clear %s" id
+  | ClearBody (_,id) -> sprintf "clearbody %s" id
+  | Compare (_,term) -> "compare " ^ pp_term_ast term
+  | Constructor (_,n) -> "constructor " ^ string_of_int n
+  | Contradiction _ -> "contradiction"
+  | Cut (_, ident, term) ->
+     "cut " ^ pp_term_ast term ^
+      (match ident with None -> "" | Some id -> " as " ^ id)
+  | DecideEquality _ -> "decide equality"
+  | Decompose (_, term) ->
+      sprintf "decompose %s" (pp_term_ast term)
+  | Discriminate (_, term) -> "discriminate " ^ pp_term_ast term
+  | Elim (_, term, using, num, idents) ->
+      sprintf "elim " ^ pp_term_ast term ^
+      (match using with None -> "" | Some term -> " using " ^ pp_term_ast term)
+      ^ pp_intros_specs (num, idents) 
+  | ElimType (_, term, using, num, idents) ->
+      sprintf "elim type " ^ pp_term_ast term ^
+      (match using with None -> "" | Some term -> " using " ^ pp_term_ast term)
+      ^ pp_intros_specs (num, idents)
+  | Exact (_, term) -> "exact " ^ pp_term_ast term
+  | Exists _ -> "exists"
+  | Fold (_, kind, term, pattern) ->
+      sprintf "fold %s %s %s" (pp_reduction_kind kind)
+       (pp_term_ast term) (pp_pattern pattern)
+  | FwdSimpl (_, hyp, idents) -> 
+      sprintf "fwd %s%s" hyp 
+        (match idents with [] -> "" | idents -> " " ^ pp_idents idents)
+  | Generalize (_, pattern, ident) ->
+     sprintf "generalize %s%s" (pp_pattern pattern)
+      (match ident with None -> "" | Some id -> " as " ^ id)
+  | Goal (_, n) -> "goal " ^ string_of_int n
+  | Fail _ -> "fail"
+  | Fourier _ -> "fourier"
+  | IdTac _ -> "id"
+  | Injection (_, term) -> "injection " ^ pp_term_ast term
+  | Intros (_, None, []) -> "intro"
+  | Intros (_, num, idents) ->
+      sprintf "intros%s%s"
+        (match num with None -> "" | Some num -> " " ^ string_of_int num)
+        (match idents with [] -> "" | idents -> " " ^ pp_idents idents)
+  | LApply (_, level_opt, terms, term, ident_opt) -> 
+      sprintf "lapply %s%s%s%s" 
+        (match level_opt with None -> "" | Some i -> " depth = " ^ string_of_int i ^ " ")  
+        (pp_term_ast term) 
+        (match terms with [] -> "" | _ -> " to " ^ pp_terms_ast terms)
+        (match ident_opt with None -> "" | Some ident -> " using " ^ ident)
+  | Left _ -> "left"
+  | LetIn (_, term, ident) -> sprintf "let %s in %s" (pp_term_ast term) ident
+  | Reduce (_, kind, pat) ->
+      sprintf "%s %s" (pp_reduction_kind kind) (pp_pattern pat)
+  | Reflexivity _ -> "reflexivity"
+  | Replace (_, pattern, t) ->
+      sprintf "replace %s with %s" (pp_pattern pattern) (pp_term_ast t)
+  | Rewrite (_, pos, t, pattern) -> 
+      sprintf "rewrite %s %s %s" 
+        (if pos = `LeftToRight then ">" else "<")
+        (pp_term_ast t)
+        (pp_pattern pattern)
+  | Right _ -> "right"
+  | Ring _ -> "ring"
+  | Split _ -> "split"
+  | Symmetry _ -> "symmetry"
+  | Transitivity (_, term) -> "transitivity " ^ pp_term_ast term
+
+let pp_flavour = function
+  | `Definition -> "Definition"
+  | `Fact -> "Fact"
+  | `Goal -> "Goal"
+  | `Lemma -> "Lemma"
+  | `Remark -> "Remark"
+  | `Theorem -> "Theorem"
+  | `Variant -> "Variant"
+
+let pp_search_kind = function
+  | `Locate -> "locate"
+  | `Hint -> "hint"
+  | `Match -> "match"
+  | `Elim -> "elim"
+  | `Instance -> "instance"
+
+let pp_macro pp_term = function 
+  (* Whelp *)
+  | WInstance (_, term) -> "whelp instance " ^ pp_term term
+  | WHint (_, t) -> "whelp hint " ^ pp_term t
+  | WLocate (_, s) -> "whelp locate " ^ s
+  | WElim (_, t) -> "whelp elim " ^ pp_term t
+  | WMatch (_, term) -> "whelp match " ^ pp_term term
+  (* real macros *)
+(*   | Abort _ -> "Abort" *)
+  | Check (_, term) -> sprintf "Check %s" (pp_term term)
+  | Hint _ -> "hint"
+(*   | Redo (_, None) -> "Redo"
+  | Redo (_, Some n) -> sprintf "Redo %d" n *)
+  | Search_pat (_, kind, pat) ->
+      sprintf "search %s \"%s\"" (pp_search_kind kind) pat
+  | Search_term (_, kind, term) ->
+      sprintf "search %s %s" (pp_search_kind kind) (pp_term term)
+(*   | Undo (_, None) -> "Undo"
+  | Undo (_, Some n) -> sprintf "Undo %d" n *)
+  | Print (_, name) -> sprintf "Print \"%s\"" name
+  | Quit _ -> "Quit"
+
+let pp_macro_ast = pp_macro pp_term_ast
+let pp_macro_cic = pp_macro pp_term_cic
+
+let pp_alias = function
+  | Ident_alias (id, uri) -> sprintf "alias id \"%s\" = \"%s\"" id uri
+  | Symbol_alias (symb, instance, desc) ->
+      sprintf "alias symbol \"%s\" (instance %d) = \"%s\""
+        symb instance desc
+  | Number_alias (instance,desc) ->
+      sprintf "alias num (instance %d) = \"%s\"" instance desc
+  
+let pp_params = function
+  | [] -> ""
+  | params ->
+      " " ^
+      String.concat " "
+        (List.map
+          (fun (name, typ) -> sprintf "(%s:%s)" name (pp_term_ast typ))
+          params)
+      
+let pp_fields fields =
+  (if fields <> [] then "\n" else "") ^ 
+  String.concat ";\n" 
+    (List.map (fun (name,ty) -> " " ^ name ^ ": " ^ pp_term_ast ty) fields)
+        
+let pp_obj = function
+ | Inductive (params, types) ->
+    let pp_constructors constructors =
+      String.concat "\n"
+        (List.map (fun (name, typ) -> sprintf "| %s: %s" name (pp_term_ast typ))
+          constructors)
+    in
+    let pp_type (name, _, typ, constructors) =
+      sprintf "\nwith %s: %s \\def\n%s" name (pp_term_ast typ)
+        (pp_constructors constructors)
+    in
+    (match types with
+    | [] -> assert false
+    | (name, inductive, typ, constructors) :: tl ->
+        let fst_typ_pp =
+          sprintf "%sinductive %s%s: %s \\def\n%s"
+            (if inductive then "" else "co") name (pp_params params)
+            (pp_term_ast typ) (pp_constructors constructors)
+        in
+        fst_typ_pp ^ String.concat "" (List.map pp_type tl))
+ | Theorem (flavour, name, typ, body) ->
+    sprintf "%s %s: %s %s"
+      (pp_flavour flavour)
+      name
+      (pp_term_ast typ)
+      (match body with
+      | None -> ""
+      | Some body -> "\\def " ^ pp_term_ast body)
+ | Record (params,name,ty,fields) ->
+    "record " ^ name ^ " " ^ pp_params params ^ " \\def {" ^
+    pp_fields fields ^ "}"
+
+let pp_command = function
+  | Include (_,path) -> "include " ^ path
+  | Qed _ -> "qed"
+  | Drop _ -> "drop"
+  | Set (_, name, value) -> sprintf "set \"%s\" \"%s\"" name value
+  | Coercion (_,term) -> sprintf "coercion %s" (pp_term_ast term)
+  | Alias (_,s) -> pp_alias s
+  | Obj (_,obj) -> pp_obj obj
+  | Default (_,what,uris) ->
+     sprintf "default \"%s\" %s" what
+      (String.concat " " (List.map UriManager.string_of_uri uris))
+
+let rec pp_tactical = function
+  | Tactic (_, tac) -> pp_tactic tac
+  | Do (_, count, tac) -> sprintf "do %d %s" count (pp_tactical tac)
+  | Repeat (_, tac) -> "repeat " ^ pp_tactical tac
+  | Seq (_, tacs) -> pp_tacticals ~sep:"; " tacs
+  | Then (_, tac, tacs) ->
+      sprintf "%s; [%s]" (pp_tactical tac) (pp_tacticals ~sep:" | " tacs)
+  | First (_, tacs) -> sprintf "tries [%s]" (pp_tacticals ~sep:" | " tacs)
+  | Try (_, tac) -> "try " ^ pp_tactical tac
+  | Solve (_, tac) -> sprintf "solve [%s]" (pp_tacticals ~sep:" | " tac)
+
+and pp_tacticals ~sep tacs =
+  String.concat sep (List.map pp_tactical tacs)
+
+let pp_tactical tac = pp_tactical tac ^ tactical_terminator
+let pp_tactic tac = pp_tactic tac ^ tactic_terminator
+let pp_command tac = pp_command tac ^ command_terminator
+
+let pp_executable = function
+  | Macro (_,x) -> pp_macro_ast x
+  | Tactical (_,x) -> pp_tactical x
+  | Command (_,x) -> pp_command x
+                      
+let pp_comment = function
+  | Note (_,str) -> sprintf "(* %s *)" str
+  | Code (_,code) -> sprintf "(** %s. **)" (pp_executable code)
+
+let pp_statement = function
+  | Executable (_, ex) -> pp_executable ex
+  | Comment (_, c) -> pp_comment c
+
+let pp_cic_command = function
+  | Include (_,path) -> "include " ^ path
+  | Qed _ -> "qed"
+  | Drop _ -> "drop"
+  | Coercion (_,term) -> sprintf "coercion %s" (CicPp.ppterm term)
+  | Set (_, _, _)
+  | Alias (_,_)
+  | Default (_,_,_)
+  | Obj (_,_) -> assert false (* not implemented *)