src/Pure/Syntax/syn_trans.ML
author wenzelm
Mon Sep 22 17:31:28 1997 +0200 (1997-09-22)
changeset 3691 f0396ac63e12
parent 2698 8bccb3ab4ca4
child 3700 3a8192e83579
permissions -rw-r--r--
tuned lambda_ast_tr, idtyp_ast_tr' to accomodate fix of idt/idts
vs. pttrn/pttrns;
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@2698
     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@2698
    14
  val mark_bound: string -> term
wenzelm@2698
    15
  val mark_boundT: string * typ -> term
wenzelm@2698
    16
  val variant_abs': string * typ * term -> string * term
wenzelm@2698
    17
end;
wenzelm@548
    18
wenzelm@548
    19
signature SYN_TRANS1 =
wenzelm@2698
    20
sig
wenzelm@548
    21
  include SYN_TRANS0
paulson@1511
    22
  val constrainAbsC: string
paulson@1511
    23
  val pure_trfuns:
paulson@1511
    24
      (string * (Ast.ast list -> Ast.ast)) list *
wenzelm@548
    25
      (string * (term list -> term)) list *
wenzelm@548
    26
      (string * (term list -> term)) list *
paulson@1511
    27
      (string * (Ast.ast list -> Ast.ast)) list
wenzelm@2698
    28
  val pure_trfunsT: (string * (typ -> term list -> term)) list
wenzelm@2698
    29
end;
wenzelm@548
    30
wenzelm@548
    31
signature SYN_TRANS =
wenzelm@2698
    32
sig
wenzelm@548
    33
  include SYN_TRANS1
paulson@1511
    34
  val abs_tr': term -> term
paulson@1511
    35
  val prop_tr': bool -> term -> term
paulson@1511
    36
  val appl_ast_tr': Ast.ast * Ast.ast list -> Ast.ast
paulson@1511
    37
  val applC_ast_tr': Ast.ast * Ast.ast list -> Ast.ast
paulson@1511
    38
  val pt_to_ast: (string -> (Ast.ast list -> Ast.ast) option) -> Parser.parsetree -> Ast.ast
paulson@1511
    39
  val ast_to_term: (string -> (term list -> term) option) -> Ast.ast -> term
wenzelm@2698
    40
end;
wenzelm@548
    41
wenzelm@2698
    42
structure SynTrans: SYN_TRANS =
wenzelm@548
    43
struct
wenzelm@2698
    44
paulson@1511
    45
open TypeExt Lexicon Ast SynExt Parser;
wenzelm@548
    46
wenzelm@2698
    47
wenzelm@548
    48
(** parse (ast) translations **)
wenzelm@548
    49
wenzelm@548
    50
(* application *)
wenzelm@548
    51
clasohm@922
    52
fun appl_ast_tr [f, args] = Appl (f :: unfold_ast "_args" args)
clasohm@922
    53
  | appl_ast_tr asts = raise_ast "appl_ast_tr" asts;
clasohm@922
    54
clasohm@1178
    55
fun applC_ast_tr [f, args] = Appl (f :: unfold_ast "_cargs" args)
clasohm@922
    56
  | applC_ast_tr asts = raise_ast "applC_ast_tr" asts;
wenzelm@548
    57
wenzelm@548
    58
wenzelm@548
    59
(* abstraction *)
wenzelm@548
    60
wenzelm@548
    61
fun idtyp_ast_tr (*"_idtyp"*) [x, ty] = Appl [Constant constrainC, x, ty]
wenzelm@548
    62
  | idtyp_ast_tr (*"_idtyp"*) asts = raise_ast "idtyp_ast_tr" asts;
wenzelm@548
    63
wenzelm@3691
    64
fun lambda_ast_tr (*"_lambda"*) [pats, body] =
wenzelm@3691
    65
      fold_ast_p "_abs" (unfold_ast "_pttrns" pats, body)
wenzelm@548
    66
  | lambda_ast_tr (*"_lambda"*) asts = raise_ast "lambda_ast_tr" asts;
wenzelm@548
    67
wenzelm@548
    68
val constrainAbsC = "_constrainAbs";
wenzelm@548
    69
wenzelm@548
    70
fun abs_tr (*"_abs"*) [Free (x, T), body] = absfree (x, T, body)
wenzelm@548
    71
  | abs_tr (*"_abs"*) (ts as [Const (c, _) $ Free (x, T) $ tT, body]) =
