src/Pure/Isar/local_syntax.ML
author wenzelm
Mon Mar 01 17:07:36 2010 +0100 (2010-03-01)
changeset 35412 b8dead547d9e
parent 33387 acea2f336721
child 35429 afa8cf9e63d8
permissions -rw-r--r--
more uniform treatment of syntax for types vs. consts;
wenzelm@18997
     1
(*  Title:      Pure/Isar/local_syntax.ML
wenzelm@18997
     2
    Author:     Makarius
wenzelm@18997
     3
wenzelm@18997
     4
Local syntax depending on theory syntax.
wenzelm@18997
     5
*)
wenzelm@18997
     6
wenzelm@32738
     7
val show_structs = Unsynchronized.ref false;
wenzelm@18997
     8
wenzelm@18997
     9
signature LOCAL_SYNTAX =
wenzelm@18997
    10
sig
wenzelm@18997
    11
  type T
wenzelm@18997
    12
  val syn_of: T -> Syntax.syntax
wenzelm@18997
    13
  val structs_of: T -> string list
wenzelm@18997
    14
  val init: theory -> T
wenzelm@18997
    15
  val rebuild: theory -> T -> T
wenzelm@35412
    16
  datatype kind = Type | Const | Fixed
wenzelm@35412
    17
  val add_syntax: theory -> (kind * (string * typ * mixfix)) list -> T -> T
wenzelm@20785
    18
  val set_mode: Syntax.mode -> T -> T
wenzelm@19541
    19
  val restore_mode: T -> T -> T
wenzelm@24951
    20
  val update_modesyntax: theory -> bool -> Syntax.mode ->
wenzelm@35412
    21
    (kind * (string * typ * mixfix)) list -> T -> T
wenzelm@19369
    22
  val extern_term: T -> term -> term
wenzelm@18997
    23
end;
wenzelm@18997
    24
wenzelm@33387
    25
structure Local_Syntax: LOCAL_SYNTAX =
wenzelm@18997
    26
struct
wenzelm@18997
    27
wenzelm@18997
    28
(* datatype T *)
wenzelm@18997
    29
wenzelm@19386
    30
type local_mixfix =
wenzelm@35412
    31
  (string * bool) *  (*name, fixed?*)
wenzelm@35412
    32
  ((bool * bool * Syntax.mode) * (string * typ * mixfix));  (*type?, add?, mode, declaration*)
wenzelm@19386
    33
wenzelm@18997
    34
datatype T = Syntax of
wenzelm@18997
    35
 {thy_syntax: Syntax.syntax,
wenzelm@18997
    36
  local_syntax: Syntax.syntax,
wenzelm@20785
    37
  mode: Syntax.mode,
wenzelm@19386
    38
  mixfixes: local_mixfix list,
wenzelm@19369
    39
  idents: string list * string list};
wenzelm@18997
    40
wenzelm@19541
    41
fun make_syntax (thy_syntax, local_syntax, mode, mixfixes, idents) =
wenzelm@18997
    42
  Syntax {thy_syntax = thy_syntax, local_syntax = local_syntax,
wenzelm@19541
    43
    mode = mode, mixfixes = mixfixes, idents = idents};
wenzelm@18997
    44
wenzelm@19541
    45
fun map_syntax f (Syntax {thy_syntax, local_syntax, mode, mixfixes, idents}) =
wenzelm@19541
    46
  make_syntax (f (thy_syntax, local_syntax, mode, mixfixes, idents));
wenzelm@18997
    47
wenzelm@19369
    48
fun is_consistent thy (Syntax {thy_syntax, ...}) =
wenzelm@18997
    49
  Syntax.eq_syntax (Sign.syn_of thy, thy_syntax);
wenzelm@18997
    50
wenzelm@18997
    51
fun syn_of (Syntax {local_syntax, ...}) = local_syntax;
wenzelm@18997
    52
fun idents_of (Syntax {idents, ...}) = idents;
wenzelm@18997
    53
val structs_of = #1 o idents_of;
wenzelm@18997
    54
wenzelm@18997
    55
wenzelm@18997
    56
(* build syntax *)
wenzelm@18997
    57
wenzelm@19541
    58
fun build_syntax thy mode mixfixes (idents as (structs, _)) =
wenzelm@18997
    59
  let
wenzelm@18997
    60
    val thy_syntax = Sign.syn_of thy;
wenzelm@35412
    61
    fun update_gram ((true, add, m), decls) = Syntax.update_type_gram add m decls
wenzelm@35412
    62
      | update_gram ((false, add, m), decls) =
wenzelm@35412
    63
          Syntax.update_const_gram add (Sign.is_logtype thy) m decls;
wenzelm@35412
    64
wenzelm@18997
    65
    val (atrs, trs, trs', atrs') = Syntax.struct_trfuns structs;
wenzelm@18997
    66
    val local_syntax = thy_syntax
wenzelm@25390
    67
      |> Syntax.update_trfuns
wenzelm@19386
    68
          (map Syntax.mk_trfun atrs, map Syntax.mk_trfun trs,
wenzelm@19386
    69
           map Syntax.mk_trfun trs', map Syntax.mk_trfun atrs')
