src/Pure/Syntax/syn_trans.ML
author wenzelm
Fri Aug 19 15:34:28 1994 +0200 (1994-08-19)
changeset 548 12208de7edfe
child 616 2b1e384fae33
permissions -rw-r--r--
added this file;
wenzelm@548
     1
(*  Title:      Pure/Syntax/syn_trans.ML
wenzelm@548
     2
    ID:         $Id$
wenzelm@548
     3
    Author:     Tobias Nipkow and Markus Wenzel, TU Muenchen
wenzelm@548
     4
wenzelm@548
     5
Syntax translation functions.
wenzelm@548
     6
*)
wenzelm@548
     7
wenzelm@548
     8
signature SYN_TRANS0 =
wenzelm@548
     9
sig
wenzelm@548
    10
  val eta_contract: bool ref
wenzelm@548
    11
  val mk_binder_tr: string * string -> string * (term list -> term)
wenzelm@548
    12
  val mk_binder_tr': string * string -> string * (term list -> term)
wenzelm@548
    13
  val dependent_tr': string * string -> term list -> term
wenzelm@548
    14
end;
wenzelm@548
    15
wenzelm@548
    16
signature SYN_TRANS1 =
wenzelm@548
    17
sig
wenzelm@548
    18
  include SYN_TRANS0
wenzelm@548
    19
  structure Parser: PARSER
wenzelm@548
    20
  local open Parser.SynExt.Ast in
wenzelm@548
    21
    val constrainAbsC: string
wenzelm@548
    22
    val pure_trfuns:
wenzelm@548
    23
      (string * (ast list -> ast)) list *
wenzelm@548
    24
      (string * (term list -> term)) list *
wenzelm@548
    25
      (string * (term list -> term)) list *
wenzelm@548
    26
      (string * (ast list -> ast)) list
wenzelm@548
    27
  end
wenzelm@548
    28
end;
wenzelm@548
    29
wenzelm@548
    30
signature SYN_TRANS =
wenzelm@548
    31
sig
wenzelm@548
    32
  include SYN_TRANS1
wenzelm@548
    33
  local open Parser Parser.SynExt Parser.SynExt.Ast in
wenzelm@548
    34
    val abs_tr': term -> term
wenzelm@548
    35
    val prop_tr': bool -> term -> term
wenzelm@548
    36
    val appl_ast_tr': ast * ast list -> ast
wenzelm@548
    37
    val pt_to_ast: (string -> (ast list -> ast) option) -> parsetree -> ast
wenzelm@548
    38
    val ast_to_term: (string -> (term list -> term) option) -> ast -> term
wenzelm@548
    39
  end
wenzelm@548
    40
end;
wenzelm@548
    41
wenzelm@548
    42
functor SynTransFun(structure TypeExt: TYPE_EXT and Parser: PARSER
wenzelm@548
    43
  sharing TypeExt.SynExt = Parser.SynExt): SYN_TRANS =
wenzelm@548
    44
struct
wenzelm@548
    45
wenzelm@548
    46
structure Parser = Parser;
wenzelm@548
    47
open TypeExt Parser.Lexicon Parser.SynExt.Ast Parser.SynExt Parser;
wenzelm@548
    48
wenzelm@548
    49
wenzelm@548
    50
(** parse (ast) translations **)
wenzelm@548
    51
wenzelm@548
    52
(* application *)
wenzelm@548
    53
wenzelm@548
    54
fun appl_ast_tr (*"_appl"*) [f, args] = Appl (f :: unfold_ast "_args" args)
wenzelm@548
    55
  | appl_ast_tr (*"_appl"*) asts = raise_ast "appl_ast_tr" asts;
wenzelm@548
    56
wenzelm@548
    57
wenzelm@548
    58
(* abstraction *)
wenzelm@548
    59
wenzelm@548
    60
fun idtyp_ast_tr (*"_idtyp"*) [x, ty] = Appl [Constant constrainC, x, ty]
wenzelm@548
    61
  | idtyp_ast_tr (*"_idtyp"*) asts = raise_ast "idtyp_ast_tr" asts;
wenzelm@548
    62
wenzelm@548
    63
fun lambda_ast_tr (*"_lambda"*) [idts, body] =
wenzelm@548
    64
      fold_ast_p "_abs" (unfold_ast "_idts" idts, body)
wenzelm@548
    65
  | lambda_ast_tr (*"_lambda"*) asts = raise_ast "lambda_ast_tr" asts;
wenzelm@548
    66
wenzelm@548
    67
