src/Pure/Proof/proof_syntax.ML
author paulson
Fri, 05 Oct 2007 09:59:03 +0200
changeset 24854 0ebcd575d3c6
parent 24848 5dbbd33c3236
child 25245 1fcfcdcba53c
permissions -rw-r--r--
filtering out some package theorems
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$
11539
0f17da240450 tuned headers;
wenzelm
parents: 11522
diff changeset
     3
    Author:     Stefan Berghofer, TU Muenchen
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
     4
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
     5
Function for parsing and printing proof terms.
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
     6
*)
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
signature PROOF_SYNTAX =
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
     9
sig
17078
db9d24c8b439 export proof_syntax, proof_of;
wenzelm
parents: 17019
diff changeset
    10
  val proofT: typ
db9d24c8b439 export proof_syntax, proof_of;
wenzelm
parents: 17019
diff changeset
    11
  val add_proof_syntax: theory -> theory
db9d24c8b439 export proof_syntax, proof_of;
wenzelm
parents: 17019
diff changeset
    12
  val disambiguate_names: theory -> Proofterm.proof ->
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    13
    Proofterm.proof * Proofterm.proof Symtab.table
17078
db9d24c8b439 export proof_syntax, proof_of;
wenzelm
parents: 17019
diff changeset
    14
  val proof_of_term: theory -> Proofterm.proof Symtab.table ->
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    15
    bool -> term -> Proofterm.proof
17078
db9d24c8b439 export proof_syntax, proof_of;
wenzelm
parents: 17019
diff changeset
    16
  val term_of_proof: Proofterm.proof -> term
db9d24c8b439 export proof_syntax, proof_of;
wenzelm
parents: 17019
diff changeset
    17
  val cterm_of_proof: theory -> Proofterm.proof -> cterm * (cterm -> Proofterm.proof)
db9d24c8b439 export proof_syntax, proof_of;
wenzelm
parents: 17019
diff changeset
    18
  val read_term: theory -> typ -> string -> term
db9d24c8b439 export proof_syntax, proof_of;
wenzelm
parents: 17019
diff changeset
    19
  val read_proof: theory -> bool -> string -> Proofterm.proof
db9d24c8b439 export proof_syntax, proof_of;
wenzelm
parents: 17019
diff changeset
    20
  val proof_syntax: Proofterm.proof -> theory -> theory
db9d24c8b439 export proof_syntax, proof_of;
wenzelm
parents: 17019
diff changeset
    21
  val proof_of: bool -> thm -> Proofterm.proof
db9d24c8b439 export proof_syntax, proof_of;
wenzelm
parents: 17019
diff changeset
    22
  val pretty_proof: theory -> Proofterm.proof -> Pretty.T
db9d24c8b439 export proof_syntax, proof_of;
wenzelm
parents: 17019
diff changeset
    23
  val pretty_proof_of: bool -> thm -> Pretty.T
db9d24c8b439 export proof_syntax, proof_of;
wenzelm
parents: 17019
diff changeset
    24
  val print_proof_of: bool -> thm -> unit
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    25
end;
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    26
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    27
structure ProofSyntax : PROOF_SYNTAX =
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    28
struct
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    29
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    30
open Proofterm;
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    31
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    32
(**** add special syntax for embedding proof terms ****)
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    33
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    34
val proofT = Type ("proof", []);
11614
3131fa12d425 - Tuned syntax
berghofe
parents: 11539
diff changeset
    35
val paramT = Type ("param", []);
3131fa12d425 - Tuned syntax
berghofe
parents: 11539
diff changeset
    36
val paramsT = Type ("params", []);
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    37
val idtT = Type ("idt", []);
24848
5dbbd33c3236 replaced literal 'a by Name.aT;
wenzelm
parents: 22796
diff changeset
    38
val aT = TFree (Name.aT, []);
11522
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
(** constants for theorems and axioms **)
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    41
16425
2427be27cc60 accomodate identification of type Sign.sg and theory;
wenzelm
parents: 16350
diff changeset
    42
fun add_proof_atom_consts names thy =
2427be27cc60 accomodate identification of type Sign.sg and theory;
wenzelm
parents: 16350
diff changeset
    43
  thy
22796
34c316d7b630 renamed some old names Theory.xxx to Sign.xxx;
wenzelm
parents: 22675
diff changeset
    44
  |> Sign.absolute_path
