src/Pure/Thy/term_style.ML
author wenzelm
Thu Nov 02 10:16:22 2017 +0100 (19 months ago)
changeset 66987 352b23c97ac8
parent 62969 9f394a16c557
child 67147 dea94b1aabc3
permissions -rw-r--r--
support focus_session, for much faster startup of Isabelle/jEdit;
more options for "isabelle jedit";
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@22107
    27
wenzelm@56030
    28
fun setup binding style thy =
wenzelm@56030
    29
  Data.map (#2 o Name_Space.define (Context.Theory thy) true (binding, style)) thy;
wenzelm@22107
    30
wenzelm@22107
    31
haftmann@32890
    32
(* style parsing *)
haftmann@32890
    33
wenzelm@56030
    34
fun parse_single ctxt =
wenzelm@62969
    35
  Parse.token Parse.name ::: Parse.args >> (fn src0 =>
wenzelm@56030
    36
    let
wenzelm@61814
    37
      val (src, parse) = Token.check_src ctxt get_data src0;
wenzelm@58011
    38
      val (f, _) = Token.syntax (Scan.lift parse) src ctxt;
wenzelm@56030
    39
    in f ctxt end);
haftmann@32890
    40
wenzelm@50637
    41
val parse = Args.context :|-- (fn ctxt => Scan.lift
haftmann@32890
    42
  (Args.parens (parse_single ctxt ::: Scan.repeat (Args.$$$ "," |-- parse_single ctxt))
haftmann@32890
    43
      >> fold I
haftmann@32890
    44
  || Scan.succeed I));
haftmann@32890
    45
haftmann@32890
    46
wenzelm@22107
    47
(* predefined styles *)
wenzelm@22107
    48
haftmann@32891
    49
fun style_lhs_rhs proj = Scan.succeed (fn ctxt => fn t =>
wenzelm@22107
    50
  let
wenzelm@59970
    51
    val concl = Object_Logic.drop_judgment ctxt (Logic.strip_imp_concl t);
wenzelm@22107
    52
  in
wenzelm@50637
    53
    (case concl of
wenzelm@56030
    54
      _ $ l $ r => proj (l, r)
wenzelm@50637
    55
    | _ => error ("Binary operator expected in term: " ^ Syntax.string_of_term ctxt concl))
haftmann@32890
    56
  end);
wenzelm@22107
    57
wenzelm@50638
    58
val style_prem = Parse.nat >> (fn i => fn ctxt => fn t =>
haftmann@32891
    59
  let
haftmann@32891
    60
    val prems = Logic.strip_imp_prems t;
haftmann@32891
    61
  in
haftmann@32891
    62
    if i <= length prems then nth prems (i - 1)
wenzelm@50637
    63
    else
wenzelm@50637
    64
      error ("Not enough premises for prem " ^ string_of_int i ^
wenzelm@50637
    65
        " in propositon: " ^ Syntax.string_of_term ctxt t)
haftmann@32891
    66
  end);
haftmann@32891
    67
wenzelm@53021
    68
fun sub_symbols (d :: s :: ss) =
wenzelm@61470
    69
      if Symbol.is_ascii_digit d andalso not (Symbol.is_control s)
wenzelm@56203
    70
      then d :: "\<^sub>" :: sub_symbols (s :: ss)
krauss@41685
    71
      else d :: s :: ss
wenzelm@53021
    72
  | sub_symbols cs = cs;
krauss@41685
    73
wenzelm@53021
    74
val sub_name = implode o rev o sub_symbols o rev o Symbol.explode;
krauss@41685
    75
wenzelm@53021
    76
fun sub_term (Free (n, T)) = Free (sub_name n, T)
wenzelm@53021
    77
  | sub_term (Var ((n, idx), T)) =
wenzelm@53021
    78
      if idx <> 0 then Var ((sub_name (n ^ string_of_int idx), 0), T)
wenzelm@53021
    79
      else Var ((sub_name n, 0), T)
wenzelm@53021
    80
  | sub_term (t $ u) = sub_term t $ sub_term u
wenzelm@53021
    81
  | sub_term (Abs (n, T, b)) = Abs (sub_name n, T, sub_term b)
wenzelm@53021
    82
  | sub_term t = t;
krauss@41685
    83
wenzelm@53171
    84
val _ = Theory.setup
wenzelm@56204
    85
 (setup @{binding lhs} (style_lhs_rhs fst) #>
wenzelm@56204
    86
  setup @{binding rhs} (style_lhs_rhs snd) #>
wenzelm@56204
    87
  setup @{binding prem} style_prem #>
wenzelm@56204
    88
  setup @{binding concl} (Scan.succeed (K Logic.strip_imp_concl)) #>
wenzelm@56204
    89
  setup @{binding sub} (Scan.succeed (K sub_term)));
wenzelm@22107
    90
wenzelm@22107
    91
end;