src/Pure/Thy/term_style.ML
author wenzelm
Tue Mar 18 15:29:58 2014 +0100 (2014-03-18)
changeset 56204 f70e69208a8c
parent 56203 76c72f4d0667
child 58011 bc6bced136e5
permissions -rw-r--r--
more antiquotations;
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
wenzelm@56030
     9
  val setup: binding -> (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@56030
    16
(* theory data *)
wenzelm@22107
    17
wenzelm@56030
    18
structure Data = Theory_Data
wenzelm@22846
    19
(
wenzelm@56030
    20
  type T = (Proof.context -> term -> term) parser Name_Space.table;
wenzelm@56030
    21
  val empty : T = Name_Space.empty_table "antiquotation_style";
wenzelm@22107
    22
  val extend = I;
wenzelm@56030
    23
  fun merge data : T = Name_Space.merge_tables data;
wenzelm@22846
    24
);
wenzelm@22107
    25
wenzelm@56030
    26
val get_data = Data.get o Proof_Context.theory_of;
wenzelm@56030
    27
val get_style = Name_Space.get o get_data;
wenzelm@22107
    28
wenzelm@56030
    29
fun setup binding style thy =
wenzelm@56030
    30
  Data.map (#2 o Name_Space.define (Context.Theory thy) true (binding, style)) thy;
wenzelm@22107
    31
wenzelm@22107
    32
haftmann@32890
    33
(* style parsing *)
haftmann@32890
    34
wenzelm@56030
    35
fun parse_single ctxt =
wenzelm@56201
    36
  Parse.position Parse.xname -- Parse.args >> (fn (name, args) =>
wenzelm@56030
    37
    let
wenzelm@56032
    38
      val (src, parse) = Args.check_src ctxt (get_data ctxt) (Args.src name args);
wenzelm@56032
    39
      val (f, _) = Args.syntax (Scan.lift parse) src ctxt;
wenzelm@56030
    40
    in f ctxt end);
haftmann@32890
    41
wenzelm@50637
    42
val parse = Args.context :|-- (fn ctxt => Scan.lift
haftmann@32890
    43
  (Args.parens (parse_single ctxt ::: Scan.repeat (Args.$$$ "," |-- parse_single ctxt))
haftmann@32890
    44
      >> fold I
haftmann@32890
    45
  || Scan.succeed I));
haftmann@32890
    46
haftmann@32890
    47
wenzelm@22107
    48
(* predefined styles *)
wenzelm@22107
    49
haftmann@32891
    50
fun style_lhs_rhs proj = Scan.succeed (fn ctxt => fn t =>
wenzelm@22107
    51
  let
wenzelm@35625
    52
    val concl =
wenzelm@42360
    53
      Object_Logic.drop_judgment (Proof_Context.theory_of ctxt) (Logic.strip_imp_concl t)
wenzelm@22107
    54
  in
wenzelm@50637
    55
    (case concl of
wenzelm@56030
    56
      _ $ l $ r => proj (l, r)
wenzelm@50637
    57
    | _ => error ("Binary operator expected in term: " ^ Syntax.string_of_term ctxt concl))
haftmann@32890
    58
  end);
wenzelm@22107
    59
wenzelm@50638
    60
val style_prem = Parse.nat >> (fn i => fn ctxt => fn t =>
haftmann@32891
    61
  let
haftmann@32891
    62
    val prems = Logic.strip_imp_prems t;
haftmann@32891
    63
  in
haftmann@32891
    64
    if i <= length prems then nth prems (i - 1)
wenzelm@50637
    65
    else
wenzelm@50637
    66
      error ("Not enough premises for prem " ^ string_of_int i ^
wenzelm@50637
    67
        " in propositon: " ^ Syntax.string_of_term ctxt t)
haftmann@32891
    68
  end);
haftmann@32891
    69
wenzelm@53021
    70
fun sub_symbols (d :: s :: ss) =
wenzelm@56203
    71
      if Symbol.is_ascii_digit d andalso not (String.isPrefix ("\<^") s)
wenzelm@56203
    72
      then d :: "\<^sub>" :: sub_symbols (s :: ss)
krauss@41685
    73
      else d :: s :: ss
wenzelm@53021
    74
  | sub_symbols cs = cs;
krauss@41685
    75
wenzelm@53021
    76
val sub_name = implode o rev o sub_symbols o rev o Symbol.explode;
krauss@41685
    77
wenzelm@53021
    78
fun sub_term (Free (n, T)) = Free (sub_name n, T)
wenzelm@53021
    79
  | sub_term (Var ((n, idx), T)) =
wenzelm@53021
    80
      if idx <> 0 then Var ((sub_name (n ^ string_of_int idx), 0), T)
wenzelm@53021
    81
      else Var ((sub_name n, 0), T)
wenzelm@53021
    82
  | sub_term (t $ u) = sub_term t $ sub_term u
wenzelm@53021
    83
  | sub_term (Abs (n, T, b)) = Abs (sub_name n, T, sub_term b)
wenzelm@53021
    84
  | sub_term t = t;
krauss@41685
    85
wenzelm@53171
    86
val _ = Theory.setup
wenzelm@56204
    87
 (setup @{binding lhs} (style_lhs_rhs fst) #>
wenzelm@56204
    88
  setup @{binding rhs} (style_lhs_rhs snd) #>
wenzelm@56204
    89
  setup @{binding prem} style_prem #>
wenzelm@56204
    90
  setup @{binding concl} (Scan.succeed (K Logic.strip_imp_concl)) #>
wenzelm@56204
    91
  setup @{binding sub} (Scan.succeed (K sub_term)));
wenzelm@22107
    92
wenzelm@22107
    93
end;