src/Pure/Thy/term_style.ML
author wenzelm
Tue, 09 Oct 2007 00:20:13 +0200
changeset 24920 2a45e400fdad
parent 23655 d2d1138e0ddc
child 26435 bdce320cd426
permissions -rw-r--r--
generic Syntax.pretty/string_of operations;
     1 (*  Title:      Pure/Thy/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"
     6 antiquotations.
     7 *)
     8 
     9 signature TERM_STYLE =
    10 sig
    11   val the_style: theory -> string -> (Proof.context -> term -> term)
    12   val add_style: string -> (Proof.context -> term -> term) -> theory -> theory
    13   val print_styles: theory -> unit
    14 end;
    15 
    16 structure TermStyle: TERM_STYLE =
    17 struct
    18 
    19 (* style data *)
    20 
    21 fun err_dup_style name =
    22   error ("Duplicate declaration of antiquote style: " ^ quote name);
    23 
    24 structure StyleData = TheoryDataFun
    25 (
    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 : T = Symtab.merge (eq_snd (op =)) tabs
    31     handle Symtab.DUP dup => err_dup_style dup;
    32 );
    33 
    34 fun print_styles thy =
    35   Pretty.writeln (Pretty.strs ("antiquote styles:" :: Symtab.keys (StyleData.get thy)));
    36 
    37 
    38 (* accessors *)
    39 
    40 fun the_style thy name =
    41   (case Symtab.lookup (StyleData.get thy) name of
    42     NONE => error ("Unknown antiquote style: " ^ quote name)
    43   | SOME (style, _) => style);
    44 
    45 fun add_style name style thy =
    46   StyleData.map (Symtab.update_new (name, (style, stamp ()))) thy
    47     handle Symtab.DUP _ => err_dup_style name;
    48 
    49 
    50 (* predefined styles *)
    51 
    52 fun style_binargs ctxt t =
    53   let
    54     val concl = ObjectLogic.drop_judgment (ProofContext.theory_of ctxt)
    55       (Logic.strip_imp_concl t)
    56   in
    57     case concl of (_ $ l $ r) => (l, r)
    58     | _ => error ("Binary operator expected in term: " ^ Syntax.string_of_term ctxt concl)
    59   end;
    60 
    61 fun style_parm_premise i ctxt t =
    62   let val prems = Logic.strip_imp_prems t in
    63     if i <= length prems then nth prems (i - 1)
    64     else error ("Not enough premises for prem" ^ string_of_int i ^
    65       " in propositon: " ^ Syntax.string_of_term ctxt t)
    66   end;
    67 
    68 val _ = Context.add_setup
    69  (add_style "lhs" (fst oo style_binargs) #>
    70   add_style "rhs" (snd oo style_binargs) #>
    71   add_style "prem1" (style_parm_premise 1) #>
    72   add_style "prem2" (style_parm_premise 2) #>
    73   add_style "prem3" (style_parm_premise 3) #>
    74   add_style "prem4" (style_parm_premise 4) #>
    75   add_style "prem5" (style_parm_premise 5) #>
    76   add_style "prem6" (style_parm_premise 6) #>
    77   add_style "prem7" (style_parm_premise 7) #>
    78   add_style "prem8" (style_parm_premise 8) #>
    79   add_style "prem9" (style_parm_premise 9) #>
    80   add_style "prem10" (style_parm_premise 10) #>
    81   add_style "prem11" (style_parm_premise 11) #>
    82   add_style "prem12" (style_parm_premise 12) #>
    83   add_style "prem13" (style_parm_premise 13) #>
    84   add_style "prem14" (style_parm_premise 14) #>
    85   add_style "prem15" (style_parm_premise 15) #>
    86   add_style "prem16" (style_parm_premise 16) #>
    87   add_style "prem17" (style_parm_premise 17) #>
    88   add_style "prem18" (style_parm_premise 18) #>
    89   add_style "prem19" (style_parm_premise 19) #>
    90   add_style "concl" (K Logic.strip_imp_concl));
    91 
    92 end;