src/Pure/Syntax/sextension.ML
author paulson
Thu Sep 25 12:09:41 1997 +0200 (1997-09-25)
changeset 3706 e57b5902822f
parent 473 fdacecc688a1
permissions -rw-r--r--
Generalized and exported biresolution_from_nets_tac to allow the declaration
of Clarify_tac
wenzelm@18
     1
(*  Title:      Pure/Syntax/sextension.ML
clasohm@0
     2
    ID:         $Id$
clasohm@0
     3
    Author:     Tobias Nipkow and Markus Wenzel, TU Muenchen
clasohm@0
     4
wenzelm@165
     5
Syntax extensions (external interface): mixfix declarations, infixes,
wenzelm@165
     6
binders, translation rules / functions and the Pure syntax.
clasohm@0
     7
clasohm@0
     8
TODO:
wenzelm@238
     9
  move ast_to_term, pt_to_ast (?)
clasohm@0
    10
*)
clasohm@0
    11
clasohm@0
    12
infix |-> <-| <->;
clasohm@0
    13
clasohm@0
    14
signature SEXTENSION0 =
clasohm@0
    15
sig
wenzelm@238
    16
  structure Parser: PARSER
wenzelm@238
    17
  local open Parser.SynExt.Ast in
clasohm@0
    18
    datatype mixfix =
clasohm@0
    19
      Mixfix of string * string * string * int list * int |
clasohm@0
    20
      Delimfix of string * string * string |
clasohm@0
    21
      Infixl of string * string * int |
clasohm@0
    22
      Infixr of string * string * int |
clasohm@0
    23
      Binder of string * string * string * int * int |
clasohm@0
    24
      TInfixl of string * string * int |
clasohm@0
    25
      TInfixr of string * string * int
clasohm@0
    26
    datatype xrule =
clasohm@0
    27
      op |-> of (string * string) * (string * string) |
clasohm@0
    28
      op <-| of (string * string) * (string * string) |
clasohm@0
    29
      op <-> of (string * string) * (string * string)
clasohm@0
    30
    datatype sext =
clasohm@0
    31
      Sext of {
clasohm@0
    32
        mixfix: mixfix list,
clasohm@0
    33
        parse_translation: (string * (term list -> term)) list,
clasohm@0
    34
        print_translation: (string * (term list -> term)) list} |
clasohm@0
    35
      NewSext of {
clasohm@0
    36
        mixfix: mixfix list,
clasohm@0
    37
        xrules: xrule list,
clasohm@0
    38
        parse_ast_translation: (string * (ast list -> ast)) list,
clasohm@0
    39
        parse_translation: (string * (term list -> term)) list,
clasohm@0
    40
        print_translation: (string * (term list -> term)) list,
clasohm@0
    41
        print_ast_translation: (string * (ast list -> ast)) list}
clasohm@0
    42
    val eta_contract: bool ref
clasohm@0
    43
    val mk_binder_tr: string * string -> string * (term list -> term)
clasohm@0
    44
    val mk_binder_tr': string * string -> string * (term list -> term)
clasohm@0
    45
    val dependent_tr': string * string -> term list -> term
clasohm@0
    46
    val max_pri: int
clasohm@0
    47
  end
clasohm@0
    48
end;
clasohm@0
    49
clasohm@0
    50
signature SEXTENSION1 =
clasohm@0
    51
sig
clasohm@0
    52
  include SEXTENSION0
wenzelm@382
    53
  local open Parser.SynExt.Ast in
wenzelm@382
    54
    val empty_sext: sext
wenzelm@382
    55
    val simple_sext: mixfix list -> sext
wenzelm@382
    56
    val constants: sext -> (string list * string) list
wenzelm@382
    57
    val pure_sext: sext
wenzelm@382
    58
    val syntax_types: string list
wenzelm@382
    59
    val syntax_consts: (string list * string) list
wenzelm@382
    60
    val constrainAbsC: string
wenzelm@382
    61
    val pure_trfuns:
wenzelm@382
    62
      (string * (ast list -> ast)) list *
wenzelm@382
    63
      (string * (term list -> term)) list *
wenzelm@382
    64
      (string * (term list -> term)) list *
wenzelm@382
    65
      (string * (ast list -> ast)) list
wenzelm@382
    66
  end
clasohm@0
    67
end;
clasohm@0
    68
clasohm@0
    69
signature SEXTENSION =
clasohm@0
    70
sig
clasohm@0
    71
  include SEXTENSION1
wenzelm@238
    72
  local open Parser Parser.SynExt Parser.SynExt.Ast in
clasohm@0
    73
    val xrules_of: sext -> xrule list
clasohm@0
    74
    val abs_tr': term -> term
wenzelm@382
    75
    val prop_tr': bool -> term -> term
