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