src/Pure/Thy/term_style.ML
author wenzelm
Sun Mar 20 22:47:08 2011 +0100 (2011-03-20)
changeset 42016 3b6826b3ed37
parent 41686 d8efc2490b8e
child 42360 da8817d01e7c
permissions -rw-r--r--
tuned;
wenzelm@22107
     1
(*  Title:      Pure/Thy/term_style.ML
wenzelm@22107
     2
    Author:     Florian Haftmann, TU Muenchen
wenzelm@22107
     3
haftmann@32890
     4
Styles for term printing.
wenzelm@22107
     5
*)
wenzelm@22107
     6
wenzelm@22107
     7
signature TERM_STYLE =
wenzelm@22107
     8
sig
haftmann@32890
     9
  val setup: string -> (Proof.context -> term -> term) parser -> theory -> theory
haftmann@32890
    10
  val parse: (term -> term) context_parser
haftmann@32890
    11
  val parse_bare: (term -> term) context_parser
wenzelm@22107
    12
end;
wenzelm@22107
    13
haftmann@32890
    14
structure Term_Style: TERM_STYLE =
wenzelm@22107
    15
struct
wenzelm@22107
    16
wenzelm@22107
    17
(* style data *)
wenzelm@22107
    18
wenzelm@23655
    19
fun err_dup_style name =
wenzelm@23655
    20
  error ("Duplicate declaration of antiquote style: " ^ quote name);
wenzelm@22107
    21
wenzelm@42016
    22
structure Styles = Theory_Data
wenzelm@22846
    23
(
haftmann@32890
    24
  type T = ((Proof.context -> term -> term) parser * stamp) Symtab.table;
wenzelm@22107
    25
  val empty = Symtab.empty;
wenzelm@22107
    26
  val extend = I;
wenzelm@33522
    27
  fun merge data : T = Symtab.merge (eq_snd (op =)) data
wenzelm@23655
    28
    handle Symtab.DUP dup => err_dup_style dup;
wenzelm@22846
    29
);
wenzelm@22107
    30
wenzelm@22107
    31
wenzelm@22107
    32
(* accessors *)
wenzelm@22107
    33
wenzelm@22107
    34
fun the_style thy name =
wenzelm@42016
    35
  (case Symtab.lookup (Styles.get thy) name of
wenzelm@22107
    36
    NONE => error ("Unknown antiquote style: " ^ quote name)
wenzelm@22107
    37
  | SOME (style, _) => style);
wenzelm@22107
    38
haftmann@32890
    39
fun setup name style thy =
wenzelm@42016
    40
  Styles.map (Symtab.update_new (name, (style, stamp ()))) thy
wenzelm@23655
    41
    handle Symtab.DUP _ => err_dup_style name;
wenzelm@22107
    42
wenzelm@22107
    43
haftmann@32890
    44
(* style parsing *)
haftmann@32890
    45
wenzelm@36950
    46
fun parse_single ctxt = Parse.position (Parse.xname -- Args.parse)
haftmann@32890
    47
  >> (fn x as ((name, _), _) => fst (Args.context_syntax "style"
haftmann@32890
    48
       (Scan.lift (the_style (ProofContext.theory_of ctxt) name))
haftmann@32890
    49
         (Args.src x) ctxt |>> (fn f => f ctxt)));
haftmann@32890
    50
haftmann@32890
    51
val parse = Args.context :|-- (fn ctxt => Scan.lift 
haftmann@32890
    52
  (Args.parens (parse_single ctxt ::: Scan.repeat (Args.$$$ "," |-- parse_single ctxt))
haftmann@32890
    53
      >> fold I
haftmann@32890
    54
  || Scan.succeed I));
haftmann@32890
    55
haftmann@33184
    56
val parse_bare = Args.context :|-- (fn ctxt => (Output.legacy_feature "Old-style antiquotation style.";
haftmann@33184
    57
  Scan.lift Args.liberal_name
haftmann@32890
    58
  >> (fn name => fst (Args.context_syntax "style"
haftmann@32890
    59
       (Scan.lift (the_style (ProofContext.theory_of ctxt) name))
haftmann@33184
    60
          (Args.src (("style", []), Position.none)) ctxt |>> (fn f => f ctxt)))));
haftmann@32890
    61
haftmann@32890
    62
wenzelm@22107
    63
(* predefined styles *)
wenzelm@22107
    64
haftmann@32891
    65
fun style_lhs_rhs proj = Scan.succeed (fn ctxt => fn t =>
wenzelm@22107
    66
  let
wenzelm@35625
    67
    val concl =
wenzelm@35625
    68
      Object_Logic.drop_judgment (ProofContext.theory_of ctxt) (Logic.strip_imp_concl t)
wenzelm@22107
    69
  in
haftmann@32890
    70
    case concl of (_ $ l $ r) => proj (l, r)
wenzelm@24920
    71
    | _ => error ("Binary operator expected in term: " ^ Syntax.string_of_term ctxt concl)
haftmann@32890
    72
  end);