clasohm@0
    76
    val appl_ast_tr': ast * ast list -> ast
clasohm@330
    77
    val syn_ext_of_sext: string list -> string list -> string list -> (string -> typ) -> sext -> syn_ext
wenzelm@238
    78
    val pt_to_ast: (string -> (ast list -> ast) option) -> parsetree -> ast
clasohm@0
    79
    val ast_to_term: (string -> (term list -> term) option) -> ast -> term
clasohm@0
    80
  end
clasohm@0
    81
end;
clasohm@0
    82
wenzelm@382
    83
functor SExtensionFun(structure TypeExt: TYPE_EXT and Parser: PARSER
wenzelm@382
    84
  sharing TypeExt.SynExt = Parser.SynExt): SEXTENSION =
clasohm@0
    85
struct
clasohm@0
    86
wenzelm@238
    87
structure Parser = Parser;
wenzelm@382
    88
open TypeExt Parser.Lexicon Parser.SynExt.Ast Parser.SynExt Parser;
clasohm@0
    89
clasohm@0
    90
wenzelm@382
    91
(** datatype sext **)   (* FIXME remove *)
clasohm@0
    92
clasohm@0
    93
datatype mixfix =
clasohm@0
    94
  Mixfix of string * string * string * int list * int |
clasohm@0
    95
  Delimfix of string * string * string |
clasohm@0
    96
  Infixl of string * string * int |
clasohm@0
    97
  Infixr of string * string * int |
clasohm@0
    98
  Binder of string * string * string * int * int |
clasohm@0
    99
  TInfixl of string * string * int |
clasohm@0
   100
  TInfixr of string * string * int;
clasohm@0
   101
wenzelm@382
   102
wenzelm@382
   103
(* FIXME -> syntax.ML, BASIC_SYNTAX, SYNTAX *)
clasohm@0
   104
datatype xrule =
clasohm@0
   105
  op |-> of (string * string) * (string * string) |
clasohm@0
   106
  op <-| of (string * string) * (string * string) |
clasohm@0
   107
  op <-> of (string * string) * (string * string);
clasohm@0
   108
clasohm@0
   109
datatype sext =
clasohm@0
   110
  Sext of {
clasohm@0
   111
    mixfix: mixfix list,
clasohm@0
   112
    parse_translation: (string * (term list -> term)) list,
clasohm@0
   113
    print_translation: (string * (term list -> term)) list} |
clasohm@0
   114
  NewSext of {
clasohm@0
   115
    mixfix: mixfix list,
clasohm@0
   116
    xrules: xrule list,
clasohm@0
   117
    parse_ast_translation: (string * (ast list -> ast)) list,
clasohm@0
   118
    parse_translation: (string * (term list -> term)) list,
clasohm@0
   119
    print_translation: (string * (term list -> term)) list,
clasohm@0
   120
    print_ast_translation: (string * (ast list -> ast)) list};
clasohm@0
   121
clasohm@0
   122
clasohm@0
   123
(* simple_sext *)
clasohm@0
   124
clasohm@0
   125
fun simple_sext mixfix =
clasohm@0
   126
  Sext {mixfix = mixfix, parse_translation = [], print_translation = []};
clasohm@0
   127
clasohm@0
   128
clasohm@0
   129
(* empty_sext *)
clasohm@0
   130
clasohm@0
   131
val empty_sext = simple_sext [];
clasohm@0
   132
clasohm@0
   133
clasohm@0
   134
(* sext_components *)
clasohm@0
   135
clasohm@0
   136
fun sext_components (Sext {mixfix, parse_translation, print_translation}) =
clasohm@0
   137
      {mixfix = mixfix,
clasohm@0
   138
        xrules = [],
clasohm@0
   139
        parse_ast_translation = [],
clasohm@0
   140
        parse_translation = parse_translation,
clasohm@0
   141
        print_translation = print_translation,
clasohm@0
   142
        print_ast_translation = []}
clasohm@0
   143
  | sext_components (NewSext cmps) = cmps;
clasohm@0
   144
clasohm@0
   145
clasohm@0
   146
(* mixfix_of *)
clasohm@0
   147
clasohm@0
   148
fun mixfix_of (Sext {mixfix, ...}) = mixfix
clasohm@0
   149
  | mixfix_of (NewSext {mixfix, ...}) = mixfix;
clasohm@0
   150
clasohm@0
   151
clasohm@0
   152
(* xrules_of *)
clasohm@0
   153
clasohm@0
   154
fun xrules_of (Sext _) = []
clasohm@0
   155
  | xrules_of (NewSext {xrules, ...}) = xrules;
clasohm@0
   156
clasohm@0
   157
clasohm@0
   158
wenzelm@382
   159
(*** translation functions ***) (* FIXME -> trans.ML *)
wenzelm@382
   160
