src/Pure/Syntax/syn_trans.ML
author clasohm
Fri Mar 03 11:48:05 1995 +0100 (1995-03-03)
changeset 922 196ca0973a6d
parent 639 c88d56f7f33b
child 987 32bb5a8d5aab
permissions -rw-r--r--
added CPure (curried functions) and ProtoPure (ancestor of Pure and CPure)
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@922
    58
fun applC_ast_tr [f, arg] =
clasohm@922
    59
      let fun unfold_ast_c (y as Appl (Constant _ :: _)) = [y]
clasohm@922
    60
            | unfold_ast_c (Appl xs) = xs
clasohm@922
    61
            | unfold_ast_c y = [y];
clasohm@922
    62
      in Appl ((unfold_ast_c f) @ [arg]) end
clasohm@922
    63
  | applC_ast_tr asts = raise_ast "applC_ast_tr" asts;
wenzelm@548
    64
wenzelm@548
    65
wenzelm@548
    66
(* abstraction *)
wenzelm@548
    67
wenzelm@548
    68
fun idtyp_ast_tr (*"_idtyp"*) [x, ty] = Appl [Constant constrainC, x, ty]
wenzelm@548
    69
  | idtyp_ast_tr (*"_idtyp"*) asts = raise_ast "idtyp_ast_tr" asts;
wenzelm@548
    70
wenzelm@548
    71
fun lambda_ast_tr (*"_lambda"*) [idts, body] =
wenzelm@548
    72
      fold_ast_p "_abs" (unfold_ast "_idts" idts, body)
wenzelm@548
    73
  | lambda_ast_tr (*"_lambda"*) asts = raise_ast "lambda_ast_tr" asts;
wenzelm@548
    74
wenzelm@548
    75
val constrainAbsC = "_constrainAbs";
wenzelm@548
    76
wenzelm@548
    77
fun abs_tr (*"_abs"*) [Free (x, T), body] = absfree (x, T, body)
wenzelm@548
    78
  | abs_tr (*"_abs"*) (ts as [Const (c, _) $ Free (x, T) $ tT, body]) =
wenzelm@548
    79
      if c = constrainC
wenzelm@548
    80
        then const constrainAbsC $ absfree (x, T, body) $ tT
wenzelm@548
    81
      else raise_term "abs_tr" ts
wenzelm@548
    82
  | abs_tr (*"_abs"*) ts = raise_term "abs_tr" ts;
wenzelm@548
    83
wenzelm@548
    84
wenzelm@548
    85
(* nondependent abstraction *)
wenzelm@548
    86
wenzelm@548
    87
fun k_tr (*"_K"*) [t] = Abs ("uu", dummyT, incr_boundvars 1 t)
wenzelm@548
    88
  | k_tr (*"_K"*) ts = raise_term "k_tr" ts;
wenzelm@548
    89
wenzelm@548
    90
wenzelm@548
    91
(* binder *)
wenzelm@548
    92
wenzelm@548
    93
fun mk_binder_tr (sy, name) =
wenzelm@548
    94
  let
wenzelm@548
    95
    fun tr (Free (x, T), t) = const name $ absfree (x, T, t)
wenzelm@548
    96
      | tr (Const ("_idts", _) $ idt $ idts, t) = tr (idt, tr (idts, t))
wenzelm@548
    97
      | tr (t1 as Const (c, _) $ Free (x, T) $ tT, t) =
wenzelm@548
    98
          if c = constrainC then
wenzelm@548
    99
            const name $ (const constrainAbsC $ absfree (x, T, t) $ tT)
wenzelm@548
   100
          else raise_term "binder_tr" [t1, t]
wenzelm@548
   101
      | tr (t1, t2) = raise_term "binder_tr" [t1, t2];
wenzelm@548
   102
wenzelm@548
   103
    fun binder_tr (*sy*) [idts, body] = tr (idts, body)
wenzelm@548
   104
      | binder_tr (*sy*) ts = raise_term "binder_tr" ts;
wenzelm@548
   105
  in
wenzelm@548
   106
    (sy, binder_tr)
wenzelm@548
   107
  end;