wenzelm@548
    72
      if c = constrainC
wenzelm@548
    73
        then const constrainAbsC $ absfree (x, T, body) $ tT
wenzelm@548
    74
      else raise_term "abs_tr" ts
wenzelm@548
    75
  | abs_tr (*"_abs"*) ts = raise_term "abs_tr" ts;
wenzelm@548
    76
wenzelm@548
    77
wenzelm@548
    78
(* nondependent abstraction *)
wenzelm@548
    79
wenzelm@548
    80
fun k_tr (*"_K"*) [t] = Abs ("uu", dummyT, incr_boundvars 1 t)
wenzelm@548
    81
  | k_tr (*"_K"*) ts = raise_term "k_tr" ts;
wenzelm@548
    82
wenzelm@548
    83
wenzelm@548
    84
(* binder *)
wenzelm@548
    85
wenzelm@548
    86
fun mk_binder_tr (sy, name) =
wenzelm@548
    87
  let
wenzelm@548
    88
    fun tr (Free (x, T), t) = const name $ absfree (x, T, t)
wenzelm@548
    89
      | tr (Const ("_idts", _) $ idt $ idts, t) = tr (idt, tr (idts, t))
wenzelm@548
    90
      | tr (t1 as Const (c, _) $ Free (x, T) $ tT, t) =
wenzelm@548
    91
          if c = constrainC then
wenzelm@548
    92
            const name $ (const constrainAbsC $ absfree (x, T, t) $ tT)
wenzelm@548
    93
          else raise_term "binder_tr" [t1, t]
wenzelm@548
    94
      | tr (t1, t2) = raise_term "binder_tr" [t1, t2];
wenzelm@548
    95
wenzelm@548
    96
    fun binder_tr (*sy*) [idts, body] = tr (idts, body)
wenzelm@548
    97
      | binder_tr (*sy*) ts = raise_term "binder_tr" ts;
wenzelm@548
    98
  in
wenzelm@548
    99
    (sy, binder_tr)
wenzelm@548
   100
  end;
wenzelm@548
   101
wenzelm@548
   102
wenzelm@548
   103
(* meta propositions *)
wenzelm@548
   104
wenzelm@548
   105
fun aprop_tr (*"_aprop"*) [t] = const constrainC $ t $ const "prop"
wenzelm@548
   106
  | aprop_tr (*"_aprop"*) ts = raise_term "aprop_tr" ts;
wenzelm@548
   107
wenzelm@548
   108
fun ofclass_tr (*"_ofclass"*) [ty, cls] =
wenzelm@548
   109
      cls $ (const constrainC $ const "TYPE" $ (const "itself" $ ty))
wenzelm@548
   110
  | ofclass_tr (*"_ofclass"*) ts = raise_term "ofclass_tr" ts;
wenzelm@548
   111
wenzelm@548
   112
wenzelm@548
   113
(* meta implication *)
wenzelm@548
   114
wenzelm@548
   115
fun bigimpl_ast_tr (*"_bigimpl"*) [asms, concl] =
wenzelm@548
   116
      fold_ast_p "==>" (unfold_ast "_asms" asms, concl)
wenzelm@548
   117
  | bigimpl_ast_tr (*"_bigimpl"*) asts = raise_ast "bigimpl_ast_tr" asts;
wenzelm@548
   118
wenzelm@548
   119
wenzelm@548
   120
wenzelm@548
   121
(** print (ast) translations **)
wenzelm@548
   122
wenzelm@548
   123
(* application *)
wenzelm@548
   124
wenzelm@548
   125
fun appl_ast_tr' (f, []) = raise_ast "appl_ast_tr'" [f]
wenzelm@548
   126
  | appl_ast_tr' (f, args) = Appl [Constant "_appl", f, fold_ast "_args" args];
wenzelm@548
   127
clasohm@922
   128
fun applC_ast_tr' (f, []) = raise_ast "applC_ast_tr'" [f]
clasohm@1178
   129
  | applC_ast_tr' (f, args) =
clasohm@1178
   130
      Appl [Constant "_applC", f, fold_ast "_cargs" args];
clasohm@922
   131
wenzelm@548
   132
wenzelm@548
   133
(* abstraction *)
wenzelm@548
   134
wenzelm@2698
   135
fun mark_boundT x_T = const "_bound" $ Free x_T;
wenzelm@2698
   136
