src/Pure/Thy/term_style.ML
author wenzelm
Wed Apr 08 19:39:08 2015 +0200 (2015-04-08)
changeset 59970 e9f73d87d904
parent 58011 bc6bced136e5
child 61470 c42960228a81
permissions -rw-r--r--
proper context for Object_Logic operations;
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@58011
    38
      val (src, parse) = Token.check_src ctxt (get_data ctxt) (Token.src name args);
wenzelm@58011
    39
      val (f, _) = Token.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@59970
    52
    val concl = Object_Logic.drop_judgment ctxt (Logic.strip_imp_concl t);
wenzelm@22107
    53
  in
wenzelm@50637
    54
    (case concl of
wenzelm@56030
    55
      _ $ l $ r => proj (l, r)
wenzelm@50637
    56
    | _ => error ("Binary operator expected in term: " ^ Syntax.string_of_term ctxt concl))
haftmann@32890
    57
  end);
wenzelm@22107
    58
wenzelm@50638
    59
val style_prem = Parse.nat >> (fn i => fn ctxt => fn t =>
haftmann@32891
    60
  let
haftmann@32891
    61
    val prems = Logic.strip_imp_prems t;
haftmann@32891
    62
  in
haftmann@32891
    63
    if i <= length prems then nth prems (i - 1)
wenzelm@50637
    64
    else
wenzelm@50637
    65
      error ("Not enough premises for prem " ^ string_of_int i ^
wenzelm@50637
    66
        " in propositon: " ^ Syntax.string_of_term ctxt t)
haftmann@32891
    67
  end);
haftmann@32891
    68
wenzelm@53021
    69
fun sub_symbols (d :: s :: ss) =
wenzelm@56203
    70
      if Symbol.is_ascii_digit d andalso not (String.isPrefix ("\<^") s)
wenzelm@56203
    71
      then d :: "\<^sub>" :: sub_symbols (s :: ss)
krauss@41685
    72
      else d :: s :: ss
wenzelm@53021
    73
  | sub_symbols cs = cs;
krauss@41685
    74
wenzelm@53021
    75
val sub_name = implode o rev o sub_symbols o rev o Symbol.explode;
krauss@41685
    76
wenzelm@53021
    77
fun sub_term (Free (n, T)) = Free (sub_name n, T)
wenzelm@53021
    78
  | sub_term (Var ((n, idx), T)) =
wenzelm@53021
    79
      if idx <> 0 then Var ((sub_name (n ^ string_of_int idx), 0), T)
wenzelm@53021
    80
      else Var ((sub_name n, 0), T)
wenzelm@53021
    81
  | sub_term (t $ u) = sub_term t $ sub_term u
wenzelm@53021
    82
  | sub_term (Abs (n, T, b)) = Abs (sub_name n, T, sub_term b)
wenzelm@53021
    83
  | sub_term t = t;
krauss@41685
    84
wenzelm@53171
    85
val _ = Theory.setup
wenzelm@56204
    86
 (setup @{binding lhs} (style_lhs_rhs fst) #>
wenzelm@56204
    87
  setup @{binding rhs} (style_lhs_rhs snd) #>
wenzelm@56204
    88
  setup @{binding prem} style_prem #>
wenzelm@56204
    89
  setup @{binding concl} (Scan.succeed (K Logic.strip_imp_concl)) #>
wenzelm@56204
    90
  setup @{binding sub} (Scan.succeed (K sub_term)));
wenzelm@22107
    91
wenzelm@22107
    92
end;