src/Pure/Thy/term_style.ML
author wenzelm
Tue Feb 11 17:44:29 2014 +0100 (2014-02-11)
changeset 55431 e0f20a44ff9d
parent 53171 a5e54d4d9081
child 56029 8bedca4bd5a3
permissions -rw-r--r--
common Command.Chunk for command source and auxiliary files (static Symbol.Index without actual String content);
more informative type Blob, to allow markup reports;
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
wenzelm@50638
    68
val style_prem = Parse.nat >> (fn i => fn ctxt => fn t =>
haftmann@32891
    69
  let
haftmann@32891
    70
    val prems = Logic.strip_imp_prems t;
haftmann@32891
    71
  in
haftmann@32891
    72
    if i <= length prems then nth prems (i - 1)
wenzelm@50637
    73
    else
wenzelm@50637
    74
      error ("Not enough premises for prem " ^ string_of_int i ^
wenzelm@50637
    75
        " in propositon: " ^ Syntax.string_of_term ctxt t)
haftmann@32891
    76
  end);
haftmann@32891
    77
wenzelm@53021
    78
fun sub_symbols (d :: s :: ss) =
krauss@41685
    79
      if Symbol.is_ascii_digit d andalso not (String.isPrefix ("\\<^") s)
wenzelm@53021
    80
      then d :: "\\<^sub>" :: sub_symbols (s :: ss)
krauss@41685
    81
      else d :: s :: ss
wenzelm@53021
    82
  | sub_symbols cs = cs;
krauss@41685
    83
wenzelm@53021
    84
val sub_name = implode o rev o sub_symbols o rev o Symbol.explode;
krauss@41685
    85
wenzelm@53021
    86
fun sub_term (Free (n, T)) = Free (sub_name n, T)
wenzelm@53021
    87
  | sub_term (Var ((n, idx), T)) =
wenzelm@53021
    88
      if idx <> 0 then Var ((sub_name (n ^ string_of_int idx), 0), T)
wenzelm@53021
    89
      else Var ((sub_name n, 0), T)
wenzelm@53021
    90
  | sub_term (t $ u) = sub_term t $ sub_term u
wenzelm@53021
    91
  | sub_term (Abs (n, T, b)) = Abs (sub_name n, T, sub_term b)
wenzelm@53021
    92
  | sub_term t = t;
krauss@41685
    93
wenzelm@53171
    94
val _ = Theory.setup
haftmann@32891
    95
 (setup "lhs" (style_lhs_rhs fst) #>
haftmann@32891
    96
  setup "rhs" (style_lhs_rhs snd) #>
haftmann@32891
    97
  setup "prem" style_prem #>
haftmann@32891
    98
  setup "concl" (Scan.succeed (K Logic.strip_imp_concl)) #>
wenzelm@53171
    99
  setup "sub" (Scan.succeed (K sub_term)));
wenzelm@22107
   100
wenzelm@22107
   101
end;