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