val constrainAbsC = "_constrainAbs";
wenzelm@548
    68
wenzelm@548
    69
fun abs_tr (*"_abs"*) [Free (x, T), body] = absfree (x, T, body)
wenzelm@548
    70
  | abs_tr (*"_abs"*) (ts as [Const (c, _) $ Free (x, T) $ tT, body]) =
wenzelm@548
    71
      if c = constrainC
wenzelm@548
    72
        then const constrainAbsC $ absfree (x, T, body) $ tT
wenzelm@548
    73
      else raise_term "abs_tr" ts
wenzelm@548
    74
  | abs_tr (*"_abs"*) ts = raise_term "abs_tr" ts;
wenzelm@548
    75
wenzelm@548
    76
wenzelm@548
    77
(* nondependent abstraction *)
wenzelm@548
    78
wenzelm@548
    79
fun k_tr (*"_K"*) [t] = Abs ("uu", dummyT, incr_boundvars 1 t)
wenzelm@548
    80
  | k_tr (*"_K"*) ts = raise_term "k_tr" ts;
wenzelm@548
    81
wenzelm@548
    82
wenzelm@548
    83
(* binder *)
wenzelm@548
    84
wenzelm@548
    85
fun mk_binder_tr (sy, name) =
wenzelm@548
    86
  let
wenzelm@548
    87
    fun tr (Free (x, T), t) = const name $ absfree (x, T, t)
wenzelm@548
    88
      | tr (Const ("_idts", _) $ idt $ idts, t) = tr (idt, tr (idts, t))
wenzelm@548
    89
      | tr (t1 as Const (c, _) $ Free (x, T) $ tT, t) =
wenzelm@548
    90
          if c = constrainC then
wenzelm@548
    91
            const name $ (const constrainAbsC $ absfree (x, T, t) $ tT)
wenzelm@548
    92
          else raise_term "binder_tr" [t1, t]
wenzelm@548
    93
      | tr (t1, t2) = raise_term "binder_tr" [t1, t2];
wenzelm@548
    94
wenzelm@548
    95
    fun binder_tr (*sy*) [idts, body] = tr (idts, body)
wenzelm@548
    96
      | binder_tr (*sy*) ts = raise_term "binder_tr" ts;
wenzelm@548
    97
  in
wenzelm@548
    98
    (sy, binder_tr)
wenzelm@548
    99
  end;
wenzelm@548
   100
wenzelm@548
   101
wenzelm@548
   102
(* meta propositions *)
wenzelm@548
   103
wenzelm@548
   104
fun aprop_tr (*"_aprop"*) [t] = const constrainC $ t $ const "prop"
wenzelm@548
   105
  | aprop_tr (*"_aprop"*) ts = raise_term "aprop_tr" ts;
wenzelm@548
   106
wenzelm@548
   107
fun ofclass_tr (*"_ofclass"*) [ty, cls] =
wenzelm@548
   108
      cls $ (const constrainC $ const "TYPE" $ (const "itself" $ ty))
wenzelm@548
   109
  | ofclass_tr (*"_ofclass"*) ts = raise_term "ofclass_tr" ts;
wenzelm@548
   110
wenzelm@548
   111
wenzelm@548
   112
(* meta implication *)
wenzelm@548
   113
wenzelm@548
   114
fun bigimpl_ast_tr (*"_bigimpl"*) [asms, concl] =
wenzelm@548
   115
      fold_ast_p "==>" (unfold_ast "_asms" asms, concl)
wenzelm@548
   116
  | bigimpl_ast_tr (*"_bigimpl"*) asts = raise_ast "bigimpl_ast_tr" asts;
wenzelm@548
   117
wenzelm@548
   118
wenzelm@548
   119
(* explode atoms *)
wenzelm@548
   120
wenzelm@548
   121
fun explode_tr (*"_explode"*) (ts as [consC, nilC, bit0, bit1, txt]) =
wenzelm@548
   122
      let
wenzelm@548
   123
        fun mk_list [] = nilC
wenzelm@548
   124
          | mk_list (t :: ts) = consC $ t $ mk_list ts;
wenzelm@548
   125
wenzelm@548
   126
        fun encode_bit 0 = bit0
wenzelm@548
   127
          | encode_bit 1 = bit1
wenzelm@548
   128
          | encode_bit _ = sys_error "encode_bit";
wenzelm@548
   129
wenzelm@548
   130
        fun encode_char c =   (* FIXME leading 0s (?) *)
wenzelm@548
   131
          mk_list (map encode_bit (radixpand (2, (ord c))));
wenzelm@548
   132