34c316d7b630 renamed some old names Theory.xxx to Sign.xxx;
wenzelm
parents: 22675
diff changeset
    45
  |> Sign.add_consts_i (map (fn name => (name, proofT, NoSyn)) names);
11522
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
(** constants for application and abstraction **)
11614
3131fa12d425 - Tuned syntax
berghofe
parents: 11539
diff changeset
    48
16425
2427be27cc60 accomodate identification of type Sign.sg and theory;
wenzelm
parents: 16350
diff changeset
    49
fun add_proof_syntax thy =
2427be27cc60 accomodate identification of type Sign.sg and theory;
wenzelm
parents: 16350
diff changeset
    50
  thy
2427be27cc60 accomodate identification of type Sign.sg and theory;
wenzelm
parents: 16350
diff changeset
    51
  |> Theory.copy
22796
34c316d7b630 renamed some old names Theory.xxx to Sign.xxx;
wenzelm
parents: 22675
diff changeset
    52
  |> Sign.root_path
34c316d7b630 renamed some old names Theory.xxx to Sign.xxx;
wenzelm
parents: 22675
diff changeset
    53
  |> Sign.add_defsort_i []
34c316d7b630 renamed some old names Theory.xxx to Sign.xxx;
wenzelm
parents: 22675
diff changeset
    54
  |> Sign.add_types [("proof", 0, NoSyn)]
34c316d7b630 renamed some old names Theory.xxx to Sign.xxx;
wenzelm
parents: 22675
diff changeset
    55
  |> Sign.add_consts_i
11614
3131fa12d425 - Tuned syntax
berghofe
parents: 11539
diff changeset
    56
      [("Appt", [proofT, aT] ---> proofT, Mixfix ("(1_ %/ _)", [4, 5], 4)),
3131fa12d425 - Tuned syntax
berghofe
parents: 11539
diff changeset
    57
       ("AppP", [proofT, proofT] ---> proofT, Mixfix ("(1_ %%/ _)", [4, 5], 4)),
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    58
       ("Abst", (aT --> proofT) --> proofT, NoSyn),
13199
18402c1f76bf Added constants for Hyp, Oracle and MinProof.
berghofe
parents: 12909
diff changeset
    59
       ("AbsP", [propT, proofT --> proofT] ---> proofT, NoSyn),
18402c1f76bf Added constants for Hyp, Oracle and MinProof.
berghofe
parents: 12909
diff changeset
    60
       ("Hyp", propT --> proofT, NoSyn),
18402c1f76bf Added constants for Hyp, Oracle and MinProof.
berghofe
parents: 12909
diff changeset
    61
       ("Oracle", propT --> proofT, NoSyn),
18402c1f76bf Added constants for Hyp, Oracle and MinProof.
berghofe
parents: 12909
diff changeset
    62
       ("MinProof", proofT, Delimfix "?")]
22796
34c316d7b630 renamed some old names Theory.xxx to Sign.xxx;
wenzelm
parents: 22675
diff changeset
    63
  |> Sign.add_nonterminals ["param", "params"]
34c316d7b630 renamed some old names Theory.xxx to Sign.xxx;
wenzelm
parents: 22675
diff changeset
    64
  |> Sign.add_syntax_i
11640
be1bc3b88480 Tuned indentation of abstractions.
berghofe
parents: 11614
diff changeset
    65
      [("_Lam", [paramsT, proofT] ---> proofT, Mixfix ("(1Lam _./ _)", [0, 3], 3)),
11614
3131fa12d425 - Tuned syntax
berghofe
parents: 11539
diff changeset
    66
       ("_Lam0", [paramT, paramsT] ---> paramsT, Mixfix ("_/ _", [1, 0], 0)),
3131fa12d425 - Tuned syntax
berghofe
parents: 11539
diff changeset
    67
       ("_Lam0", [idtT, paramsT] ---> paramsT, Mixfix ("_/ _", [1, 0], 0)),
3131fa12d425 - Tuned syntax
berghofe
parents: 11539
diff changeset
    68
       ("_Lam1", [idtT, propT] ---> paramT, Mixfix ("_: _", [0, 0], 0)),
3131fa12d425 - Tuned syntax
berghofe
parents: 11539
diff changeset
    69
       ("", paramT --> paramT, Delimfix "'(_')"),
3131fa12d425 - Tuned syntax
berghofe
parents: 11539
diff changeset
    70
       ("", idtT --> paramsT, Delimfix "_"),
3131fa12d425 - Tuned syntax
berghofe
parents: 11539
diff changeset
    71
       ("", paramT --> paramsT, Delimfix "_")]
