src/Pure/Thy/term_style.ML
author wenzelm
Wed, 21 Jan 2009 23:21:44 +0100
changeset 29606 fedb8be05f24
parent 26463 9283b4185fdf
child 32890 77df12652210
permissions -rw-r--r--
removed Ids;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
22107
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/Thy/term_style.ML
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
     2
    Author:     Florian Haftmann, TU Muenchen
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
     3
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
     4
Styles for terms, to use with the "term_style" and "thm_style"
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
     5
antiquotations.
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
     6
*)
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
     7
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
     8
signature TERM_STYLE =
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
     9
sig
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    10
  val the_style: theory -> string -> (Proof.context -> term -> term)
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    11
  val add_style: string -> (Proof.context -> term -> term) -> theory -> theory
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    12
  val print_styles: theory -> unit
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    13
end;
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    14
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    15
structure TermStyle: TERM_STYLE =
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    16
struct
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    17
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    18
(* style data *)
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    19
23655
d2d1138e0ddc replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
wenzelm
parents: 23577
diff changeset
    20
fun err_dup_style name =
d2d1138e0ddc replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
wenzelm
parents: 23577
diff changeset
    21
  error ("Duplicate declaration of antiquote style: " ^ quote name);
22107
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    22
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    23
structure StyleData = TheoryDataFun
22846
fb79144af9a3 simplified DataFun interfaces;
wenzelm
parents: 22107
diff changeset
    24
(
22107
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    25
  type T = ((Proof.context -> term -> term) * stamp) Symtab.table;
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    26
  val empty = Symtab.empty;
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    27
  val copy = I;
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    28
  val extend = I;
23577
c5b93c69afd3 avoid polymorphic equality;
wenzelm
parents: 22846
diff changeset
    29
  fun merge _ tabs : T = Symtab.merge (eq_snd (op =)) tabs
23655
d2d1138e0ddc replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
wenzelm
parents: 23577
diff changeset
    30
    handle Symtab.DUP dup => err_dup_style dup;
22846
fb79144af9a3 simplified DataFun interfaces;
wenzelm
parents: 22107
diff changeset
    31
);
22107
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    32
22846
fb79144af9a3 simplified DataFun interfaces;
wenzelm
parents: 22107
diff changeset
    33
fun print_styles thy =
fb79144af9a3 simplified DataFun interfaces;
wenzelm
parents: 22107
diff changeset
    34
  Pretty.writeln (Pretty.strs ("antiquote styles:" :: Symtab.keys (StyleData.get thy)));
22107
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    35
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    36
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    37
(* accessors *)
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    38
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    39
fun the_style thy name =
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    40
  (case Symtab.lookup (StyleData.get thy) name of
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    41
    NONE => error ("Unknown antiquote style: " ^ quote name)
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    42
  | SOME (style, _) => style);
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    43
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    44
fun add_style name style thy =
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    45
  StyleData.map (Symtab.update_new (name, (style, stamp ()))) thy
23655
d2d1138e0ddc replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
wenzelm
parents: 23577
diff changeset
    46
    handle Symtab.DUP _ => err_dup_style name;
22107
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    47
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    48
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    49
(* predefined styles *)
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    50
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    51
fun style_binargs ctxt t =
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    52
  let
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    53
    val concl = ObjectLogic.drop_judgment (ProofContext.theory_of ctxt)
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    54
      (Logic.strip_imp_concl t)
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    55
  in
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    56
    case concl of (_ $ l $ r) => (l, r)
24920
2a45e400fdad generic Syntax.pretty/string_of operations;
wenzelm
parents: 23655
diff changeset
    57
    | _ => error ("Binary operator expected in term: " ^ Syntax.string_of_term ctxt concl)
22107
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    58
  end;
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    59
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    60
fun style_parm_premise i ctxt t =
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    61
  let val prems = Logic.strip_imp_prems t in
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    62
    if i <= length prems then nth prems (i - 1)
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    63
    else error ("Not enough premises for prem" ^ string_of_int i ^
24920
2a45e400fdad generic Syntax.pretty/string_of operations;
wenzelm
parents: 23655
diff changeset
    64
      " in propositon: " ^ Syntax.string_of_term ctxt t)
22107
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    65
  end;
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    66
26463
9283b4185fdf Context.>> : operate on Context.generic;
wenzelm
parents: 26435
diff changeset
    67
val _ = Context.>> (Context.map_theory
22107
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    68
 (add_style "lhs" (fst oo style_binargs) #>
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    69
  add_style "rhs" (snd oo style_binargs) #>
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    70
  add_style "prem1" (style_parm_premise 1) #>
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    71
  add_style "prem2" (style_parm_premise 2) #>
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    72
  add_style "prem3" (style_parm_premise 3) #>
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    73
  add_style "prem4" (style_parm_premise 4) #>
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    74
  add_style "prem5" (style_parm_premise 5) #>
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    75
  add_style "prem6" (style_parm_premise 6) #>
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    76
  add_style "prem7" (style_parm_premise 7) #>
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    77
  add_style "prem8" (style_parm_premise 8) #>
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    78
  add_style "prem9" (style_parm_premise 9) #>
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    79
  add_style "prem10" (style_parm_premise 10) #>
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    80
  add_style "prem11" (style_parm_premise 11) #>
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    81
  add_style "prem12" (style_parm_premise 12) #>
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    82
  add_style "prem13" (style_parm_premise 13) #>
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    83
  add_style "prem14" (style_parm_premise 14) #>
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    84
  add_style "prem15" (style_parm_premise 15) #>
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    85
  add_style "prem16" (style_parm_premise 16) #>
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    86
  add_style "prem17" (style_parm_premise 17) #>
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    87
  add_style "prem18" (style_parm_premise 18) #>
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    88
  add_style "prem19" (style_parm_premise 19) #>
26463
9283b4185fdf Context.>> : operate on Context.generic;
wenzelm
parents: 26435
diff changeset
    89
  add_style "concl" (K Logic.strip_imp_concl)));
22107
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    90
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    91
end;