wenzelm@382
   161
fun const c = Const (c, dummyT);
wenzelm@382
   162
wenzelm@382
   163
wenzelm@18
   164
(** parse (ast) translations **)
clasohm@0
   165
clasohm@0
   166
(* application *)
clasohm@0
   167
clasohm@0
   168
fun appl_ast_tr (*"_appl"*) [f, args] = Appl (f :: unfold_ast "_args" args)
clasohm@0
   169
  | appl_ast_tr (*"_appl"*) asts = raise_ast "appl_ast_tr" asts;
clasohm@0
   170
clasohm@0
   171
clasohm@0
   172
(* abstraction *)
clasohm@0
   173
clasohm@0
   174
fun idtyp_ast_tr (*"_idtyp"*) [x, ty] = Appl [Constant constrainC, x, ty]
clasohm@0
   175
  | idtyp_ast_tr (*"_idtyp"*) asts = raise_ast "idtyp_ast_tr" asts;
clasohm@0
   176
clasohm@0
   177
fun lambda_ast_tr (*"_lambda"*) [idts, body] =
wenzelm@18
   178
      fold_ast_p "_abs" (unfold_ast "_idts" idts, body)
clasohm@0
   179
  | lambda_ast_tr (*"_lambda"*) asts = raise_ast "lambda_ast_tr" asts;
clasohm@0
   180
wenzelm@18
   181
fun abs_tr (*"_abs"*) [Free (x, T), body] = absfree (x, T, body)
wenzelm@18
   182
  | abs_tr (*"_abs"*) (ts as [Const (c, _) $ Free (x, T) $ tT, body]) =
clasohm@0
   183
      if c = constrainC then
wenzelm@382
   184
        const "_constrainAbs" $ absfree (x, T, body) $ tT
wenzelm@18
   185
      else raise_term "abs_tr" ts
wenzelm@18
   186
  | abs_tr (*"_abs"*) ts = raise_term "abs_tr" ts;
clasohm@0
   187
clasohm@0
   188
wenzelm@18
   189
(* nondependent abstraction *)
wenzelm@18
   190
lcp@276
   191
fun k_tr (*"_K"*) [t] = Abs ("uu", dummyT, incr_boundvars 1 t)
wenzelm@18
   192
  | k_tr (*"_K"*) ts = raise_term "k_tr" ts;
wenzelm@18
   193
wenzelm@18
   194
wenzelm@18
   195
(* binder *)
clasohm@0
   196
clasohm@0
   197
fun mk_binder_tr (sy, name) =
clasohm@0
   198
  let
wenzelm@382
   199
    fun tr (Free (x, T), t) = const name $ absfree (x, T, t)
clasohm@0
   200
      | tr (Const ("_idts", _) $ idt $ idts, t) = tr (idt, tr (idts, t))
wenzelm@18
   201
      | tr (t1 as Const (c, _) $ Free (x, T) $ tT, t) =
clasohm@0
   202
          if c = constrainC then
wenzelm@382
   203
            const name $ (const "_constrainAbs" $ absfree (x, T, t) $ tT)
wenzelm@18
   204
          else raise_term "binder_tr" [t1, t]
wenzelm@18
   205
      | tr (t1, t2) = raise_term "binder_tr" [t1, t2];
clasohm@0
   206
clasohm@0
   207
    fun binder_tr (*sy*) [idts, body] = tr (idts, body)
wenzelm@18
   208
      | binder_tr (*sy*) ts = raise_term "binder_tr" ts;
clasohm@0
   209
  in
clasohm@0
   210
    (sy, binder_tr)
clasohm@0
   211
  end;
clasohm@0
   212
clasohm@0
   213
wenzelm@382
   214
(* meta propositions *)
clasohm@0
   215
wenzelm@382
   216
fun aprop_tr (*"_aprop"*) [t] = const constrainC $ t $ const "prop"
wenzelm@18
   217
  | aprop_tr (*"_aprop"*) ts = raise_term "aprop_tr" ts;
clasohm@0
   218
wenzelm@473
   219
fun ofclass_tr (*"_ofclass"*) [ty, cls] =
wenzelm@473
   220
      cls $ (const constrainC $ const "TYPE" $ (const "itself" $ ty))
wenzelm@473
   221
  | ofclass_tr (*"_ofclass"*) ts = raise_term "ofclass_tr" ts;
wenzelm@382
   222
clasohm@0
   223
clasohm@0
   224
(* meta implication *)
clasohm@0
   225
clasohm@0
   226
fun bigimpl_ast_tr (*"_bigimpl"*) [asms, concl] =
clasohm@0
   227
      fold_ast_p "==>" (unfold_ast "_asms" asms, concl)
clasohm@0
   228
  | bigimpl_ast_tr (*"_bigimpl"*) asts = raise_ast "bigimpl_ast_tr" asts;
