src/Pure/Proof/proof_syntax.ML
author wenzelm
Mon Nov 02 20:50:48 2009 +0100 (2009-11-02)
changeset 33388 d64545e6cba5
parent 31943 5e960a0780a2
child 35122 305b3eb5b9d5
permissions -rw-r--r--
modernized structure Proof_Syntax;
berghofe@11522
     1
(*  Title:      Pure/Proof/proof_syntax.ML
wenzelm@11539
     2
    Author:     Stefan Berghofer, TU Muenchen
berghofe@11522
     3
berghofe@11522
     4
Function for parsing and printing proof terms.
berghofe@11522
     5
*)
berghofe@11522
     6
berghofe@11522
     7
signature PROOF_SYNTAX =
berghofe@11522
     8
sig
wenzelm@17078
     9
  val proofT: typ
wenzelm@17078
    10
  val add_proof_syntax: theory -> theory
wenzelm@28807
    11
  val proof_of_term: theory -> bool -> term -> Proofterm.proof
wenzelm@17078
    12
  val term_of_proof: Proofterm.proof -> term
wenzelm@17078
    13
  val cterm_of_proof: theory -> Proofterm.proof -> cterm * (cterm -> Proofterm.proof)
wenzelm@17078
    14
  val read_term: theory -> typ -> string -> term
wenzelm@17078
    15
  val read_proof: theory -> bool -> string -> Proofterm.proof
wenzelm@17078
    16
  val proof_syntax: Proofterm.proof -> theory -> theory
wenzelm@17078
    17
  val proof_of: bool -> thm -> Proofterm.proof
wenzelm@27260
    18
  val pretty_proof: Proof.context -> Proofterm.proof -> Pretty.T
wenzelm@27260
    19
  val pretty_proof_of: Proof.context -> bool -> thm -> Pretty.T
berghofe@11522
    20
end;
berghofe@11522
    21
wenzelm@33388
    22
structure Proof_Syntax : PROOF_SYNTAX =
berghofe@11522
    23
struct
berghofe@11522
    24
berghofe@11522
    25
open Proofterm;
berghofe@11522
    26
berghofe@11522
    27
(**** add special syntax for embedding proof terms ****)
berghofe@11522
    28
berghofe@11522
    29
val proofT = Type ("proof", []);
berghofe@11614
    30
val paramT = Type ("param", []);
berghofe@11614
    31
val paramsT = Type ("params", []);
berghofe@11522
    32
val idtT = Type ("idt", []);
wenzelm@24848
    33
val aT = TFree (Name.aT, []);
berghofe@11522
    34
berghofe@11522
    35
(** constants for theorems and axioms **)
berghofe@11522
    36
wenzelm@16425
    37
fun add_proof_atom_consts names thy =
wenzelm@16425
    38
  thy
wenzelm@30435
    39
  |> Sign.root_path
wenzelm@30435
    40
  |> Sign.add_consts_i (map (fn name => (Binding.qualified_name name, proofT, NoSyn)) names);
berghofe@11522
    41
berghofe@11522
    42
(** constants for application and abstraction **)
berghofe@11614
    43
wenzelm@16425
    44
fun add_proof_syntax thy =
wenzelm@16425
    45
  thy
wenzelm@16425
    46
  |> Theory.copy
wenzelm@22796
    47
  |> Sign.root_path
wenzelm@22796
    48
  |> Sign.add_defsort_i []
wenzelm@30344
    49
  |> Sign.add_types [(Binding.name "proof", 0, NoSyn)]
wenzelm@22796
    50
  |> Sign.add_consts_i
wenzelm@30344
    51
      [(Binding.name "Appt", [proofT, aT] ---> proofT, Mixfix ("(1_ %/ _)", [4, 5], 4)),
wenzelm@30344
    52
       (Binding.name "AppP", [proofT, proofT] ---> proofT, Mixfix ("(1_ %%/ _)", [4, 5], 4)),
wenzelm@30344
    53
       (Binding.name "Abst", (aT --> proofT) --> proofT, NoSyn),
wenzelm@30344
    54
       (Binding.name "AbsP", [propT, proofT --> proofT] ---> proofT, NoSyn),
wenzelm@30344
    55
       (Binding.name "Hyp", propT --> proofT, NoSyn),
wenzelm@30344
    56
       (Binding.name "Oracle", propT --> proofT, NoSyn),
wenzelm@31943
    57
       (Binding.name "OfClass", (Term.a_itselfT --> propT) --> proofT, NoSyn),
wenzelm@30344
    58
       (Binding.name "MinProof", proofT, Delimfix "?")]
