src/Pure/Isar/local_syntax.ML
author wenzelm
Tue May 16 21:33:11 2006 +0200 (2006-05-16)
changeset 19660 e3ec6839c631
parent 19618 9050a3b01e62
child 20785 d60f81c56fd4
permissions -rw-r--r--
added add_modesyntax;
tuned;
wenzelm@18997
     1
(*  Title:      Pure/Isar/local_syntax.ML
wenzelm@18997
     2
    ID:         $Id$
wenzelm@18997
     3
    Author:     Makarius
wenzelm@18997
     4
wenzelm@18997
     5
Local syntax depending on theory syntax.
wenzelm@18997
     6
*)
wenzelm@18997
     7
wenzelm@18997
     8
val show_structs = ref false;
wenzelm@18997
     9
wenzelm@18997
    10
signature LOCAL_SYNTAX =
wenzelm@18997
    11
sig
wenzelm@18997
    12
  type T
wenzelm@18997
    13
  val syn_of: T -> Syntax.syntax
wenzelm@18997
    14
  val structs_of: T -> string list
wenzelm@18997
    15
  val init: theory -> T
wenzelm@18997
    16
  val rebuild: theory -> T -> T
wenzelm@19660
    17
  val add_syntax: theory -> (bool * (string * typ * mixfix)) list -> T -> T
wenzelm@19541
    18
  val set_mode: string * bool -> T -> T
wenzelm@19541
    19
  val restore_mode: T -> T -> T
wenzelm@19660
    20
  val add_modesyntax: theory -> string * bool -> (bool * (string * typ * mixfix)) list -> T -> T
wenzelm@19369
    21
  val extern_term: T -> term -> term
wenzelm@18997
    22
end;
wenzelm@18997
    23
wenzelm@19016
    24
structure LocalSyntax: LOCAL_SYNTAX =
wenzelm@18997
    25
struct
wenzelm@18997
    26
wenzelm@18997
    27
(* datatype T *)
wenzelm@18997
    28
wenzelm@19386
    29
type local_mixfix =
wenzelm@19386
    30
  ((string * bool) * string list list) *          (*name, fixed?, mixfix content*)
wenzelm@19541
    31
  ((string * bool) * (string * typ * mixfix));    (*mode, mixfix syntax*)
wenzelm@19386
    32
wenzelm@18997
    33
datatype T = Syntax of
wenzelm@18997
    34
 {thy_syntax: Syntax.syntax,
wenzelm@18997
    35
  local_syntax: Syntax.syntax,
wenzelm@19541
    36
  mode: string * bool,
wenzelm@19386
    37
  mixfixes: local_mixfix list,
wenzelm@19369
    38
  idents: string list * string list};
wenzelm@18997
    39
wenzelm@19541
    40
fun make_syntax (thy_syntax, local_syntax, mode, mixfixes, idents) =
wenzelm@18997
    41
  Syntax {thy_syntax = thy_syntax, local_syntax = local_syntax,
wenzelm@19541
    42
    mode = mode, mixfixes = mixfixes, idents = idents};
wenzelm@18997
    43
wenzelm@19541
    44
fun map_syntax f (Syntax {thy_syntax, local_syntax, mode, mixfixes, idents}) =
wenzelm@19541
    45
  make_syntax (f (thy_syntax, local_syntax, mode, mixfixes, idents));
wenzelm@18997
    46
wenzelm@19369
    47
fun is_consistent thy (Syntax {thy_syntax, ...}) =
wenzelm@18997
    48
  Syntax.eq_syntax (Sign.syn_of thy, thy_syntax);
wenzelm@18997
    49
wenzelm@18997
    50
fun syn_of (Syntax {local_syntax, ...}) = local_syntax;
wenzelm@18997
    51
fun idents_of (Syntax {idents, ...}) = idents;
wenzelm@18997
    52
val structs_of = #1 o idents_of;
wenzelm@18997
    53
wenzelm@18997
    54
wenzelm@18997
    55
(* build syntax *)
wenzelm@18997
    56
wenzelm@19541
    57
fun build_syntax thy mode mixfixes (idents as (structs, _)) =
wenzelm@18997
    58
  let
wenzelm@18997
    59
    val thy_syntax = Sign.syn_of thy;
wenzelm@18997
    60
    val is_logtype = Sign.is_logtype thy;
wenzelm@18997
    61
    val (atrs, trs, trs', atrs') = Syntax.struct_trfuns structs;
wenzelm@18997
    62
    val local_syntax = thy_syntax
wenzelm@18997
    63
      |> Syntax.extend_trfuns
wenzelm@19386
    64
          (map Syntax.mk_trfun atrs, map Syntax.mk_trfun trs,
wenzelm@19386
    65
           map Syntax.mk_trfun trs', map Syntax.mk_trfun atrs')
wenzelm@19386
    66
      |> fold (uncurry (Syntax.extend_const_gram is_logtype))
haftmann@19454
    67
          (AList.coalesce (op =) (rev (map snd mixfixes)));