clasohm@0
   229
clasohm@0
   230
wenzelm@259
   231
(* explode atoms *)
wenzelm@238
   232
wenzelm@272
   233
fun explode_tr (*"_explode"*) (ts as [consC, nilC, bit0, bit1, txt]) =
wenzelm@238
   234
      let
wenzelm@238
   235
        fun mk_list [] = nilC
wenzelm@238
   236
          | mk_list (t :: ts) = consC $ t $ mk_list ts;
wenzelm@238
   237
wenzelm@238
   238
        fun encode_bit 0 = bit0
wenzelm@238
   239
          | encode_bit 1 = bit1
wenzelm@238
   240
          | encode_bit _ = sys_error "encode_bit";
wenzelm@238
   241
wenzelm@259
   242
        fun encode_char c =   (* FIXME leading 0s (?) *)
wenzelm@238
   243
          mk_list (map encode_bit (radixpand (2, (ord c))));
wenzelm@272
   244
wenzelm@272
   245
        val str =
wenzelm@272
   246
          (case txt of
wenzelm@272
   247
            Free (s, _) => s
wenzelm@272
   248
          | Const (s, _) => s
wenzelm@272
   249
          | _ => raise_term "explode_tr" ts);
wenzelm@238
   250
      in
wenzelm@238
   251
        mk_list (map encode_char (explode str))
wenzelm@238
   252
      end
wenzelm@238
   253
  | explode_tr (*"_explode"*) ts = raise_term "explode_tr" ts;
wenzelm@238
   254
wenzelm@238
   255
clasohm@0
   256
wenzelm@18
   257
(** print (ast) translations **)
clasohm@0
   258
clasohm@0
   259
(* application *)
clasohm@0
   260
clasohm@0
   261
fun appl_ast_tr' (f, []) = raise_ast "appl_ast_tr'" [f]
clasohm@0
   262
  | appl_ast_tr' (f, args) = Appl [Constant "_appl", f, fold_ast "_args" args];
clasohm@0
   263
clasohm@0
   264
wenzelm@18
   265
(* abstraction *)
clasohm@0
   266
clasohm@0
   267
fun strip_abss vars_of body_of tm =
clasohm@0
   268
  let
wenzelm@18
   269
    fun free (x, _) = Free (x, dummyT);
wenzelm@18
   270
clasohm@0
   271
    val vars = vars_of tm;
clasohm@0
   272
    val body = body_of tm;
clasohm@0
   273
    val rev_new_vars = rename_wrt_term body vars;
clasohm@0
   274
  in
wenzelm@18
   275
    (map Free (rev rev_new_vars), subst_bounds (map free rev_new_vars, body))
clasohm@0
   276
  end;
clasohm@0
   277
clasohm@0
   278
(*do (partial) eta-contraction before printing*)
clasohm@0
   279
clasohm@0
   280
val eta_contract = ref false;
clasohm@0
   281
clasohm@0
   282
fun eta_contr tm =
clasohm@0
   283
  let
clasohm@0
   284
    fun eta_abs (Abs (a, T, t)) =
