src/Pure/Syntax/syn_trans.ML
author wenzelm
Wed Nov 05 11:43:37 1997 +0100 (1997-11-05)
changeset 4148 e0e5a2820ac1
parent 3928 787d2659ce4a
child 4700 20ade76722d6
permissions -rw-r--r--
adapted pure_trfunsT;
added type_tr/tr';
eliminated mk_ofclassS_tr';
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@4148
    28
  val pure_trfunsT: (string * (bool -> 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
wenzelm@4148
    35
  val prop_tr': 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)
wenzelm@3777
    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)
wenzelm@3777
    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@3777
    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@3777
    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@3777
    74
      else raise TERM ("abs_tr", ts)
wenzelm@3777
    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@3777
    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@3777
    93
          else raise TERM ("binder_tr", [t1, t])
wenzelm@3777
    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@3777
    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@3777
   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@3777
   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@3777
   117
  | bigimpl_ast_tr (*"_bigimpl"*) asts = raise AST ("bigimpl_ast_tr", asts);
wenzelm@548
   118
wenzelm@548
   119
wenzelm@4148
   120
(* type reflection *)
wenzelm@4148
   121
wenzelm@4148
   122
fun type_tr (*"_TYPE"*) [ty] =
wenzelm@4148
   123
      const constrainC $ const "TYPE" $ (const "itself" $ ty)
wenzelm@4148
   124
  | type_tr (*"_TYPE"*) ts = raise TERM ("type_tr", ts);
wenzelm@4148
   125
wenzelm@548
   126
wenzelm@548
   127
(** print (ast) translations **)
wenzelm@548
   128
wenzelm@548
   129
(* application *)
wenzelm@548
   130
wenzelm@3777
   131
fun appl_ast_tr' (f, []) = raise AST ("appl_ast_tr'", [f])
wenzelm@548
   132
  | appl_ast_tr' (f, args) = Appl [Constant "_appl", f, fold_ast "_args" args];
wenzelm@548
   133
wenzelm@3777
   134
fun applC_ast_tr' (f, []) = raise AST ("applC_ast_tr'", [f])
clasohm@1178
   135
  | applC_ast_tr' (f, args) =
clasohm@1178
   136
      Appl [Constant "_applC", f, fold_ast "_cargs" args];
clasohm@922
   137
wenzelm@548
   138
wenzelm@548
   139
(* abstraction *)
wenzelm@548
   140
wenzelm@2698
   141
fun mark_boundT x_T = const "_bound" $ Free x_T;
wenzelm@2698
   142
fun mark_bound x = mark_boundT (x, dummyT);
wenzelm@2698
   143
wenzelm@548
   144
fun strip_abss vars_of body_of tm =
wenzelm@548
   145
  let
wenzelm@548
   146
    val vars = vars_of tm;
wenzelm@548
   147
    val body = body_of tm;
wenzelm@548
   148
    val rev_new_vars = rename_wrt_term body vars;
wenzelm@548
   149
  in
wenzelm@2698
   150
    (map mark_boundT (rev rev_new_vars),
wenzelm@2698
   151
      subst_bounds (map (mark_bound o #1) rev_new_vars, body))
wenzelm@548
   152
  end;
wenzelm@548
   153
wenzelm@3928
   154
wenzelm@548
   155
(*do (partial) eta-contraction before printing*)
wenzelm@548
   156
nipkow@1326
   157
val eta_contract = ref true;
wenzelm@548
   158
wenzelm@548
   159
fun eta_contr tm =
wenzelm@548
   160
  let
wenzelm@3928
   161
    fun is_aprop (Const ("_aprop", _)) = true
wenzelm@3928
   162
      | is_aprop _ = false;
wenzelm@3928
   163
wenzelm@548
   164
    fun eta_abs (Abs (a, T, t)) =
wenzelm@548
   165
          (case eta_abs t of
wenzelm@548
   166
            t' as f $ u =>
wenzelm@548
   167
              (case eta_abs u of
wenzelm@548
   168
                Bound 0 =>
wenzelm@3928
   169
                  if 0 mem loose_bnos f orelse is_aprop f then Abs (a, T, t')
wenzelm@3928
   170
                  else  incr_boundvars ~1 f
wenzelm@548
   171
              | _ => Abs (a, T, t'))
wenzelm@548
   172
          | t' => Abs (a, T, t'))
wenzelm@548
   173
      | eta_abs t = t;
wenzelm@548
   174
  in
wenzelm@548
   175
    if ! eta_contract then eta_abs tm else tm
wenzelm@548
   176
  end;
wenzelm@548
   177
wenzelm@548
   178
wenzelm@548
   179
fun abs_tr' tm =
wenzelm@548
   180
  foldr (fn (x, t) => const "_abs" $ x $ t)
wenzelm@548
   181
    (strip_abss strip_abs_vars strip_abs_body (eta_contr tm));
wenzelm@548
   182
wenzelm@548
   183
wenzelm@548
   184
fun abs_ast_tr' (*"_abs"*) asts =
wenzelm@548
   185
  (case unfold_ast_p "_abs" (Appl (Constant "_abs" :: asts)) of
wenzelm@3777
   186
    ([], _) => raise AST ("abs_ast_tr'", asts)
wenzelm@3691
   187
  | (xs, body) => Appl [Constant "_lambda", fold_ast "_pttrns" xs, body]);
wenzelm@548
   188
wenzelm@548
   189
wenzelm@548
   190
(* binder *)
wenzelm@548
   191
wenzelm@548
   192
fun mk_binder_tr' (name, sy) =
wenzelm@548
   193
  let
wenzelm@548
   194
    fun mk_idts [] = raise Match    (*abort translation*)
wenzelm@548
   195
      | mk_idts [idt] = idt
wenzelm@548
   196
      | mk_idts (idt :: idts) = const "_idts" $ idt $ mk_idts idts;
wenzelm@548
   197
wenzelm@548
   198
    fun tr' t =
wenzelm@548
   199
      let
wenzelm@548
   200
        val (xs, bd) = strip_abss (strip_qnt_vars name) (strip_qnt_body name) t;
wenzelm@548
   201
      in
wenzelm@548
   202
        const sy $ mk_idts xs $ bd
wenzelm@548
   203
      end;
wenzelm@548
   204
wenzelm@548
   205
    fun binder_tr' (*name*) (t :: ts) =
wenzelm@548
   206
          list_comb (tr' (const name $ t), ts)
wenzelm@548
   207
      | binder_tr' (*name*) [] = raise Match;
wenzelm@548
   208
  in
wenzelm@548
   209
    (name, binder_tr')
wenzelm@548
   210
  end;
wenzelm@548
   211
wenzelm@548
   212
wenzelm@3691
   213
(* idtyp constraints *)
wenzelm@548
   214
wenzelm@3691
   215
fun idtyp_ast_tr' a [Appl [Constant c, x, ty], xs] =
wenzelm@548
   216
      if c = constrainC then
wenzelm@3691
   217
        Appl [Constant a, Appl [Constant "_idtyp", x, ty], xs]
wenzelm@548
   218
      else raise Match
wenzelm@3691
   219
  | idtyp_ast_tr' _ _ = raise Match;
wenzelm@548
   220
wenzelm@548
   221
wenzelm@548
   222
(* meta propositions *)
wenzelm@548
   223
wenzelm@4148
   224
fun prop_tr' tm =
wenzelm@548
   225
  let
wenzelm@548
   226
    fun aprop t = const "_aprop" $ t;
wenzelm@548
   227
wenzelm@2698
   228
    fun is_prop Ts t =
wenzelm@2698
   229
      fastype_of1 (Ts, t) = propT handle TERM _ => false;
wenzelm@548
   230
wenzelm@548
   231
    fun tr' _ (t as Const _) = t
wenzelm@2698
   232
      | tr' _ (t as Free (x, T)) =
wenzelm@2698
   233
          if T = propT then aprop (free x) else t
wenzelm@2698
   234
      | tr' _ (t as Var (xi, T)) =
wenzelm@2698
   235
          if T = propT then aprop (var xi) else t
wenzelm@2698
   236
      | tr' Ts (t as Bound _) =
wenzelm@2698
   237
          if is_prop Ts t then aprop t else t
wenzelm@2698
   238
      | tr' Ts (Abs (x, T, t)) = Abs (x, T, tr' (T :: Ts) t)
wenzelm@2698
   239
      | tr' Ts (t as t1 $ (t2 as Const ("TYPE", Type ("itself", [T])))) =
wenzelm@4148
   240
          if is_prop Ts t then Const ("_mk_ofclass", T) $ tr' Ts t1
wenzelm@2698
   241
          else tr' Ts t1 $ tr' Ts t2
wenzelm@2698
   242
      | tr' Ts (t as t1 $ t2) =
wenzelm@2698
   243
          (if is_Const (head_of t) orelse not (is_prop Ts t)
wenzelm@2698
   244
            then I else aprop) (tr' Ts t1 $ tr' Ts t2);
wenzelm@548
   245
  in
wenzelm@548
   246
    tr' [] tm
wenzelm@548
   247
  end;
wenzelm@548
   248
wenzelm@4148
   249
fun mk_ofclass_tr' show_sorts (*"_mk_ofclass"*) T [t] =
wenzelm@4148
   250
      const "_ofclass" $ term_of_typ show_sorts T $ t
wenzelm@4148
   251
  | mk_ofclass_tr' _ (*"_mk_ofclass"*) T ts = raise TYPE ("mk_ofclass_tr'", [T], ts);
wenzelm@2698
   252
wenzelm@2698
   253
wenzelm@548
   254
(* meta implication *)
wenzelm@548
   255
wenzelm@548
   256
fun impl_ast_tr' (*"==>"*) asts =
wenzelm@548
   257
  (case unfold_ast_p "==>" (Appl (Constant "==>" :: asts)) of
wenzelm@548
   258
    (asms as _ :: _ :: _, concl)
wenzelm@548
   259
      => Appl [Constant "_bigimpl", fold_ast "_asms" asms, concl]
wenzelm@548
   260
  | _ => raise Match);
wenzelm@548
   261
wenzelm@548
   262
wenzelm@4148
   263
(* type reflection *)
wenzelm@4148
   264
wenzelm@4148
   265
fun type_tr' show_sorts (*"TYPE"*) (Type ("itself", [T])) ts =
wenzelm@4148
   266
      list_comb (const "_TYPE" $ term_of_typ show_sorts T, ts)
wenzelm@4148
   267
  | type_tr' _ _ _ = raise Match;
wenzelm@4148
   268
wenzelm@4148
   269
wenzelm@548
   270
(* dependent / nondependent quantifiers *)
wenzelm@548
   271
wenzelm@2698
   272
fun variant_abs' (x, T, B) =
wenzelm@2698
   273
  let val x' = variant (add_term_names (B, [])) x in
wenzelm@2698
   274
    (x', subst_bound (mark_boundT (x', T), B))
wenzelm@2698
   275
  end;
wenzelm@2698
   276
wenzelm@548
   277
fun dependent_tr' (q, r) (A :: Abs (x, T, B) :: ts) =
wenzelm@548
   278
      if 0 mem (loose_bnos B) then
wenzelm@2698
   279
        let val (x', B') = variant_abs' (x, dummyT, B);
wenzelm@2698
   280
        in list_comb (const q $ mark_boundT (x', T) $ A $ B', ts) end
wenzelm@548
   281
      else list_comb (const r $ A $ B, ts)
wenzelm@548
   282
  | dependent_tr' _ _ = raise Match;
wenzelm@548
   283
wenzelm@548
   284
wenzelm@548
   285
wenzelm@548
   286
(** pure_trfuns **)
wenzelm@548
   287
wenzelm@548
   288
val pure_trfuns =
clasohm@922
   289
 ([("_appl", appl_ast_tr), ("_applC", applC_ast_tr),
clasohm@922
   290
   ("_lambda", lambda_ast_tr), ("_idtyp", idtyp_ast_tr),
clasohm@922
   291
   ("_bigimpl", bigimpl_ast_tr)],
clasohm@922
   292
  [("_abs", abs_tr), ("_aprop", aprop_tr), ("_ofclass", ofclass_tr),
wenzelm@4148
   293
   ("_TYPE", type_tr), ("_K", k_tr)],
wenzelm@3700
   294
  []: (string * (term list -> term)) list,
wenzelm@3691
   295
  [("_abs", abs_ast_tr'), ("_idts", idtyp_ast_tr' "_idts"),
wenzelm@3691
   296
   ("_pttrns", idtyp_ast_tr' "_pttrns"), ("==>", impl_ast_tr')]);
wenzelm@548
   297
wenzelm@2698
   298
val pure_trfunsT =
wenzelm@4148
   299
  [("_mk_ofclass", mk_ofclass_tr'), ("TYPE", type_tr')];
wenzelm@2698
   300
wenzelm@548
   301
wenzelm@548
   302
wenzelm@548
   303
(** pt_to_ast **)
wenzelm@548
   304
wenzelm@548
   305
fun pt_to_ast trf pt =
wenzelm@548
   306
  let
wenzelm@548
   307
    fun trans a args =
wenzelm@548
   308
      (case trf a of
wenzelm@548
   309
        None => mk_appl (Constant a) args
wenzelm@548
   310
      | Some f => f args handle exn
clasohm@987
   311
          => (writeln ("Error in parse ast translation for " ^ quote a);
clasohm@987
   312
              raise exn));
wenzelm@548
   313
clasohm@987
   314
    (*translate pt bottom-up*)
wenzelm@548
   315
    fun ast_of (Node (a, pts)) = trans a (map ast_of pts)
wenzelm@548
   316
      | ast_of (Tip tok) = Variable (str_of_token tok);
wenzelm@548
   317
  in
clasohm@1178
   318
    ast_of pt
wenzelm@548
   319
  end;
wenzelm@548
   320
wenzelm@548
   321
wenzelm@548
   322
wenzelm@548
   323
(** ast_to_term **)
wenzelm@548
   324
wenzelm@548
   325
fun ast_to_term trf ast =
wenzelm@548
   326
  let
wenzelm@548
   327
    fun trans a args =
wenzelm@548
   328
      (case trf a of
wenzelm@548
   329
        None => list_comb (const a, args)
wenzelm@548
   330
      | Some f => f args handle exn
clasohm@987
   331
          => (writeln ("Error in parse translation for " ^ quote a);
clasohm@987
   332
              raise exn));
wenzelm@548
   333
wenzelm@548
   334
    fun term_of (Constant a) = trans a []
wenzelm@548
   335
      | term_of (Variable x) = scan_var x
wenzelm@548
   336
      | term_of (Appl (Constant a :: (asts as _ :: _))) =
wenzelm@548
   337
          trans a (map term_of asts)
wenzelm@548
   338
      | term_of (Appl (ast :: (asts as _ :: _))) =
wenzelm@548
   339
          list_comb (term_of ast, map term_of asts)
wenzelm@3777
   340
      | term_of (ast as Appl _) = raise AST ("ast_to_term: malformed ast", [ast]);
wenzelm@548
   341
  in
wenzelm@548
   342
    term_of ast
wenzelm@548
   343
  end;
wenzelm@548
   344
wenzelm@548
   345
end;