src/Pure/Syntax/syn_trans.ML
author nipkow
Sun Nov 12 13:14:13 1995 +0100 (1995-11-12)
changeset 1326 1fbf9407757c
parent 1178 b28c6ecc3e6d
child 1511 09354d37a5ab
permissions -rw-r--r--
Set eta_contract to true.
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
clasohm@922
    37
    val applC_ast_tr': ast * ast list -> ast
wenzelm@548
    38
    val pt_to_ast: (string -> (ast list -> ast) option) -> parsetree -> ast
wenzelm@548
    39
    val ast_to_term: (string -> (term list -> term) option) -> ast -> term
wenzelm@548
    40
  end
wenzelm@548
    41
end;
wenzelm@548
    42
wenzelm@548
    43
functor SynTransFun(structure TypeExt: TYPE_EXT and Parser: PARSER
wenzelm@548
    44
  sharing TypeExt.SynExt = Parser.SynExt): SYN_TRANS =
wenzelm@548
    45
struct
wenzelm@548
    46
wenzelm@548
    47
structure Parser = Parser;
wenzelm@548
    48
open TypeExt Parser.Lexicon Parser.SynExt.Ast Parser.SynExt Parser;
wenzelm@548
    49
wenzelm@548
    50
wenzelm@548
    51
(** parse (ast) translations **)
wenzelm@548
    52
wenzelm@548
    53
(* application *)
wenzelm@548
    54
clasohm@922
    55
fun appl_ast_tr [f, args] = Appl (f :: unfold_ast "_args" args)
clasohm@922
    56
  | appl_ast_tr asts = raise_ast "appl_ast_tr" asts;
clasohm@922
    57
clasohm@1178
    58
fun applC_ast_tr [f, args] = Appl (f :: unfold_ast "_cargs" args)
clasohm@922
    59
  | applC_ast_tr asts = raise_ast "applC_ast_tr" asts;
wenzelm@548
    60
wenzelm@548
    61
wenzelm@548
    62
(* abstraction *)
wenzelm@548
    63
wenzelm@548
    64
fun idtyp_ast_tr (*"_idtyp"*) [x, ty] = Appl [Constant constrainC, x, ty]
wenzelm@548
    65
  | idtyp_ast_tr (*"_idtyp"*) asts = raise_ast "idtyp_ast_tr" asts;
wenzelm@548
    66
wenzelm@548
    67
fun lambda_ast_tr (*"_lambda"*) [idts, body] =
wenzelm@548
    68
      fold_ast_p "_abs" (unfold_ast "_idts" idts, body)
wenzelm@548
    69
  | lambda_ast_tr (*"_lambda"*) asts = raise_ast "lambda_ast_tr" asts;
wenzelm@548
    70
wenzelm@548
    71
val constrainAbsC = "_constrainAbs";
wenzelm@548
    72
wenzelm@548
    73
fun abs_tr (*"_abs"*) [Free (x, T), body] = absfree (x, T, body)
wenzelm@548
    74
  | abs_tr (*"_abs"*) (ts as [Const (c, _) $ Free (x, T) $ tT, body]) =
wenzelm@548
    75
      if c = constrainC
wenzelm@548
    76
        then const constrainAbsC $ absfree (x, T, body) $ tT
wenzelm@548
    77
      else raise_term "abs_tr" ts
wenzelm@548
    78
  | abs_tr (*"_abs"*) ts = raise_term "abs_tr" ts;
wenzelm@548
    79
wenzelm@548
    80
wenzelm@548
    81
(* nondependent abstraction *)
wenzelm@548
    82
wenzelm@548
    83
fun k_tr (*"_K"*) [t] = Abs ("uu", dummyT, incr_boundvars 1 t)
wenzelm@548
    84
  | k_tr (*"_K"*) ts = raise_term "k_tr" ts;
wenzelm@548
    85
wenzelm@548
    86
wenzelm@548
    87
(* binder *)
wenzelm@548
    88
wenzelm@548
    89
fun mk_binder_tr (sy, name) =
wenzelm@548
    90
  let
wenzelm@548
    91
    fun tr (Free (x, T), t) = const name $ absfree (x, T, t)
wenzelm@548
    92
      | tr (Const ("_idts", _) $ idt $ idts, t) = tr (idt, tr (idts, t))
wenzelm@548
    93
      | tr (t1 as Const (c, _) $ Free (x, T) $ tT, t) =
wenzelm@548
    94
          if c = constrainC then
wenzelm@548
    95
            const name $ (const constrainAbsC $ absfree (x, T, t) $ tT)
