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