wenzelm@19541
    68
  in make_syntax (thy_syntax, local_syntax, mode, mixfixes, idents) end;
wenzelm@19541
    69
wenzelm@19541
    70
fun init thy = build_syntax thy Syntax.default_mode [] ([], []);
wenzelm@18997
    71
wenzelm@19541
    72
fun rebuild thy (syntax as Syntax {mode, mixfixes, idents, ...}) =
wenzelm@19541
    73
  if is_consistent thy syntax then syntax
wenzelm@19541
    74
  else build_syntax thy mode mixfixes idents;
wenzelm@18997
    75
wenzelm@19541
    76
wenzelm@18997
    77
(* mixfix declarations *)
wenzelm@18997
    78
wenzelm@18997
    79
local
wenzelm@18997
    80
wenzelm@19016
    81
fun mixfix_nosyn (_, (_, _, mx)) = mx = NoSyn;
wenzelm@19016
    82
fun mixfix_struct (_, (_, _, mx)) = mx = Structure;
wenzelm@19016
    83
wenzelm@19386
    84
fun mixfix_conflict ((content1: string list list, inout1), ((_, content2), ((_, inout2), _))) =
wenzelm@19386
    85
  inout1 andalso inout2 andalso exists (fn x => exists (fn y => x = y) content2) content1;
wenzelm@18997
    86
wenzelm@19386
    87
fun add_mixfix (mode, inout) (fixed, (x, T, mx)) =
wenzelm@19016
    88
  let
wenzelm@19016
    89
    val content = Syntax.mixfix_content mx;
wenzelm@19369
    90
    val c = if fixed then Syntax.fixedN ^ x else x;
wenzelm@19386
    91
  in
wenzelm@19386
    92
    remove mixfix_conflict (content, inout) #>
wenzelm@19386
    93
    cons (((x, fixed), content), ((mode, inout), (c, T, mx)))
wenzelm@19386
    94
  end;
wenzelm@19016
    95
wenzelm@19016
    96
fun prep_struct (fixed, (c, _, Structure)) =
wenzelm@19016
    97
      if fixed then SOME c
wenzelm@19016
    98
      else error ("Bad mixfix declaration for const: " ^ quote c)
wenzelm@19016
    99
  | prep_struct _ = NONE;
wenzelm@18997
   100
wenzelm@18997
   101
in
wenzelm@18997
   102
wenzelm@19541
   103
fun add_syntax thy raw_decls (syntax as (Syntax {mode, mixfixes, idents = (structs, _), ...})) =
wenzelm@19016
   104
  (case filter_out mixfix_nosyn raw_decls of
wenzelm@19016
   105
    [] => syntax
wenzelm@19016
   106
  | decls =>
wenzelm@19016
   107
      let
wenzelm@19541
   108
        val mixfixes' = mixfixes |> fold (add_mixfix mode) (filter_out mixfix_struct decls);
wenzelm@19016
   109
        val fixes' = fold (fn (((x, true), _), _) => cons x | _ => I) mixfixes' [];
wenzelm@19482
   110
        val structs' = structs @ map_filter prep_struct decls;
wenzelm@19541
   111
      in build_syntax thy mode mixfixes' (structs', fixes') end);
wenzelm@18997
   112
wenzelm@18997
   113
end;
wenzelm@18997
   114
wenzelm@18997
   115
wenzelm@19660
   116
(* syntax mode *)
wenzelm@19660
   117
wenzelm@19660
   118
fun set_mode mode = map_syntax (fn (thy_syntax, local_syntax, _, mixfixes, idents) =>
wenzelm@19660
   119
  (thy_syntax, local_syntax, mode, mixfixes, idents));
wenzelm@19660
   120
wenzelm@19660
   121
fun restore_mode (Syntax {mode, ...}) = set_mode mode;
wenzelm@19660
   122
wenzelm@19660
   123
fun add_modesyntax thy mode args syntax =
wenzelm@19660
   124
  syntax |> set_mode mode |> add_syntax thy args |> restore_mode syntax;
wenzelm@19660
   125
wenzelm@19660
   126
wenzelm@18997
   127
(* extern_term *)
wenzelm@18997
   128
wenzelm@19369
   129
fun extern_term syntax =
wenzelm@18997
   130
  let
wenzelm@19369
   131
    val (structs, fixes) = idents_of syntax;
wenzelm@18997
   132
    fun map_free (t as Free (x, T)) =
wenzelm@19618
   133
          let val i = find_index (fn s => s = x) structs + 1 in
wenzelm@18997
   134
            if i = 0 andalso member (op =) fixes x then
wenzelm@18997
   135
              Const (Syntax.fixedN ^ x, T)
wenzelm@18997
   136
            else if i = 1 andalso not (! show_structs) then
wenzelm@18997
   137
              Syntax.const "_struct" $ Syntax.const "_indexdefault"
wenzelm@18997
   138
            else t
wenzelm@18997
   139
          end
wenzelm@18997
   140
      | map_free t = t;
wenzelm@19369
   141
  in Term.map_aterms map_free end;
wenzelm@18997
   142
wenzelm@18997
   143
end;