wenzelm@22107
    73
haftmann@32891
    74
val style_prem = Args.name >> (fn raw_i => fn ctxt => fn t =>
haftmann@32891
    75
  let
haftmann@32891
    76
    val i = (the o Int.fromString) raw_i;
haftmann@32891
    77
    val prems = Logic.strip_imp_prems t;
haftmann@32891
    78
  in
haftmann@32891
    79
    if i <= length prems then nth prems (i - 1)
haftmann@32891
    80
    else error ("Not enough premises for prem " ^ string_of_int i ^
haftmann@32891
    81
      " in propositon: " ^ Syntax.string_of_term ctxt t)
haftmann@32891
    82
  end);
haftmann@32891
    83
haftmann@32890
    84
fun style_parm_premise i = Scan.succeed (fn ctxt => fn t =>
haftmann@32891
    85
  let
haftmann@32891
    86
    val i_str = string_of_int i;
haftmann@33184
    87
    val _ = Output.legacy_feature (quote ("prem" ^ i_str)
haftmann@33184
    88
      ^ " term style encountered; use explicit argument syntax "
haftmann@33184
    89
      ^ quote ("prem " ^ i_str) ^ " instead.");
haftmann@32891
    90
    val prems = Logic.strip_imp_prems t;
haftmann@32891
    91
  in
wenzelm@22107
    92
    if i <= length prems then nth prems (i - 1)
haftmann@33184
    93
    else error ("Not enough premises for prem" ^ i_str ^
wenzelm@24920
    94
      " in propositon: " ^ Syntax.string_of_term ctxt t)
haftmann@32890
    95
  end);
wenzelm@22107
    96
krauss@41685
    97
fun isub_symbols (d :: s :: ss) =
krauss@41685
    98
      if Symbol.is_ascii_digit d andalso not (String.isPrefix ("\\<^") s)
krauss@41685
    99
      then d :: "\\<^isub>" :: isub_symbols (s :: ss)
krauss@41685
   100
      else d :: s :: ss
krauss@41685
   101
  | isub_symbols cs = cs;
krauss@41685
   102
krauss@41685
   103
val isub_name = implode o rev o isub_symbols o rev o Symbol.explode;
krauss@41685
   104
krauss@41685
   105
fun isub_term (Free (n, T)) = Free (isub_name n, T)
krauss@41685
   106
  | isub_term (Var ((n, idx), T)) =
krauss@41685
   107
      if idx <> 0 then Var ((isub_name (n ^ string_of_int idx), 0), T)
krauss@41685
   108
      else Var ((isub_name n, 0), T)
krauss@41685
   109
  | isub_term (t $ u) = isub_term t $ isub_term u
krauss@41685
   110
  | isub_term (Abs (n, T, b)) = Abs (isub_name n, T, isub_term b)
krauss@41685
   111
  | isub_term t = t;
krauss@41685
   112
wenzelm@26463
   113
val _ = Context.>> (Context.map_theory
haftmann@32891
   114
 (setup "lhs" (style_lhs_rhs fst) #>
haftmann@32891
   115
  setup "rhs" (style_lhs_rhs snd) #>
haftmann@32891
   116
  setup "prem" style_prem #>
haftmann@32891
   117
  setup "concl" (Scan.succeed (K Logic.strip_imp_concl)) #>
krauss@41686
   118
  setup "isub" (Scan.succeed (K isub_term)) #>
haftmann@32890
   119
  setup "prem1" (style_parm_premise 1) #>
haftmann@32890
   120
  setup "prem2" (style_parm_premise 2) #>
haftmann@32890
   121
  setup "prem3" (style_parm_premise 3) #>
haftmann@32890
   122
  setup "prem4" (style_parm_premise 4) #>
haftmann@32890
   123
  setup "prem5" (style_parm_premise 5) #>
haftmann@32890
   124
  setup "prem6" (style_parm_premise 6) #>
haftmann@32890
   125
  setup "prem7" (style_parm_premise 7) #>
haftmann@32890
   126
  setup "prem8" (style_parm_premise 8) #>
haftmann@32890
   127
  setup "prem9" (style_parm_premise 9) #>
haftmann@32890
   128
  setup "prem10" (style_parm_premise 10) #>
haftmann@32890
   129
  setup "prem11" (style_parm_premise 11) #>
haftmann@32890
   130
  setup "prem12" (style_parm_premise 12) #>
haftmann@32890
   131
  setup "prem13" (style_parm_premise 13) #>
haftmann@32890
   132
  setup "prem14" (style_parm_premise 14) #>
haftmann@32890
   133
  setup "prem15" (style_parm_premise 15) #>
haftmann@32890
   134
  setup "prem16" (style_parm_premise 16) #>
haftmann@32890
   135
  setup "prem17" (style_parm_premise 17) #>
haftmann@32890
   136
  setup "prem18" (style_parm_premise 18) #>
haftmann@32891
   137
  setup "prem19" (style_parm_premise 19)));
wenzelm@22107
   138
wenzelm@22107
   139
end;