wenzelm@548
   108
wenzelm@548
   109
wenzelm@548
   110
(* meta propositions *)
wenzelm@548
   111
wenzelm@548
   112
fun aprop_tr (*"_aprop"*) [t] = const constrainC $ t $ const "prop"
wenzelm@548
   113
  | aprop_tr (*"_aprop"*) ts = raise_term "aprop_tr" ts;
wenzelm@548
   114
wenzelm@548
   115
fun ofclass_tr (*"_ofclass"*) [ty, cls] =
wenzelm@548
   116
      cls $ (const constrainC $ const "TYPE" $ (const "itself" $ ty))
wenzelm@548
   117
  | ofclass_tr (*"_ofclass"*) ts = raise_term "ofclass_tr" ts;
wenzelm@548
   118
wenzelm@548
   119
wenzelm@548
   120
(* meta implication *)
wenzelm@548
   121
wenzelm@548
   122
fun bigimpl_ast_tr (*"_bigimpl"*) [asms, concl] =
wenzelm@548
   123
      fold_ast_p "==>" (unfold_ast "_asms" asms, concl)
wenzelm@548
   124
  | bigimpl_ast_tr (*"_bigimpl"*) asts = raise_ast "bigimpl_ast_tr" asts;
wenzelm@548
   125
wenzelm@548
   126
wenzelm@548
   127
wenzelm@548
   128
(** print (ast) translations **)
wenzelm@548
   129
wenzelm@548
   130
(* application *)
wenzelm@548
   131
wenzelm@548
   132
fun appl_ast_tr' (f, []) = raise_ast "appl_ast_tr'" [f]
wenzelm@548
   133
  | appl_ast_tr' (f, args) = Appl [Constant "_appl", f, fold_ast "_args" args];
wenzelm@548
   134
clasohm@922
   135
fun applC_ast_tr' (f, []) = raise_ast "applC_ast_tr'" [f]
clasohm@922
   136
  | applC_ast_tr' (f, arg::args) =
clasohm@922
   137
      let (* fold curried function application *)
clasohm@922
   138
          fun fold [] result = result
clasohm@922
   139
            | fold (x :: xs) result =
clasohm@922
   140
                fold xs (Appl [Constant "_applC", result, x]);
clasohm@922
   141
      in fold args (Appl [Constant "_applC", f, arg]) end;
clasohm@922
   142
(*
clasohm@922
   143
        f a b c  ()
clasohm@922
   144
        ("_applC" f a)
clasohm@922
   145
clasohm@922
   146
        b c      ("_applC" f a)
clasohm@922
   147
	("_applC" ("_applC" f a) b)
clasohm@922
   148
clasohm@922
   149
        c	 ("_applC" ("_applC" f a) b)
clasohm@922
   150
	("_applC" ("_applC" ("_applC" f a) b) c)
clasohm@922
   151
*)
clasohm@922
   152
wenzelm@548
   153
wenzelm@548
   154
(* abstraction *)
wenzelm@548
   155
wenzelm@548
   156
fun strip_abss vars_of body_of tm =
wenzelm@548
   157
  let
wenzelm@548
   158
    val vars = vars_of tm;
wenzelm@548
   159
    val body = body_of tm;
wenzelm@548
   160
    val rev_new_vars = rename_wrt_term body vars;
wenzelm@548
   161
  in
