src/Pure/Proof/proof_syntax.ML
author berghofe
Fri, 31 Aug 2001 16:17:05 +0200
changeset 11522 42fbb6abed5a
child 11539 0f17da240450
permissions -rw-r--r--
Initial revision of tools for proof terms.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
     1
(*  Title:      Pure/Proof/proof_syntax.ML
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
     2
    ID:         $Id$
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
     3
    Author:     Stefan Berghofer
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
     4
    Copyright   2000  TU Muenchen
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
     5
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
     6
Function for parsing and printing proof terms.
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
     7
*)
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
     8
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
     9
signature PROOF_SYNTAX =
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    10
sig
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    11
  val proofT : typ
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    12
  val add_proof_syntax : Sign.sg -> Sign.sg
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    13
  val disambiguate_names : theory -> Proofterm.proof ->
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    14
    Proofterm.proof * Proofterm.proof Symtab.table
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    15
  val proof_of_term : theory -> Proofterm.proof Symtab.table ->
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    16
    bool -> term -> Proofterm.proof
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    17
  val term_of_proof : Proofterm.proof -> term
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    18
  val cterm_of_proof : theory -> Proofterm.proof -> cterm * (cterm -> Proofterm.proof)
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    19
  val read_term : theory -> typ -> string -> term
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    20
  val read_proof : theory -> bool -> string -> Proofterm.proof
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    21
  val pretty_proof : Sign.sg -> Proofterm.proof -> Pretty.T
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    22
  val pretty_proof_of : bool -> thm -> Pretty.T
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    23
  val print_proof_of : bool -> thm -> unit
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    24
end;
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    25
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    26
structure ProofSyntax : PROOF_SYNTAX =
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    27
struct
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    28
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    29
open Proofterm;
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    30
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    31
(**** add special syntax for embedding proof terms ****)
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    32
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    33
val proofT = Type ("proof", []);
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    34
val lamT = Type ("lam_syn", []);
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    35
val idtT = Type ("idt", []);
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    36
val aT = TFree ("'a", ["logic"]);
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    37
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    38
(** constants for theorems and axioms **)
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    39
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    40
fun add_prefix a b = NameSpace.pack (a :: NameSpace.unpack b);
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    41
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    42
fun add_proof_atom_consts names sg = Sign.add_consts_i
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    43
  (map (fn name => (name, proofT, NoSyn)) names) (Sign.add_path "//" sg);
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    44
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    45
(** constants for application and abstraction **)
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    46
  
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    47
fun add_proof_syntax sg =
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    48
  sg
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    49
  |> Sign.copy
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    50
  |> Sign.add_path "/"
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    51
  |> Sign.add_defsort_i ["logic"]
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    52
  |> Sign.add_types [("proof", 0, NoSyn)]
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    53
  |> Sign.add_arities [("proof", [], "logic")]
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    54
  |> Sign.add_consts_i
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    55
      [("Appt", [proofT, aT] ---> proofT, Mixfix ("(1_ %%/ _)", [4, 5], 4)),
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    56
       ("AppP", [proofT, proofT] ---> proofT, Mixfix ("(1_ %/ _)", [4, 5], 4)),
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    57
       ("Abst", (aT --> proofT) --> proofT, NoSyn),
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    58
       ("AbsP", [propT, proofT --> proofT] ---> proofT, NoSyn)]
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    59
  |> Sign.add_nonterminals ["lam_syn"]
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    60
  |> Sign.add_syntax_i
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    61
      [("_Lam", [lamT, proofT] ---> proofT, Mixfix ("(3Lam _./ _)", [0,0], 1)),
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    62
       ("_Lam0", [lamT, lamT] ---> lamT, Mixfix ("_,/ _", [1, 0], 0)),
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    63
       ("_Lam1", [idtT, propT] ---> lamT, Mixfix ("_ : _", [0, 0], 1)),
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    64
       ("_Lam2", idtT --> lamT, Mixfix ("_", [0], 1))]
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    65
  |> Sign.add_modesyntax_i (("xsymbols", true),
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    66
      [("_Lam", [lamT, proofT] ---> proofT, Mixfix ("(3\\<Lambda>_./ _)", [0,0], 1)),
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    67
       ("Appt", [proofT, aT] ---> proofT, Mixfix ("(1_ \\<cdot>/ _)", [4, 5], 4)),
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    68
       ("AppP", [proofT, proofT] ---> proofT, Mixfix ("(1_ \\<bullet>/ _)", [4, 5], 4))])
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    69
  |> Sign.add_trrules_i (map Syntax.ParsePrintRule
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    70
      [(Syntax.mk_appl (Constant "_Lam")
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    71
          [Syntax.mk_appl (Constant "_Lam1") [Variable "x", Variable "A"], Variable "B"],
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    72
        Syntax.mk_appl (Constant "AbsP") [Variable "A",
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    73
          (Syntax.mk_appl (Constant "_abs") [Variable "x", Variable "B"])]),
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    74
       (Syntax.mk_appl (Constant "_Lam")
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    75
          [Syntax.mk_appl (Constant "_Lam2") [Variable "x"], Variable "A"],
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    76
        Syntax.mk_appl (Constant "Abst")
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    77
          [(Syntax.mk_appl (Constant "_abs") [Variable "x", Variable "A"])]),
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    78
       (Syntax.mk_appl (Constant "_Lam")
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    79
          [Syntax.mk_appl (Constant "_Lam0") [Variable "l", Variable "m"], Variable "A"],
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    80
        Syntax.mk_appl (Constant "_Lam")
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    81
          [Variable "l", Syntax.mk_appl (Constant "_Lam") [Variable "m", Variable "A"]])]);
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    82
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    83
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    84
(**** create unambiguous theorem names ****)
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    85
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    86
fun disambiguate_names thy prf =
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    87
  let
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    88
    val thms = thms_of_proof Symtab.empty prf;
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    89
    val thms' = map (apsnd (#prop o rep_thm)) (flat
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    90
      (map PureThy.thms_of (thy :: Theory.ancestors_of thy)));
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    91
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    92
    val tab = Symtab.foldl (fn (tab, (key, ps)) =>
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    93
      let val prop = if_none (assoc (thms', key)) (Bound 0)
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    94
      in fst (foldr (fn ((prop', prf), x as (tab, i)) =>
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    95
        if prop <> prop' then
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    96
          (Symtab.update ((key ^ "_" ^ string_of_int i, prf), tab), i+1)
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    97
        else x) (ps, (tab, 1)))
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    98
      end) (Symtab.empty, thms);
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    99
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   100
    fun rename (Abst (s, T, prf)) = Abst (s, T, rename prf)
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   101
      | rename (AbsP (s, t, prf)) = AbsP (s, t, rename prf)
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   102
      | rename (prf1 % prf2) = rename prf1 % rename prf2
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   103
      | rename (prf %% t) = rename prf %% t
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   104
      | rename (prf' as PThm ((s, tags), prf, prop, Ts)) =
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   105
          let
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   106
            val prop' = if_none (assoc (thms', s)) (Bound 0);
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   107
            val ps = map fst (the (Symtab.lookup (thms, s))) \ prop'
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   108
          in if prop = prop' then prf' else
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   109
            PThm ((s ^ "_" ^ string_of_int (length ps - find_index_eq prop ps), tags),
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   110
              prf, prop, Ts)
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   111
          end
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   112
      | rename prf = prf
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   113
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   114
  in (rename prf, tab) end;
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   115
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   116
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   117
(**** translation between proof terms and pure terms ****)
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   118
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   119
fun change_type T (PThm (name, prf, prop, _)) = PThm (name, prf, prop, T)
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   120
  | change_type T (PAxm (name, prop, _)) = PAxm (name, prop, T)
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   121
  | change_type _ _ = error "Not a proper theorem";
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   122
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   123
fun proof_of_term thy tab ty =
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   124
  let
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   125
    val thys = thy :: Theory.ancestors_of thy;
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   126
    val thms = flat (map thms_of thys);
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   127
    val axms = flat (map (Symtab.dest o #axioms o rep_theory) thys);
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   128
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   129
    fun prf_of [] (Bound i) = PBound i
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   130
      | prf_of Ts (Const (s, Type ("proof", _))) =
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   131
          change_type (if ty then Some Ts else None)
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   132
            (case NameSpace.unpack s of
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   133
               "Axm" :: xs =>
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   134
                 let
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   135
                   val name = NameSpace.pack xs;
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   136
                   val prop = (case assoc (axms, name) of
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   137
                       Some prop => prop
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   138
                     | None => error ("Unknown axiom " ^ quote name))
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   139
                 in PAxm (name, prop, None) end
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   140
             | "Thm" :: xs =>
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   141
                 let val name = NameSpace.pack xs;
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   142
                 in (case assoc (thms, name) of
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   143
                     Some thm => fst (strip_combt (#2 (#der (rep_thm thm))))
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   144
                   | None => (case Symtab.lookup (tab, name) of
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   145
                         Some prf => prf
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   146
                       | None => error ("Unknown theorem " ^ quote name)))
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   147
                 end
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   148
             | _ => error ("Illegal proof constant name: " ^ quote s))
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   149
      | prf_of Ts (v as Var ((_, Type ("proof", _)))) = Hyp v
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   150
      | prf_of [] (Const ("Abst", _) $ Abs (s, T, prf)) =
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   151
          Abst (s, if ty then Some T else None,
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   152
            incr_pboundvars (~1) 0 (prf_of [] prf))
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   153
      | prf_of [] (Const ("AbsP", _) $ t $ Abs (s, _, prf)) =
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   154
          AbsP (s, case t of Const ("dummy_pattern", _) => None | _ => Some t,
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   155
            incr_pboundvars 0 (~1) (prf_of [] prf))
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   156
      | prf_of [] (Const ("AppP", _) $ prf1 $ prf2) =
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   157
          prf_of [] prf1 % prf_of [] prf2
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   158
      | prf_of Ts (Const ("Appt", _) $ prf $ Const ("TYPE", Type (_, [T]))) =
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   159
          prf_of (T::Ts) prf
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   160
      | prf_of [] (Const ("Appt", _) $ prf $ t) = prf_of [] prf %%
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   161
          (case t of Const ("dummy_pattern", _) => None | _ => Some t)
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   162
      | prf_of _ t = error ("Not a proof term:\n" ^
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   163
          Sign.string_of_term (sign_of thy) t)
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   164
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   165
  in prf_of [] end;
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   166
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   167
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   168
val AbsPt = Const ("AbsP", [propT, proofT --> proofT] ---> proofT);
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   169
val AppPt = Const ("AppP", [proofT, proofT] ---> proofT);
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   170
val Hypt = Free ("Hyp", propT --> proofT);
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   171
val Oraclet = Free ("Oracle", propT --> proofT);
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   172
val MinProoft = Free ("?", proofT);
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   173
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   174
val mk_tyapp = foldl (fn (prf, T) => Const ("Appt",
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   175
  [proofT, itselfT T] ---> proofT) $ prf $ Logic.mk_type T);
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   176
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   177
fun term_of _ (PThm ((name, _), _, _, None)) =
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   178
      Const (add_prefix "Thm" name, proofT)
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   179
  | term_of _ (PThm ((name, _), _, _, Some Ts)) =
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   180
      mk_tyapp (Const (add_prefix "Thm" name, proofT), Ts)
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   181
  | term_of _ (PAxm (name, _, None)) = Const (add_prefix "Axm" name, proofT)
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   182
  | term_of _ (PAxm (name, _, Some Ts)) =
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   183
      mk_tyapp (Const (add_prefix "Axm" name, proofT), Ts)
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   184
  | term_of _ (PBound i) = Bound i
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   185
  | term_of Ts (Abst (s, opT, prf)) = 
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   186
      let val T = if_none opT dummyT
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   187
      in Const ("Abst", (T --> proofT) --> proofT) $
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   188
        Abs (s, T, term_of (T::Ts) (incr_pboundvars 1 0 prf))
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   189
      end
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   190
  | term_of Ts (AbsP (s, t, prf)) =
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   191
      AbsPt $ if_none t (Const ("dummy_pattern", propT)) $
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   192
        Abs (s, proofT, term_of (proofT::Ts) (incr_pboundvars 0 1 prf))
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   193
  | term_of Ts (prf1 % prf2) =
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   194
      AppPt $ term_of Ts prf1 $ term_of Ts prf2
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   195
  | term_of Ts (prf %% opt) = 
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   196
      let val t = if_none opt (Const ("dummy_pattern", dummyT))
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   197
      in Const ("Appt",
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   198
        [proofT, fastype_of1 (Ts, t) handle TERM _ => dummyT] ---> proofT) $
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   199
          term_of Ts prf $ t
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   200
      end
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   201
  | term_of Ts (Hyp t) = Hypt $ t
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   202
  | term_of Ts (Oracle (_, t, _)) = Oraclet $ t
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   203
  | term_of Ts (MinProof _) = MinProoft;
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   204
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   205
val term_of_proof = term_of [];
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   206
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   207
fun cterm_of_proof thy prf =
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   208
  let
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   209
    val (prf', tab) = disambiguate_names thy prf;
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   210
    val thys = thy :: Theory.ancestors_of thy;
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   211
    val thm_names = filter_out (equal "") (map fst (flat (map thms_of thys))) @
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   212
      map fst (Symtab.dest tab);
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   213
    val axm_names = map fst (flat (map (Symtab.dest o #axioms o rep_theory) thys));
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   214
    val sg = sign_of thy |>
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   215
      add_proof_syntax |>
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   216
      add_proof_atom_consts
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   217
        (map (add_prefix "Thm") thm_names @ map (add_prefix "Axm") axm_names)
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   218
  in
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   219
    (cterm_of sg (term_of_proof prf'),
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   220
     proof_of_term thy tab true o Thm.term_of)
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   221
  end;
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   222
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   223
fun read_term thy =
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   224
  let
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   225
    val thys = thy :: Theory.ancestors_of thy;
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   226
    val thm_names = filter_out (equal "") (map fst (flat (map thms_of thys)));
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   227
    val axm_names = map fst (flat (map (Symtab.dest o #axioms o rep_theory) thys));
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   228
    val sg = sign_of thy |>
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   229
      add_proof_syntax |>
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   230
      add_proof_atom_consts
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   231
        (map (add_prefix "Thm") thm_names @ map (add_prefix "Axm") axm_names)
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   232
  in
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   233
    (fn T => fn s => Thm.term_of (read_cterm sg (s, T)))
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   234
  end;
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   235
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   236
fun read_proof thy =
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   237
  let val rd = read_term thy proofT
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   238
  in
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   239
    (fn ty => fn s => proof_of_term thy Symtab.empty ty (Logic.varify (rd s)))
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   240
  end;
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   241
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   242
fun pretty_proof sg prf =
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   243
  let
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   244
    val thm_names = map fst (Symtab.dest (thms_of_proof Symtab.empty prf)) \ "";
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   245
    val axm_names = map fst (Symtab.dest (axms_of_proof Symtab.empty prf));
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   246
    val sg' = sg |>
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   247
      add_proof_syntax |>
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   248
      add_proof_atom_consts
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   249
        (map (add_prefix "Thm") thm_names @ map (add_prefix "Axm") axm_names)
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   250
  in
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   251
    Sign.pretty_term sg' (term_of_proof prf)
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   252
  end;
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   253
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   254
fun pretty_proof_of full thm =
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   255
  let
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   256
    val {sign, der = (_, prf), prop, ...} = rep_thm thm;
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   257
    val prf' = (case strip_combt (fst (strip_combP prf)) of
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   258
        (PThm (_, prf', prop', _), _) => if prop=prop' then prf' else prf
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   259
      | _ => prf)
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   260
  in
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   261
    pretty_proof sign
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   262
      (if full then Reconstruct.reconstruct_prf sign prop prf' else prf')
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   263
  end;
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   264
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   265
val print_proof_of = Pretty.writeln oo pretty_proof_of;
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   266
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   267
end;