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