wenzelm@35412
    70
      |> fold update_gram (AList.coalesce (op =) (rev (map snd mixfixes)));
wenzelm@19541
    71
  in make_syntax (thy_syntax, local_syntax, mode, mixfixes, idents) end;
wenzelm@19541
    72
wenzelm@24960
    73
fun init thy = build_syntax thy Syntax.mode_default [] ([], []);
wenzelm@18997
    74
wenzelm@19541
    75
fun rebuild thy (syntax as Syntax {mode, mixfixes, idents, ...}) =
wenzelm@19541
    76
  if is_consistent thy syntax then syntax
wenzelm@19541
    77
  else build_syntax thy mode mixfixes idents;
wenzelm@18997
    78
wenzelm@19541
    79
wenzelm@18997
    80
(* mixfix declarations *)
wenzelm@18997
    81
wenzelm@35412
    82
datatype kind = Type | Const | Fixed;
wenzelm@35412
    83
wenzelm@18997
    84
local
wenzelm@18997
    85
wenzelm@35412
    86
fun prep_mixfix _ _ (_, (_, _, Structure)) = NONE
wenzelm@35412
    87
  | prep_mixfix add mode (Type, decl as (x, _, _)) = SOME ((x, false), ((true, add, mode), decl))
wenzelm@35412
    88
  | prep_mixfix add mode (Const, decl as (x, _, _)) = SOME ((x, false), ((false, add, mode), decl))
wenzelm@35412
    89
  | prep_mixfix add mode (Fixed, (x, T, mx)) =
wenzelm@35412
    90
      SOME ((x, true), ((false, add, mode), (Syntax.mark_fixed x, T, mx)));
wenzelm@19016
    91
wenzelm@35412
    92
fun prep_struct (Fixed, (c, _, Structure)) = SOME c
wenzelm@35412
    93
  | prep_struct (_, (c, _, Structure)) = error ("Bad mixfix declaration for " ^ quote c)
wenzelm@19016
    94
  | prep_struct _ = NONE;
wenzelm@18997
    95
wenzelm@18997
    96
in
wenzelm@18997
    97
wenzelm@24951
    98
fun update_syntax add thy raw_decls
wenzelm@24951
    99
    (syntax as (Syntax {mode, mixfixes, idents = (structs, _), ...})) =
wenzelm@24951
   100
  (case filter_out (fn (_, (_, _, mx)) => mx = NoSyn) raw_decls of
wenzelm@19016
   101
    [] => syntax
wenzelm@19016
   102
  | decls =>
wenzelm@19016
   103
      let
wenzelm@35412
   104
        val new_mixfixes = map_filter (prep_mixfix add mode) decls;
wenzelm@24954
   105
        val new_structs = map_filter prep_struct decls;
wenzelm@24954
   106
        val mixfixes' = rev new_mixfixes @ mixfixes;
wenzelm@24954
   107
        val structs' =
wenzelm@24954
   108
          if add then structs @ new_structs
wenzelm@24954
   109
          else subtract (op =) new_structs structs;
wenzelm@20785
   110
        val fixes' = fold (fn ((x, true), _) => cons x | _ => I) mixfixes' [];
wenzelm@19541
   111
      in build_syntax thy mode mixfixes' (structs', fixes') end);
wenzelm@18997
   112
wenzelm@24951
   113
val add_syntax = update_syntax true;
wenzelm@24951
   114
wenzelm@18997
   115
end;
wenzelm@18997
   116
wenzelm@18997
   117
wenzelm@19660
   118
(* syntax mode *)
wenzelm@19660
   119
wenzelm@19660
   120
fun set_mode mode = map_syntax (fn (thy_syntax, local_syntax, _, mixfixes, idents) =>
wenzelm@19660
   121
  (thy_syntax, local_syntax, mode, mixfixes, idents));
wenzelm@19660
   122
wenzelm@19660
   123
fun restore_mode (Syntax {mode, ...}) = set_mode mode;
wenzelm@19660
   124
wenzelm@24951
   125
fun update_modesyntax thy add mode args syntax =
wenzelm@24951
   126
  syntax |> set_mode mode |> update_syntax add thy args |> restore_mode syntax;
wenzelm@19660
   127
wenzelm@19660
   128
wenzelm@18997
   129
(* extern_term *)
wenzelm@18997
   130
wenzelm@19369
   131
fun extern_term syntax =
wenzelm@18997
   132
  let
wenzelm@19369
   133
    val (structs, fixes) = idents_of syntax;
wenzelm@18997
   134
    fun map_free (t as Free (x, T)) =
wenzelm@19618
   135
          let val i = find_index (fn s => s = x) structs + 1 in
wenzelm@18997
   136
            if i = 0 andalso member (op =) fixes x then
wenzelm@35412
   137
              Term.Const (Syntax.mark_fixed x, T)
wenzelm@18997
   138
            else if i = 1 andalso not (! show_structs) then
wenzelm@18997
   139
              Syntax.const "_struct" $ Syntax.const "_indexdefault"
wenzelm@18997
   140
            else t
wenzelm@18997
   141
          end
wenzelm@18997
   142
      | map_free t = t;
wenzelm@19369
   143
  in Term.map_aterms map_free end;
wenzelm@18997
   144
wenzelm@18997
   145
end;