clasohm@0
   285
          (case eta_abs t of
wenzelm@18
   286
            t' as f $ u =>
clasohm@0
   287
              (case eta_abs u of
clasohm@0
   288
                Bound 0 =>
clasohm@0
   289
                  if not (0 mem loose_bnos f) then incr_boundvars ~1 f
clasohm@0
   290
                  else Abs (a, T, t')
clasohm@0
   291
              | _ => Abs (a, T, t'))
clasohm@0
   292
          | t' => Abs (a, T, t'))
clasohm@0
   293
      | eta_abs t = t;
clasohm@0
   294
  in
clasohm@0
   295
    if ! eta_contract then eta_abs tm else tm
clasohm@0
   296
  end;
clasohm@0
   297
clasohm@0
   298
clasohm@0
   299
fun abs_tr' tm =
wenzelm@382
   300
  foldr (fn (x, t) => const "_abs" $ x $ t)
clasohm@0
   301
    (strip_abss strip_abs_vars strip_abs_body (eta_contr tm));
clasohm@0
   302
clasohm@0
   303
wenzelm@18
   304
fun abs_ast_tr' (*"_abs"*) asts =
wenzelm@18
   305
  (case unfold_ast_p "_abs" (Appl (Constant "_abs" :: asts)) of
wenzelm@18
   306
    ([], _) => raise_ast "abs_ast_tr'" asts
clasohm@0
   307
  | (xs, body) => Appl [Constant "_lambda", fold_ast "_idts" xs, body]);
clasohm@0
   308
clasohm@0
   309
wenzelm@18
   310
(* binder *)
clasohm@0
   311
clasohm@0
   312
fun mk_binder_tr' (name, sy) =
clasohm@0
   313
  let
clasohm@0
   314
    fun mk_idts [] = raise Match    (*abort translation*)
clasohm@0
   315
      | mk_idts [idt] = idt
wenzelm@382
   316
      | mk_idts (idt :: idts) = const "_idts" $ idt $ mk_idts idts;
clasohm@0
   317
clasohm@0
   318
    fun tr' t =
clasohm@0
   319
      let
clasohm@0
   320
        val (xs, bd) = strip_abss (strip_qnt_vars name) (strip_qnt_body name) t;
clasohm@0
   321
      in
wenzelm@382
   322
        const sy $ mk_idts xs $ bd
clasohm@0
   323
      end;
clasohm@0
   324
clasohm@0
   325
    fun binder_tr' (*name*) (t :: ts) =
wenzelm@382
   326
          list_comb (tr' (const name $ t), ts)
clasohm@0
   327
      | binder_tr' (*name*) [] = raise Match;
clasohm@0
   328
  in
clasohm@0
   329
    (name, binder_tr')
clasohm@0
   330
  end;
clasohm@0
   331
clasohm@0
   332
clasohm@0
   333
(* idts *)
clasohm@0
   334
clasohm@0
   335
fun idts_ast_tr' (*"_idts"*) [Appl [Constant c, x, ty], xs] =
clasohm@0
   336
      if c = constrainC then
clasohm@0
   337
        Appl [Constant "_idts", Appl [Constant "_idtyp", x, ty], xs]
clasohm@0
   338
      else raise Match
clasohm@0
   339
  | idts_ast_tr' (*"_idts"*) _ = raise Match;
clasohm@0
   340
clasohm@0
   341
wenzelm@382
   342
(* meta propositions *)
wenzelm@382
   343
wenzelm@382
   344
fun prop_tr' show_sorts tm =
wenzelm@382
   345
  let
wenzelm@382
   346
    fun aprop t = const "_aprop" $ t;
wenzelm@382
   347
wenzelm@382
   348
    fun is_prop tys t =
wenzelm@382
   349
      fastype_of1 (tys, t) = propT handle TERM _ => false;
wenzelm@382
   350
wenzelm@382
   351
    fun tr' _ (t as Const _) = t
wenzelm@382
   352
      | tr' _ (t as Free (x, ty)) =
wenzelm@382
   353
          if ty = propT then aprop (Free (x, dummyT)) else t
wenzelm@382
   354
      | tr' _ (t as Var (xi, ty)) =
wenzelm@382
   355
          if ty = propT then aprop (Var (xi, dummyT)) else t
wenzelm@382
   356
      | tr' tys (t as Bound _) =
wenzelm@382
   357
          if is_prop tys t then aprop t else t
wenzelm@382
   358
      | tr' tys (Abs (x, ty, t)) = Abs (x, ty, tr' (ty :: tys) t)
wenzelm@382
   359
      | tr' tys (t as t1 $ (t2 as Const ("TYPE", Type ("itself", [ty])))) =
wenzelm@382
   360
          if is_prop tys t then
wenzelm@473
   361
            const "_ofclass" $ term_of_typ show_sorts ty $ tr' tys t1
wenzelm@382
   362
          else tr' tys t1 $ tr' tys t2
wenzelm@382
   363
      | tr' tys (t as t1 $ t2) =
wenzelm@382
   364
          (if is_Const (head_of t) orelse not (is_prop tys t)
wenzelm@382
   365
            then I else aprop) (tr' tys t1 $ tr' tys t2);
wenzelm@382
   366
  in
wenzelm@382
   367
    tr' [] tm
wenzelm@382
   368
  end;
wenzelm@382
   369
wenzelm@382
   370
clasohm@0
   371
(* meta implication *)
clasohm@0
   372
clasohm@0
   373
fun impl_ast_tr' (*"==>"*) asts =
clasohm@0
   374
  (case unfold_ast_p "==>" (Appl (Constant "==>" :: asts)) of
wenzelm@18
   375
    (asms as _ :: _ :: _, concl)
clasohm@0
   376
      => Appl [Constant "_bigimpl", fold_ast "_asms" asms, concl]
clasohm@0
   377
  | _ => raise Match);
clasohm@0
   378
clasohm@0
   379
wenzelm@18
   380
(* dependent / nondependent quantifiers *)
clasohm@0
   381
wenzelm@18
   382
fun dependent_tr' (q, r) (A :: Abs (x, T, B) :: ts) =
clasohm@0
   383
      if 0 mem (loose_bnos B) then
clasohm@0
   384
        let val (x', B') = variant_abs (x, dummyT, B);
wenzelm@382
   385
        in list_comb (const q $ Free (x', T) $ A $ B', ts) end
wenzelm@382
   386
      else list_comb (const r $ A $ B, ts)
clasohm@0
   387
  | dependent_tr' _ _ = raise Match;
clasohm@0
   388
clasohm@0
   389
wenzelm@259
   390
(* implode atoms *)
clasohm@0
   391
wenzelm@238
   392
fun implode_ast_tr' (*"_implode"*) (asts as [Constant cons_name, nilC,
wenzelm@238
   393
    bit0, bit1, bitss]) =
wenzelm@238
   394
      let
wenzelm@259
   395
        fun err () = raise_ast "implode_ast_tr'" asts;
wenzelm@238
   396
wenzelm@238
   397
        fun strip_list lst =
wenzelm@238
   398
          let val (xs, y) = unfold_ast_p cons_name lst
wenzelm@259
   399
          in if y = nilC then xs else err ()
wenzelm@238
   400
          end;
wenzelm@238
   401
wenzelm@238
   402
        fun decode_bit bit =
wenzelm@259
   403
          if bit = bit0 then "0"
wenzelm@259
   404
          else if bit = bit1 then "1"
wenzelm@259
   405
          else err ();
wenzelm@238
   406
wenzelm@238
   407
        fun decode_char bits =
wenzelm@238
   408
          chr (#1 (scan_radixint (2, map decode_bit (strip_list bits))));
wenzelm@238
   409
      in
wenzelm@238
   410
        Variable (implode (map decode_char (strip_list bitss)))
wenzelm@238
   411
      end
wenzelm@238
   412
  | implode_ast_tr' (*"_implode"*) asts = raise_ast "implode_ast_tr'" asts;
wenzelm@238
   413
wenzelm@238
   414
wenzelm@238
   415
wenzelm@382
   416
wenzelm@382
   417
(** syn_ext_of_sext **)   (* FIXME remove *)
clasohm@0
   418
wenzelm@18
   419
fun strip_esc str =
clasohm@0
   420
  let
wenzelm@18
   421
    fun strip ("'" :: c :: cs) = c :: strip cs
wenzelm@18
   422
      | strip ["'"] = []
wenzelm@18
   423
      | strip (c :: cs) = c :: strip cs
wenzelm@18
   424
      | strip [] = [];
clasohm@0
   425
  in
wenzelm@18
   426
    implode (strip (explode str))
clasohm@0
   427
  end;
clasohm@0
   428
wenzelm@18
   429
fun infix_name sy = "op " ^ strip_esc sy;
clasohm@0
   430
clasohm@0
   431
clasohm@330
   432
fun syn_ext_of_sext all_roots new_roots xconsts read_typ sext =
clasohm@0
   433
  let
wenzelm@18
   434
    val {mixfix, parse_ast_translation, parse_translation, print_translation,
wenzelm@18
   435
      print_ast_translation, ...} = sext_components sext;
clasohm@0
   436
wenzelm@18
   437
    val tinfixT = [typeT, typeT] ---> typeT;
clasohm@0
   438
clasohm@0
   439
    fun binder (Binder (sy, _, name, _, _)) = Some (sy, name)
clasohm@0
   440
      | binder _ = None;
clasohm@0
   441
clasohm@0
   442
    fun binder_typ ty =
clasohm@0
   443
      (case read_typ ty of
clasohm@0
   444
        Type ("fun", [Type ("fun", [_, T2]), T3]) =>
clasohm@0
   445
          [Type ("idts", []), T2] ---> T3
wenzelm@18
   446
      | _ => error ("Illegal binder type " ^ quote ty));
clasohm@0
   447
wenzelm@238
   448
    fun mk_infix sy ty c p1 p2 p3 =
wenzelm@238
   449
      [Mfix ("(_ " ^ sy ^ "/ _)", ty, c, [p1, p2], p3),
wenzelm@238
   450
       Mfix ("op " ^ sy, ty, c, [], max_pri)];
wenzelm@18
   451
wenzelm@18
   452
    fun mfix_of (Mixfix (sy, ty, c, ps, p)) = [Mfix (sy, read_typ ty, c, ps, p)]
clasohm@0
   453
      | mfix_of (Delimfix (sy, ty, c)) = [Mfix (sy, read_typ ty, c, [], max_pri)]
clasohm@0
   454
      | mfix_of (Infixl (sy, ty, p)) =
wenzelm@18
   455
          mk_infix sy (read_typ ty) (infix_name sy) p (p + 1) p
clasohm@0
   456
      | mfix_of (Infixr (sy, ty, p)) =
wenzelm@18
   457
          mk_infix sy (read_typ ty) (infix_name sy) (p + 1) p p
clasohm@0
   458
      | mfix_of (Binder (sy, ty, _, p, q)) =
clasohm@0
   459
          [Mfix ("(3" ^ sy ^ "_./ _)", binder_typ ty, sy, [0, p], q)]
clasohm@0
   460
      | mfix_of (TInfixl (s, c, p)) =
wenzelm@18
   461
          [Mfix ("(_ " ^ s ^ "/ _)", tinfixT, c, [p, p + 1], p)]
clasohm@0
   462
      | mfix_of (TInfixr (s, c, p)) =
wenzelm@18
   463
          [Mfix ("(_ " ^ s ^ "/ _)", tinfixT, c, [p + 1, p], p)];
clasohm@0
   464
clasohm@0
   465
    val mfix = flat (map mfix_of mixfix);
wenzelm@18
   466
    val binders = mapfilter binder mixfix;
wenzelm@18
   467
    val bparses = map mk_binder_tr binders;
wenzelm@238
   468
    val bprints = map (mk_binder_tr' o swap) binders;
clasohm@0
   469
  in
clasohm@330
   470
    syn_ext all_roots new_roots mfix (distinct (filter is_xid xconsts))
wenzelm@238
   471
      (parse_ast_translation,
wenzelm@238
   472
        bparses @ parse_translation,
wenzelm@238
   473
        bprints @ print_translation,
wenzelm@238
   474
        print_ast_translation)
wenzelm@238
   475
      ([], [])
clasohm@0
   476
  end;
clasohm@0
   477
clasohm@0
   478
clasohm@0
   479
wenzelm@382
   480
(** constants **)     (* FIXME remove *)
wenzelm@18
   481
wenzelm@18
   482
fun constants sext =
wenzelm@18
   483
  let
wenzelm@18
   484
    fun consts (Delimfix (_, ty, c)) = ([c], ty)
wenzelm@18
   485
      | consts (Mixfix (_, ty, c, _, _)) = ([c], ty)
wenzelm@18
   486
      | consts (Infixl (c, ty, _)) = ([infix_name c], ty)
wenzelm@18
   487
      | consts (Infixr (c, ty, _)) = ([infix_name c], ty)
wenzelm@18
   488
      | consts (Binder (_, ty, c, _, _)) = ([c], ty)
wenzelm@18
   489
      | consts _ = ([""], "");    (*is filtered out below*)
wenzelm@18
   490
  in
wenzelm@18
   491
    distinct (filter_out (fn (l, _) => l = [""]) (map consts (mixfix_of sext)))
wenzelm@18
   492
  end;
wenzelm@18
   493
wenzelm@18
   494
wenzelm@18
   495
wenzelm@238
   496
(** pt_to_ast **)
wenzelm@238
   497
wenzelm@238
   498
fun pt_to_ast trf pt =
wenzelm@238
   499
  let
wenzelm@238
   500
    fun trans a args =
wenzelm@238
   501
      (case trf a of
wenzelm@238
   502
        None => mk_appl (Constant a) args
wenzelm@238
   503
      | Some f => f args handle exn
wenzelm@238
   504
          => (writeln ("Error in parse ast translation for " ^ quote a); raise exn));
wenzelm@238
   505
wenzelm@238
   506
    fun ast_of (Node (a, pts)) = trans a (map ast_of pts)
wenzelm@238
   507
      | ast_of (Tip tok) = Variable (str_of_token tok);
wenzelm@238
   508
  in
wenzelm@238
   509
    ast_of pt
wenzelm@238
   510
  end;
wenzelm@238
   511
wenzelm@238
   512
wenzelm@238
   513
clasohm@0
   514
(** ast_to_term **)
clasohm@0
   515
clasohm@0
   516
fun ast_to_term trf ast =
clasohm@0
   517
  let
clasohm@0
   518
    fun trans a args =
clasohm@0
   519
      (case trf a of
wenzelm@382
   520
        None => list_comb (const a, args)
wenzelm@18
   521
      | Some f => f args handle exn
wenzelm@18
   522
          => (writeln ("Error in parse translation for " ^ quote a); raise exn));
clasohm@0
   523
wenzelm@18
   524
    fun term_of (Constant a) = trans a []
wenzelm@18
   525
      | term_of (Variable x) = scan_var x
wenzelm@18
   526
      | term_of (Appl (Constant a :: (asts as _ :: _))) =
wenzelm@18
   527
          trans a (map term_of asts)
wenzelm@18
   528
      | term_of (Appl (ast :: (asts as _ :: _))) =
wenzelm@18
   529
          list_comb (term_of ast, map term_of asts)
wenzelm@18
   530
      | term_of (ast as Appl _) = raise_ast "ast_to_term: malformed ast" [ast];
clasohm@0
   531
  in
wenzelm@18
   532
    term_of ast
clasohm@0
   533
  end;
clasohm@0
   534
clasohm@0
   535
clasohm@0
   536
wenzelm@382
   537
(** pure_trfuns **)
wenzelm@382
   538
wenzelm@382
   539
val pure_trfuns =
wenzelm@382
   540
 ([(applC, appl_ast_tr), ("_lambda", lambda_ast_tr), ("_idtyp", idtyp_ast_tr),
wenzelm@382
   541
    ("_bigimpl", bigimpl_ast_tr)],
wenzelm@473
   542
  [("_abs", abs_tr), ("_aprop", aprop_tr), ("_ofclass", ofclass_tr),
wenzelm@473
   543
    ("_K", k_tr), ("_explode", explode_tr)],
wenzelm@382
   544
  [],
wenzelm@382
   545
  [("_abs", abs_ast_tr'), ("_idts", idts_ast_tr'), ("==>", impl_ast_tr'),
wenzelm@382
   546
    ("_implode", implode_ast_tr')]);
wenzelm@382
   547
wenzelm@382
   548
val constrainAbsC = "_constrainAbs";
wenzelm@382
   549
wenzelm@382
   550
wenzelm@382
   551
(** the Pure syntax **)   (* FIXME remove *)
clasohm@0
   552
clasohm@0
   553
val pure_sext =
clasohm@0
   554
  NewSext {
clasohm@0
   555
    mixfix = [
clasohm@0
   556
      Mixfix   ("(3%_./ _)",  "[idts, 'a] => ('b => 'a)",      "_lambda", [0], 0),
clasohm@0
   557
      Delimfix ("_",          "'a => " ^ args,                 ""),
clasohm@0
   558
      Delimfix ("_,/ _",      "['a, " ^ args ^ "] => " ^ args, "_args"),
clasohm@0
   559
      Delimfix ("_",          "id => idt",                     ""),
clasohm@0
   560
      Mixfix   ("_::_",       "[id, type] => idt",             "_idtyp", [0, 0], 0),
clasohm@0
   561
      Delimfix ("'(_')",      "idt => idt",                    ""),
clasohm@0
   562
      Delimfix ("_",          "idt => idts",                   ""),
clasohm@0
   563
      Mixfix   ("_/ _",       "[idt, idts] => idts",           "_idts", [1, 0], 0),
clasohm@0
   564
      Delimfix ("_",          "id => aprop",                   ""),
clasohm@0
   565
      Delimfix ("_",          "var => aprop",                  ""),
wenzelm@272
   566
      Mixfix   ("(1_/(1'(_')))", "[('b => 'a), " ^ args ^ "] => aprop", applC, [max_pri, 0], max_pri),
clasohm@0
   567
      Delimfix ("PROP _",     "aprop => prop",                 "_aprop"),
clasohm@0
   568
      Delimfix ("_",          "prop => asms",                  ""),
clasohm@0
   569
      Delimfix ("_;/ _",      "[prop, asms] => asms",          "_asms"),
clasohm@0
   570
      Mixfix   ("((3[| _ |]) ==>/ _)", "[asms, prop] => prop", "_bigimpl", [0, 1], 1),
clasohm@0
   571
      Mixfix   ("(_ ==/ _)",  "['a::{}, 'a] => prop",          "==", [3, 2], 2),
clasohm@0
   572
      Mixfix   ("(_ =?=/ _)", "['a::{}, 'a] => prop",          "=?=", [3, 2], 2),
clasohm@0
   573
      Mixfix   ("(_ ==>/ _)", "[prop, prop] => prop",          "==>", [2, 1], 1),
clasohm@0
   574
      Binder   ("!!",         "('a::logic => prop) => prop",   "all", 0, 0)],
clasohm@0
   575
    xrules = [],
wenzelm@238
   576
    parse_ast_translation = [(applC, appl_ast_tr), ("_lambda", lambda_ast_tr),
wenzelm@238
   577
      ("_idtyp", idtyp_ast_tr), ("_bigimpl", bigimpl_ast_tr)],
wenzelm@238
   578
    parse_translation = [("_abs", abs_tr), ("_aprop", aprop_tr), ("_K", k_tr),
wenzelm@238
   579
      ("_explode", explode_tr)],
clasohm@0
   580
    print_translation = [],
wenzelm@18
   581
    print_ast_translation = [("_abs", abs_ast_tr'), ("_idts", idts_ast_tr'),
wenzelm@238
   582
      ("==>", impl_ast_tr'), ("_implode", implode_ast_tr')]};
clasohm@0
   583
wenzelm@259
   584
val syntax_types = terminals @ ["syntax", logic, "type", "types", "sort",
wenzelm@259
   585
  "classes", args, "idt", "idts", "aprop", "asms"];
wenzelm@259
   586
wenzelm@259
   587
val syntax_consts = [(["_K", "_explode", "_implode"], "syntax")];
clasohm@0
   588
clasohm@0
   589
clasohm@0
   590
end;
clasohm@0
   591