src/Pure/Thy/term_style.ML
author wenzelm
Fri, 19 Jan 2007 22:08:15 +0100
changeset 22107 926afa3361e1
child 22846 fb79144af9a3
permissions -rw-r--r--
renamed Isar/term_style.ML to Thy/term_style.ML;
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
    ID:         $Id$
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
     3
    Author:     Florian Haftmann, TU Muenchen
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
     4
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
     5
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
     6
antiquotations.
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
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
     9
signature TERM_STYLE =
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    10
sig
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    11
  val the_style: theory -> string -> (Proof.context -> term -> term)
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    12
  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
    13
  val print_styles: theory -> unit
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    14
end;
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    15
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    16
structure TermStyle: TERM_STYLE =
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    17
struct
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    18
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    19
(* style data *)
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    20
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    21
fun err_dup_styles names =
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    22
  error ("Duplicate declaration of antiquote style(s): " ^ commas_quote names);
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    23
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    24
structure StyleData = TheoryDataFun
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    25
(struct
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    26
  val name = "Isar/antiquote_style";
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    27
  type T = ((Proof.context -> term -> term) * stamp) Symtab.table;
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    28
  val empty = Symtab.empty;
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    29
  val copy = I;
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    30
  val extend = I;
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    31
  fun merge _ tabs = Symtab.merge (eq_snd (op =)) tabs
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    32
    handle Symtab.DUPS dups => err_dup_styles dups;
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    33
  fun print _ tab = Pretty.writeln (Pretty.strs ("antiquote styles:" :: Symtab.keys tab));
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    34
end);
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
val _ = Context.add_setup StyleData.init;
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    37
val print_styles = StyleData.print;
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
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    40
(* accessors *)
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    41
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    42
fun the_style thy name =
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    43
  (case Symtab.lookup (StyleData.get thy) name of
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    44
    NONE => error ("Unknown antiquote style: " ^ quote name)
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    45
  | SOME (style, _) => style);
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    46
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    47
fun add_style name style thy =
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    48
  StyleData.map (Symtab.update_new (name, (style, stamp ()))) thy
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    49
    handle Symtab.DUP _ => err_dup_styles [name];
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
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    52
(* predefined styles *)
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    53
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    54
fun style_binargs ctxt t =
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    55
  let
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    56
    val concl = ObjectLogic.drop_judgment (ProofContext.theory_of ctxt)
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    57
      (Logic.strip_imp_concl t)
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    58
  in
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    59
    case concl of (_ $ l $ r) => (l, r)
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    60
    | _ => error ("Binary operator expected in term: " ^ ProofContext.string_of_term ctxt concl)
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    61
  end;
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    62
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    63
fun style_parm_premise i ctxt t =
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    64
  let val prems = Logic.strip_imp_prems t in
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    65
    if i <= length prems then nth prems (i - 1)
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    66
    else error ("Not enough premises for prem" ^ string_of_int i ^
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    67
      " in propositon: " ^ ProofContext.string_of_term ctxt t)
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    68
  end;
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    69
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    70
val _ = Context.add_setup
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    71
 (add_style "lhs" (fst oo style_binargs) #>
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    72
  add_style "rhs" (snd oo style_binargs) #>
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    73
  add_style "prem1" (style_parm_premise 1) #>
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    74
  add_style "prem2" (style_parm_premise 2) #>
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    75
  add_style "prem3" (style_parm_premise 3) #>
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    76
  add_style "prem4" (style_parm_premise 4) #>
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    77
  add_style "prem5" (style_parm_premise 5) #>
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    78
  add_style "prem6" (style_parm_premise 6) #>
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    79
  add_style "prem7" (style_parm_premise 7) #>
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    80
  add_style "prem8" (style_parm_premise 8) #>
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    81
  add_style "prem9" (style_parm_premise 9) #>
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    82
  add_style "prem10" (style_parm_premise 10) #>
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    83
  add_style "prem11" (style_parm_premise 11) #>
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    84
  add_style "prem12" (style_parm_premise 12) #>
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    85
  add_style "prem13" (style_parm_premise 13) #>
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    86
  add_style "prem14" (style_parm_premise 14) #>
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    87
  add_style "prem15" (style_parm_premise 15) #>
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    88
  add_style "prem16" (style_parm_premise 16) #>
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    89
  add_style "prem17" (style_parm_premise 17) #>
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    90
  add_style "prem18" (style_parm_premise 18) #>
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    91
  add_style "prem19" (style_parm_premise 19) #>
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    92
  add_style "concl" (K Logic.strip_imp_concl));
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    93
926afa3361e1 renamed Isar/term_style.ML to Thy/term_style.ML;
wenzelm
parents:
diff changeset
    94
end;