src/Pure/Tools/codegen_thingol.ML
author haftmann
Mon, 14 Nov 2005 15:23:33 +0100
changeset 18169 45def66f86cb
child 18170 73ce773f12de
permissions -rw-r--r--
added modules for code generator generation two, not operational yet
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
     1
(*  Title:      Pure/Tools/codegen_thingol.ML
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
     2
    ID:         $Id$
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
     3
    Author:     Florian Haftmann, TU Muenchen
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
     4
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
     5
Intermediate language ("Thin-gol") for code extraction.
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
     6
*)
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
     7
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
     8
signature CODEGEN_THINGOL =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
     9
sig
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    10
  type vname = string;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    11
  datatype itype =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    12
      IType of string * itype list
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    13
    | IFun of itype * itype
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    14
    | IVarT of vname * sort;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    15
  datatype ipat =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    16
      ICons of (string * ipat list) * itype
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    17
    | IVarP of vname * itype;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    18
  datatype iexpr =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    19
      IConst of string * itype
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    20
    | IVarE of vname * itype
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    21
    | IApp of iexpr * iexpr
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    22
    | IInst of iexpr * ClassPackage.sortlookup list list
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    23
    | IAbs of (vname * itype) * iexpr
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    24
    | ICase of iexpr * (ipat * iexpr) list;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    25
  val eq_itype: itype * itype -> bool
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    26
  val eq_ipat: ipat * ipat -> bool
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    27
  val eq_iexpr: iexpr * iexpr -> bool
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    28
  val mk_funs: itype list * itype -> itype;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    29
  val mk_apps: iexpr * iexpr list -> iexpr;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    30
  val pretty_itype: itype -> Pretty.T;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    31
  val pretty_ipat: ipat -> Pretty.T;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    32
  val pretty_iexpr: iexpr -> Pretty.T;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    33
  val unfold_fun: itype -> itype list * itype;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    34
  val unfold_app: iexpr -> iexpr * iexpr list;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    35
  val unfold_let: iexpr -> (ipat * iexpr) list * iexpr;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    36
  val itype_of_iexpr: iexpr -> itype;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    37
  val itype_of_ipat: ipat -> itype;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    38
  val ipat_of_iexpr: iexpr -> ipat;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    39
  val vars_of_ipats: ipat list -> vname list;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    40
  val instant_itype: vname * itype -> itype -> itype;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    41
  val invent_tvar_names: itype list -> int -> vname list -> vname -> vname list;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    42
  val invent_evar_names: iexpr list -> int -> vname list -> vname -> vname list;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    43