fun mark_bound x = mark_boundT (x, dummyT);
wenzelm@2698
   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@2698
   144
    (map mark_boundT (rev rev_new_vars),
wenzelm@2698
   145
      subst_bounds (map (mark_bound 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@3691
   177
  | (xs, body) => Appl [Constant "_lambda", fold_ast "_pttrns" 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@3691
   203
(* idtyp constraints *)
wenzelm@548
   204
wenzelm@3691
   205
fun idtyp_ast_tr' a [Appl [Constant c, x, ty], xs] =
wenzelm@548
   206
      if c = constrainC then
wenzelm@3691
   207
        Appl [Constant a, Appl [Constant "_idtyp", x, ty], xs]
wenzelm@548
   208
      else raise Match
wenzelm@3691
   209
  | idtyp_ast_tr' _ _ = 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@2698
   217
    val mk_ofclass = if show_sorts then "_mk_ofclassS" else "_mk_ofclass";
wenzelm@548
   218
wenzelm@2698
   219
    fun is_prop Ts t =
wenzelm@2698
   220
      fastype_of1 (Ts, t) = propT handle TERM _ => false;
wenzelm@548
   221
wenzelm@548
   222
    fun tr' _ (t as Const _) = t
wenzelm@2698
   223
      | tr' _ (t as Free (x, T)) =
wenzelm@2698
   224
          if T = propT then aprop (free x) else t
wenzelm@2698
   225
      | tr' _ (t as Var (xi, T)) =
wenzelm@2698
   226
          if T = propT then aprop (var xi) else t
wenzelm@2698
   227
      | tr' Ts (t as Bound _) =
wenzelm@2698
   228
          if is_prop Ts t then aprop t else t
wenzelm@2698
   229
      | tr' Ts (Abs (x, T, t)) = Abs (x, T, tr' (T :: Ts) t)
wenzelm@2698
   230
      | tr' Ts (t as t1 $ (t2 as Const ("TYPE", Type ("itself", [T])))) =
wenzelm@2698
   231
          if is_prop Ts t then Const (mk_ofclass, T) $ tr' Ts t1
wenzelm@2698
   232
          else tr' Ts t1 $ tr' Ts t2
wenzelm@2698
   233
      | tr' Ts (t as t1 $ t2) =
wenzelm@2698
   234
          (if is_Const (head_of t) orelse not (is_prop Ts t)
wenzelm@2698
   235
            then I else aprop) (tr' Ts t1 $ tr' Ts t2);
wenzelm@548
   236
  in
wenzelm@548
   237
    tr' [] tm
wenzelm@548
   238
  end;
wenzelm@548
   239
wenzelm@548
   240
wenzelm@2698
   241
fun mk_ofclass_tr' (*"_mk_ofclass"*) T [t] =
wenzelm@2698
   242
      const "_ofclass" $ term_of_typ false T $ t
wenzelm@2698
   243
  | mk_ofclass_tr' (*"_mk_ofclass"*) T ts = raise_type "mk_ofclass_tr'" [T] ts;
wenzelm@2698
   244
wenzelm@2698
   245
fun mk_ofclassS_tr' (*"_mk_ofclassS"*) T [t] =
wenzelm@2698
   246
      const "_ofclass" $ term_of_typ true T $ t
wenzelm@2698
   247
  | mk_ofclassS_tr' (*"_mk_ofclassS"*) T ts = raise_type "mk_ofclassS_tr'" [T] ts;
wenzelm@2698
   248
wenzelm@2698
   249
wenzelm@548
   250
(* meta implication *)
wenzelm@548
   251
wenzelm@548
   252
fun impl_ast_tr' (*"==>"*) asts =
wenzelm@548
   253
  (case unfold_ast_p "==>" (Appl (Constant "==>" :: asts)) of
wenzelm@548
   254
    (asms as _ :: _ :: _, concl)
wenzelm@548
   255
      => Appl [Constant "_bigimpl", fold_ast "_asms" asms, concl]
wenzelm@548
   256
  | _ => raise Match);
wenzelm@548
   257
wenzelm@548
   258
wenzelm@548
   259
(* dependent / nondependent quantifiers *)
wenzelm@548
   260
wenzelm@2698
   261
fun variant_abs' (x, T, B) =
wenzelm@2698
   262
  let val x' = variant (add_term_names (B, [])) x in
wenzelm@2698
   263
    (x', subst_bound (mark_boundT (x', T), B))
wenzelm@2698
   264
  end;
wenzelm@2698
   265
wenzelm@548
   266
fun dependent_tr' (q, r) (A :: Abs (x, T, B) :: ts) =
wenzelm@548
   267
      if 0 mem (loose_bnos B) then
wenzelm@2698
   268
        let val (x', B') = variant_abs' (x, dummyT, B);
wenzelm@2698
   269
        in list_comb (const q $ mark_boundT (x', T) $ A $ B', ts) end
wenzelm@548
   270
      else list_comb (const r $ A $ B, ts)
wenzelm@548
   271
  | dependent_tr' _ _ = raise Match;
wenzelm@548
   272
wenzelm@548
   273
wenzelm@548
   274
wenzelm@548
   275
(** pure_trfuns **)
wenzelm@548
   276
wenzelm@548
   277
val pure_trfuns =
clasohm@922
   278
 ([("_appl", appl_ast_tr), ("_applC", applC_ast_tr),
clasohm@922
   279
   ("_lambda", lambda_ast_tr), ("_idtyp", idtyp_ast_tr),
clasohm@922
   280
   ("_bigimpl", bigimpl_ast_tr)],
clasohm@922
   281
  [("_abs", abs_tr), ("_aprop", aprop_tr), ("_ofclass", ofclass_tr),
clasohm@922
   282
   ("_K", k_tr)],
wenzelm@548
   283
  [],
wenzelm@3691
   284
  [("_abs", abs_ast_tr'), ("_idts", idtyp_ast_tr' "_idts"),
wenzelm@3691
   285
   ("_pttrns", idtyp_ast_tr' "_pttrns"), ("==>", impl_ast_tr')]);
wenzelm@548
   286
wenzelm@2698
   287
val pure_trfunsT =
wenzelm@2698
   288
  [("_mk_ofclass", mk_ofclass_tr'), ("_mk_ofclassS", mk_ofclassS_tr')];
wenzelm@2698
   289
wenzelm@548
   290
wenzelm@548
   291
wenzelm@548
   292
(** pt_to_ast **)
wenzelm@548
   293
wenzelm@548
   294
fun pt_to_ast trf pt =
wenzelm@548
   295
  let
wenzelm@548
   296
    fun trans a args =
wenzelm@548
   297
      (case trf a of
wenzelm@548
   298
        None => mk_appl (Constant a) args
wenzelm@548
   299
      | Some f => f args handle exn
clasohm@987
   300
          => (writeln ("Error in parse ast translation for " ^ quote a);
clasohm@987
   301
              raise exn));
wenzelm@548
   302
clasohm@987
   303
    (*translate pt bottom-up*)
wenzelm@548
   304
    fun ast_of (Node (a, pts)) = trans a (map ast_of pts)
wenzelm@548
   305
      | ast_of (Tip tok) = Variable (str_of_token tok);
wenzelm@548
   306
  in
clasohm@1178
   307
    ast_of pt
wenzelm@548
   308
  end;
wenzelm@548
   309
wenzelm@548
   310
wenzelm@548
   311
wenzelm@548
   312
(** ast_to_term **)
wenzelm@548
   313
wenzelm@548
   314
fun ast_to_term trf ast =
wenzelm@548
   315
  let
wenzelm@548
   316
    fun trans a args =
wenzelm@548
   317
      (case trf a of
wenzelm@548
   318
        None => list_comb (const a, args)
wenzelm@548
   319
      | Some f => f args handle exn
clasohm@987
   320
          => (writeln ("Error in parse translation for " ^ quote a);
clasohm@987
   321
              raise exn));
wenzelm@548
   322
wenzelm@548
   323
    fun term_of (Constant a) = trans a []
wenzelm@548
   324
      | term_of (Variable x) = scan_var x
wenzelm@548
   325
      | term_of (Appl (Constant a :: (asts as _ :: _))) =
wenzelm@548
   326
          trans a (map term_of asts)
wenzelm@548
   327
      | term_of (Appl (ast :: (asts as _ :: _))) =
wenzelm@548
   328
          list_comb (term_of ast, map term_of asts)
wenzelm@548
   329
      | term_of (ast as Appl _) = raise_ast "ast_to_term: malformed ast" [ast];
wenzelm@548
   330
  in
wenzelm@548
   331
    term_of ast
wenzelm@548
   332
  end;
wenzelm@548
   333
wenzelm@548
   334
end;