wenzelm@30344
    59
  |> Sign.add_nonterminals [Binding.name "param", Binding.name "params"]
wenzelm@22796
    60
  |> Sign.add_syntax_i
berghofe@11640
    61
      [("_Lam", [paramsT, proofT] ---> proofT, Mixfix ("(1Lam _./ _)", [0, 3], 3)),
berghofe@11614
    62
       ("_Lam0", [paramT, paramsT] ---> paramsT, Mixfix ("_/ _", [1, 0], 0)),
berghofe@11614
    63
       ("_Lam0", [idtT, paramsT] ---> paramsT, Mixfix ("_/ _", [1, 0], 0)),
berghofe@11614
    64
       ("_Lam1", [idtT, propT] ---> paramT, Mixfix ("_: _", [0, 0], 0)),
berghofe@11614
    65
       ("", paramT --> paramT, Delimfix "'(_')"),
berghofe@11614
    66
       ("", idtT --> paramsT, Delimfix "_"),
berghofe@11614
    67
       ("", paramT --> paramsT, Delimfix "_")]
wenzelm@22796
    68
  |> Sign.add_modesyntax_i ("xsymbols", true)
berghofe@11640
    69
      [("_Lam", [paramsT, proofT] ---> proofT, Mixfix ("(1\\<Lambda>_./ _)", [0, 3], 3)),
berghofe@11522
    70
       ("Appt", [proofT, aT] ---> proofT, Mixfix ("(1_ \\<cdot>/ _)", [4, 5], 4)),
wenzelm@16425
    71
       ("AppP", [proofT, proofT] ---> proofT, Mixfix ("(1_ \\<bullet>/ _)", [4, 5], 4))]
wenzelm@22796
    72
  |> Sign.add_modesyntax_i ("latex", false)
wenzelm@16425
    73
      [("_Lam", [paramsT, proofT] ---> proofT, Mixfix ("(1\\<^bold>\\<lambda>_./ _)", [0, 3], 3))]
wenzelm@22796
    74
  |> Sign.add_trrules_i (map Syntax.ParsePrintRule
berghofe@11522
    75
      [(Syntax.mk_appl (Constant "_Lam")
berghofe@11614
    76
          [Syntax.mk_appl (Constant "_Lam0") [Variable "l", Variable "m"], Variable "A"],
berghofe@11614
    77
        Syntax.mk_appl (Constant "_Lam")
berghofe@11614
    78
          [Variable "l", Syntax.mk_appl (Constant "_Lam") [Variable "m", Variable "A"]]),
berghofe@11614
    79
       (Syntax.mk_appl (Constant "_Lam")
berghofe@11522
    80
          [Syntax.mk_appl (Constant "_Lam1") [Variable "x", Variable "A"], Variable "B"],
berghofe@11522
    81
        Syntax.mk_appl (Constant "AbsP") [Variable "A",
berghofe@11522
    82
          (Syntax.mk_appl (Constant "_abs") [Variable "x", Variable "B"])]),
berghofe@11614
    83
       (Syntax.mk_appl (Constant "_Lam") [Variable "x", Variable "A"],
berghofe@11522
    84
        Syntax.mk_appl (Constant "Abst")
berghofe@11614
    85
          [(Syntax.mk_appl (Constant "_abs") [Variable "x", Variable "A"])])]);
berghofe@11522
    86
berghofe@11522
    87
berghofe@11522
    88
(**** translation between proof terms and pure terms ****)
berghofe@11522
    89
wenzelm@28807
    90
fun proof_of_term thy ty =
berghofe@11522
    91
  let
wenzelm@16350
    92
    val thms = PureThy.all_thms_of thy;
wenzelm@16350
    93
    val axms = Theory.all_axioms_of thy;
berghofe@11522
    94
wenzelm@20548
    95
    fun mk_term t = (if ty then I else map_types (K dummyT))