wenzelm@548
   162
    (map Free (rev rev_new_vars),
wenzelm@548
   163
      subst_bounds (map (free o #1) rev_new_vars, body))
wenzelm@548
   164
  end;
wenzelm@548
   165
wenzelm@548
   166
(*do (partial) eta-contraction before printing*)
wenzelm@548
   167
wenzelm@548
   168
val eta_contract = ref false;
wenzelm@548
   169
wenzelm@548
   170
fun eta_contr tm =
wenzelm@548
   171
  let
wenzelm@548
   172
    fun eta_abs (Abs (a, T, t)) =
wenzelm@548
   173
          (case eta_abs t of
wenzelm@548
   174
            t' as f $ u =>
wenzelm@548
   175
              (case eta_abs u of
wenzelm@548
   176
                Bound 0 =>
wenzelm@548
   177
                  if not (0 mem loose_bnos f) then incr_boundvars ~1 f
wenzelm@548
   178
                  else Abs (a, T, t')
wenzelm@548
   179
              | _ => Abs (a, T, t'))
wenzelm@548
   180
          | t' => Abs (a, T, t'))
wenzelm@548
   181
      | eta_abs t = t;
wenzelm@548
   182
  in
wenzelm@548
   183
    if ! eta_contract then eta_abs tm else tm
wenzelm@548
   184
  end;
wenzelm@548
   185
wenzelm@548
   186
wenzelm@548
   187
fun abs_tr' tm =
wenzelm@548
   188
  foldr (fn (x, t) => const "_abs" $ x $ t)
wenzelm@548
   189
    (strip_abss strip_abs_vars strip_abs_body (eta_contr tm));
wenzelm@548
   190
wenzelm@548
   191
wenzelm@548
   192
fun abs_ast_tr' (*"_abs"*) asts =
wenzelm@548
   193
  (case unfold_ast_p "_abs" (Appl (Constant "_abs" :: asts)) of
wenzelm@548
   194
    ([], _) => raise_ast "abs_ast_tr'" asts
wenzelm@548
   195
  | (xs, body) => Appl [Constant "_lambda", fold_ast "_idts" xs, body]);
wenzelm@548
   196
wenzelm@548
   197
wenzelm@548
   198
(* binder *)
wenzelm@548
   199
wenzelm@548
   200
fun mk_binder_tr' (name, sy) =
wenzelm@548
   201
  let
wenzelm@548
   202
    fun mk_idts [] = raise Match    (*abort translation*)
wenzelm@548
   203
      | mk_idts [idt] = idt
wenzelm@548
   204
      | mk_idts (idt :: idts) = const "_idts" $ idt $ mk_idts idts;
wenzelm@548
   205
wenzelm@548
   206
    fun tr' t =
wenzelm@548
   207
      let
wenzelm@548
   208
        val (xs, bd) = strip_abss (strip_qnt_vars name) (strip_qnt_body name) t;
wenzelm@548
   209
      in
wenzelm@548
   210
        const sy $ mk_idts xs $ bd
wenzelm@548
   211
      end;
wenzelm@548
   212
wenzelm@548
   213
    fun binder_tr' (*name*) (t :: ts) =
wenzelm@548
   214
          list_comb (tr' (const name $ t), ts)
wenzelm@548
   215
      | binder_tr' (*name*) [] = raise Match;
wenzelm@548
   216
  in
wenzelm@548
   217
    (name, binder_tr')
wenzelm@548
   218
  end;
wenzelm@548
   219
wenzelm@548
   220
wenzelm@548
   221
(* idts *)
wenzelm@548
   222
wenzelm@548
   223
fun idts_ast_tr' (*"_idts"*) [Appl [Constant c, x, ty], xs] =
wenzelm@548
   224
      if c = constrainC then
wenzelm@548
   225
        Appl [Constant "_idts", Appl [Constant "_idtyp", x, ty], xs]
wenzelm@548
   226
      else raise Match
wenzelm@548
   227
  | idts_ast_tr' (*"_idts"*) _ = raise Match;
wenzelm@548
   228
wenzelm@548
   229
wenzelm@548
   230
(* meta propositions *)
wenzelm@548
   231
wenzelm@548
   232
fun prop_tr' show_sorts tm =
wenzelm@548
   233
  let
wenzelm@548
   234
    fun aprop t = const "_aprop" $ t;
wenzelm@548
   235
wenzelm@548
   236
    fun is_prop tys t =
wenzelm@548
   237
      fastype_of1 (tys, t) = propT handle TERM _ => false;
wenzelm@548
   238
wenzelm@548
   239
    fun tr' _ (t as Const _) = t
wenzelm@548
   240
      | tr' _ (t as Free (x, ty)) =
wenzelm@548
   241
          if ty = propT then aprop (free x) else t
wenzelm@548
   242
      | tr' _ (t as Var (xi, ty)) =
wenzelm@548
   243
          if ty = propT then aprop (var xi) else t
wenzelm@548
   244
      | tr' tys (t as Bound _) =
wenzelm@548
   245
          if is_prop tys t then aprop t else t
wenzelm@548
   246
      | tr' tys (Abs (x, ty, t)) = Abs (x, ty, tr' (ty :: tys) t)
wenzelm@548
   247
      | tr' tys (t as t1 $ (t2 as Const ("TYPE", Type ("itself", [ty])))) =
wenzelm@548
   248
          if is_prop tys t then
wenzelm@548
   249
            const "_ofclass" $ term_of_typ show_sorts ty $ tr' tys t1
wenzelm@548
   250
          else tr' tys t1 $ tr' tys t2
wenzelm@548
   251
      | tr' tys (t as t1 $ t2) =
wenzelm@548
   252
          (if is_Const (head_of t) orelse not (is_prop tys t)
wenzelm@548
   253
            then I else aprop) (tr' tys t1 $ tr' tys t2);
wenzelm@548
   254
  in
wenzelm@548
   255
    tr' [] tm
wenzelm@548
   256
  end;
wenzelm@548
   257
wenzelm@548
   258
wenzelm@548
   259
(* meta implication *)
wenzelm@548
   260
wenzelm@548
   261
fun impl_ast_tr' (*"==>"*) asts =
wenzelm@548
   262
  (case unfold_ast_p "==>" (Appl (Constant "==>" :: asts)) of
wenzelm@548
   263
    (asms as _ :: _ :: _, concl)
wenzelm@548
   264
      => Appl [Constant "_bigimpl", fold_ast "_asms" asms, concl]
wenzelm@548
   265
  | _ => raise Match);
wenzelm@548
   266
wenzelm@548
   267
wenzelm@548
   268
(* dependent / nondependent quantifiers *)
wenzelm@548
   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@548
   272
        let val (x', B') = variant_abs (x, dummyT, B);
wenzelm@548
   273
        in list_comb (const q $ Free (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@548
   287
  [],
wenzelm@639
   288
  [("_abs", abs_ast_tr'), ("_idts", idts_ast_tr'), ("==>", impl_ast_tr')]);
wenzelm@548
   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
wenzelm@548
   300
          => (writeln ("Error in parse ast translation for " ^ quote a); raise exn));
wenzelm@548
   301
wenzelm@548
   302
    fun ast_of (Node (a, pts)) = trans a (map ast_of pts)
wenzelm@548
   303
      | ast_of (Tip tok) = Variable (str_of_token tok);
wenzelm@548
   304
  in
wenzelm@548
   305
    ast_of pt
wenzelm@548
   306
  end;
wenzelm@548
   307
wenzelm@548
   308
wenzelm@548
   309
wenzelm@548
   310
(** ast_to_term **)
wenzelm@548
   311
wenzelm@548
   312
fun ast_to_term trf ast =
wenzelm@548
   313
  let
wenzelm@548
   314
    fun trans a args =
wenzelm@548
   315
      (case trf a of
wenzelm@548
   316
        None => list_comb (const a, args)
wenzelm@548
   317
      | Some f => f args handle exn
wenzelm@548
   318
          => (writeln ("Error in parse translation for " ^ quote a); raise exn));
wenzelm@548
   319
wenzelm@548
   320
    fun term_of (Constant a) = trans a []
wenzelm@548
   321
      | term_of (Variable x) = scan_var x
wenzelm@548
   322
      | term_of (Appl (Constant a :: (asts as _ :: _))) =
wenzelm@548
   323
          trans a (map term_of asts)
wenzelm@548
   324
      | term_of (Appl (ast :: (asts as _ :: _))) =
wenzelm@548
   325
          list_comb (term_of ast, map term_of asts)
wenzelm@548
   326
      | term_of (ast as Appl _) = raise_ast "ast_to_term: malformed ast" [ast];
wenzelm@548
   327
  in
wenzelm@548
   328
    term_of ast
wenzelm@548
   329
  end;
wenzelm@548
   330
wenzelm@548
   331
wenzelm@548
   332
end;
wenzelm@548
   333