wenzelm@548
   133
        val str =
wenzelm@548
   134
          (case txt of
wenzelm@548
   135
            Free (s, _) => s
wenzelm@548
   136
          | Const (s, _) => s
wenzelm@548
   137
          | _ => raise_term "explode_tr" ts);
wenzelm@548
   138
      in
wenzelm@548
   139
        mk_list (map encode_char (explode str))
wenzelm@548
   140
      end
wenzelm@548
   141
  | explode_tr (*"_explode"*) ts = raise_term "explode_tr" ts;
wenzelm@548
   142
wenzelm@548
   143
wenzelm@548
   144
wenzelm@548
   145
(** print (ast) translations **)
wenzelm@548
   146
wenzelm@548
   147
(* application *)
wenzelm@548
   148
wenzelm@548
   149
fun appl_ast_tr' (f, []) = raise_ast "appl_ast_tr'" [f]
wenzelm@548
   150
  | appl_ast_tr' (f, args) = Appl [Constant "_appl", f, fold_ast "_args" args];
wenzelm@548
   151
wenzelm@548
   152
wenzelm@548
   153
(* abstraction *)
wenzelm@548
   154
wenzelm@548
   155
fun strip_abss vars_of body_of tm =
wenzelm@548
   156
  let
wenzelm@548
   157
    val vars = vars_of tm;
wenzelm@548
   158
    val body = body_of tm;
wenzelm@548
   159
    val rev_new_vars = rename_wrt_term body vars;
wenzelm@548
   160
  in
