src/Pure/Isar/local_syntax.ML
author wenzelm
Fri, 10 Feb 2006 02:22:39 +0100
changeset 18997 3229c88bbbdf
child 19016 f26377a4605a
permissions -rw-r--r--
Local syntax depending on theory syntax.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
18997
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/Isar/local_syntax.ML
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
     3
    Author:     Makarius
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
     4
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
     5
Local syntax depending on theory syntax.
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
     6
*)
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
     7
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
     8
val show_structs = ref false;
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
     9
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    10
signature LOCAL_SYNTAX =
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    11
sig
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    12
  type T
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    13
  val syn_of: T -> Syntax.syntax
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    14
  val structs_of: T -> string list
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    15
  val init: theory -> T
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    16
  val rebuild: theory -> T -> T
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    17
  val add_syntax: theory -> (bool * (string * typ * mixfix)) list -> T -> T
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    18
  val extern_term: (string -> xstring) -> theory -> T -> term -> term
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    19
end;
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    20
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    21
structure LocalSyntax (* : LOCAL_SYNTAX *) =
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    22
struct
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    23
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    24
(* datatype T *)
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    25
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    26
datatype T = Syntax of
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    27
 {thy_syntax: Syntax.syntax,
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    28
  local_syntax: Syntax.syntax,
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    29
  mixfixes: (string * typ * mixfix) list * (string * typ * mixfix) list,
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    30
  idents: string list * string list * string list};
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    31
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    32
fun make_syntax (thy_syntax, local_syntax, mixfixes, idents) =
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    33
  Syntax {thy_syntax = thy_syntax, local_syntax = local_syntax,
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    34
    mixfixes = mixfixes, idents = idents};
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    35
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    36
fun map_syntax f (Syntax {thy_syntax, local_syntax, mixfixes, idents}) =
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    37
  make_syntax (f (thy_syntax, local_syntax, mixfixes, idents));
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    38
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    39
fun is_consistent thy (syntax as Syntax {thy_syntax, ...}) =
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    40
  Syntax.eq_syntax (Sign.syn_of thy, thy_syntax);
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    41
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    42
fun syn_of (Syntax {local_syntax, ...}) = local_syntax;
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    43
fun idents_of (Syntax {idents, ...}) = idents;
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    44
val structs_of = #1 o idents_of;
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    45
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    46
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    47
(* build syntax *)
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    48
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    49
fun build_syntax thy (mixfixes as (mxs, mxs_output), idents as (structs, _, _)) =
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    50
  let
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    51
    val thy_syntax = Sign.syn_of thy;
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    52
    val is_logtype = Sign.is_logtype thy;
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    53
    val (atrs, trs, trs', atrs') = Syntax.struct_trfuns structs;
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    54
    val local_syntax = thy_syntax
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    55
      |> Syntax.extend_trfuns
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    56
         (map Syntax.mk_trfun atrs, map Syntax.mk_trfun trs,
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    57
          map Syntax.mk_trfun trs', map Syntax.mk_trfun atrs')
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    58
      |> Syntax.extend_const_gram is_logtype ("", false) mxs_output
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    59
      |> Syntax.extend_const_gram is_logtype ("", true) mxs;
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    60
  in make_syntax (thy_syntax, local_syntax, mixfixes, idents) end
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    61
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    62
fun init thy = build_syntax thy (([], []), ([], [], []));
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    63
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    64
fun rebuild thy (syntax as Syntax {mixfixes, idents, ...}) =
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    65
  if is_consistent thy syntax then syntax
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    66
  else build_syntax thy (mixfixes, idents);
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    67
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    68
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    69
(* mixfix declarations *)
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    70
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    71
local
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    72
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    73
fun mixfix_conflict (mx1, (_, _, mx2)) = Syntax.mixfix_conflict (mx1, mx2);
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    74
fun mixfix_proper (_, _, mx) = mx <> NoSyn andalso mx <> Structure;
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    75
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    76
fun mixfix_output (c, T, _) =
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    77
  let val c' = unprefix Syntax.fixedN c handle Fail _ => unprefix Syntax.constN c
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    78
  in (c, T, Syntax.literal c') end;
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    79
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    80
in
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    81
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    82
fun add_syntax thy decls (Syntax {mixfixes = (mxs, _), idents = (structs, _, _), ...}) =
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    83
  let
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    84
    fun add_mx (fixed, (c, T, mx)) =
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    85
      remove mixfix_conflict mx #>
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    86
      cons (if fixed then Syntax.fixedN ^ c else Syntax.constN ^ c, T, mx);
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    87
    val mxs' = mxs |> fold add_mx (filter (mixfix_proper o snd) decls);
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    88
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    89
    val fixes' = List.mapPartial (try (unprefix Syntax.fixedN) o #1) mxs';
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    90
    val consts' = List.mapPartial (try (unprefix Syntax.constN) o #1) mxs';
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    91
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    92
    fun prep_struct (fixed, (c, _, Structure)) =
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    93
          if fixed then SOME c
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    94
          else error ("Bad mixfix declaration for const: " ^ quote c)
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    95
      | prep_struct _ = NONE;
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    96
    val structs' = structs @ List.mapPartial prep_struct decls;
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    97
  in build_syntax thy ((mxs', map mixfix_output mxs'), (structs', fixes', consts')) end;
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    98
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
    99
end;
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
   100
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
   101
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
   102
(* extern_term *)
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
   103
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
   104
fun extern_term extern_const thy syntax =
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
   105
  let
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
   106
    val (structs, fixes, consts) = idents_of syntax;
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
   107
    fun map_const c =
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
   108
      if member (op =) consts c then Syntax.constN ^ c else extern_const c;
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
   109
    fun map_free (t as Free (x, T)) =
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
   110
          let val i = Library.find_index_eq x structs + 1 in
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
   111
            if i = 0 andalso member (op =) fixes x then
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
   112
              Const (Syntax.fixedN ^ x, T)
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
   113
            else if i = 1 andalso not (! show_structs) then
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
   114
              Syntax.const "_struct" $ Syntax.const "_indexdefault"
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
   115
            else t
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
   116
          end
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
   117
      | map_free t = t;
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
   118
  in Sign.extern_term map_const thy #> Term.map_aterms map_free end;
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
   119
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
   120
3229c88bbbdf Local syntax depending on theory syntax.
wenzelm
parents:
diff changeset
   121
end;