src/Pure/Thy/term_style.ML
author wenzelm
Sun Dec 30 16:33:05 2012 +0100 (2012-12-30)
changeset 50637 81d6fe75ea5b
parent 50592 a39250169636
child 50638 eedc01eca736
permissions -rw-r--r--
tuned whitespace;
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
wenzelm@22107
    11
end;
wenzelm@22107
    12
haftmann@32890
    13
structure Term_Style: TERM_STYLE =
wenzelm@22107
    14
struct
wenzelm@22107
    15
wenzelm@22107
    16
(* style data *)
wenzelm@22107
    17
wenzelm@23655
    18
fun err_dup_style name =
wenzelm@23655
    19
  error ("Duplicate declaration of antiquote style: " ^ quote name);
wenzelm@22107
    20
wenzelm@42016
    21
structure Styles = Theory_Data
wenzelm@22846
    22
(
haftmann@32890
    23
  type T = ((Proof.context -> term -> term) parser * stamp) Symtab.table;
wenzelm@22107
    24
  val empty = Symtab.empty;
wenzelm@22107
    25
  val extend = I;
wenzelm@33522
    26
  fun merge data : T = Symtab.merge (eq_snd (op =)) data
wenzelm@23655
    27
    handle Symtab.DUP dup => err_dup_style dup;
wenzelm@22846
    28
);
wenzelm@22107
    29
wenzelm@22107
    30
wenzelm@22107
    31
(* accessors *)
wenzelm@22107
    32
wenzelm@22107
    33
fun the_style thy name =
wenzelm@42016
    34
  (case Symtab.lookup (Styles.get thy) name of
wenzelm@22107
    35
    NONE => error ("Unknown antiquote style: " ^ quote name)
wenzelm@22107
    36
  | SOME (style, _) => style);
wenzelm@22107
    37
haftmann@32890
    38
fun setup name style thy =
wenzelm@42016
    39
  Styles.map (Symtab.update_new (name, (style, stamp ()))) thy
wenzelm@23655
    40
    handle Symtab.DUP _ => err_dup_style name;
wenzelm@22107
    41
wenzelm@22107
    42
haftmann@32890
    43
(* style parsing *)
haftmann@32890
    44
wenzelm@36950
    45
fun parse_single ctxt = Parse.position (Parse.xname -- Args.parse)
haftmann@32890
    46
  >> (fn x as ((name, _), _) => fst (Args.context_syntax "style"
wenzelm@42360
    47
       (Scan.lift (the_style (Proof_Context.theory_of ctxt) name))
haftmann@32890
    48
         (Args.src x) ctxt |>> (fn f => f ctxt)));
haftmann@32890
    49
wenzelm@50637
    50
val parse = Args.context :|-- (fn ctxt => Scan.lift
haftmann@32890
    51
  (Args.parens (parse_single ctxt ::: Scan.repeat (Args.$$$ "," |-- parse_single ctxt))
haftmann@32890
    52
      >> fold I
haftmann@32890
    53
  || Scan.succeed I));
haftmann@32890
    54
haftmann@32890
    55
wenzelm@22107
    56
(* predefined styles *)
wenzelm@22107
    57
haftmann@32891
    58
fun style_lhs_rhs proj = Scan.succeed (fn ctxt => fn t =>
wenzelm@22107
    59
  let
wenzelm@35625
    60
    val concl =
wenzelm@42360
    61
      Object_Logic.drop_judgment (Proof_Context.theory_of ctxt) (Logic.strip_imp_concl t)
wenzelm@22107
    62
  in
wenzelm@50637
    63
    (case concl of
wenzelm@50637
    64
      (_ $ l $ r) => proj (l, r)
wenzelm@50637
    65
    | _ => error ("Binary operator expected in term: " ^ Syntax.string_of_term ctxt concl))
haftmann@32890
    66
  end);
wenzelm@22107
    67
haftmann@32891
    68
val style_prem = Args.name >> (fn raw_i => fn ctxt => fn t =>
haftmann@32891
    69
  let
haftmann@32891
    70
    val i = (the o Int.fromString) raw_i;
haftmann@32891
    71
    val prems = Logic.strip_imp_prems t;
haftmann@32891
    72
  in
haftmann@32891
    73
    if i <= length prems then nth prems (i - 1)
wenzelm@50637
    74
    else
wenzelm@50637
    75
      error ("Not enough premises for prem " ^ string_of_int i ^
wenzelm@50637
    76
        " in propositon: " ^ Syntax.string_of_term ctxt t)
haftmann@32891
    77
  end);
haftmann@32891
    78
krauss@41685
    79
fun isub_symbols (d :: s :: ss) =
krauss@41685
    80
      if Symbol.is_ascii_digit d andalso not (String.isPrefix ("\\<^") s)
krauss@41685
    81
      then d :: "\\<^isub>" :: isub_symbols (s :: ss)
krauss@41685
    82
      else d :: s :: ss
krauss@41685
    83
  | isub_symbols cs = cs;
krauss@41685
    84
krauss@41685
    85
val isub_name = implode o rev o isub_symbols o rev o Symbol.explode;
krauss@41685
    86
krauss@41685
    87
fun isub_term (Free (n, T)) = Free (isub_name n, T)
krauss@41685
    88
  | isub_term (Var ((n, idx), T)) =
krauss@41685
    89
      if idx <> 0 then Var ((isub_name (n ^ string_of_int idx), 0), T)
krauss@41685
    90
      else Var ((isub_name n, 0), T)
krauss@41685
    91
  | isub_term (t $ u) = isub_term t $ isub_term u
krauss@41685
    92
  | isub_term (Abs (n, T, b)) = Abs (isub_name n, T, isub_term b)
krauss@41685
    93
  | isub_term t = t;
krauss@41685
    94
wenzelm@26463
    95
val _ = Context.>> (Context.map_theory
haftmann@32891
    96
 (setup "lhs" (style_lhs_rhs fst) #>
haftmann@32891
    97
  setup "rhs" (style_lhs_rhs snd) #>
haftmann@32891
    98
  setup "prem" style_prem #>
haftmann@32891
    99
  setup "concl" (Scan.succeed (K Logic.strip_imp_concl)) #>
haftmann@50592
   100
  setup "isub" (Scan.succeed (K isub_term))));
wenzelm@22107
   101
wenzelm@22107
   102
end;