src/Pure/Thy/term_style.ML
author haftmann
Mon Oct 26 09:41:26 2009 +0100 (2009-10-26)
changeset 33184 de8cc01e8d9e
parent 32891 d403b99287ff
child 33522 737589bb9bb8
permissions -rw-r--r--
legacy warnings for old-style term styles
     1 (*  Title:      Pure/Thy/term_style.ML
     2     Author:     Florian Haftmann, TU Muenchen
     3 
     4 Styles for term printing.
     5 *)
     6 
     7 signature TERM_STYLE =
     8 sig
     9   val setup: string -> (Proof.context -> term -> term) parser -> theory -> theory
    10   val parse: (term -> term) context_parser
    11   val parse_bare: (term -> term) context_parser
    12 end;
    13 
    14 structure Term_Style: TERM_STYLE =
    15 struct
    16 
    17 (* style data *)
    18 
    19 fun err_dup_style name =
    20   error ("Duplicate declaration of antiquote style: " ^ quote name);
    21 
    22 structure StyleData = TheoryDataFun
    23 (
    24   type T = ((Proof.context -> term -> term) parser * stamp) Symtab.table;
    25   val empty = Symtab.empty;
    26   val copy = I;
    27   val extend = I;
    28   fun merge _ tabs : T = Symtab.merge (eq_snd (op =)) tabs
    29     handle Symtab.DUP dup => err_dup_style dup;
    30 );
    31 
    32 
    33 (* accessors *)
    34 
    35 fun the_style thy name =
    36   (case Symtab.lookup (StyleData.get thy) name of
    37     NONE => error ("Unknown antiquote style: " ^ quote name)
    38   | SOME (style, _) => style);
    39 
    40 fun setup name style thy =
    41   StyleData.map (Symtab.update_new (name, (style, stamp ()))) thy
    42     handle Symtab.DUP _ => err_dup_style name;
    43 
    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 => (Output.legacy_feature "Old-style antiquotation style.";
    58   Scan.lift Args.liberal_name
    59   >> (fn name => fst (Args.context_syntax "style"
    60        (Scan.lift (the_style (ProofContext.theory_of ctxt) name))
    61           (Args.src (("style", []), Position.none)) ctxt |>> (fn f => f ctxt)))));
    62 
    63 
    64 (* predefined styles *)
    65 
    66 fun style_lhs_rhs proj = Scan.succeed (fn ctxt => fn t =>
    67   let
    68     val concl = ObjectLogic.drop_judgment (ProofContext.theory_of ctxt)
    69       (Logic.strip_imp_concl t)
    70   in
    71     case concl of (_ $ l $ r) => proj (l, r)
    72     | _ => error ("Binary operator expected in term: " ^ Syntax.string_of_term ctxt concl)
    73   end);
    74 
    75 val style_prem = Args.name >> (fn raw_i => fn ctxt => fn t =>
    76   let
    77     val i = (the o Int.fromString) raw_i;
    78     val prems = Logic.strip_imp_prems t;
    79   in
    80     if i <= length prems then nth prems (i - 1)
    81     else error ("Not enough premises for prem " ^ string_of_int i ^
    82       " in propositon: " ^ Syntax.string_of_term ctxt t)
    83   end);
    84 
    85 fun style_parm_premise i = Scan.succeed (fn ctxt => fn t =>
    86   let
    87     val i_str = string_of_int i;
    88     val _ = Output.legacy_feature (quote ("prem" ^ i_str)
    89       ^ " term style encountered; use explicit argument syntax "
    90       ^ quote ("prem " ^ i_str) ^ " instead.");
    91     val prems = Logic.strip_imp_prems t;
    92   in
    93     if i <= length prems then nth prems (i - 1)
    94     else error ("Not enough premises for prem" ^ i_str ^
    95       " in propositon: " ^ Syntax.string_of_term ctxt t)
    96   end);
    97 
    98 val _ = Context.>> (Context.map_theory
    99  (setup "lhs" (style_lhs_rhs fst) #>
   100   setup "rhs" (style_lhs_rhs snd) #>
   101   setup "prem" style_prem #>
   102   setup "concl" (Scan.succeed (K Logic.strip_imp_concl)) #>
   103   setup "prem1" (style_parm_premise 1) #>
   104   setup "prem2" (style_parm_premise 2) #>
   105   setup "prem3" (style_parm_premise 3) #>
   106   setup "prem4" (style_parm_premise 4) #>
   107   setup "prem5" (style_parm_premise 5) #>
   108   setup "prem6" (style_parm_premise 6) #>
   109   setup "prem7" (style_parm_premise 7) #>
   110   setup "prem8" (style_parm_premise 8) #>
   111   setup "prem9" (style_parm_premise 9) #>
   112   setup "prem10" (style_parm_premise 10) #>
   113   setup "prem11" (style_parm_premise 11) #>
   114   setup "prem12" (style_parm_premise 12) #>
   115   setup "prem13" (style_parm_premise 13) #>
   116   setup "prem14" (style_parm_premise 14) #>
   117   setup "prem15" (style_parm_premise 15) #>
   118   setup "prem16" (style_parm_premise 16) #>
   119   setup "prem17" (style_parm_premise 17) #>
   120   setup "prem18" (style_parm_premise 18) #>
   121   setup "prem19" (style_parm_premise 19)));
   122 
   123 end;