src/Pure/Syntax/sextension.ML
author clasohm
Thu, 16 Sep 1993 12:20:38 +0200
changeset 0 a5a9c433f639
child 18 c9ec452ff08f
permissions -rw-r--r--
Initial revision
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     1
(*  Title:      Pure/Syntax/sextension
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     2
    ID:         $Id$
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     3
    Author:     Tobias Nipkow and Markus Wenzel, TU Muenchen
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     4
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     5
Syntax extensions: mixfix declarations, syntax rules, infixes, binders and
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     6
the Pure syntax.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     7
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     8
Changes:
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     9
  SEXTENSION: added Ast, xrule
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    10
  changed sext
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    11
  added ast_to_term
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    12
  ext_of_sext: added xconsts
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    13
  SEXTENSION1: added empty_sext, appl_ast_tr'
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    14
  SEXTENSION1: removed appl_tr'
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    15
TODO:
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    16
*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    17
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    18
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    19
infix |-> <-| <->;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    20
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    21
signature SEXTENSION0 =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    22
sig
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    23
  structure Ast: AST
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    24
  local open Ast in
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    25
    datatype mixfix =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    26
      Mixfix of string * string * string * int list * int |
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    27
      Delimfix of string * string * string |
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    28
      Infixl of string * string * int |
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    29
      Infixr of string * string * int |
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    30
      Binder of string * string * string * int * int |
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    31
      TInfixl of string * string * int |
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    32
      TInfixr of string * string * int
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    33
    datatype xrule =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    34
      op |-> of (string * string) * (string * string) |
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    35
      op <-| of (string * string) * (string * string) |
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    36
      op <-> of (string * string) * (string * string)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    37
    datatype sext =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    38
      Sext of {
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    39
        mixfix: mixfix list,
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    40
        parse_translation: (string * (term list -> term)) list,
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    41
        print_translation: (string * (term list -> term)) list} |
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    42
      NewSext of {
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    43
        mixfix: mixfix list,
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    44
        xrules: xrule list,
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    45
        parse_ast_translation: (string * (ast list -> ast)) list,
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    46
        parse_preproc: (ast -> ast) option,
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    47
        parse_postproc: (ast -> ast) option,
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    48
        parse_translation: (string * (term list -> term)) list,
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    49
        print_translation: (string * (term list -> term)) list,
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    50
        print_preproc: (ast -> ast) option,
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    51
        print_postproc: (ast -> ast) option,
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    52
        print_ast_translation: (string * (ast list -> ast)) list}
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    53
    val eta_contract: bool ref
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    54
    val mk_binder_tr: string * string -> string * (term list -> term)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    55
    val mk_binder_tr': string * string -> string * (term list -> term)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    56
    val ndependent_tr: string -> term list -> term
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    57
    val dependent_tr': string * string -> term list -> term
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    58
    val max_pri: int
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    59
  end
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    60
end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    61
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    62
signature SEXTENSION1 =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    63
sig
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    64
  include SEXTENSION0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    65
  val empty_sext: sext
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    66
  val simple_sext: mixfix list -> sext
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    67
  val constants: sext -> (string list * string) list
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    68
  val pure_sext: sext
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    69
  val syntax_types: string list
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    70
  val constrainAbsC: string
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    71
end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    72
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    73
signature SEXTENSION =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    74
sig
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    75
  include SEXTENSION1
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    76
  structure Extension: EXTENSION
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    77
  sharing Extension.XGram.Ast = Ast
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    78
  local open Extension Ast in
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    79
    val xrules_of: sext -> xrule list
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    80
    val abs_tr': term -> term
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    81
    val appl_ast_tr': ast * ast list -> ast
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    82
    val ext_of_sext: string list -> string list -> (string -> typ) -> sext -> ext
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    83
    val ast_to_term: (string -> (term list -> term) option) -> ast -> term
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    84
    val constrainIdtC: string
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    85
    val apropC: string
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    86
  end
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    87
end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    88
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    89
functor SExtensionFun(structure TypeExt: TYPE_EXT and Lexicon: LEXICON)(*: SEXTENSION *) = (* FIXME *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    90
struct
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    91
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    92
structure Extension = TypeExt.Extension;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    93
structure Ast = Extension.XGram.Ast;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    94
open Extension Ast;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    95
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    96
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    97
(** datatype sext **)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    98
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    99
datatype mixfix =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   100
  Mixfix of string * string * string * int list * int |
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   101
  Delimfix of string * string * string |
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   102
  Infixl of string * string * int |
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   103
  Infixr of string * string * int |
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   104
  Binder of string * string * string * int * int |
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   105
  TInfixl of string * string * int |
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   106
  TInfixr of string * string * int;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   107
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   108
datatype xrule =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   109
  op |-> of (string * string) * (string * string) |
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   110
  op <-| of (string * string) * (string * string) |
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   111
  op <-> of (string * string) * (string * string);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   112
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   113
datatype sext =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   114
  Sext of {
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   115
    mixfix: mixfix list,
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   116
    parse_translation: (string * (term list -> term)) list,
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   117
    print_translation: (string * (term list -> term)) list} |
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   118
  NewSext of {
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   119
    mixfix: mixfix list,
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   120
    xrules: xrule list,
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   121
    parse_ast_translation: (string * (ast list -> ast)) list,
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   122
    parse_preproc: (ast -> ast) option,
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   123
    parse_postproc: (ast -> ast) option,
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   124
    parse_translation: (string * (term list -> term)) list,
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   125
    print_translation: (string * (term list -> term)) list,
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   126
    print_preproc: (ast -> ast) option,
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   127
    print_postproc: (ast -> ast) option,
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   128
    print_ast_translation: (string * (ast list -> ast)) list};
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   129
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   130
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   131
(* simple_sext *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   132
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   133
fun simple_sext mixfix =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   134
  Sext {mixfix = mixfix, parse_translation = [], print_translation = []};
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   135
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   136
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   137
(* empty_sext *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   138
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   139
val empty_sext = simple_sext [];
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   140
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   141
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   142
(* sext_components *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   143
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   144
fun sext_components (Sext {mixfix, parse_translation, print_translation}) =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   145
      {mixfix = mixfix,
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   146
        xrules = [],
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   147
        parse_ast_translation = [],
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   148
        parse_preproc = None,
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   149
        parse_postproc = None,
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   150
        parse_translation = parse_translation,
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   151
        print_translation = print_translation,
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   152
        print_preproc = None,
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   153
        print_postproc = None,
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   154
        print_ast_translation = []}
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   155
  | sext_components (NewSext cmps) = cmps;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   156
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   157
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   158
(* mixfix_of *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   159
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   160
fun mixfix_of (Sext {mixfix, ...}) = mixfix
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   161
  | mixfix_of (NewSext {mixfix, ...}) = mixfix;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   162
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   163
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   164
(* xrules_of *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   165
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   166
fun xrules_of (Sext _) = []
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   167
  | xrules_of (NewSext {xrules, ...}) = xrules;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   168
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   169
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   170
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   171
(** parse translations **)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   172
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   173
(* application *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   174
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   175
fun appl_ast_tr (*"_appl"*) [f, args] = Appl (f :: unfold_ast "_args" args)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   176
  | appl_ast_tr (*"_appl"*) asts = raise_ast "appl_ast_tr" asts;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   177
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   178
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   179
(* abstraction *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   180
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   181
fun idtyp_ast_tr (*"_idtyp"*) [x, ty] = Appl [Constant constrainC, x, ty]
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   182
  | idtyp_ast_tr (*"_idtyp"*) asts = raise_ast "idtyp_ast_tr" asts;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   183
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   184
fun lambda_ast_tr (*"_lambda"*) [idts, body] =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   185
      fold_ast_p "_%" (unfold_ast "_idts" idts, body)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   186
  | lambda_ast_tr (*"_lambda"*) asts = raise_ast "lambda_ast_tr" asts;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   187
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   188
fun abs_tr (*"_%"*) [Free (x, T), body] = absfree (x, T, body)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   189
  | abs_tr (*"_%"*) (ts as [Const (c, _) $ Free (x, T) $ tT, body]) =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   190
      if c = constrainC then
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   191
        Const ("_constrainAbs", dummyT) $ absfree (x, T, body) $ tT
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   192
      else raise (TERM ("abs_tr", ts))
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   193
  | abs_tr (*"_%"*) ts = raise (TERM ("abs_tr", ts));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   194
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   195
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   196
(* binder *)  (* FIXME check *) (* FIXME check *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   197
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   198
fun mk_binder_tr (sy, name) =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   199
  let
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   200
    val const = Const (name, dummyT);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   201
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   202
    fun tr (Free (x, T), t) = const $ absfree (x, T, t)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   203
      | tr (Const ("_idts", _) $ idt $ idts, t) = tr (idt, tr (idts, t))
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   204
      | tr (t1 as (Const (c, _) $ Free (x, T) $ tT), t) =   (* FIXME *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   205
          if c = constrainC then
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   206
            const $ (Const ("_constrainAbs", dummyT) $ absfree (x, T, t) $ tT)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   207
          else raise (TERM ("binder_tr", [t1, t]))
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   208
      | tr (t1, t2) = raise (TERM ("binder_tr", [t1, t2]));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   209
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   210
    fun binder_tr (*sy*) [idts, body] = tr (idts, body)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   211
      | binder_tr (*sy*) ts = raise (TERM ("binder_tr", ts));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   212
  in
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   213
    (sy, binder_tr)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   214
  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   215
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   216
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   217
(* atomic props *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   218
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   219
fun aprop_ast_tr (*"_aprop"*) [ast] = ast
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   220
  | aprop_ast_tr (*"_aprop"*) asts = raise_ast "aprop_ast_tr" asts;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   221
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   222
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   223
(* meta implication *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   224
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   225
fun bigimpl_ast_tr (*"_bigimpl"*) [asms, concl] =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   226
      fold_ast_p "==>" (unfold_ast "_asms" asms, concl)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   227
  | bigimpl_ast_tr (*"_bigimpl"*) asts = raise_ast "bigimpl_ast_tr" asts;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   228
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   229
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   230
(* 'dependent' type operators *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   231
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   232
fun ndependent_tr q [A, B] =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   233
      Const (q, dummyT) $ A $ Abs ("x", dummyT, incr_boundvars 1 B)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   234
  | ndependent_tr _ _ = raise Match;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   235
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   236
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   237
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   238
(** print translations **)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   239
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   240
(* application *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   241
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   242
fun appl_ast_tr' (f, []) = raise_ast "appl_ast_tr'" [f]
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   243
  | appl_ast_tr' (f, args) = Appl [Constant "_appl", f, fold_ast "_args" args];
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   244
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   245
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   246
(* abstraction *)   (* FIXME check *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   247
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   248
fun strip_abss vars_of body_of tm =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   249
  let
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   250
    val vars = vars_of tm;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   251
    val body = body_of tm;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   252
    val rev_new_vars = rename_wrt_term body vars;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   253
  in
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   254
    (map Free (rev rev_new_vars),
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   255
      subst_bounds (map (fn (x, _) => Free (x, dummyT)) rev_new_vars, body))
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   256
  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   257
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   258
(*do (partial) eta-contraction before printing*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   259
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   260
val eta_contract = ref false;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   261
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   262
fun eta_contr tm =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   263
  let
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   264
    fun eta_abs (Abs (a, T, t)) =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   265
          (case eta_abs t of
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   266
            t' as (f $ u) =>
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   267
              (case eta_abs u of
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   268
                Bound 0 =>
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   269
                  if not (0 mem loose_bnos f) then incr_boundvars ~1 f
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   270
                  else Abs (a, T, t')
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   271
              | _ => Abs (a, T, t'))
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   272
          | t' => Abs (a, T, t'))
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   273
      | eta_abs t = t;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   274
  in
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   275
    if ! eta_contract then eta_abs tm else tm
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   276
  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   277
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   278
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   279
fun abs_tr' tm =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   280
  foldr (fn (x, t) => Const ("_%", dummyT) $ x $ t)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   281
    (strip_abss strip_abs_vars strip_abs_body (eta_contr tm));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   282
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   283
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   284
fun lambda_ast_tr' (*"_%"*) asts =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   285
  (case unfold_ast_p "_%" (Appl (Constant "_%" :: asts)) of
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   286
    ([], _) => raise_ast "lambda_ast_tr'" asts
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   287
  | (xs, body) => Appl [Constant "_lambda", fold_ast "_idts" xs, body]);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   288
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   289
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   290
(* binder *) (* FIXME check *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   291
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   292
fun mk_binder_tr' (name, sy) =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   293
  let
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   294
    fun mk_idts [] = raise Match    (*abort translation*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   295
      | mk_idts [idt] = idt
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   296
      | mk_idts (idt :: idts) = Const ("_idts", dummyT) $ idt $ mk_idts idts;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   297
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   298
    fun tr' t =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   299
      let
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   300
        val (xs, bd) = strip_abss (strip_qnt_vars name) (strip_qnt_body name) t;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   301
      in
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   302
        Const (sy, dummyT) $ mk_idts xs $ bd
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   303
      end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   304
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   305
    fun binder_tr' (*name*) (t :: ts) =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   306
          list_comb (tr' (Const (name, dummyT) $ t), ts)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   307
      | binder_tr' (*name*) [] = raise Match;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   308
  in
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   309
    (name, binder_tr')
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   310
  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   311
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   312
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   313
(* idts *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   314
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   315
fun idts_ast_tr' (*"_idts"*) [Appl [Constant c, x, ty], xs] =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   316
      if c = constrainC then
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   317
        Appl [Constant "_idts", Appl [Constant "_idtyp", x, ty], xs]
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   318
      else raise Match
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   319
  | idts_ast_tr' (*"_idts"*) _ = raise Match;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   320
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   321
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   322
(* meta implication *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   323
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   324
fun impl_ast_tr' (*"==>"*) asts =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   325
  (case unfold_ast_p "==>" (Appl (Constant "==>" :: asts)) of
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   326
    (asms as (_ :: _ :: _), concl)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   327
      => Appl [Constant "_bigimpl", fold_ast "_asms" asms, concl]
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   328
  | _ => raise Match);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   329
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   330
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   331
(* 'dependent' type operators *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   332
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   333
fun dependent_tr' (q, r) [A, Abs (x, T, B)] =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   334
      if 0 mem (loose_bnos B) then
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   335
        let val (x', B') = variant_abs (x, dummyT, B);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   336
        in Const (q, dummyT) $ Free (x', T) $ A $ B' end
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   337
      else Const (r, dummyT) $ A $ B
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   338
  | dependent_tr' _ _ = raise Match;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   339
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   340
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   341
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   342
(** constants **)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   343
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   344
(* FIXME opn, clean: move *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   345
val clean =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   346
  let
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   347
    fun q ("'" :: c :: cs) = c ^ q cs
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   348
      | q (c :: cs) = c ^ q cs
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   349
      | q ([]) = ""
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   350
  in q o explode end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   351
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   352
val opn = "op ";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   353
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   354
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   355
fun constants sext =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   356
  let
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   357
    fun consts (Delimfix (_, ty, c)) = ([c], ty)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   358
      | consts (Mixfix (_, ty, c, _, _)) = ([c], ty)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   359
      | consts (Infixl (c, ty, _)) = ([opn ^ clean c], ty)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   360
      | consts (Infixr (c, ty, _)) = ([opn ^ clean c], ty)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   361
      | consts (Binder (_, ty, c, _, _)) = ([c], ty)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   362
      | consts _ = ([""], "");    (*is filtered out below*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   363
  in
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   364
    distinct (filter_out (fn (l, _) => l = [""]) (map consts (mixfix_of sext)))
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   365
  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   366
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   367
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   368
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   369
(** ext_of_sext **)   (* FIXME check, clean *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   370
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   371
fun ext_of_sext roots xconsts read_typ sext =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   372
  let
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   373
    val
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   374
      {mixfix, parse_ast_translation, parse_preproc, parse_postproc,
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   375
        parse_translation, print_translation, print_preproc, print_postproc,
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   376
        print_ast_translation, ...} = sext_components sext;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   377
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   378
    val infixT = [typeT, typeT] ---> typeT;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   379
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   380
    fun binder (Binder (sy, _, name, _, _)) = Some (sy, name)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   381
      | binder _ = None;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   382
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   383
    fun binder_typ ty =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   384
      (case read_typ ty of
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   385
        Type ("fun", [Type ("fun", [_, T2]), T3]) =>
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   386
          [Type ("idts", []), T2] ---> T3
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   387
      | _ => error (quote ty ^ " is not a valid binder type."));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   388
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   389
    fun mfix_of (Mixfix (sy, ty, c, pl, p)) = [Mfix (sy, read_typ ty, c, pl, p)]
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   390
      | mfix_of (Delimfix (sy, ty, c)) = [Mfix (sy, read_typ ty, c, [], max_pri)]
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   391
      | mfix_of (Infixl (sy, ty, p)) =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   392
          let val T = read_typ ty and c = opn ^ sy and c' = opn ^ clean sy
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   393
          in
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   394
            [Mfix (c, T, c', [], max_pri),
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   395
             Mfix("(_ " ^ sy ^ "/ _)", T, c', [p, p + 1], p)]
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   396
          end
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   397
      | mfix_of (Infixr (sy, ty, p)) =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   398
          let val T = read_typ ty and c = opn ^ sy and c' = opn ^ clean sy
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   399
          in
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   400
            [Mfix(c, T, c', [], max_pri),
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   401
             Mfix("(_ " ^ sy ^ "/ _)", T, c', [p + 1, p], p)]
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   402
          end
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   403
      | mfix_of (Binder (sy, ty, _, p, q)) =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   404
          [Mfix ("(3" ^ sy ^ "_./ _)", binder_typ ty, sy, [0, p], q)]
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   405
      | mfix_of (TInfixl (s, c, p)) =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   406
          [Mfix ("(_ " ^ s ^ "/ _)", infixT, c, [p, p + 1], p)]
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   407
      | mfix_of (TInfixr (s, c, p)) =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   408
          [Mfix ("(_ " ^ s ^ "/ _)", infixT, c, [p + 1, p], p)];
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   409
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   410
    val mfix = flat (map mfix_of mixfix);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   411
    val mfix_consts = map (fn (Mfix (_, _, c, _, _)) => c) mfix;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   412
    val bs = mapfilter binder mixfix;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   413
    val bparses = map mk_binder_tr bs;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   414
    val bprints = map (mk_binder_tr' o (fn (x, y) => (y, x))) bs;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   415
  in
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   416
    Ext {
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   417
      roots = roots, mfix = mfix,
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   418
      extra_consts = distinct (filter Lexicon.is_identifier (xconsts @ mfix_consts)),
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   419
      parse_ast_translation = parse_ast_translation,
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   420
      parse_preproc = parse_preproc,
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   421
      parse_postproc = parse_postproc,
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   422
      parse_translation = bparses @ parse_translation,
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   423
      print_translation = bprints @ print_translation,
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   424
      print_preproc = print_preproc,
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   425
      print_postproc = print_postproc,
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   426
      print_ast_translation = print_ast_translation}
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   427
  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   428
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   429
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   430
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   431
(** ast_to_term **)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   432
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   433
fun ast_to_term trf ast =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   434
  let
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   435
    fun scan_vname prfx cs =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   436
      (case Lexicon.scan_varname cs of
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   437
        ((x, i), []) => Var ((prfx ^ x, i), dummyT)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   438
      | _ => error ("ast_to_term: bad variable name " ^ quote (implode cs)));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   439
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   440
    fun vname_to_var v =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   441
      (case explode v of
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   442
        "?" :: "'" :: cs => scan_vname "'" cs
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   443
      | "?" :: cs => scan_vname "" cs
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   444
      | _ => Free (v, dummyT));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   445
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   446
    fun trans a args =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   447
      (case trf a of
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   448
        None => list_comb (Const (a, dummyT), args)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   449
      | Some f => ((f args)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   450
          handle _ => error ("ast_to_term: error in translation for " ^ quote a)));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   451
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   452
    fun trav (Constant a) = trans a []
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   453
      | trav (Appl (Constant a :: (asts as _ :: _))) = trans a (map trav asts)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   454
      | trav (Appl (ast :: (asts as _ :: _))) =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   455
          list_comb (trav ast, map trav asts)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   456
      | trav (ast as (Appl _)) = raise_ast "ast_to_term: malformed ast" [ast]
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   457
      | trav (Variable x) = vname_to_var x;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   458
  in
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   459
    trav ast
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   460
  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   461
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   462
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   463
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   464
(** the Pure syntax **)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   465
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   466
val pure_sext =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   467
  NewSext {
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   468
    mixfix = [
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   469
      Mixfix   ("(3%_./ _)",  "[idts, 'a] => ('b => 'a)",      "_lambda", [0], 0),
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   470
      Delimfix ("_",          "'a => " ^ args,                 ""),
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   471
      Delimfix ("_,/ _",      "['a, " ^ args ^ "] => " ^ args, "_args"),
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   472
      Delimfix ("_",          "id => idt",                     ""),
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   473
      Mixfix   ("_::_",       "[id, type] => idt",             "_idtyp", [0, 0], 0),
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   474
      Delimfix ("'(_')",      "idt => idt",                    ""),
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   475
      Delimfix ("_",          "idt => idts",                   ""),
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   476
      Mixfix   ("_/ _",       "[idt, idts] => idts",           "_idts", [1, 0], 0),
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   477
      Delimfix ("_",          "id => aprop",                   ""),
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   478
      Delimfix ("_",          "var => aprop",                  ""),
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   479
      Mixfix   ("_'(_')",     "[('b => 'a), " ^ args ^ "] => aprop", applC, [max_pri, 0], 0),
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   480
      Delimfix ("PROP _",     "aprop => prop",                 "_aprop"),
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   481
      Delimfix ("_",          "prop => asms",                  ""),
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   482
      Delimfix ("_;/ _",      "[prop, asms] => asms",          "_asms"),
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   483
      Mixfix   ("((3[| _ |]) ==>/ _)", "[asms, prop] => prop", "_bigimpl", [0, 1], 1),
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   484
      Mixfix   ("(_ ==/ _)",  "['a::{}, 'a] => prop",          "==", [3, 2], 2),
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   485
      Mixfix   ("(_ =?=/ _)", "['a::{}, 'a] => prop",          "=?=", [3, 2], 2),
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   486
      Mixfix   ("(_ ==>/ _)", "[prop, prop] => prop",          "==>", [2, 1], 1),
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   487
      Binder   ("!!",         "('a::logic => prop) => prop",   "all", 0, 0)],
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   488
    xrules = [],
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   489
    parse_ast_translation =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   490
      [(applC, appl_ast_tr), ("_lambda", lambda_ast_tr), ("_idtyp", idtyp_ast_tr),
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   491
        ("_aprop", aprop_ast_tr), ("_bigimpl", bigimpl_ast_tr)],
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   492
    parse_preproc = None,
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   493
    parse_postproc = None,
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   494
    parse_translation = [("_%", abs_tr)],
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   495
    print_translation = [],
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   496
    print_preproc = None,
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   497
    print_postproc = None,
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   498
    print_ast_translation = [("_%", lambda_ast_tr'), ("_idts", idts_ast_tr'),
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   499
      ("==>", impl_ast_tr')]};
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   500
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   501
val syntax_types =    (* FIXME clean, check *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   502
  [logic, "aprop", args, "asms", id, "idt", "idts", tfree, tvar, "type", "types",
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   503
    var, "sort", "classes"]
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   504
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   505
val constrainIdtC = "_idtyp";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   506
val constrainAbsC = "_constrainAbs";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   507
val apropC = "_aprop";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   508
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   509
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   510
end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   511