22796
34c316d7b630 renamed some old names Theory.xxx to Sign.xxx;
wenzelm
parents: 22675
diff changeset
    72
  |> Sign.add_modesyntax_i ("xsymbols", true)
11640
be1bc3b88480 Tuned indentation of abstractions.
berghofe
parents: 11614
diff changeset
    73
      [("_Lam", [paramsT, proofT] ---> proofT, Mixfix ("(1\\<Lambda>_./ _)", [0, 3], 3)),
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    74
       ("Appt", [proofT, aT] ---> proofT, Mixfix ("(1_ \\<cdot>/ _)", [4, 5], 4)),
16425
2427be27cc60 accomodate identification of type Sign.sg and theory;
wenzelm
parents: 16350
diff changeset
    75
       ("AppP", [proofT, proofT] ---> proofT, Mixfix ("(1_ \\<bullet>/ _)", [4, 5], 4))]
22796
34c316d7b630 renamed some old names Theory.xxx to Sign.xxx;
wenzelm
parents: 22675
diff changeset
    76
  |> Sign.add_modesyntax_i ("latex", false)
16425
2427be27cc60 accomodate identification of type Sign.sg and theory;
wenzelm
parents: 16350
diff changeset
    77
      [("_Lam", [paramsT, proofT] ---> proofT, Mixfix ("(1\\<^bold>\\<lambda>_./ _)", [0, 3], 3))]
22796
34c316d7b630 renamed some old names Theory.xxx to Sign.xxx;
wenzelm
parents: 22675
diff changeset
    78
  |> Sign.add_trrules_i (map Syntax.ParsePrintRule
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    79
      [(Syntax.mk_appl (Constant "_Lam")
11614
3131fa12d425 - Tuned syntax
berghofe
parents: 11539
diff changeset
    80
          [Syntax.mk_appl (Constant "_Lam0") [Variable "l", Variable "m"], Variable "A"],
3131fa12d425 - Tuned syntax
berghofe
parents: 11539
diff changeset
    81
        Syntax.mk_appl (Constant "_Lam")
3131fa12d425 - Tuned syntax
berghofe
parents: 11539
diff changeset
    82
          [Variable "l", Syntax.mk_appl (Constant "_Lam") [Variable "m", Variable "A"]]),
3131fa12d425 - Tuned syntax
berghofe
parents: 11539
diff changeset
    83
       (Syntax.mk_appl (Constant "_Lam")
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    84
          [Syntax.mk_appl (Constant "_Lam1") [Variable "x", Variable "A"], Variable "B"],
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    85
        Syntax.mk_appl (Constant "AbsP") [Variable "A",
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    86
          (Syntax.mk_appl (Constant "_abs") [Variable "x", Variable "B"])]),
11614
3131fa12d425 - Tuned syntax
berghofe
parents: 11539
diff changeset
    87
       (Syntax.mk_appl (Constant "_Lam") [Variable "x", Variable "A"],
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    88
        Syntax.mk_appl (Constant "Abst")
11614
3131fa12d425 - Tuned syntax
berghofe
parents: 11539
diff changeset
    89
          [(Syntax.mk_appl (Constant "_abs") [Variable "x", Variable "A"])])]);
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    90
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
(**** create unambiguous theorem names ****)
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    93
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    94
fun disambiguate_names thy prf =
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    95
  let
17019
f68598628d08 Adapted to new interface og thms_of_proof / axms_of_proof.
berghofe
parents: 16866
diff changeset
    96
    val thms = thms_of_proof prf Symtab.empty;