berghofe@11614
    96
      (Term.no_dummy_patterns t);
berghofe@11614
    97
berghofe@11522
    98
    fun prf_of [] (Bound i) = PBound i
berghofe@11522
    99
      | prf_of Ts (Const (s, Type ("proof", _))) =
skalberg@15531
   100
          change_type (if ty then SOME Ts else NONE)
wenzelm@30364
   101
            (case Long_Name.explode s of
berghofe@11614
   102
               "axm" :: xs =>
berghofe@11522
   103
                 let
wenzelm@30364
   104
                   val name = Long_Name.implode xs;
wenzelm@17223
   105
                   val prop = (case AList.lookup (op =) axms name of
skalberg@15531
   106
                       SOME prop => prop
skalberg@15531
   107
                     | NONE => error ("Unknown axiom " ^ quote name))
skalberg@15531
   108
                 in PAxm (name, prop, NONE) end
berghofe@11614
   109
             | "thm" :: xs =>
wenzelm@30364
   110
                 let val name = Long_Name.implode xs;
wenzelm@17223
   111
                 in (case AList.lookup (op =) thms name of
wenzelm@28814
   112
                     SOME thm => fst (strip_combt (Thm.proof_of thm))
wenzelm@28807
   113
                   | NONE => error ("Unknown theorem " ^ quote name))
berghofe@11522
   114
                 end
berghofe@11522
   115
             | _ => error ("Illegal proof constant name: " ^ quote s))
wenzelm@31943
   116
      | prf_of Ts (Const ("OfClass", _) $ Const (c_class, _)) =
wenzelm@31903
   117
          (case try Logic.class_of_const c_class of
wenzelm@31903
   118
            SOME c =>
wenzelm@31903
   119
              change_type (if ty then SOME Ts else NONE)
wenzelm@31943
   120
                (OfClass (TVar ((Name.aT, 0), []), c))
wenzelm@31903
   121
          | NONE => error ("Bad class constant: " ^ quote c_class))
berghofe@13199
   122
      | prf_of Ts (Const ("Hyp", _) $ prop) = Hyp prop
berghofe@11522
   123
      | prf_of Ts (v as Var ((_, Type ("proof", _)))) = Hyp v
berghofe@11522
   124
      | prf_of [] (Const ("Abst", _) $ Abs (s, T, prf)) =
berghofe@25245
   125
          if T = proofT then
berghofe@25245
   126
            error ("Term variable abstraction may not bind proof variable " ^ quote s)
berghofe@25245
   127
          else Abst (s, if ty then SOME T else NONE,
berghofe@11522
   128
            incr_pboundvars (~1) 0 (prf_of [] prf))
berghofe@11522
   129
      | prf_of [] (Const ("AbsP", _) $ t $ Abs (s, _, prf)) =
berghofe@11614
   130
          AbsP (s, case t of
skalberg@15531
   131
                Const ("dummy_pattern", _) => NONE
skalberg@15531
   132
              | _ $ Const ("dummy_pattern", _) => NONE
skalberg@15531
   133
              | _ => SOME (mk_term t),
berghofe@11522
   134
            incr_pboundvars 0 (~1) (prf_of [] prf))
berghofe@11522
   135
      | prf_of [] (Const ("AppP", _) $ prf1 $ prf2) =
berghofe@11614
   136
          prf_of [] prf1 %% prf_of [] prf2
berghofe@11522
   137
      | prf_of Ts (Const ("Appt", _) $ prf $ Const ("TYPE", Type (_, [T]))) =
berghofe@11522
   138
          prf_of (T::Ts) prf
berghofe@11614
   139
      | prf_of [] (Const ("Appt", _) $ prf $ t) = prf_of [] prf %
skalberg@15531
   140
          (case t of Const ("dummy_pattern", _) => NONE | _ => SOME (mk_term t))
berghofe@11522
   141
      | prf_of _ t = error ("Not a proof term:\n" ^
wenzelm@26939
   142
          Syntax.string_of_term_global thy t)
berghofe@11522
   143
berghofe@11522
   144
  in prf_of [] end;
berghofe@11522
   145
berghofe@11522
   146
berghofe@11522
   147
