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