16866
cde0b6864924 tuned assoc;
wenzelm
parents: 16425
diff changeset
    97
    val thms' = map (apsnd Thm.full_prop_of) (PureThy.all_thms_of thy);
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    98
21056
2cfe839e8d58 Symtab.foldl replaced by Symtab.fold
haftmann
parents: 20548
diff changeset
    99
    val tab = Symtab.fold (fn (key, ps) => fn tab =>
19473
wenzelm
parents: 19391
diff changeset
   100
      let val prop = the_default (Bound 0) (AList.lookup (op =) thms' key)
21056
2cfe839e8d58 Symtab.foldl replaced by Symtab.fold
haftmann
parents: 20548
diff changeset
   101
      in fst (fold_rev (fn (prop', prf) => fn x as (tab, i) => 
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   102
        if prop <> prop' then
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17223
diff changeset
   103
          (Symtab.update (key ^ "_" ^ string_of_int i, prf) tab, i+1)
21056
2cfe839e8d58 Symtab.foldl replaced by Symtab.fold
haftmann
parents: 20548
diff changeset
   104
        else x) ps (tab, 1))
2cfe839e8d58 Symtab.foldl replaced by Symtab.fold
haftmann
parents: 20548
diff changeset
   105
      end) thms Symtab.empty;
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   106
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   107
    fun rename (Abst (s, T, prf)) = Abst (s, T, rename prf)
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   108
      | rename (AbsP (s, t, prf)) = AbsP (s, t, rename prf)
11614
3131fa12d425 - Tuned syntax
berghofe
parents: 11539
diff changeset
   109
      | rename (prf1 %% prf2) = rename prf1 %% rename prf2
3131fa12d425 - Tuned syntax
berghofe
parents: 11539
diff changeset
   110
      | rename (prf % t) = rename prf % t
21646
c07b5b0e8492 thm/prf: separate official name vs. additional tags;
wenzelm
parents: 21056
diff changeset
   111
      | rename (prf' as PThm (s, prf, prop, Ts)) =
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   112
          let
19473
wenzelm
parents: 19391
diff changeset
   113
            val prop' = the_default (Bound 0) (AList.lookup (op =) thms' s);
19305
5c16895d548b avoid polymorphic equality;
wenzelm
parents: 18939
diff changeset
   114
            val ps = remove (op =) prop' (map fst (the (Symtab.lookup thms s)))
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   115
          in if prop = prop' then prf' else
21646
c07b5b0e8492 thm/prf: separate official name vs. additional tags;
wenzelm
parents: 21056
diff changeset
   116
            PThm (s ^ "_" ^ string_of_int (length ps - find_index (fn p => p = prop) ps),
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   117
              prf, prop, Ts)
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   118
          end
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   119
      | rename prf = prf
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   120
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   121
  in (rename prf, tab) end;
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
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   124
(**** translation between proof terms and pure terms ****)
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   125
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   126
fun proof_of_term thy tab ty =
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   127
  let
16350
caa9b780ad91 Theory.all_axioms_of, PureThy.all_thms_of;
wenzelm
parents: 16195
diff changeset
   128
    val thms = PureThy.all_thms_of thy;
caa9b780ad91 Theory.all_axioms_of, PureThy.all_thms_of;
wenzelm
parents: 16195
diff changeset
   129
    val axms = Theory.all_axioms_of thy;
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   130
20548
8ef25fe585a8 renamed Term.map_term_types to Term.map_types (cf. Term.fold_types);
wenzelm
parents: 19618
diff changeset
   131
    fun mk_term t = (if ty then I else map_types (K dummyT))
11614
3131fa12d425 - Tuned syntax
berghofe
parents: 11539
diff changeset
   132
      (Term.no_dummy_patterns t);
3131fa12d425 - Tuned syntax
berghofe
parents: 11539
diff changeset
   133
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   134
    fun prf_of [] (Bound i) = PBound i
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   135
      | prf_of Ts (Const (s, Type ("proof", _))) =
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
   136
          change_type (if ty then SOME Ts else NONE)
21858
05f57309170c avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents: 21646
diff changeset
   137
            (case NameSpace.explode s of
11614
3131fa12d425 - Tuned syntax
berghofe
parents: 11539
diff changeset
   138
               "axm" :: xs =>
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   139
                 let
21858
05f57309170c avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents: 21646
diff changeset
   140
                   val name = NameSpace.implode xs;
17223
430edc6b7826 curried_lookup/update;
wenzelm
parents: 17078
diff changeset
   141
                   val prop = (case AList.lookup (op =) axms name of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
   142
                       SOME prop => prop
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
   143
                     | NONE => error ("Unknown axiom " ^ quote name))
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
   144
                 in PAxm (name, prop, NONE) end
11614
3131fa12d425 - Tuned syntax
berghofe
parents: 11539
diff changeset
   145
             | "thm" :: xs =>
21858
05f57309170c avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents: 21646
diff changeset
   146
                 let val name = NameSpace.implode xs;
17223
430edc6b7826 curried_lookup/update;
wenzelm
parents: 17078
diff changeset
   147
                 in (case AList.lookup (op =) thms name of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
   148
                     SOME thm => fst (strip_combt (Thm.proof_of thm))
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17223
diff changeset
   149
                   | NONE => (case Symtab.lookup tab name of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
   150
                         SOME prf => prf
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
   151
                       | NONE => error ("Unknown theorem " ^ quote name)))
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   152
                 end
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   153
             | _ => error ("Illegal proof constant name: " ^ quote s))
13199
18402c1f76bf Added constants for Hyp, Oracle and MinProof.
berghofe
parents: 12909
diff changeset
   154
      | prf_of Ts (Const ("Hyp", _) $ prop) = Hyp prop
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   155
      | prf_of Ts (v as Var ((_, Type ("proof", _)))) = Hyp v
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   156
      | prf_of [] (Const ("Abst", _) $ Abs (s, T, prf)) =
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
   157
          Abst (s, if ty then SOME T else NONE,
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   158
            incr_pboundvars (~1) 0 (prf_of [] prf))
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   159
      | prf_of [] (Const ("AbsP", _) $ t $ Abs (s, _, prf)) =
11614
3131fa12d425 - Tuned syntax
berghofe
parents: 11539
diff changeset
   160
          AbsP (s, case t of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
   161
                Const ("dummy_pattern", _) => NONE
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
   162
              | _ $ Const ("dummy_pattern", _) => NONE
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
   163
              | _ => SOME (mk_term t),
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   164
            incr_pboundvars 0 (~1) (prf_of [] prf))
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   165
      | prf_of [] (Const ("AppP", _) $ prf1 $ prf2) =
11614
3131fa12d425 - Tuned syntax
berghofe
parents: 11539
diff changeset
   166
          prf_of [] prf1 %% prf_of [] prf2
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   167
      | prf_of Ts (Const ("Appt", _) $ prf $ Const ("TYPE", Type (_, [T]))) =
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   168
          prf_of (T::Ts) prf
11614
3131fa12d425 - Tuned syntax
berghofe
parents: 11539
diff changeset
   169
      | prf_of [] (Const ("Appt", _) $ prf $ t) = prf_of [] prf %
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
   170
          (case t of Const ("dummy_pattern", _) => NONE | _ => SOME (mk_term t))
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   171
      | prf_of _ t = error ("Not a proof term:\n" ^
16425
2427be27cc60 accomodate identification of type Sign.sg and theory;
wenzelm
parents: 16350
diff changeset
   172
          Sign.string_of_term thy t)
11522
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
  in prf_of [] end;
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   175
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
val AbsPt = Const ("AbsP", [propT, proofT --> proofT] ---> proofT);
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   178
val AppPt = Const ("AppP", [proofT, proofT] ---> proofT);
13199
18402c1f76bf Added constants for Hyp, Oracle and MinProof.
berghofe
parents: 12909
diff changeset
   179
val Hypt = Const ("Hyp", propT --> proofT);
18402c1f76bf Added constants for Hyp, Oracle and MinProof.
berghofe
parents: 12909
diff changeset
   180
val Oraclet = Const ("Oracle", propT --> proofT);
18402c1f76bf Added constants for Hyp, Oracle and MinProof.
berghofe
parents: 12909
diff changeset
   181
val MinProoft = Const ("MinProof", proofT);
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   182
19473
wenzelm
parents: 19391
diff changeset
   183
val mk_tyapp = fold (fn T => fn prf => Const ("Appt",
19391
4812d28c90a6 Term.itselfT;
wenzelm
parents: 19305
diff changeset
   184
  [proofT, Term.itselfT T] ---> proofT) $ prf $ Logic.mk_type T);
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   185
21646
c07b5b0e8492 thm/prf: separate official name vs. additional tags;
wenzelm
parents: 21056
diff changeset
   186
fun term_of _ (PThm (name, _, _, NONE)) =
16195
wenzelm
parents: 16182
diff changeset
   187
      Const (NameSpace.append "thm" name, proofT)
21646
c07b5b0e8492 thm/prf: separate official name vs. additional tags;
wenzelm
parents: 21056
diff changeset
   188
  | term_of _ (PThm (name, _, _, SOME Ts)) =
19473
wenzelm
parents: 19391
diff changeset
   189
      mk_tyapp Ts (Const (NameSpace.append "thm" name, proofT))
16195
wenzelm
parents: 16182
diff changeset
   190
  | term_of _ (PAxm (name, _, NONE)) = Const (NameSpace.append "axm" name, proofT)
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
   191
  | term_of _ (PAxm (name, _, SOME Ts)) =
19473
wenzelm
parents: 19391
diff changeset
   192
      mk_tyapp Ts (Const (NameSpace.append "axm" name, proofT))
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   193
  | term_of _ (PBound i) = Bound i
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   194
  | term_of Ts (Abst (s, opT, prf)) = 
18939
wenzelm
parents: 17412
diff changeset
   195
      let val T = the_default dummyT opT
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   196
      in Const ("Abst", (T --> proofT) --> proofT) $
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   197
        Abs (s, T, term_of (T::Ts) (incr_pboundvars 1 0 prf))
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   198
      end
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   199
  | term_of Ts (AbsP (s, t, prf)) =
18939
wenzelm
parents: 17412
diff changeset
   200
      AbsPt $ the_default (Term.dummy_pattern propT) t $
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   201
        Abs (s, proofT, term_of (proofT::Ts) (incr_pboundvars 0 1 prf))
11614
3131fa12d425 - Tuned syntax
berghofe
parents: 11539
diff changeset
   202
  | term_of Ts (prf1 %% prf2) =
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   203
      AppPt $ term_of Ts prf1 $ term_of Ts prf2
11614
3131fa12d425 - Tuned syntax
berghofe
parents: 11539
diff changeset
   204
  | term_of Ts (prf % opt) = 
18939
wenzelm
parents: 17412
diff changeset
   205
      let val t = the_default (Term.dummy_pattern dummyT) opt
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   206
      in Const ("Appt",
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   207
        [proofT, fastype_of1 (Ts, t) handle TERM _ => dummyT] ---> proofT) $
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   208
          term_of Ts prf $ t
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   209
      end
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   210
  | term_of Ts (Hyp t) = Hypt $ t
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   211
  | term_of Ts (Oracle (_, t, _)) = Oraclet $ t
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   212
  | term_of Ts (MinProof _) = MinProoft;
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   213
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   214
val term_of_proof = term_of [];
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   215
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   216
fun cterm_of_proof thy prf =
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   217
  let
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   218
    val (prf', tab) = disambiguate_names thy prf;
16350
caa9b780ad91 Theory.all_axioms_of, PureThy.all_thms_of;
wenzelm
parents: 16195
diff changeset
   219
    val thm_names = filter_out (equal "")
caa9b780ad91 Theory.all_axioms_of, PureThy.all_thms_of;
wenzelm
parents: 16195
diff changeset
   220
      (map fst (PureThy.all_thms_of thy) @ map fst (Symtab.dest tab));
caa9b780ad91 Theory.all_axioms_of, PureThy.all_thms_of;
wenzelm
parents: 16195
diff changeset
   221
    val axm_names = map fst (Theory.all_axioms_of thy);
16425
2427be27cc60 accomodate identification of type Sign.sg and theory;
wenzelm
parents: 16350
diff changeset
   222
    val thy' = thy
2427be27cc60 accomodate identification of type Sign.sg and theory;
wenzelm
parents: 16350
diff changeset
   223
      |> add_proof_syntax
2427be27cc60 accomodate identification of type Sign.sg and theory;
wenzelm
parents: 16350
diff changeset
   224
      |> add_proof_atom_consts
16195
wenzelm
parents: 16182
diff changeset
   225
        (map (NameSpace.append "axm") axm_names @ map (NameSpace.append "thm") thm_names)
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   226
  in
16425
2427be27cc60 accomodate identification of type Sign.sg and theory;
wenzelm
parents: 16350
diff changeset
   227
    (cterm_of thy' (term_of_proof prf'),
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   228
     proof_of_term thy tab true o Thm.term_of)
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   229
  end;
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   230
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   231
fun read_term thy =
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   232
  let
16350
caa9b780ad91 Theory.all_axioms_of, PureThy.all_thms_of;
wenzelm
parents: 16195
diff changeset
   233
    val thm_names = filter_out (equal "") (map fst (PureThy.all_thms_of thy));
caa9b780ad91 Theory.all_axioms_of, PureThy.all_thms_of;
wenzelm
parents: 16195
diff changeset
   234
    val axm_names = map fst (Theory.all_axioms_of thy);
16425
2427be27cc60 accomodate identification of type Sign.sg and theory;
wenzelm
parents: 16350
diff changeset
   235
    val thy' = thy
2427be27cc60 accomodate identification of type Sign.sg and theory;
wenzelm
parents: 16350
diff changeset
   236
      |> add_proof_syntax
2427be27cc60 accomodate identification of type Sign.sg and theory;
wenzelm
parents: 16350
diff changeset
   237
      |> add_proof_atom_consts
16195
wenzelm
parents: 16182
diff changeset
   238
        (map (NameSpace.append "axm") axm_names @ map (NameSpace.append "thm") thm_names)
22675
acf10be7dcca cleaned/simplified Sign.read_typ, Thm.read_cterm etc.;
wenzelm
parents: 21858
diff changeset
   239
  in Sign.simple_read_term thy' end;
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   240
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   241
fun read_proof thy =
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   242
  let val rd = read_term thy proofT
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   243
  in
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   244
    (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
   245
  end;
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   246
17078
db9d24c8b439 export proof_syntax, proof_of;
wenzelm
parents: 17019
diff changeset
   247
fun proof_syntax prf =
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   248
  let
19305
5c16895d548b avoid polymorphic equality;
wenzelm
parents: 18939
diff changeset
   249
    val thm_names = filter_out (equal "")
5c16895d548b avoid polymorphic equality;
wenzelm
parents: 18939
diff changeset
   250
      (map fst (Symtab.dest (thms_of_proof prf Symtab.empty)));
17019
f68598628d08 Adapted to new interface og thms_of_proof / axms_of_proof.
berghofe
parents: 16866
diff changeset
   251
    val axm_names = map fst (Symtab.dest (axms_of_proof prf Symtab.empty));
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   252
  in
17078
db9d24c8b439 export proof_syntax, proof_of;
wenzelm
parents: 17019
diff changeset
   253
    add_proof_syntax #>
db9d24c8b439 export proof_syntax, proof_of;
wenzelm
parents: 17019
diff changeset
   254
    add_proof_atom_consts
db9d24c8b439 export proof_syntax, proof_of;
wenzelm
parents: 17019
diff changeset
   255
      (map (NameSpace.append "thm") thm_names @ map (NameSpace.append "axm") axm_names)
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   256
  end;
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   257
17078
db9d24c8b439 export proof_syntax, proof_of;
wenzelm
parents: 17019
diff changeset
   258
fun proof_of full thm =
db9d24c8b439 export proof_syntax, proof_of;
wenzelm
parents: 17019
diff changeset
   259
  let
db9d24c8b439 export proof_syntax, proof_of;
wenzelm
parents: 17019
diff changeset
   260
    val {thy, der = (_, prf), ...} = Thm.rep_thm thm;
db9d24c8b439 export proof_syntax, proof_of;
wenzelm
parents: 17019
diff changeset
   261
    val prop = Thm.full_prop_of thm;
db9d24c8b439 export proof_syntax, proof_of;
wenzelm
parents: 17019
diff changeset
   262
    val prf' = (case strip_combt (fst (strip_combP prf)) of
db9d24c8b439 export proof_syntax, proof_of;
wenzelm
parents: 17019
diff changeset
   263
        (PThm (_, prf', prop', _), _) => if prop = prop' then prf' else prf
db9d24c8b439 export proof_syntax, proof_of;
wenzelm
parents: 17019
diff changeset
   264
      | _ => prf)
db9d24c8b439 export proof_syntax, proof_of;
wenzelm
parents: 17019
diff changeset
   265
  in if full then Reconstruct.reconstruct_proof thy prop prf' else prf' end;
db9d24c8b439 export proof_syntax, proof_of;
wenzelm
parents: 17019
diff changeset
   266
db9d24c8b439 export proof_syntax, proof_of;
wenzelm
parents: 17019
diff changeset
   267
fun pretty_proof thy prf =
db9d24c8b439 export proof_syntax, proof_of;
wenzelm
parents: 17019
diff changeset
   268
  Sign.pretty_term (proof_syntax prf thy) (term_of_proof prf);
db9d24c8b439 export proof_syntax, proof_of;
wenzelm
parents: 17019
diff changeset
   269
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   270
fun pretty_proof_of full thm =
17078
db9d24c8b439 export proof_syntax, proof_of;
wenzelm
parents: 17019
diff changeset
   271
  pretty_proof (Thm.theory_of_thm thm) (proof_of full thm);
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   272
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   273
val print_proof_of = Pretty.writeln oo pretty_proof_of;
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   274
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   275
end;