src/Pure/Proof/proof_syntax.ML
author wenzelm
Thu May 27 17:41:27 2010 +0200 (2010-05-27)
changeset 37145 01aa36932739
parent 36610 bafd82950e24
child 37236 739d8b9c59da
permissions -rw-r--r--
renamed structure TypeInfer to Type_Infer, keeping the old name as legacy alias for some time;
     1 (*  Title:      Pure/Proof/proof_syntax.ML
     2     Author:     Stefan Berghofer, TU Muenchen
     3 
     4 Function for parsing and printing proof terms.
     5 *)
     6 
     7 signature PROOF_SYNTAX =
     8 sig
     9   val proofT: typ
    10   val add_proof_syntax: theory -> theory
    11   val proof_of_term: theory -> bool -> term -> Proofterm.proof
    12   val term_of_proof: Proofterm.proof -> term
    13   val cterm_of_proof: theory -> Proofterm.proof -> cterm * (cterm -> Proofterm.proof)
    14   val read_term: theory -> typ -> string -> term
    15   val read_proof: theory -> bool -> string -> Proofterm.proof
    16   val proof_syntax: Proofterm.proof -> theory -> theory
    17   val proof_of: bool -> thm -> Proofterm.proof
    18   val pretty_proof: Proof.context -> Proofterm.proof -> Pretty.T
    19   val pretty_proof_of: Proof.context -> bool -> thm -> Pretty.T
    20 end;
    21 
    22 structure Proof_Syntax : PROOF_SYNTAX =
    23 struct
    24 
    25 open Proofterm;
    26 
    27 (**** add special syntax for embedding proof terms ****)
    28 
    29 val proofT = Type ("proof", []);
    30 val paramT = Type ("param", []);
    31 val paramsT = Type ("params", []);
    32 val idtT = Type ("idt", []);
    33 val aT = TFree (Name.aT, []);
    34 
    35 (** constants for theorems and axioms **)
    36 
    37 fun add_proof_atom_consts names thy =
    38   thy
    39   |> Sign.root_path
    40   |> Sign.add_consts_i (map (fn name => (Binding.qualified_name name, proofT, NoSyn)) names);
    41 
    42 (** constants for application and abstraction **)
    43 
    44 fun add_proof_syntax thy =
    45   thy
    46   |> Theory.copy
    47   |> Sign.root_path
    48   |> Sign.set_defsort []
    49   |> Sign.add_types [(Binding.name "proof", 0, NoSyn)]
    50   |> fold (snd oo Sign.declare_const)
    51       [((Binding.name "Appt", [proofT, aT] ---> proofT), Mixfix ("(1_ %/ _)", [4, 5], 4)),
    52        ((Binding.name "AppP", [proofT, proofT] ---> proofT), Mixfix ("(1_ %%/ _)", [4, 5], 4)),
    53        ((Binding.name "Abst", (aT --> proofT) --> proofT), NoSyn),
    54        ((Binding.name "AbsP", [propT, proofT --> proofT] ---> proofT), NoSyn),
    55        ((Binding.name "Hyp", propT --> proofT), NoSyn),
    56        ((Binding.name "Oracle", propT --> proofT), NoSyn),
    57        ((Binding.name "OfClass", (Term.a_itselfT --> propT) --> proofT), NoSyn),
    58        ((Binding.name "MinProof", proofT), Delimfix "?")]
    59   |> Sign.add_nonterminals [Binding.name "param", Binding.name "params"]
    60   |> Sign.add_syntax_i
    61       [("_Lam", [paramsT, proofT] ---> proofT, Mixfix ("(1Lam _./ _)", [0, 3], 3)),
    62        ("_Lam0", [paramT, paramsT] ---> paramsT, Mixfix ("_/ _", [1, 0], 0)),
    63        ("_Lam0", [idtT, paramsT] ---> paramsT, Mixfix ("_/ _", [1, 0], 0)),
    64        ("_Lam1", [idtT, propT] ---> paramT, Mixfix ("_: _", [0, 0], 0)),
    65        ("", paramT --> paramT, Delimfix "'(_')"),
    66        ("", idtT --> paramsT, Delimfix "_"),
    67        ("", paramT --> paramsT, Delimfix "_")]
    68   |> Sign.add_modesyntax_i (Symbol.xsymbolsN, true)
    69       [("_Lam", [paramsT, proofT] ---> proofT, Mixfix ("(1\\<Lambda>_./ _)", [0, 3], 3)),
    70        (Syntax.mark_const "Appt", [proofT, aT] ---> proofT, Mixfix ("(1_ \\<cdot>/ _)", [4, 5], 4)),
    71        (Syntax.mark_const "AppP", [proofT, proofT] ---> proofT, Mixfix ("(1_ \\<bullet>/ _)", [4, 5], 4))]
    72   |> Sign.add_modesyntax_i ("latex", false)
    73       [("_Lam", [paramsT, proofT] ---> proofT, Mixfix ("(1\\<^bold>\\<lambda>_./ _)", [0, 3], 3))]
    74   |> Sign.add_trrules_i (map Syntax.ParsePrintRule
    75       [(Syntax.mk_appl (Constant "_Lam")
    76           [Syntax.mk_appl (Constant "_Lam0") [Variable "l", Variable "m"], Variable "A"],
    77         Syntax.mk_appl (Constant "_Lam")
    78           [Variable "l", Syntax.mk_appl (Constant "_Lam") [Variable "m", Variable "A"]]),
    79        (Syntax.mk_appl (Constant "_Lam")
    80           [Syntax.mk_appl (Constant "_Lam1") [Variable "x", Variable "A"], Variable "B"],
    81         Syntax.mk_appl (Constant (Syntax.mark_const "AbsP")) [Variable "A",
    82           (Syntax.mk_appl (Constant "_abs") [Variable "x", Variable "B"])]),
    83        (Syntax.mk_appl (Constant "_Lam") [Variable "x", Variable "A"],
    84         Syntax.mk_appl (Constant (Syntax.mark_const "Abst"))
    85           [(Syntax.mk_appl (Constant "_abs") [Variable "x", Variable "A"])])]);
    86 
    87 
    88 (**** translation between proof terms and pure terms ****)
    89 
    90 fun proof_of_term thy ty =
    91   let
    92     val thms = PureThy.all_thms_of thy;
    93     val axms = Theory.all_axioms_of thy;
    94 
    95     fun mk_term t = (if ty then I else map_types (K dummyT))
    96       (Term.no_dummy_patterns t);
    97 
    98     fun prf_of [] (Bound i) = PBound i
    99       | prf_of Ts (Const (s, Type ("proof", _))) =
   100           change_type (if ty then SOME Ts else NONE)
   101             (case Long_Name.explode s of
   102                "axm" :: xs =>
   103                  let
   104                    val name = Long_Name.implode xs;
   105                    val prop = (case AList.lookup (op =) axms name of
   106                        SOME prop => prop
   107                      | NONE => error ("Unknown axiom " ^ quote name))
   108                  in PAxm (name, prop, NONE) end
   109              | "thm" :: xs =>
   110                  let val name = Long_Name.implode xs;
   111                  in (case AList.lookup (op =) thms name of
   112                      SOME thm => fst (strip_combt (Thm.proof_of thm))
   113                    | NONE => error ("Unknown theorem " ^ quote name))
   114                  end
   115              | _ => error ("Illegal proof constant name: " ^ quote s))
   116       | prf_of Ts (Const ("OfClass", _) $ Const (c_class, _)) =
   117           (case try Logic.class_of_const c_class of
   118             SOME c =>
   119               change_type (if ty then SOME Ts else NONE)
   120                 (OfClass (TVar ((Name.aT, 0), []), c))
   121           | NONE => error ("Bad class constant: " ^ quote c_class))
   122       | prf_of Ts (Const ("Hyp", _) $ prop) = Hyp prop
   123       | prf_of Ts (v as Var ((_, Type ("proof", _)))) = Hyp v
   124       | prf_of [] (Const ("Abst", _) $ Abs (s, T, prf)) =
   125           if T = proofT then
   126             error ("Term variable abstraction may not bind proof variable " ^ quote s)
   127           else Abst (s, if ty then SOME T else NONE,
   128             incr_pboundvars (~1) 0 (prf_of [] prf))
   129       | prf_of [] (Const ("AbsP", _) $ t $ Abs (s, _, prf)) =
   130           AbsP (s, case t of
   131                 Const ("dummy_pattern", _) => NONE
   132               | _ $ Const ("dummy_pattern", _) => NONE
   133               | _ => SOME (mk_term t),
   134             incr_pboundvars 0 (~1) (prf_of [] prf))
   135       | prf_of [] (Const ("AppP", _) $ prf1 $ prf2) =
   136           prf_of [] prf1 %% prf_of [] prf2
   137       | prf_of Ts (Const ("Appt", _) $ prf $ Const ("TYPE", Type (_, [T]))) =
   138           prf_of (T::Ts) prf
   139       | prf_of [] (Const ("Appt", _) $ prf $ t) = prf_of [] prf %
   140           (case t of Const ("dummy_pattern", _) => NONE | _ => SOME (mk_term t))
   141       | prf_of _ t = error ("Not a proof term:\n" ^
   142           Syntax.string_of_term_global thy t)
   143 
   144   in prf_of [] end;
   145 
   146 
   147 val AbsPt = Const ("AbsP", [propT, proofT --> proofT] ---> proofT);
   148 val AppPt = Const ("AppP", [proofT, proofT] ---> proofT);
   149 val Hypt = Const ("Hyp", propT --> proofT);
   150 val Oraclet = Const ("Oracle", propT --> proofT);
   151 val OfClasst = Const ("OfClass", (Term.itselfT dummyT --> propT) --> proofT);
   152 val MinProoft = Const ("MinProof", proofT);
   153 
   154 val mk_tyapp = fold (fn T => fn prf => Const ("Appt",
   155   [proofT, Term.itselfT T] ---> proofT) $ prf $ Logic.mk_type T);
   156 
   157 fun term_of _ (PThm (_, ((name, _, NONE), _))) =
   158       Const (Long_Name.append "thm" name, proofT)
   159   | term_of _ (PThm (_, ((name, _, SOME Ts), _))) =
   160       mk_tyapp Ts (Const (Long_Name.append "thm" name, proofT))
   161   | term_of _ (PAxm (name, _, NONE)) = Const (Long_Name.append "axm" name, proofT)
   162   | term_of _ (PAxm (name, _, SOME Ts)) =
   163       mk_tyapp Ts (Const (Long_Name.append "axm" name, proofT))
   164   | term_of _ (OfClass (T, c)) =
   165       mk_tyapp [T] (OfClasst $ Const (Logic.const_of_class c, Term.itselfT dummyT --> propT))
   166   | term_of _ (PBound i) = Bound i
   167   | term_of Ts (Abst (s, opT, prf)) =
   168       let val T = the_default dummyT opT
   169       in Const ("Abst", (T --> proofT) --> proofT) $
   170         Abs (s, T, term_of (T::Ts) (incr_pboundvars 1 0 prf))
   171       end
   172   | term_of Ts (AbsP (s, t, prf)) =
   173       AbsPt $ the_default (Term.dummy_pattern propT) t $
   174         Abs (s, proofT, term_of (proofT::Ts) (incr_pboundvars 0 1 prf))
   175   | term_of Ts (prf1 %% prf2) =
   176       AppPt $ term_of Ts prf1 $ term_of Ts prf2
   177   | term_of Ts (prf % opt) =
   178       let val t = the_default (Term.dummy_pattern dummyT) opt
   179       in Const ("Appt",
   180         [proofT, fastype_of1 (Ts, t) handle TERM _ => dummyT] ---> proofT) $
   181           term_of Ts prf $ t
   182       end
   183   | term_of Ts (Hyp t) = Hypt $ t
   184   | term_of Ts (Oracle (_, t, _)) = Oraclet $ t
   185   | term_of Ts MinProof = MinProoft;
   186 
   187 val term_of_proof = term_of [];
   188 
   189 fun cterm_of_proof thy prf =
   190   let
   191     val thm_names = map fst (PureThy.all_thms_of thy);
   192     val axm_names = map fst (Theory.all_axioms_of thy);
   193     val thy' = thy
   194       |> add_proof_syntax
   195       |> add_proof_atom_consts
   196         (map (Long_Name.append "axm") axm_names @ map (Long_Name.append "thm") thm_names);
   197   in
   198     (cterm_of thy' (term_of_proof prf), proof_of_term thy true o Thm.term_of)
   199   end;
   200 
   201 fun read_term thy =
   202   let
   203     val thm_names = filter_out (fn s => s = "") (map fst (PureThy.all_thms_of thy));
   204     val axm_names = map fst (Theory.all_axioms_of thy);
   205     val ctxt = thy
   206       |> add_proof_syntax
   207       |> add_proof_atom_consts
   208         (map (Long_Name.append "axm") axm_names @ map (Long_Name.append "thm") thm_names)
   209       |> ProofContext.init_global
   210       |> ProofContext.allow_dummies
   211       |> ProofContext.set_mode ProofContext.mode_schematic;
   212   in
   213     fn ty => fn s =>
   214       (if ty = propT then Syntax.parse_prop else Syntax.parse_term) ctxt s
   215       |> Type_Infer.constrain ty |> Syntax.check_term ctxt
   216   end;
   217 
   218 fun read_proof thy =
   219   let val rd = read_term thy proofT
   220   in fn ty => fn s => proof_of_term thy ty (Logic.varify_global (rd s)) end;
   221 
   222 fun proof_syntax prf =
   223   let
   224     val thm_names = Symtab.keys (fold_proof_atoms true
   225       (fn PThm (_, ((name, _, _), _)) => if name <> "" then Symtab.update (name, ()) else I
   226         | _ => I) [prf] Symtab.empty);
   227     val axm_names = Symtab.keys (fold_proof_atoms true
   228       (fn PAxm (name, _, _) => Symtab.update (name, ()) | _ => I) [prf] Symtab.empty);
   229   in
   230     add_proof_syntax #>
   231     add_proof_atom_consts
   232       (map (Long_Name.append "thm") thm_names @ map (Long_Name.append "axm") axm_names)
   233   end;
   234 
   235 fun proof_of full thm =
   236   let
   237     val thy = Thm.theory_of_thm thm;
   238     val prop = Thm.full_prop_of thm;
   239     val prf = Thm.proof_of thm;
   240     val prf' = (case strip_combt (fst (strip_combP prf)) of
   241         (PThm (_, ((_, prop', _), body)), _) => if prop = prop' then join_proof body else prf
   242       | _ => prf)
   243   in if full then Reconstruct.reconstruct_proof thy prop prf' else prf' end;
   244 
   245 fun pretty_proof ctxt prf =
   246   ProofContext.pretty_term_abbrev
   247     (ProofContext.transfer_syntax (proof_syntax prf (ProofContext.theory_of ctxt)) ctxt)
   248     (term_of_proof prf);
   249 
   250 fun pretty_proof_of ctxt full th =
   251   pretty_proof ctxt (proof_of full th);
   252 
   253 end;