end;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    44
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    45
signature CODEGEN_THINGOL_OP =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    46
sig
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    47
  include CODEGEN_THINGOL;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    48
  val `%% : string * itype list -> itype;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    49
  val `-> : itype * itype -> itype;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    50
  val `--> : itype list * itype -> itype;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    51
  val `$ : iexpr * iexpr -> iexpr;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    52
  val `$$ : iexpr * iexpr list -> iexpr;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    53
end;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    54
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    55
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    56
structure CodegenThingolOp: CODEGEN_THINGOL_OP =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    57
struct
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    58
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    59
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    60
(* auxiliary *)
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    61
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    62
fun foldl' f (l, []) = the l
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    63
  | foldl' f (_, (r::rs)) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    64
      let
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    65
        fun itl (l, [])  = l
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    66
          | itl (l, r::rs) = itl (f (l, r), rs)
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    67
      in itl (r, rs) end;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    68
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    69
fun foldr' f ([], r) = the r
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    70
  | foldr' f (ls, _) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    71
      let
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    72
        fun itr [l] = l
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    73
          | itr (l::ls) = f (l, itr ls)
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    74
      in itr ls end;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    75
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    76
fun unfoldl dest x =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    77
  case dest x
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    78
   of NONE => (x, [])
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    79
    | SOME (x1, x2) =>
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    80
        let val (x', xs') = unfoldl dest x1 in (x', xs' @ [x2]) end;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    81
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    82
fun unfoldr dest x =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    83
  case dest x
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    84
   of NONE => ([], x)
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    85
    | SOME (x1, x2) =>
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    86
        let val (xs', x') = unfoldr dest x2 in (x1::xs', x') end;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    87
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    88
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    89
(* language representation *)
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    90
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    91
infix 8 `%%;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    92
infixr 6 `->;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    93
infixr 6 `-->;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    94
infix 4 `$;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    95
infix 4 `$$;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    96
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    97
type vname = string;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    98
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    99
datatype itype =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   100
    IType of string * itype list
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   101
  | IFun of itype * itype
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   102
  | IVarT of vname * sort;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   103
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   104
datatype ipat =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   105
    ICons of (string * ipat list) * itype
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   106
  | IVarP of vname * itype;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   107
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   108
datatype iexpr =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   109
    IConst of string * itype
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   110
  | IVarE of vname * itype
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   111
  | IApp of iexpr * iexpr
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   112
  | IInst of iexpr * ClassPackage.sortlookup list list
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   113
  | IAbs of (vname * itype) * iexpr
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   114
  | ICase of iexpr * (ipat * iexpr) list;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   115
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   116
val eq_itype = (op =);
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   117
val eq_ipat = (op =);
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   118
val eq_iexpr = (op =);
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   119
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   120
val mk_funs = Library.foldr IFun;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   121
val mk_apps = Library.foldl IApp;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   122
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   123
fun tyco `%% tys = IType (tyco, tys);
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   124
val op `-> = IFun;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   125
fun f `$ x = IApp (f, x);
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   126
val op `--> = mk_funs;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   127
val op `$$ = mk_apps;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   128
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   129
val unfold_fun = unfoldr
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   130
  (fn IFun t => SOME t
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   131
    | _ => NONE);
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   132
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   133
val unfold_app = unfoldl
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   134
  (fn IApp e => SOME e
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   135
    | _ => NONE);
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   136
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   137
val unfold_let = unfoldr
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   138
  (fn ICase (e, [(p, e')]) => SOME ((p, e), e')
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   139
    | _ => NONE);
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   140
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   141
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   142
(* simple diagnosis *)
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   143
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   144
fun pretty_itype (IType (tyco, tys)) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   145
      Pretty.gen_list "" "(" ")" (Pretty.str tyco :: map pretty_itype tys)
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   146
  | pretty_itype (IFun (ty1, ty2)) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   147
      Pretty.gen_list "" "(" ")" [pretty_itype ty1, Pretty.str "->", pretty_itype ty2]
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   148
  | pretty_itype (IVarT (v, sort)) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   149
      Pretty.str (v ^ enclose "|" "|" (space_implode "|" sort))
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   150
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   151
fun pretty_ipat (ICons ((cons, ps), ty)) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   152
      Pretty.gen_list " " "(" ")"
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   153
        (Pretty.str cons :: map pretty_ipat ps @ [Pretty.str ":: ", pretty_itype ty])
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   154
  | pretty_ipat (IVarP (v, ty)) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   155
      Pretty.block [Pretty.str ("?" ^ v ^ "::"), pretty_itype ty]
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   156
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   157
fun pretty_iexpr (IConst (f, ty)) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   158
      Pretty.block [Pretty.str (f ^ "::"), pretty_itype ty]
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   159
  | pretty_iexpr (IVarE (v, ty)) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   160
      Pretty.block [Pretty.str ("?" ^ v ^ "::"), pretty_itype ty]
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   161
  | pretty_iexpr (IApp (e1, e2)) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   162
      Pretty.enclose "(" ")" [pretty_iexpr e1, Pretty.brk 1, pretty_iexpr e2]
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   163
  | pretty_iexpr (IInst (e, c)) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   164
      pretty_iexpr e
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   165
  | pretty_iexpr (IAbs ((v, ty), e)) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   166
      Pretty.enclose "(" ")" [Pretty.str ("?" ^ v ^ " |->"), Pretty.brk 1, pretty_iexpr e]
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   167
  | pretty_iexpr (ICase (e, cs)) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   168
      Pretty.enclose "(" ")" [
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   169
        Pretty.str "case ",
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   170
        pretty_iexpr e,
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   171
        Pretty.enclose "(" ")" (map (fn (p, e) =>
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   172
          Pretty.block [
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   173
            pretty_ipat p,
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   174
            Pretty.str " => ",
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   175
            pretty_iexpr e
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   176
          ]
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   177
        ) cs)
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   178
      ]
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   179
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   180
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   181
(* language auxiliary *)
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   182
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   183
fun itype_of_iexpr (IConst (_, ty)) = ty
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   184
  | itype_of_iexpr (IVarE (_, ty)) = ty
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   185
  | itype_of_iexpr (e as IApp (e1, e2)) = (case itype_of_iexpr e1
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   186
      of (IFun (ty2, ty')) =>
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   187
            if ty2 = itype_of_iexpr e2
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   188
            then ty'
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   189
            else error ("inconsistent application: in " ^ Pretty.output (pretty_iexpr e)
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   190
              ^ ", " ^ (Pretty.output o pretty_itype) ty2 ^ " vs. " ^ (Pretty.output o pretty_itype o itype_of_iexpr) e2)
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   191
       | _ => error ("expression is not a function: " ^ Pretty.output (pretty_iexpr e1)))
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   192
  | itype_of_iexpr (IInst (e, cs)) = error ""
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   193
  | itype_of_iexpr (IAbs ((_, ty1), e2)) = ty1 `-> itype_of_iexpr e2
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   194
  | itype_of_iexpr (ICase ((_, [(_, e)]))) = itype_of_iexpr e;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   195
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   196
fun itype_of_ipat (ICons (_, ty)) = ty
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   197
  | itype_of_ipat (IVarP (_, ty)) = ty
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   198
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   199
fun ipat_of_iexpr (IConst (f, ty)) = ICons ((f, []), ty)
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   200
  | ipat_of_iexpr (IVarE v) = IVarP v
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   201
  | ipat_of_iexpr (e as IApp _) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   202
      case unfold_app e of (IConst (f, ty), es) =>
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   203
        ICons ((f, map ipat_of_iexpr es), (snd o unfold_fun) ty);
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   204
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   205
fun vars_of_ipats ps =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   206
  let
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   207
    fun vars (ICons ((_, ps), _)) = fold vars ps
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   208
      | vars (IVarP (v, _)) = cons v
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   209
  in fold vars ps [] end;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   210
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   211
fun instant_itype (v, sty) ty =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   212
  let
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   213
    fun instant (IType (tyco, tys)) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   214
          tyco `%% map instant tys
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   215
      | instant (IFun (ty1, ty2)) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   216
          instant ty1 `-> instant ty2
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   217
      | instant (w as (IVarT (u, _))) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   218
          if v = u then sty else w
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   219
  in instant ty end;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   220
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   221
fun invent_tvar_names tys n used a =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   222
  let
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   223
    fun invent (IType (_, tys)) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   224
          fold invent tys
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   225
      | invent (IFun (ty1, ty2)) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   226
          invent ty1 #> invent ty2
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   227
      | invent (IVarT (v, _)) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   228
          cons v
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   229
in Term.invent_names (fold invent tys used) a n end;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   230
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   231
fun invent_evar_names es n used a =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   232
  let
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   233
    fun invent (IConst (f, _)) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   234
          I
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   235
      | invent (IVarE (v, _)) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   236
          cons v
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   237
      | invent (IApp (e1, e2)) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   238
          invent e1 #> invent e2
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   239
      | invent (IAbs ((v, _), e)) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   240
          cons v #> invent e
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   241
      | invent (ICase (e, cs)) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   242
          invent e
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   243
          #>
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   244
          fold (fn (p, e) => append (vars_of_ipats [p]) #> invent e) cs
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   245
  in Term.invent_names (fold invent es used) a n end;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   246
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   247
end; (* struct *)
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   248
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   249
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   250
structure CodegenThingol : CODEGEN_THINGOL =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   251
struct
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   252
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   253
infix 8 `%%;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   254
infixr 6 `->;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   255
infixr 6 `-->;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   256
infix 4 `$;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   257
infix 4 `$$;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   258
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   259
open CodegenThingolOp;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   260
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   261
end; (* struct *)
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   262