wenzelm@548
    96
          else raise_term "binder_tr" [t1, t]
wenzelm@548
    97
      | tr (t1, t2) = raise_term "binder_tr" [t1, t2];
wenzelm@548
    98
wenzelm@548
    99
    fun binder_tr (*sy*) [idts, body] = tr (idts, body)
wenzelm@548
   100
      | binder_tr (*sy*) ts = raise_term "binder_tr" ts;
wenzelm@548
   101
  in
wenzelm@548
   102
    (sy, binder_tr)
wenzelm@548
   103
  end;
wenzelm@548
   104
wenzelm@548
   105
wenzelm@548
   106
(* meta propositions *)
wenzelm@548
   107
wenzelm@548
   108
fun aprop_tr (*"_aprop"*) [t] = const constrainC $ t $ const "prop"
wenzelm@548
   109
  | aprop_tr (*"_aprop"*) ts = raise_term "aprop_tr" ts;
wenzelm@548
   110
wenzelm@548
   111
fun ofclass_tr (*"_ofclass"*) [ty, cls] =
wenzelm@548
   112
      cls $ (const constrainC $ const "TYPE" $ (const "itself" $ ty))
wenzelm@548
   113
  | ofclass_tr (*"_ofclass"*) ts = raise_term "ofclass_tr" ts;
wenzelm@548
   114
wenzelm@548
   115
wenzelm@548
   116
(* meta implication *)
wenzelm@548
   117
wenzelm@548
   118
fun bigimpl_ast_tr (*"_bigimpl"*) [asms, concl] =
wenzelm@548
   119
      fold_ast_p "==>" (unfold_ast "_asms" asms, concl)
wenzelm@548
   120
  | bigimpl_ast_tr (*"_bigimpl"*) asts = raise_ast "bigimpl_ast_tr" asts;
wenzelm@548
   121
wenzelm@548
   122
wenzelm@548
   123
wenzelm@548
   124
(** print (ast) translations **)
wenzelm@548
   125
wenzelm@548
   126
(* application *)
wenzelm@548
   127
wenzelm@548
   128
fun appl_ast_tr' (f, []) = raise_ast "appl_ast_tr'" [f]
wenzelm@548
   129
  | appl_ast_tr' (f, args) = Appl [Constant "_appl", f, fold_ast "_args" args];
wenzelm@548
   130
clasohm@922
   131
fun applC_ast_tr' (f, []) = raise_ast "applC_ast_tr'" [f]
clasohm@1178
   132
  | applC_ast_tr' (f, args) =
clasohm@1178
   133
      Appl [Constant "_applC", f, fold_ast "_cargs" args];
clasohm@922
   134
wenzelm@548
   135
wenzelm@548
   136
(* abstraction *)
wenzelm@548
   137
wenzelm@548
   138
fun strip_abss vars_of body_of tm =
wenzelm@548
   139
  let
wenzelm@548
   140
    val vars = vars_of tm;
wenzelm@548
   141
    val body = body_of tm;
wenzelm@548
   142
    val rev_new_vars = rename_wrt_term body vars;
wenzelm@548
   143
  in