val AbsPt = Const ("AbsP", [propT, proofT --> proofT] ---> proofT);
berghofe@11522
   148
val AppPt = Const ("AppP", [proofT, proofT] ---> proofT);
berghofe@13199
   149
val Hypt = Const ("Hyp", propT --> proofT);
berghofe@13199
   150
val Oraclet = Const ("Oracle", propT --> proofT);
wenzelm@31943
   151
val OfClasst = Const ("OfClass", (Term.itselfT dummyT --> propT) --> proofT);
berghofe@13199
   152
val MinProoft = Const ("MinProof", proofT);
berghofe@11522
   153
wenzelm@19473
   154
val mk_tyapp = fold (fn T => fn prf => Const ("Appt",
wenzelm@19391
   155
  [proofT, Term.itselfT T] ---> proofT) $ prf $ Logic.mk_type T);
berghofe@11522
   156
wenzelm@28807
   157
fun term_of _ (PThm (_, ((name, _, NONE), _))) =
wenzelm@30364
   158
      Const (Long_Name.append "thm" name, proofT)
wenzelm@28807
   159
  | term_of _ (PThm (_, ((name, _, SOME Ts), _))) =
wenzelm@30364
   160
      mk_tyapp Ts (Const (Long_Name.append "thm" name, proofT))
wenzelm@30364
   161
  | term_of _ (PAxm (name, _, NONE)) = Const (Long_Name.append "axm" name, proofT)
skalberg@15531
   162
  | term_of _ (PAxm (name, _, SOME Ts)) =
wenzelm@30364
   163
      mk_tyapp Ts (Const (Long_Name.append "axm" name, proofT))
wenzelm@31943
   164
  | term_of _ (OfClass (T, c)) =
wenzelm@31943
   165
      mk_tyapp [T] (OfClasst $ Const (Logic.const_of_class c, Term.itselfT dummyT --> propT))
berghofe@11522
   166
  | term_of _ (PBound i) = Bound i
wenzelm@27260
   167
  | term_of Ts (Abst (s, opT, prf)) =
wenzelm@18939
   168
      let val T = the_default dummyT opT
berghofe@11522
   169
      in Const ("Abst", (T --> proofT) --> proofT) $
berghofe@11522
   170
        Abs (s, T, term_of (T::Ts) (incr_pboundvars 1 0 prf))
berghofe@11522
   171
      end
berghofe@11522
   172
  | term_of Ts (AbsP (s, t, prf)) =
wenzelm@18939
   173
      AbsPt $ the_default (Term.dummy_pattern propT) t $
berghofe@11522
   174
        Abs (s, proofT, term_of (proofT::Ts) (incr_pboundvars 0 1 prf))
berghofe@11614
   175
  | term_of Ts (prf1 %% prf2) =
berghofe@11522
   176
      AppPt $ term_of Ts prf1 $ term_of Ts prf2
wenzelm@27260
   177
  | term_of Ts (prf % opt) =
wenzelm@18939
   178
      let val t = the_default (Term.dummy_pattern dummyT) opt
berghofe@11522
   179
      in Const ("Appt",
berghofe@11522
   180
        [proofT, fastype_of1 (Ts, t) handle TERM _ => dummyT] ---> proofT) $
berghofe@11522
   181
          term_of Ts prf $ t
berghofe@11522
   182
      end
berghofe@11522
   183
  | term_of Ts (Hyp t) = Hypt $ t
berghofe@11522
   184
  | term_of Ts (Oracle (_, t, _)) = Oraclet $ t
wenzelm@28807
   185
  | term_of Ts MinProof = MinProoft;
berghofe@11522
   186
berghofe@11522
   187
val term_of_proof = term_of [];
berghofe@11522
   188
berghofe@11522
   189
fun cterm_of_proof thy prf =
berghofe@11522
   190
  let
wenzelm@28807
   191
    val thm_names = map fst (PureThy.all_thms_of thy);
wenzelm@16350
   192
    val axm_names = map fst (Theory.all_axioms_of thy);
wenzelm@16425
   193
    val thy' = thy
wenzelm@16425
   194
      |> add_proof_syntax
wenzelm@16425
   195
      |> add_proof_atom_consts
