src/Pure/Isar/term_style.ML
author wenzelm
Fri Jun 17 18:33:05 2005 +0200 (2005-06-17 ago)
changeset 16424 18a07ad8fea8
parent 16167 b2e4c4058b71
child 16541 d539d47cce69
permissions -rw-r--r--
accomodate change of TheoryDataFun;
     1 (*  Title:      Pure/Isar/term_style.ML
     2     ID:         $Id$
     3     Author:     Florian Haftmann, TU Muenchen
     4 
     5 Styles for terms, to use with the "term_style" and "thm_style" antiquotations.
     6 *)
     7 
     8 signature TERM_STYLE =
     9 sig
    10   val the_style: theory -> string -> (Proof.context -> term -> term)
    11   val add_style: string -> (Proof.context -> term -> term) -> theory -> theory
    12   val print_styles: theory -> unit
    13 end;
    14 
    15 structure TermStyle: TERM_STYLE =
    16 struct
    17 
    18 (* style data *)
    19 
    20 fun err_dup_styles names =
    21   error ("Duplicate declaration of antiquote style(s): " ^ commas_quote names);
    22 
    23 structure StyleData = TheoryDataFun
    24 (struct
    25   val name = "Isar/style";
    26   type T = ((Proof.context -> term -> term) * stamp) Symtab.table;
    27   val empty = Symtab.empty;
    28   val copy = I;
    29   val extend = I;
    30   fun merge _ tabs = Symtab.merge eq_snd tabs
    31     handle Symtab.DUPS dups => err_dup_styles dups;
    32   fun print _ tab = Pretty.writeln (Pretty.strs ("antiquote styles:" :: Symtab.keys tab));
    33 end);
    34 
    35 val _ = Context.add_setup [StyleData.init];
    36 val print_styles = StyleData.print;
    37 
    38 
    39 (* accessors *)
    40 
    41 fun the_style thy name =
    42   (case Symtab.lookup (StyleData.get thy, name) of
    43     NONE => error ("Unknown antiquote style: " ^ quote name)
    44   | SOME (style, _) => style);
    45 
    46 fun add_style name style thy =
    47   StyleData.map (curry Symtab.update_new (name, (style, stamp ()))) thy
    48     handle Symtab.DUP _ => err_dup_styles [name];
    49 
    50 
    51 (* predefined styles *)
    52 
    53 fun style_binargs ctxt t =
    54   let
    55     val concl = ObjectLogic.drop_judgment (ProofContext.theory_of ctxt)
    56       (Logic.strip_imp_concl t)
    57   in
    58     case concl of (_ $ l $ r) => (l, r)
    59     | _ => error ("Binary operator expected in term: " ^ ProofContext.string_of_term ctxt concl)
    60   end;
    61 
    62 fun style_parm_premise i ctxt t =
    63   let val prems = Logic.strip_imp_prems t in
    64     if i <= length prems then List.nth (prems, i - 1)
    65     else error ("Not enough premises for prem" ^ string_of_int i ^
    66       " in propositon: " ^ ProofContext.string_of_term ctxt t)
    67   end;
    68 
    69 val _ = Context.add_setup
    70  [add_style "lhs" (fst oo style_binargs),
    71   add_style "rhs" (snd oo style_binargs),
    72   add_style "prem1" (style_parm_premise 1),
    73   add_style "prem2" (style_parm_premise 2),
    74   add_style "prem3" (style_parm_premise 3),
    75   add_style "prem4" (style_parm_premise 4),
    76   add_style "prem5" (style_parm_premise 5),
    77   add_style "prem6" (style_parm_premise 6),
    78   add_style "prem7" (style_parm_premise 7),
    79   add_style "prem8" (style_parm_premise 8),
    80   add_style "prem9" (style_parm_premise 9),
    81   add_style "concl" (K Logic.strip_imp_concl)];
    82 
    83 end;