wenzelm@548
   144
    (map Free (rev rev_new_vars),
wenzelm@548
   145
      subst_bounds (map (free o #1) rev_new_vars, body))
wenzelm@548
   146
  end;
wenzelm@548
   147
wenzelm@548
   148
(*do (partial) eta-contraction before printing*)
wenzelm@548
   149
nipkow@1326
   150
val eta_contract = ref true;
wenzelm@548
   151
wenzelm@548
   152
fun eta_contr tm =
wenzelm@548
   153
  let
wenzelm@548
   154
    fun eta_abs (Abs (a, T, t)) =
wenzelm@548
   155
          (case eta_abs t of
wenzelm@548
   156
            t' as f $ u =>
wenzelm@548
   157
              (case eta_abs u of
wenzelm@548
   158
                Bound 0 =>
wenzelm@548
   159
                  if not (0 mem loose_bnos f) then incr_boundvars ~1 f
wenzelm@548
   160
                  else Abs (a, T, t')
wenzelm@548
   161
              | _ => Abs (a, T, t'))
wenzelm@548
   162
          | t' => Abs (a, T, t'))
wenzelm@548
   163
      | eta_abs t = t;
wenzelm@548
   164
  in
wenzelm@548
   165
    if ! eta_contract then eta_abs tm else tm
wenzelm@548
   166
  end;
wenzelm@548
   167
wenzelm@548
   168
wenzelm@548
   169
fun abs_tr' tm =
wenzelm@548
   170
  foldr (fn (x, t) => const "_abs" $ x $ t)
wenzelm@548
   171
    (strip_abss strip_abs_vars strip_abs_body (eta_contr tm));
wenzelm@548
   172
wenzelm@548
   173
wenzelm@548
   174
fun abs_ast_tr' (*"_abs"*) asts =
wenzelm@548
   175
  (case unfold_ast_p "_abs" (Appl (Constant "_abs" :: asts)) of
wenzelm@548
   176
    ([], _) => raise_ast "abs_ast_tr'" asts
wenzelm@548
   177
  | (xs, body) => Appl [Constant "_lambda", fold_ast "_idts" xs, body]);
wenzelm@548
   178
wenzelm@548
   179
wenzelm@548
   180
(* binder *)
wenzelm@548
   181
wenzelm@548
   182
fun mk_binder_tr' (name, sy) =
wenzelm@548
   183
  let
wenzelm@548
   184
    fun mk_idts [] = raise Match    (*abort translation*)
wenzelm@548
   185
      | mk_idts [idt] = idt
wenzelm@548
   186
      | mk_idts (idt :: idts) = const "_idts" $ idt $ mk_idts idts;
wenzelm@548
   187
wenzelm@548
   188
    fun tr' t =
wenzelm@548
   189
      let
wenzelm@548
   190
        val (xs, bd) = strip_abss (strip_qnt_vars name) (strip_qnt_body name) t;
wenzelm@548
   191
      in
wenzelm@548
   192
        const sy $ mk_idts xs $ bd
wenzelm@548
   193
      end;
wenzelm@548
   194
wenzelm@548
   195
    fun binder_tr' (*name*) (t :: ts) =
wenzelm@548
   196
          list_comb (tr' (const name $ t), ts)
wenzelm@548
   197
      | binder_tr' (*name*) [] = raise Match;
wenzelm@548
   198
  in
wenzelm@548
   199
    (name, binder_tr')
wenzelm@548
   200
  end;
wenzelm@548
   201
wenzelm@548
   202
wenzelm@548
   203
(* idts *)
wenzelm@548
   204
wenzelm@548
   205
fun idts_ast_tr' (*"_idts"*) [Appl [Constant c, x, ty], xs] =
wenzelm@548
   206
      if c = constrainC then
wenzelm@548
   207
        Appl [Constant "_idts", Appl [Constant "_idtyp", x, ty], xs]
wenzelm@548
   208
      else raise Match
wenzelm@548
   209
  | idts_ast_tr' (*"_idts"*) _ = raise Match;
wenzelm@548
   210
wenzelm@548
   211
wenzelm@548
   212
(* meta propositions *)
wenzelm@548
   213
wenzelm@548
   214
fun prop_tr' show_sorts tm =
wenzelm@548
   215
  let
wenzelm@548
   216
    fun aprop t = const "_aprop" $ t;
wenzelm@548
   217
wenzelm@548
   218
    fun is_prop tys t =
wenzelm@548
   219
      fastype_of1 (tys, t) = propT handle TERM _ => false;
wenzelm@548
   220
wenzelm@548
   221
    fun tr' _ (t as Const _) = t
wenzelm@548
   222
      | tr' _ (t as Free (x, ty)) =
wenzelm@548
   223
          if ty = propT then aprop (free x) else t
wenzelm@548
   224
      | tr' _ (t as Var (xi, ty)) =
wenzelm@548
   225
          if ty = propT then aprop (var xi) else t
wenzelm@548
   226
      | tr' tys (t as Bound _) =
wenzelm@548
   227
          if is_prop tys t then aprop t else t
wenzelm@548
   228
      | tr' tys (Abs (x, ty, t)) = Abs (x, ty, tr' (ty :: tys) t)
wenzelm@548
   229
      | tr' tys (t as t1 $ (t2 as Const ("TYPE", Type ("itself", [ty])))) =
wenzelm@548
   230
          if is_prop tys t then
wenzelm@548
   231
            const "_ofclass" $ term_of_typ show_sorts ty $ tr' tys t1
wenzelm@548
   232
          else tr' tys t1 $ tr' tys t2
wenzelm@548
   233
      | tr' tys (t as t1 $ t2) =
wenzelm@548
   234
          (if is_Const (head_of t) orelse not (is_prop tys t)
wenzelm@548
   235
            then I else aprop) (tr' tys t1 $ tr' tys t2);
wenzelm@548
   236
  in
wenzelm@548
   237
    tr' [] tm
wenzelm@548
   238
  end;
wenzelm@548
   239
wenzelm@548
   240
wenzelm@548
   241
(* meta implication *)
wenzelm@548
   242
wenzelm@548
   243
fun impl_ast_tr' (*"==>"*) asts =
wenzelm@548
   244
  (case unfold_ast_p "==>" (Appl (Constant "==>" :: asts)) of
wenzelm@548
   245
    (asms as _ :: _ :: _, concl)
wenzelm@548
   246
      => Appl [Constant "_bigimpl", fold_ast "_asms" asms, concl]
wenzelm@548
   247
  | _ => raise Match);
wenzelm@548
   248
wenzelm@548
   249
wenzelm@548
   250
(* dependent / nondependent quantifiers *)
wenzelm@548
   251
wenzelm@548
   252
fun dependent_tr' (q, r) (A :: Abs (x, T, B) :: ts) =
wenzelm@548
   253
      if 0 mem (loose_bnos B) then
wenzelm@548
   254
        let val (x', B') = variant_abs (x, dummyT, B);
wenzelm@548
   255
        in list_comb (const q $ Free (x', T) $ A $ B', ts) end
wenzelm@548
   256
      else list_comb (const r $ A $ B, ts)
wenzelm@548
   257
  | dependent_tr' _ _ = raise Match;
wenzelm@548
   258
wenzelm@548
   259
wenzelm@548
   260
wenzelm@548
   261
(** pure_trfuns **)
wenzelm@548
   262
wenzelm@548
   263
val pure_trfuns =
clasohm@922
   264
 ([("_appl", appl_ast_tr), ("_applC", applC_ast_tr),
clasohm@922
   265
   ("_lambda", lambda_ast_tr), ("_idtyp", idtyp_ast_tr),
clasohm@922
   266
   ("_bigimpl", bigimpl_ast_tr)],
clasohm@922
   267
  [("_abs", abs_tr), ("_aprop", aprop_tr), ("_ofclass", ofclass_tr),
clasohm@922
   268
   ("_K", k_tr)],
wenzelm@548
   269
  [],
wenzelm@639
   270
  [("_abs", abs_ast_tr'), ("_idts", idts_ast_tr'), ("==>", impl_ast_tr')]);
wenzelm@548
   271
wenzelm@548
   272
wenzelm@548
   273
wenzelm@548
   274
(** pt_to_ast **)
wenzelm@548
   275
wenzelm@548
   276
fun pt_to_ast trf pt =
wenzelm@548
   277
  let
wenzelm@548
   278
    fun trans a args =
wenzelm@548
   279
      (case trf a of
wenzelm@548
   280
        None => mk_appl (Constant a) args
wenzelm@548
   281
      | Some f => f args handle exn
clasohm@987
   282
          => (writeln ("Error in parse ast translation for " ^ quote a);
clasohm@987
   283
              raise exn));
wenzelm@548
   284
clasohm@987
   285
    (*translate pt bottom-up*)
wenzelm@548
   286
    fun ast_of (Node (a, pts)) = trans a (map ast_of pts)
wenzelm@548
   287
      | ast_of (Tip tok) = Variable (str_of_token tok);
wenzelm@548
   288
  in
clasohm@1178
   289
    ast_of pt
wenzelm@548
   290
  end;
wenzelm@548
   291
wenzelm@548
   292
wenzelm@548
   293
wenzelm@548
   294
(** ast_to_term **)
wenzelm@548
   295
wenzelm@548
   296
fun ast_to_term trf ast =
wenzelm@548
   297
  let
wenzelm@548
   298
    fun trans a args =
wenzelm@548
   299
      (case trf a of
wenzelm@548
   300
        None => list_comb (const a, args)
wenzelm@548
   301
      | Some f => f args handle exn
clasohm@987
   302
          => (writeln ("Error in parse translation for " ^ quote a);
clasohm@987
   303
              raise exn));
wenzelm@548
   304
wenzelm@548
   305
    fun term_of (Constant a) = trans a []
wenzelm@548
   306
      | term_of (Variable x) = scan_var x
wenzelm@548
   307
      | term_of (Appl (Constant a :: (asts as _ :: _))) =
wenzelm@548
   308
          trans a (map term_of asts)
wenzelm@548
   309
      | term_of (Appl (ast :: (asts as _ :: _))) =
wenzelm@548
   310
          list_comb (term_of ast, map term_of asts)
wenzelm@548
   311
      | term_of (ast as Appl _) = raise_ast "ast_to_term: malformed ast" [ast];
wenzelm@548
   312
  in
wenzelm@548
   313
    term_of ast
wenzelm@548
   314
  end;
wenzelm@548
   315
wenzelm@548
   316
wenzelm@548
   317
end;