wenzelm@30364
   196
        (map (Long_Name.append "axm") axm_names @ map (Long_Name.append "thm") thm_names);
berghofe@11522
   197
  in
wenzelm@28807
   198
    (cterm_of thy' (term_of_proof prf), proof_of_term thy true o Thm.term_of)
berghofe@11522
   199
  end;
berghofe@11522
   200
berghofe@11522
   201
fun read_term thy =
berghofe@11522
   202
  let
wenzelm@28375
   203
    val thm_names = filter_out (fn s => s = "") (map fst (PureThy.all_thms_of thy));
wenzelm@16350
   204
    val axm_names = map fst (Theory.all_axioms_of thy);
wenzelm@27260
   205
    val ctxt = thy
wenzelm@16425
   206
      |> add_proof_syntax
wenzelm@16425
   207
      |> add_proof_atom_consts
wenzelm@30364
   208
        (map (Long_Name.append "axm") axm_names @ map (Long_Name.append "thm") thm_names)
wenzelm@27260
   209
      |> ProofContext.init
wenzelm@27260
   210
      |> ProofContext.allow_dummies
wenzelm@27260
   211
      |> ProofContext.set_mode ProofContext.mode_schematic;
wenzelm@27260
   212
  in
wenzelm@27260
   213
    fn ty => fn s =>
wenzelm@27260
   214
      (if ty = propT then Syntax.parse_prop else Syntax.parse_term) ctxt s
wenzelm@27260
   215
      |> TypeInfer.constrain ty |> Syntax.check_term ctxt
wenzelm@27260
   216
  end;
berghofe@11522
   217
berghofe@11522
   218
fun read_proof thy =
berghofe@11522
   219
  let val rd = read_term thy proofT
wenzelm@28807
   220
  in fn ty => fn s => proof_of_term thy ty (Logic.varify (rd s)) end;
berghofe@11522
   221
wenzelm@17078
   222
fun proof_syntax prf =
berghofe@11522
   223
  let
wenzelm@28807
   224
    val thm_names = Symtab.keys (fold_proof_atoms true
wenzelm@28807
   225
      (fn PThm (_, ((name, _, _), _)) => if name <> "" then Symtab.update (name, ()) else I
wenzelm@28807
   226
        | _ => I) [prf] Symtab.empty);
wenzelm@28807
   227
    val axm_names = Symtab.keys (fold_proof_atoms true
wenzelm@28807
   228
      (fn PAxm (name, _, _) => Symtab.update (name, ()) | _ => I) [prf] Symtab.empty);
berghofe@11522
   229
  in
wenzelm@17078
   230
    add_proof_syntax #>
wenzelm@17078
   231
    add_proof_atom_consts
wenzelm@30364
   232
      (map (Long_Name.append "thm") thm_names @ map (Long_Name.append "axm") axm_names)
berghofe@11522
   233
  end;
berghofe@11522
   234
wenzelm@17078
   235
fun proof_of full thm =
wenzelm@17078
   236
  let
wenzelm@26626
   237
    val thy = Thm.theory_of_thm thm;
wenzelm@17078
   238
    val prop = Thm.full_prop_of thm;
wenzelm@28814
   239
    val prf = Thm.proof_of thm;
wenzelm@17078
   240
    val prf' = (case strip_combt (fst (strip_combP prf)) of
wenzelm@29635
   241
        (PThm (_, ((_, prop', _), body)), _) => if prop = prop' then join_proof body else prf
wenzelm@17078
   242
      | _ => prf)
wenzelm@17078
   243
  in if full then Reconstruct.reconstruct_proof thy prop prf' else prf' end;
wenzelm@17078
   244
wenzelm@27260
   245
fun pretty_proof ctxt prf =
wenzelm@27260
   246
  ProofContext.pretty_term_abbrev
wenzelm@27260
   247
    (ProofContext.transfer_syntax (proof_syntax prf (ProofContext.theory_of ctxt)) ctxt)
wenzelm@27260
   248
    (term_of_proof prf);
wenzelm@17078
   249
wenzelm@27260
   250
fun pretty_proof_of ctxt full th =
wenzelm@27260
   251
  pretty_proof ctxt (proof_of full th);
berghofe@11522
   252
berghofe@11522
   253
end;