wenzelm@548
   161
    (map Free (rev rev_new_vars),
wenzelm@548
   162
      subst_bounds (map (free o #1) rev_new_vars, body))
wenzelm@548
   163
  end;
wenzelm@548
   164
wenzelm@548
   165
(*do (partial) eta-contraction before printing*)
wenzelm@548
   166
wenzelm@548
   167
val eta_contract = ref false;
wenzelm@548
   168
wenzelm@548
   169
fun eta_contr tm =
wenzelm@548
   170
  let
wenzelm@548
   171
    fun eta_abs (Abs (a, T, t)) =
wenzelm@548
   172
          (case eta_abs t of
wenzelm@548
   173
            t' as f $ u =>
wenzelm@548
   174
              (case eta_abs u of
wenzelm@548
   175
                Bound 0 =>
wenzelm@548
   176
                  if not (0 mem loose_bnos f) then incr_boundvars ~1 f
wenzelm@548
   177
                  else Abs (a, T, t')
wenzelm@548
   178
              | _ => Abs (a, T, t'))
wenzelm@548
   179
          | t' => Abs (a, T, t'))
wenzelm@548
   180
      | eta_abs t = t;
wenzelm@548
   181
  in
wenzelm@548
   182
    if ! eta_contract then eta_abs tm else tm
wenzelm@548
   183
  end;
wenzelm@548
   184
wenzelm@548
   185
wenzelm@548
   186
fun abs_tr' tm =
wenzelm@548
   187
  foldr (fn (x, t) => const "_abs" $ x $ t)
wenzelm@548
   188
    (strip_abss strip_abs_vars strip_abs_body (eta_contr tm));
wenzelm@548
   189
wenzelm@548
   190
wenzelm@548
   191
fun abs_ast_tr' (*"_abs"*) asts =
wenzelm@548
   192
  (case unfold_ast_p "_abs" (Appl (Constant "_abs" :: asts)) of
wenzelm@548
   193
    ([], _) => raise_ast "abs_ast_tr'" asts
wenzelm@548
   194
  | (xs, body) => Appl [Constant "_lambda", fold_ast "_idts" xs, body]);
wenzelm@548
   195
wenzelm@548
   196
wenzelm@548
   197
(* binder *)
wenzelm@548
   198
wenzelm@548
   199
fun mk_binder_tr' (name, sy) =
wenzelm@548
   200
  let
wenzelm@548
   201
    fun mk_idts [] = raise Match    (*abort translation*)
wenzelm@548
   202
      | mk_idts [idt] = idt
wenzelm@548
   203
      | mk_idts (idt :: idts) = const "_idts" $ idt $ mk_idts idts;
wenzelm@548
   204
wenzelm@548
   205
    fun tr' t =
wenzelm@548
   206
      let
wenzelm@548
   207
        val (xs, bd) = strip_abss (strip_qnt_vars name) (strip_qnt_body name) t;
wenzelm@548
   208
      in
wenzelm@548
   209
        const sy $ mk_idts xs $ bd
wenzelm@548
   210
      end;
wenzelm@548
   211
wenzelm@548
   212
    fun binder_tr' (*name*) (t :: ts) =
wenzelm@548
   213
          list_comb (tr' (const name $ t), ts)
wenzelm@548
   214
      | binder_tr' (*name*) [] = raise Match;
wenzelm@548
   215
  in
wenzelm@548
   216
    (name, binder_tr')
wenzelm@548
   217
  end;
wenzelm@548
   218
wenzelm@548
   219
wenzelm@548
   220
(* idts *)
wenzelm@548
   221
wenzelm@548
   222
fun idts_ast_tr' (*"_idts"*) [Appl [Constant c, x, ty], xs] =
wenzelm@548
   223
      if c = constrainC then
wenzelm@548
   224
        Appl [Constant "_idts", Appl [Constant "_idtyp", x, ty], xs]
wenzelm@548
   225
      else raise Match
wenzelm@548
   226
  | idts_ast_tr' (*"_idts"*) _ = raise Match;
wenzelm@548
   227
wenzelm@548
   228
wenzelm@548
   229
(* meta propositions *)
wenzelm@548
   230
wenzelm@548
   231
fun prop_tr' show_sorts tm =
wenzelm@548
   232
  let
wenzelm@548
   233
    fun aprop t = const "_aprop" $ t;
wenzelm@548
   234
wenzelm@548
   235
    fun is_prop tys t =
wenzelm@548
   236
      fastype_of1 (tys, t) = propT handle TERM _ => false;
wenzelm@548
   237
wenzelm@548
   238
    fun tr' _ (t as Const _) = t
wenzelm@548
   239
      | tr' _ (t as Free (x, ty)) =
wenzelm@548
   240
          if ty = propT then aprop (free x) else t
wenzelm@548
   241
      | tr' _ (t as Var (xi, ty)) =
wenzelm@548
   242
          if ty = propT then aprop (var xi) else t
wenzelm@548
   243
      | tr' tys (t as Bound _) =
wenzelm@548
   244
          if is_prop tys t then aprop t else t
wenzelm@548
   245
      | tr' tys (Abs (x, ty, t)) = Abs (x, ty, tr' (ty :: tys) t)
wenzelm@548
   246
      | tr' tys (t as t1 $ (t2 as Const ("TYPE", Type ("itself", [ty])))) =
wenzelm@548
   247
          if is_prop tys t then
wenzelm@548
   248
            const "_ofclass" $ term_of_typ show_sorts ty $ tr' tys t1
wenzelm@548
   249
          else tr' tys t1 $ tr' tys t2
wenzelm@548
   250
      | tr' tys (t as t1 $ t2) =
wenzelm@548
   251
          (if is_Const (head_of t) orelse not (is_prop tys t)
wenzelm@548
   252
            then I else aprop) (tr' tys t1 $ tr' tys t2);
wenzelm@548
   253
  in
wenzelm@548
   254
    tr' [] tm
wenzelm@548
   255
  end;
wenzelm@548
   256
wenzelm@548
   257
wenzelm@548
   258
(* meta implication *)
wenzelm@548
   259
wenzelm@548
   260
fun impl_ast_tr' (*"==>"*) asts =
wenzelm@548
   261
  (case unfold_ast_p "==>" (Appl (Constant "==>" :: asts)) of
wenzelm@548
   262
    (asms as _ :: _ :: _, concl)
wenzelm@548
   263
      => Appl [Constant "_bigimpl", fold_ast "_asms" asms, concl]
wenzelm@548
   264
  | _ => raise Match);
wenzelm@548
   265
wenzelm@548
   266
wenzelm@548
   267
(* dependent / nondependent quantifiers *)
wenzelm@548
   268
wenzelm@548
   269
fun dependent_tr' (q, r) (A :: Abs (x, T, B) :: ts) =
wenzelm@548
   270
      if 0 mem (loose_bnos B) then
wenzelm@548
   271
        let val (x', B') = variant_abs (x, dummyT, B);
wenzelm@548
   272
        in list_comb (const q $ Free (x', T) $ A $ B', ts) end
wenzelm@548
   273
      else list_comb (const r $ A $ B, ts)
wenzelm@548
   274
  | dependent_tr' _ _ = raise Match;
wenzelm@548
   275
wenzelm@548
   276
wenzelm@548
   277
(* implode atoms *)
wenzelm@548
   278
wenzelm@548
   279
fun implode_ast_tr' (*"_implode"*) (asts as [Constant cons_name, nilC,
wenzelm@548
   280
    bit0, bit1, bitss]) =
wenzelm@548
   281
      let
wenzelm@548
   282
        fun err () = raise_ast "implode_ast_tr'" asts;
wenzelm@548
   283
wenzelm@548
   284
        fun strip_list lst =
wenzelm@548
   285
          let val (xs, y) = unfold_ast_p cons_name lst
wenzelm@548
   286
          in if y = nilC then xs else err ()
wenzelm@548
   287
          end;
wenzelm@548
   288
wenzelm@548
   289
        fun decode_bit bit =
wenzelm@548
   290
          if bit = bit0 then "0"
wenzelm@548
   291
          else if bit = bit1 then "1"
wenzelm@548
   292
          else err ();
wenzelm@548
   293
wenzelm@548
   294
        fun decode_char bits =
wenzelm@548
   295
          chr (#1 (scan_radixint (2, map decode_bit (strip_list bits))));
wenzelm@548
   296
      in
wenzelm@548
   297
        Variable (implode (map decode_char (strip_list bitss)))
wenzelm@548
   298
      end
wenzelm@548
   299
  | implode_ast_tr' (*"_implode"*) asts = raise_ast "implode_ast_tr'" asts;
wenzelm@548
   300
wenzelm@548
   301
wenzelm@548
   302
wenzelm@548
   303
(** pure_trfuns **)
wenzelm@548
   304
wenzelm@548
   305
val pure_trfuns =
wenzelm@548
   306
 ([(applC, appl_ast_tr), ("_lambda", lambda_ast_tr), ("_idtyp", idtyp_ast_tr),
wenzelm@548
   307
    ("_bigimpl", bigimpl_ast_tr)],
wenzelm@548
   308
  [("_abs", abs_tr), ("_aprop", aprop_tr), ("_ofclass", ofclass_tr),
wenzelm@548
   309
    ("_K", k_tr), ("_explode", explode_tr)],
wenzelm@548
   310
  [],
wenzelm@548
   311
  [("_abs", abs_ast_tr'), ("_idts", idts_ast_tr'), ("==>", impl_ast_tr'),
wenzelm@548
   312
    ("_implode", implode_ast_tr')]);
wenzelm@548
   313
wenzelm@548
   314
wenzelm@548
   315
wenzelm@548
   316
(** pt_to_ast **)
wenzelm@548
   317
wenzelm@548
   318
fun pt_to_ast trf pt =
wenzelm@548
   319
  let
wenzelm@548
   320
    fun trans a args =
wenzelm@548
   321
      (case trf a of
wenzelm@548
   322
        None => mk_appl (Constant a) args
wenzelm@548
   323
      | Some f => f args handle exn
wenzelm@548
   324
          => (writeln ("Error in parse ast translation for " ^ quote a); raise exn));
wenzelm@548
   325
wenzelm@548
   326
    fun ast_of (Node (a, pts)) = trans a (map ast_of pts)
wenzelm@548
   327
      | ast_of (Tip tok) = Variable (str_of_token tok);
wenzelm@548
   328
  in
wenzelm@548
   329
    ast_of pt
wenzelm@548
   330
  end;
wenzelm@548
   331
wenzelm@548
   332
wenzelm@548
   333
wenzelm@548
   334
(** ast_to_term **)
wenzelm@548
   335
wenzelm@548
   336
fun ast_to_term trf ast =
wenzelm@548
   337
  let
wenzelm@548
   338
    fun trans a args =
wenzelm@548
   339
      (case trf a of
wenzelm@548
   340
        None => list_comb (const a, args)
wenzelm@548
   341
      | Some f => f args handle exn
wenzelm@548
   342
          => (writeln ("Error in parse translation for " ^ quote a); raise exn));
wenzelm@548
   343
wenzelm@548
   344
    fun term_of (Constant a) = trans a []
wenzelm@548
   345
      | term_of (Variable x) = scan_var x
wenzelm@548
   346
      | term_of (Appl (Constant a :: (asts as _ :: _))) =
wenzelm@548
   347
          trans a (map term_of asts)
wenzelm@548
   348
      | term_of (Appl (ast :: (asts as _ :: _))) =
wenzelm@548
   349
          list_comb (term_of ast, map term_of asts)
wenzelm@548
   350
      | term_of (ast as Appl _) = raise_ast "ast_to_term: malformed ast" [ast];
wenzelm@548
   351
  in
wenzelm@548
   352
    term_of ast
wenzelm@548
   353
  end;
wenzelm@548
   354
wenzelm@548
   355
wenzelm@548
   356
end;
wenzelm@548
   357