src/Pure/Thy/term_style.ML
changeset 32890 77df12652210
parent 29606 fedb8be05f24
child 32891 d403b99287ff
equal deleted inserted replaced
32888:ae17e72aac80 32890:77df12652210
     1 (*  Title:      Pure/Thy/term_style.ML
     1 (*  Title:      Pure/Thy/term_style.ML
     2     Author:     Florian Haftmann, TU Muenchen
     2     Author:     Florian Haftmann, TU Muenchen
     3 
     3 
     4 Styles for terms, to use with the "term_style" and "thm_style"
     4 Styles for term printing.
     5 antiquotations.
       
     6 *)
     5 *)
     7 
     6 
     8 signature TERM_STYLE =
     7 signature TERM_STYLE =
     9 sig
     8 sig
    10   val the_style: theory -> string -> (Proof.context -> term -> term)
     9   val setup: string -> (Proof.context -> term -> term) parser -> theory -> theory
    11   val add_style: string -> (Proof.context -> term -> term) -> theory -> theory
    10   val parse: (term -> term) context_parser
    12   val print_styles: theory -> unit
    11   val parse_bare: (term -> term) context_parser
    13 end;
    12 end;
    14 
    13 
    15 structure TermStyle: TERM_STYLE =
    14 structure Term_Style: TERM_STYLE =
    16 struct
    15 struct
    17 
    16 
    18 (* style data *)
    17 (* style data *)
    19 
    18 
    20 fun err_dup_style name =
    19 fun err_dup_style name =
    21   error ("Duplicate declaration of antiquote style: " ^ quote name);
    20   error ("Duplicate declaration of antiquote style: " ^ quote name);
    22 
    21 
    23 structure StyleData = TheoryDataFun
    22 structure StyleData = TheoryDataFun
    24 (
    23 (
    25   type T = ((Proof.context -> term -> term) * stamp) Symtab.table;
    24   type T = ((Proof.context -> term -> term) parser * stamp) Symtab.table;
    26   val empty = Symtab.empty;
    25   val empty = Symtab.empty;
    27   val copy = I;
    26   val copy = I;
    28   val extend = I;
    27   val extend = I;
    29   fun merge _ tabs : T = Symtab.merge (eq_snd (op =)) tabs
    28   fun merge _ tabs : T = Symtab.merge (eq_snd (op =)) tabs
    30     handle Symtab.DUP dup => err_dup_style dup;
    29     handle Symtab.DUP dup => err_dup_style dup;
    31 );
    30 );
    32 
       
    33 fun print_styles thy =
       
    34   Pretty.writeln (Pretty.strs ("antiquote styles:" :: Symtab.keys (StyleData.get thy)));
       
    35 
    31 
    36 
    32 
    37 (* accessors *)
    33 (* accessors *)
    38 
    34 
    39 fun the_style thy name =
    35 fun the_style thy name =
    40   (case Symtab.lookup (StyleData.get thy) name of
    36   (case Symtab.lookup (StyleData.get thy) name of
    41     NONE => error ("Unknown antiquote style: " ^ quote name)
    37     NONE => error ("Unknown antiquote style: " ^ quote name)
    42   | SOME (style, _) => style);
    38   | SOME (style, _) => style);
    43 
    39 
    44 fun add_style name style thy =
    40 fun setup name style thy =
    45   StyleData.map (Symtab.update_new (name, (style, stamp ()))) thy
    41   StyleData.map (Symtab.update_new (name, (style, stamp ()))) thy
    46     handle Symtab.DUP _ => err_dup_style name;
    42     handle Symtab.DUP _ => err_dup_style name;
    47 
    43 
    48 
    44 
       
    45 (* style parsing *)
       
    46 
       
    47 fun parse_single ctxt = OuterParse.position (OuterParse.xname -- Args.parse)
       
    48   >> (fn x as ((name, _), _) => fst (Args.context_syntax "style"
       
    49        (Scan.lift (the_style (ProofContext.theory_of ctxt) name))
       
    50          (Args.src x) ctxt |>> (fn f => f ctxt)));
       
    51 
       
    52 val parse = Args.context :|-- (fn ctxt => Scan.lift 
       
    53   (Args.parens (parse_single ctxt ::: Scan.repeat (Args.$$$ "," |-- parse_single ctxt))
       
    54       >> fold I
       
    55   || Scan.succeed I));
       
    56 
       
    57 val parse_bare = Args.context :|-- (fn ctxt => Scan.lift Args.liberal_name
       
    58   >> (fn name => fst (Args.context_syntax "style"
       
    59        (Scan.lift (the_style (ProofContext.theory_of ctxt) name))
       
    60           (Args.src (("style", []), Position.none)) ctxt |>> (fn f => f ctxt))));
       
    61 
       
    62 
    49 (* predefined styles *)
    63 (* predefined styles *)
    50 
    64 
    51 fun style_binargs ctxt t =
    65 fun style_binargs proj = Scan.succeed (fn ctxt => fn t =>
    52   let
    66   let
    53     val concl = ObjectLogic.drop_judgment (ProofContext.theory_of ctxt)
    67     val concl = ObjectLogic.drop_judgment (ProofContext.theory_of ctxt)
    54       (Logic.strip_imp_concl t)
    68       (Logic.strip_imp_concl t)
    55   in
    69   in
    56     case concl of (_ $ l $ r) => (l, r)
    70     case concl of (_ $ l $ r) => proj (l, r)
    57     | _ => error ("Binary operator expected in term: " ^ Syntax.string_of_term ctxt concl)
    71     | _ => error ("Binary operator expected in term: " ^ Syntax.string_of_term ctxt concl)
    58   end;
    72   end);
    59 
    73 
    60 fun style_parm_premise i ctxt t =
    74 fun style_parm_premise i = Scan.succeed (fn ctxt => fn t =>
    61   let val prems = Logic.strip_imp_prems t in
    75   let val prems = Logic.strip_imp_prems t in
    62     if i <= length prems then nth prems (i - 1)
    76     if i <= length prems then nth prems (i - 1)
    63     else error ("Not enough premises for prem" ^ string_of_int i ^
    77     else error ("Not enough premises for prem" ^ string_of_int i ^
    64       " in propositon: " ^ Syntax.string_of_term ctxt t)
    78       " in propositon: " ^ Syntax.string_of_term ctxt t)
    65   end;
    79   end);
    66 
    80 
    67 val _ = Context.>> (Context.map_theory
    81 val _ = Context.>> (Context.map_theory
    68  (add_style "lhs" (fst oo style_binargs) #>
    82  (setup "lhs" (style_binargs fst) #>
    69   add_style "rhs" (snd oo style_binargs) #>
    83   setup "rhs" (style_binargs snd) #>
    70   add_style "prem1" (style_parm_premise 1) #>
    84   setup "prem1" (style_parm_premise 1) #>
    71   add_style "prem2" (style_parm_premise 2) #>
    85   setup "prem2" (style_parm_premise 2) #>
    72   add_style "prem3" (style_parm_premise 3) #>
    86   setup "prem3" (style_parm_premise 3) #>
    73   add_style "prem4" (style_parm_premise 4) #>
    87   setup "prem4" (style_parm_premise 4) #>
    74   add_style "prem5" (style_parm_premise 5) #>
    88   setup "prem5" (style_parm_premise 5) #>
    75   add_style "prem6" (style_parm_premise 6) #>
    89   setup "prem6" (style_parm_premise 6) #>
    76   add_style "prem7" (style_parm_premise 7) #>
    90   setup "prem7" (style_parm_premise 7) #>
    77   add_style "prem8" (style_parm_premise 8) #>
    91   setup "prem8" (style_parm_premise 8) #>
    78   add_style "prem9" (style_parm_premise 9) #>
    92   setup "prem9" (style_parm_premise 9) #>
    79   add_style "prem10" (style_parm_premise 10) #>
    93   setup "prem10" (style_parm_premise 10) #>
    80   add_style "prem11" (style_parm_premise 11) #>
    94   setup "prem11" (style_parm_premise 11) #>
    81   add_style "prem12" (style_parm_premise 12) #>
    95   setup "prem12" (style_parm_premise 12) #>
    82   add_style "prem13" (style_parm_premise 13) #>
    96   setup "prem13" (style_parm_premise 13) #>
    83   add_style "prem14" (style_parm_premise 14) #>
    97   setup "prem14" (style_parm_premise 14) #>
    84   add_style "prem15" (style_parm_premise 15) #>
    98   setup "prem15" (style_parm_premise 15) #>
    85   add_style "prem16" (style_parm_premise 16) #>
    99   setup "prem16" (style_parm_premise 16) #>
    86   add_style "prem17" (style_parm_premise 17) #>
   100   setup "prem17" (style_parm_premise 17) #>
    87   add_style "prem18" (style_parm_premise 18) #>
   101   setup "prem18" (style_parm_premise 18) #>
    88   add_style "prem19" (style_parm_premise 19) #>
   102   setup "prem19" (style_parm_premise 19) #>
    89   add_style "concl" (K Logic.strip_imp_concl)));
   103   setup "concl" (Scan.succeed (K Logic.strip_imp_concl))));
    90 
   104 
    91 end;
   105 end;