src/Pure/Tools/codegen_thingol.ML
author haftmann
Tue, 15 Nov 2005 10:11:52 +0100
changeset 18172 8ff5bcfae27a
parent 18170 73ce773f12de
child 18216 db7d43b25c99
permissions -rw-r--r--
added generic transformators
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
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
    14
    | IVarT of vname * sort
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
    15
    | IDictT of (string * itype) list;
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    16
  datatype ipat =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    17
      ICons of (string * ipat list) * itype
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    18
    | IVarP of vname * itype;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    19
  datatype iexpr =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    20
      IConst of string * itype
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    21
    | IVarE of vname * itype
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    22
    | IApp of iexpr * iexpr
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    23
    | IInst of iexpr * ClassPackage.sortlookup list list
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    24
    | IAbs of (vname * itype) * iexpr
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
    25
    | ICase of iexpr * (ipat * iexpr) list
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
    26
    | IDictE of (string * iexpr) list
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
    27
    | ILookup of (string list * vname);
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    28
  val eq_itype: itype * itype -> bool
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    29
  val eq_ipat: ipat * ipat -> bool
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    30
  val eq_iexpr: iexpr * iexpr -> bool
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    31
  val mk_funs: itype list * itype -> itype;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    32
  val mk_apps: iexpr * iexpr list -> iexpr;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    33
  val pretty_itype: itype -> Pretty.T;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    34
  val pretty_ipat: ipat -> Pretty.T;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    35
  val pretty_iexpr: iexpr -> Pretty.T;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    36
  val unfold_fun: itype -> itype list * itype;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    37
  val unfold_app: iexpr -> iexpr * iexpr list;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    38
  val unfold_let: iexpr -> (ipat * iexpr) list * iexpr;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    39
  val itype_of_iexpr: iexpr -> itype;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    40
  val itype_of_ipat: ipat -> itype;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    41
  val ipat_of_iexpr: iexpr -> ipat;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    42
  val vars_of_ipats: ipat list -> vname list;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    43
  val instant_itype: vname * itype -> itype -> itype;
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
    44
  val invent_var_t_names: itype list -> int -> vname list -> vname -> vname list;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
    45
  val invent_var_e_names: iexpr list -> int -> vname list -> vname -> vname list;
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    46
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    47
  datatype def =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    48
      Nop
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    49
    | Fun of (ipat list * iexpr) list * (ClassPackage.sortcontext * itype)
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
    50
    | Typesyn of (vname * string list) list * itype
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
    51
    | Datatype of (vname * string list) list * string list * string list
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
    52
    | Datatypecons of string * itype list
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    53
    | Class of string list * string list * string list
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    54
    | Classmember of string * vname * itype
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    55
    | Classinst of string * (string * string list list) * (string * string) list;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    56
  type module;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    57
  type transact;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    58
  type 'dst transact_fin;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    59
  type ('src, 'dst) gen_codegen = 'src -> transact -> 'dst transact_fin;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    60
  type gen_defgen = string -> transact -> (def * string list) transact_fin;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    61
  val eq_def: def * def -> bool;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    62
  val pretty_def: def -> Pretty.T;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    63
  val pretty_module: module -> Pretty.T;  
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    64
  val empty_module: module;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    65
  val get_def: module -> string -> def;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    66
  val merge_module: module * module -> module;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    67
  val partof: string list -> module -> module;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    68
  val succeed: 'a -> transact -> 'a transact_fin;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    69
  val fail: string -> transact -> 'a transact_fin;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    70
  val gen_invoke: (string * ('src, 'dst) gen_codegen) list -> string
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    71
    -> 'src -> transact -> 'dst * transact;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    72
  val gen_ensure_def: (string * gen_defgen) list -> string
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    73
    -> string -> transact -> transact;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    74
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
    75
  val prims: string list;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
    76
  val extract_defs: iexpr -> string list;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
    77
  val eta_expand: (string -> int) -> module -> module;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
    78
  val connect_datatypes_clsdecls: module -> module;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
    79
  val tupelize_cons: module -> module;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
    80
  val eliminate_dtconstr: module -> module;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
    81
  val eliminate_classes: module -> module;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
    82
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    83
  val debug_level : int ref;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    84
  val debug : int -> ('a -> string) -> 'a -> 'a;
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    85
end;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    86
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    87
signature CODEGEN_THINGOL_OP =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    88
sig
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    89
  include CODEGEN_THINGOL;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    90
  val `%% : string * itype list -> itype;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    91
  val `-> : itype * itype -> itype;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    92
  val `--> : itype list * itype -> itype;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    93
  val `$ : iexpr * iexpr -> iexpr;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    94
  val `$$ : iexpr * iexpr list -> iexpr;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    95
end;
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
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    98
structure CodegenThingolOp: CODEGEN_THINGOL_OP =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    99
struct
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   100
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   101
(** auxiliary **)
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   102
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   103
val debug_level = ref 0;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   104
fun debug d f x = (if d <= !debug_level then Output.debug (f x) else (); x);
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   105
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   106
fun foldl' f (l, []) = the l
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   107
  | foldl' f (_, (r::rs)) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   108
      let
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   109
        fun itl (l, [])  = l
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   110
          | itl (l, r::rs) = itl (f (l, r), rs)
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   111
      in itl (r, rs) end;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   112
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   113
fun foldr' f ([], r) = the r
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   114
  | foldr' f (ls, _) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   115
      let
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   116
        fun itr [l] = l
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   117
          | itr (l::ls) = f (l, itr ls)
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   118
      in itr ls end;
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
fun unfoldl dest x =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   121
  case dest x
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   122
   of NONE => (x, [])
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   123
    | SOME (x1, x2) =>
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   124
        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
   125
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   126
fun unfoldr dest x =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   127
  case dest x
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   128
   of NONE => ([], x)
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   129
    | SOME (x1, x2) =>
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   130
        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
   131
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   132
fun map_yield f [] = ([], [])
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   133
  | map_yield f (x::xs) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   134
      let
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   135
        val (y, x') = f x
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   136
        val (ys, xs') = map_yield f xs
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   137
      in (y::ys, x'::xs') end;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   138
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   139
fun get_prefix eq ([], ys) = ([], [], ys)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   140
  | get_prefix eq (xs, []) = ([], xs, [])
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   141
  | get_prefix eq (xs as x::xs', ys as y::ys') =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   142
      if eq (x, y) then
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   143
        let val (ps', xs'', ys'') = get_prefix eq (xs', ys')
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   144
        in (x::ps', xs'', ys'') end
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   145
      else ([], xs, ys);
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   146
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   147
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   148
(** language core - types, pattern, expressions **)
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   149
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   150
(* language representation *)
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   151
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   152
infix 8 `%%;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   153
infixr 6 `->;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   154
infixr 6 `-->;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   155
infix 4 `$;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   156
infix 4 `$$;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   157
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   158
type vname = string;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   159
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   160
datatype itype =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   161
    IType of string * itype list
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   162
  | IFun of itype * itype
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   163
  | IVarT of vname * sort
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   164
    (*ML auxiliary*)
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   165
  | IDictT of (string * itype) list;
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   166
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   167
datatype ipat =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   168
    ICons of (string * ipat list) * itype
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   169
  | IVarP of vname * itype;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   170
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   171
datatype iexpr =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   172
    IConst of string * itype
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   173
  | IVarE of vname * itype
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   174
  | IApp of iexpr * iexpr
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   175
  | IInst of iexpr * ClassPackage.sortlookup list list
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   176
  | IAbs of (vname * itype) * iexpr
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   177
  | ICase of iexpr * (ipat * iexpr) list
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   178
    (*ML auxiliary*)
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   179
  | IDictE of (string * iexpr) list
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   180
  | ILookup of (string list * vname);
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   181
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   182
val eq_itype = (op =);
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   183
val eq_ipat = (op =);
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   184
val eq_iexpr = (op =);
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   185
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   186
val mk_funs = Library.foldr IFun;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   187
val mk_apps = Library.foldl IApp;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   188
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   189
fun tyco `%% tys = IType (tyco, tys);
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   190
val op `-> = IFun;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   191
fun f `$ x = IApp (f, x);
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   192
val op `--> = mk_funs;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   193
val op `$$ = mk_apps;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   194
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   195
val unfold_fun = unfoldr
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   196
  (fn IFun t => SOME t
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   197
    | _ => NONE);
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
val unfold_app = unfoldl
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   200
  (fn IApp e => SOME e
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   201
    | _ => NONE);
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   202
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   203
val unfold_let = unfoldr
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   204
  (fn ICase (e, [(p, e')]) => SOME ((p, e), e')
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   205
    | _ => NONE);
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   206
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   207
fun map_itype f_itype (IType (tyco, tys)) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   208
      tyco `%% map f_itype tys
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   209
  | map_itype f_itype (IFun (t1, t2)) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   210
      f_itype t1 `-> f_itype t2
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   211
  | map_itype _ (ty as IVarT _) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   212
      ty;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   213
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   214
fun map_ipat f_itype f_ipat (ICons ((c, ps), ty)) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   215
      ICons ((c, map f_ipat ps), f_itype ty)
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   216
  | map_ipat _ _ (p as IVarP _) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   217
      p;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   218
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   219
fun map_iexpr f_itype f_ipat f_iexpr (IApp (e1, e2)) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   220
      f_iexpr e1 `$ f_iexpr e2
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   221
  | map_iexpr f_itype f_ipat f_iexpr (IInst (e, c)) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   222
      IInst (f_iexpr e, c)
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   223
  | map_iexpr f_itype f_ipat f_iexpr (IAbs (v, e)) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   224
      IAbs (v, f_iexpr e)
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   225
  | map_iexpr f_itype f_ipat f_iexpr (ICase (e, ps)) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   226
      ICase (f_iexpr e, map (fn (p, e) => (f_ipat p, f_iexpr e)) ps)
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   227
  | map_iexpr _ _ _ (e as IConst _) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   228
      e
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   229
  | map_iexpr _ _ _ (e as IVarE _) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   230
      e;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   231
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   232
fun fold_itype f_itype (IFun (t1, t2)) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   233
      f_itype t1 #> f_itype t2
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   234
  | fold_itype _ (ty as IType _) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   235
      I
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   236
  | fold_itype _ (ty as IVarT _) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   237
      I;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   238
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   239
fun fold_ipat f_itype f_ipat (ICons ((_, ps), ty)) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   240
      f_itype ty #> fold f_ipat ps
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   241
  | fold_ipat f_itype f_ipat (p as IVarP _) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   242
      I;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   243
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   244
fun fold_iexpr f_itype f_ipat f_iexpr (IApp (e1, e2)) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   245
      f_iexpr e1 #> f_iexpr e2
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   246
  | fold_iexpr f_itype f_ipat f_iexpr (IInst (e, c)) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   247
      f_iexpr e
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   248
  | fold_iexpr f_itype f_ipat f_iexpr (IAbs (v, e)) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   249
      f_iexpr e
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   250
  | fold_iexpr f_itype f_ipat f_iexpr (ICase (e, ps)) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   251
      f_iexpr e #> fold (fn (p, e) => f_ipat p #> f_iexpr e) ps 
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   252
  | fold_iexpr _ _ _ (e as IConst _) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   253
      I
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   254
  | fold_iexpr _ _ _ (e as IVarE _) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   255
      I;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   256
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   257
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   258
(* simple diagnosis *)
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   259
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   260
fun pretty_itype (IType (tyco, tys)) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   261
      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
   262
  | pretty_itype (IFun (ty1, ty2)) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   263
      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
   264
  | pretty_itype (IVarT (v, sort)) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   265
      Pretty.str (v ^ enclose "|" "|" (space_implode "|" sort))
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   266
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   267
fun pretty_ipat (ICons ((cons, ps), ty)) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   268
      Pretty.gen_list " " "(" ")"
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   269
        (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
   270
  | pretty_ipat (IVarP (v, ty)) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   271
      Pretty.block [Pretty.str ("?" ^ v ^ "::"), pretty_itype ty]
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   272
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   273
fun pretty_iexpr (IConst (f, ty)) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   274
      Pretty.block [Pretty.str (f ^ "::"), pretty_itype ty]
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   275
  | pretty_iexpr (IVarE (v, ty)) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   276
      Pretty.block [Pretty.str ("?" ^ v ^ "::"), pretty_itype ty]
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   277
  | pretty_iexpr (IApp (e1, e2)) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   278
      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
   279
  | pretty_iexpr (IInst (e, c)) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   280
      pretty_iexpr e
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   281
  | pretty_iexpr (IAbs ((v, ty), e)) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   282
      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
   283
  | pretty_iexpr (ICase (e, cs)) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   284
      Pretty.enclose "(" ")" [
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   285
        Pretty.str "case ",
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   286
        pretty_iexpr e,
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   287
        Pretty.enclose "(" ")" (map (fn (p, e) =>
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   288
          Pretty.block [
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   289
            pretty_ipat p,
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   290
            Pretty.str " => ",
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   291
            pretty_iexpr e
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   292
          ]
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   293
        ) cs)
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   294
      ]
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   295
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   296
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   297
(* language auxiliary *)
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   298
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   299
fun itype_of_iexpr (IConst (_, ty)) = ty
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   300
  | itype_of_iexpr (IVarE (_, ty)) = ty
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   301
  | 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
   302
      of (IFun (ty2, ty')) =>
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   303
            if ty2 = itype_of_iexpr e2
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   304
            then ty'
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   305
            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
   306
              ^ ", " ^ (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
   307
       | _ => 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
   308
  | itype_of_iexpr (IInst (e, cs)) = error ""
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   309
  | 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
   310
  | itype_of_iexpr (ICase ((_, [(_, e)]))) = itype_of_iexpr e;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   311
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   312
fun itype_of_ipat (ICons (_, ty)) = ty
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   313
  | itype_of_ipat (IVarP (_, ty)) = ty
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   314
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   315
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
   316
  | ipat_of_iexpr (IVarE v) = IVarP v
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   317
  | ipat_of_iexpr (e as IApp _) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   318
      case unfold_app e of (IConst (f, ty), es) =>
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   319
        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
   320
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   321
fun vars_of_ipats ps =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   322
  let
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   323
    fun vars (ICons ((_, ps), _)) = fold vars ps
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   324
      | vars (IVarP (v, _)) = cons v
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   325
  in fold vars ps [] end;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   326
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   327
fun instant_itype (v, sty) ty =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   328
  let
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   329
    fun instant (IType (tyco, tys)) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   330
          tyco `%% map instant tys
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   331
      | instant (IFun (ty1, ty2)) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   332
          instant ty1 `-> instant ty2
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   333
      | instant (w as (IVarT (u, _))) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   334
          if v = u then sty else w
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   335
  in instant ty end;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   336
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   337
fun invent_var_t_names tys n used a =
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   338
  let
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   339
    fun invent (IType (_, tys)) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   340
          fold invent tys
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   341
      | invent (IFun (ty1, ty2)) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   342
          invent ty1 #> invent ty2
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   343
      | invent (IVarT (v, _)) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   344
          cons v
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   345
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
   346
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   347
fun invent_var_e_names es n used a =
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   348
  let
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   349
    fun invent (IConst (f, _)) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   350
          I
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   351
      | invent (IVarE (v, _)) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   352
          cons v
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   353
      | invent (IApp (e1, e2)) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   354
          invent e1 #> invent e2
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   355
      | invent (IAbs ((v, _), e)) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   356
          cons v #> invent e
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   357
      | invent (ICase (e, cs)) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   358
          invent e
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   359
          #>
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   360
          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
   361
  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
   362
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   363
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   364
(** language module system - definitions, modules, transactions **)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   365
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   366
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   367
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   368
(* type definitions *)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   369
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   370
datatype def =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   371
    Nop
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   372
  | Fun of (ipat list * iexpr) list * (ClassPackage.sortcontext * itype)
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   373
  | Typesyn of (vname * string list) list * itype
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   374
  | Datatype of (vname * string list) list * string list * string list
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   375
  | Datatypecons of string * itype list
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   376
  | Class of string list * string list * string list
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   377
  | Classmember of string * string * itype
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   378
  | Classinst of string * (string * string list list) * (string * string) list;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   379
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   380
datatype node = Def of def | Module of node Graph.T;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   381
type module = node Graph.T;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   382
type transact = Graph.key list * module;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   383
datatype 'dst transact_res = Succeed of 'dst | Fail of string;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   384
type 'dst transact_fin = 'dst transact_res * transact;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   385
type ('src, 'dst) gen_codegen = 'src -> transact -> 'dst transact_fin;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   386
type gen_defgen = string -> transact -> (def * string list) transact_fin;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   387
exception FAIL of string;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   388
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   389
val eq_def = (op =);
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   390
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   391
(* simple diagnosis *)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   392
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   393
fun pretty_def Nop =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   394
      Pretty.str "<NOP>"
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   395
  | pretty_def (Fun (eqs, (_, ty))) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   396
      Pretty.gen_list " |" "" "" (
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   397
        map (fn (ps, body) =>
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   398
          Pretty.block [
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   399
            Pretty.gen_list "," "[" "]" (map pretty_ipat ps),
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   400
            Pretty.str " |->",
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   401
            Pretty.brk 1,
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   402
            pretty_iexpr body,
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   403
            Pretty.str "::",
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   404
            pretty_itype ty
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   405
          ]) eqs
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   406
        )
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   407
  | pretty_def (Typesyn (vs, ty)) =
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   408
      Pretty.block [
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   409
        Pretty.list "(" ")" (map (pretty_itype o IVarT) vs),
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   410
        Pretty.str " |=> ",
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   411
        pretty_itype ty
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   412
      ]
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   413
  | pretty_def (Datatype (vs, cs, clss)) =
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   414
      Pretty.block [
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   415
        Pretty.list "(" ")" (map (pretty_itype o IVarT) vs),
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   416
        Pretty.str " |=> ",
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   417
        Pretty.gen_list " |" "" "" (map Pretty.str cs),
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   418
        Pretty.str ", instance of ",
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   419
        Pretty.gen_list "," "[" "]" (map Pretty.str clss)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   420
      ]
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   421
  | pretty_def (Datatypecons (dtname, tys)) =
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   422
      Pretty.block [
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   423
        Pretty.str "cons ",
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   424
        Pretty.gen_list " ->" "" "" (map pretty_itype tys @ [Pretty.str dtname])
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   425
      ]
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   426
  | pretty_def (Class (supcls, mems, insts)) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   427
      Pretty.str "Class ..."
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   428
  | pretty_def (Classmember (cls, v, ty)) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   429
      Pretty.str "Classmember ..."
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   430
  | pretty_def (Classinst (cls, insts, mems)) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   431
      Pretty.str "Classinst ..."
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   432
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   433
fun pretty_module modl =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   434
  let
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   435
    fun pretty (name, Module modl) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   436
          Pretty.block (
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   437
            Pretty.str ("module " ^ name ^ " {")
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   438
            :: Pretty.brk 1
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   439
            :: Pretty.chunks (map pretty (AList.make (Graph.get_node modl)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   440
                 (Graph.strong_conn modl |> List.concat |> rev)))
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   441
            :: Pretty.str "}" :: nil
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   442
          )
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   443
      | pretty (name, Def def) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   444
          Pretty.block [Pretty.str name, Pretty.str " :=", Pretty.brk 1, pretty_def def]
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   445
  in pretty ("//", Module modl) end;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   446
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   447
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   448
(* name handling *)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   449
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   450
fun dest_name name =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   451
  let
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   452
    val name' = NameSpace.unpack name
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   453
    val (name'', name_base) = split_last name'
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   454
    val (modl, shallow) = split_last name''
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   455
  in (modl, NameSpace.pack [shallow, name_base]) end
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   456
  handle Empty => error ("not a qualified name: " ^ quote name);
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   457
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   458
fun dest_modl (Module m) = m;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   459
fun dest_def (Def d) = d;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   460
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   461
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   462
(* modules *)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   463
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   464
val empty_module = Graph.empty; (*read: "depends on"*)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   465
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   466
fun get_def modl name =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   467
  case dest_name name
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   468
   of (modlname, base) =>
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   469
        let
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   470
          fun get (Module node) [] =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   471
                (dest_def o Graph.get_node node) base
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   472
            | get (Module node) (m::ms) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   473
                get (Graph.get_node node m) ms
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   474
        in get (Module modl) modlname end;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   475
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   476
fun add_def (name, def) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   477
  let
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   478
    val (modl, base) = dest_name name;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   479
    fun add [] =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   480
          Graph.new_node (base, Def def)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   481
      | add (m::ms) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   482
          Graph.default_node (m, Module empty_module)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   483
          #> Graph.map_node m (Module o add ms o dest_modl)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   484
  in add modl end;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   485
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   486
fun map_def name f =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   487
  let
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   488
    val (modl, base) = dest_name name;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   489
    fun mapp [] =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   490
          Graph.map_node base (Def o f o dest_def)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   491
      | mapp (m::ms) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   492
          Graph.map_node m (Module o mapp ms o dest_modl)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   493
  in mapp modl end;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   494
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   495
fun add_dep (name1, name2) modl =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   496
  if name1 = name2 then modl
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   497
  else
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   498
    let
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   499
      val m1 = dest_name name1 |> apsnd single |> (op @);
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   500
      val m2 = dest_name name2 |> apsnd single |> (op @);
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   501
      val (ms, r1, r2) = get_prefix (op =) (m1, m2);
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   502
      val (ms, s1::r1, s2::r2) = get_prefix (op =) (m1, m2);
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   503
      val add_edge =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   504
        if null r1 andalso null r2
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   505
        then Graph.add_edge
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   506
        else Graph.add_edge_acyclic
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   507
      fun add [] node =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   508
            node
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   509
            |> add_edge (s1, s2)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   510
        | add (m::ms) node =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   511
            node
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   512
            |> Graph.map_node m (Module o add ms o dest_modl);
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   513
    in add ms modl end;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   514
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   515
fun map_defs f =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   516
  let
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   517
    fun mapp (Def def) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   518
          (Def o f) def
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   519
      | mapp (Module modl) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   520
          (Module o Graph.map_nodes mapp) modl
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   521
  in dest_modl o mapp o Module end;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   522
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   523
fun fold_defs f =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   524
  let
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   525
    fun fol prfix (name, Def def) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   526
          f (NameSpace.pack (prfix @ [name]), def)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   527
      | fol prfix (name, Module modl) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   528
          Graph.fold_nodes (fol (prfix @ [name])) modl
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   529
  in Graph.fold_nodes (fol []) end;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   530
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   531
fun add_deps f modl =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   532
  modl
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   533
  |> fold add_dep ([] |> fold_defs (append o f) modl);
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   534
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   535
fun fold_map_defs f =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   536
  let
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   537
    fun foldmap prfix (name, Def def) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   538
          apfst Def o f (NameSpace.pack (prfix @ [name]), def)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   539
      | foldmap prfix (name, Module modl) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   540
          apfst Module o Graph.fold_map_nodes (foldmap (prfix @ [name])) modl
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   541
  in Graph.fold_map_nodes (foldmap []) end;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   542
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   543
fun map_def_fun f_ipat f_iexpr (Fun (eqs, cty)) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   544
      Fun (map (fn (ps, rhs) => (map f_ipat ps, f_iexpr rhs)) eqs, cty)
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   545
  | map_def_fun _ _ def = def;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   546
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   547
fun transform_defs f_def f_ipat f_iexpr s modl =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   548
  let
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   549
    val (modl', s') = fold_map_defs f_def modl s
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   550
  in
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   551
    modl'
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   552
    |> map_defs (map_def_fun (f_ipat s') (f_iexpr s'))
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   553
  end;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   554
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   555
fun merge_module modl12 =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   556
  let
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   557
    fun join_module (Module m1, Module m2) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   558
          (SOME o Module) (merge_module (m1, m2))
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   559
      | join_module (Def d1, Def d2) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   560
          if eq_def (d1, d2) then (SOME o Def) d1 else NONE
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   561
      | join_module _ =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   562
          NONE
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   563
  in Graph.join (K join_module) modl12 end;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   564
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   565
fun partof names modl =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   566
      let
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   567
        datatype pathnode = PN of (string list * (string * pathnode) list);
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   568
        fun mk_ipath ([], base) (PN (defs, modls)) =
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   569
              PN (base :: defs, modls)
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   570
          | mk_ipath (n::ns, base) (PN (defs, modls)) =
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   571
              modls
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   572
              |> AList.default (op =) (n, PN ([], []))
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   573
              |> AList.map_entry (op =) n (mk_ipath (ns, base))
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   574
              |> (pair defs #> PN);
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   575
        fun select (PN (defs, modls)) (Module module) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   576
          module
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   577
          |> Graph.subgraph (Graph.all_succs module (defs @ map fst modls))
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   578
          |> fold (fn (name, modls) => Graph.map_node name (select modls)) modls
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   579
          |> Module;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   580
      in
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   581
        Module modl
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   582
        |> select (fold (mk_ipath o dest_name) (filter NameSpace.is_qualified names) (PN ([], [])))
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   583
        |> dest_modl
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   584
      end;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   585
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   586
fun add_check_transform (name, (Datatypecons (dtname, _))) =
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   587
      ([([dtname],
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   588
          fn [Datatype (_, _, [])] => NONE | _ => "attempted to add constructor to already instantiating datatype" |> SOME)],
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   589
       [(dtname,
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   590
          fn Datatype (vs, cs, clss) => Datatype (vs, name::cs, clss)
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   591
           | def => "attempted to add datatype constructor to non-datatype: "
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   592
              ^ (Pretty.output o pretty_def) def |> error)])
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   593
  | add_check_transform (name, Classmember (clsname, v, ty)) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   594
      let
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   595
        fun check_var (IType (tyco, tys)) s =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   596
              fold check_var tys s
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   597
          | check_var (IFun (ty1, ty2)) s =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   598
              s
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   599
              |> check_var ty1
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   600
              |> check_var ty2
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   601
          | check_var (IVarT (w, sort)) s =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   602
              if v = w
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   603
              andalso member (op =) sort clsname
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   604
              then "additional class appears at type variable" |> SOME
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   605
              else NONE
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   606
      in
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   607
        ([([], fn [] => check_var ty NONE),
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   608
          ([clsname],
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   609
             fn [Class (_, _, [])] => NONE
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   610
              | _ => "attempted to add class member to witnessed class" |> SOME)],
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   611
         [(clsname,
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   612
             fn Class (supcs, mems, insts) => Class (supcs, name::mems, insts)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   613
              | def => "attempted to add class member to non-class"
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   614
                 ^ (Pretty.output o pretty_def) def |> error)])
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   615
      end
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   616
  | add_check_transform (name, Classinst (clsname, (tyco, arity), memdefs)) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   617
      let
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   618
        fun check [Classmember (_, v, mtyp_c), Fun (_, (_, mtyp_i))] =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   619
          let
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   620
            val mtyp_i' = instant_itype (v, tyco `%%
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   621
              map2 IVarT ((invent_var_t_names [mtyp_c] (length arity) [] "a"), arity)) mtyp_c;
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   622
          in if eq_itype (mtyp_i', mtyp_i) (*! PERHAPS TOO STRICT !*)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   623
          then NONE
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   624
          else "wrong type signature for class member: "
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   625
            ^ (Pretty.output o pretty_itype) mtyp_i' ^ " expected,"
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   626
            ^ (Pretty.output o pretty_itype) mtyp_i ^ " given" |> SOME end
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   627
      in
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   628
        (map (fn (memname, memprim) => ((writeln memname; writeln memprim; [memname, memprim]), check)) memdefs,
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   629
          [(clsname,
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   630
              fn Class (supcs, mems, insts) => Class (supcs, mems, name::insts)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   631
               | def => "attempted to add class instance to non-class"
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   632
                  ^ (Pretty.output o pretty_def) def |> error),
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   633
           (tyco,
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   634
              fn Datatype (vs, cs, clss) => Datatype (vs, cs, clsname::clss)
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   635
               | Nop => Nop
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   636
               | def => "attempted to instantiate non-type to class instance"
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   637
                  ^ (Pretty.output o pretty_def) def |> error)])
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   638
      end
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   639
  | add_check_transform _ = ([], []);
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   640
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   641
fun succeed some = (pair o Succeed) some;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   642
fun fail msg = (pair o Fail) msg;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   643
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   644
fun check_fail msg' (Succeed dst, trns) = (dst, trns)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   645
  | check_fail msg' (Fail errmsg, _) = (tracing ("ROLLBACK CHECK: " ^ errmsg ^ "\n" ^ msg'); raise FAIL errmsg);
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   646
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   647
fun handle_fail msg f modl =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   648
  f modl handle FAIL msg' => ([], modl) |> fail (msg ^ "\n" ^ msg');
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   649
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   650
fun select_generator print_msg src [] trns =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   651
      fail ("no code generator available") trns
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   652
  | select_generator print_msg src [(gname, cgen)] trns =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   653
      (print_msg gname; cgen src trns)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   654
  | select_generator print_msg src ((gname, cgen)::cgens) trns =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   655
      case cgen src trns
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   656
       of result as (Succeed _, _) =>
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   657
            (print_msg gname; result)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   658
        | _ => select_generator print_msg src cgens trns
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   659
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   660
fun gen_invoke codegens msg src (deps, modl) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   661
  ([], modl)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   662
  |> select_generator (fn gname => "choosing code generator " ^ gname ^ " for source " ^ quote msg)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   663
       src codegens
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   664
  |> check_fail msg
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   665
  ||> (fn (deps', modl') => (append deps' deps, modl'));
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   666
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   667
fun gen_ensure_def defgens msg name (deps, modl) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   668
  let
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   669
    fun add (name, def) (deps, modl) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   670
      let
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   671
        val (checks, trans) = add_check_transform (name, def);
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   672
        fun check (check_defs, checker) modl =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   673
          case checker (check_defs |> filter NameSpace.is_qualified |> map (get_def modl))
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   674
           of NONE => modl
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   675
            | SOME e => raise FAIL e;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   676
        fun transform (name, f) modl =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   677
          modl
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   678
          |> K (NameSpace.is_qualified name) ? map_def name f;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   679
      in
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   680
        modl
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   681
        |> fold check checks
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   682
        |> fold (curry add_dep name) deps
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   683
        |> map_def name (fn _ => def)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   684
        |> fold transform trans
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   685
      end;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   686
    fun ensure_node name modl =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   687
      if can (get_def modl) name
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   688
      then ([name], modl)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   689
      else
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   690
        ([], modl |> add_def (name, Nop))
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   691
        |> select_generator (fn gname => "choosing code generator " ^ gname ^ " for definition of " ^ quote name)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   692
             name defgens
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   693
        |> check_fail msg
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   694
        |-> (fn (def, names') =>
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   695
           add (name, def)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   696
           #> fold_map ensure_node names')
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   697
        |-> (fn names' => pair (name :: Library.flat names'))
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   698
  in
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   699
    modl
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   700
    |> ensure_node name
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   701
    |-> (fn names => pair (names@deps))
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   702
  end;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   703
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   704
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   705
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   706
(** primitive language constructs **)
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   707
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   708
val class_eq = "Eqtype"; (*defined for all primitve types and extensionally for all datatypes*)
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   709
val type_bool = "Bool";
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   710
val type_integer = "Integer"; (*infinite!*)
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   711
val type_float = "Float";
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   712
val type_pair = "Pair";
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   713
val type_list = "List";
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   714
val cons_true = "True";
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   715
val cons_false = "False";
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   716
val cons_not = "not";
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   717
val cons_pair = "Pair";
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   718
val cons_nil = "Nil";
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   719
val cons_cons = "Cons";
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   720
val fun_primeq = "primeq"; (*defined for all primitive types*)
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   721
val fun_eq = "eq"; (*to class eq*)
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   722
val fun_not = "not";
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   723
val fun_and = "and";
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   724
val fun_or = "or";
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   725
val fun_if = "if";
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   726
val fun_fst = "fst";
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   727
val fun_snd = "snd";
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   728
val fun_add = "add";
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   729
val fun_mult = "mult";
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   730
val fun_minus = "minus";
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   731
val fun_lt = "lt";
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   732
val fun_le = "le";
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   733
val fun_wfrec = "wfrec";
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   734
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   735
local
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   736
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   737
val A = IVarT ("a", []);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   738
val B = IVarT ("b", []);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   739
val E = IVarT ("e", [class_eq]);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   740
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   741
in
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   742
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   743
val Type_bool = type_bool `%% [];
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   744
val Type_integer = type_integer `%% [];
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   745
val Type_float = type_float `%% [];
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   746
fun Type_pair a b = type_pair `%% [a, b];
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   747
fun Type_list a = type_list `%% [a];
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   748
val Cons_true = IConst (cons_true, Type_bool);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   749
val Cons_false = IConst (cons_false, Type_bool);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   750
val Cons_pair = IConst (cons_pair, A `-> B `-> Type_pair A B);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   751
val Cons_nil = IConst (cons_nil, Type_list A);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   752
val Cons_cons = IConst (cons_cons, A `-> Type_list A `-> Type_list A);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   753
val Fun_eq = IConst (fun_eq, E `-> E `-> Type_bool);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   754
val Fun_not = IConst (fun_not, Type_bool `-> Type_bool);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   755
val Fun_and = IConst (fun_and, Type_bool `-> Type_bool `-> Type_bool);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   756
val Fun_or = IConst (fun_or, Type_bool `-> Type_bool `-> Type_bool);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   757
val Fun_if = IConst (fun_if, Type_bool `-> A `-> A `-> A);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   758
val Fun_fst = IConst (fun_fst, Type_pair A B `-> A);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   759
val Fun_snd = IConst (fun_snd, Type_pair A B `-> B);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   760
val Fun_0 = IConst ("0", Type_integer);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   761
val Fun_1 = IConst ("1", Type_integer);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   762
val Fun_add = IConst (fun_add, Type_integer `-> Type_integer `-> Type_integer);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   763
val Fun_mult = IConst (fun_mult, Type_integer `-> Type_integer `-> Type_integer);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   764
val Fun_minus = IConst (fun_minus, Type_integer `-> Type_integer);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   765
val Fun_lt = IConst (fun_lt, Type_integer `-> Type_integer `-> Type_bool);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   766
val Fun_le = IConst (fun_le, Type_integer `-> Type_integer `-> Type_bool);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   767
val Fun_wfrec = IConst (fun_wfrec, ((A `-> B) `-> A `-> B) `-> A `-> B);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   768
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   769
infix 7 xx;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   770
infix 5 **;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   771
infix 5 &&;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   772
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   773
fun a xx b = Type_pair a b;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   774
fun a ** b =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   775
  let
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   776
    val ty_a = itype_of_iexpr a;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   777
    val ty_b = itype_of_iexpr b;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   778
  in IConst (cons_pair, ty_a `-> ty_b `-> ty_a xx ty_b) `$ a `$ b end;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   779
fun a && b =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   780
  let
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   781
    val ty_a = itype_of_ipat a;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   782
    val ty_b = itype_of_ipat b;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   783
  in ICons ((cons_pair, [a, b]), ty_a xx ty_b) end;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   784
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   785
end; (* local *)
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   786
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   787
val prims = [class_eq, type_bool, type_integer, type_float, type_pair, type_list,
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   788
  cons_true, cons_false, cons_pair, cons_nil, cons_cons, fun_primeq, fun_eq, fun_not, fun_and,
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   789
  fun_or, fun_if, fun_fst, fun_snd, fun_add, fun_mult, fun_minus, fun_lt, fun_le, fun_wfrec];
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   790
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   791
fun extract_defs e =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   792
  let
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   793
    fun extr_itype (ty as IType (tyco, _)) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   794
          cons tyco #> fold_itype extr_itype ty
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   795
      | extr_itype ty =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   796
          fold_itype extr_itype ty
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   797
    fun extr_ipat (p as ICons ((c, _), _)) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   798
          cons c #> fold_ipat extr_itype extr_ipat p
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   799
      | extr_ipat p =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   800
          fold_ipat extr_itype extr_ipat p
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   801
    fun extr_iexpr (e as IConst (f, _)) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   802
          cons f #> fold_iexpr extr_itype extr_ipat extr_iexpr e
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   803
      | extr_iexpr e =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   804
          fold_iexpr extr_itype extr_ipat extr_iexpr e
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   805
  in extr_iexpr e [] end;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   806
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   807
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   808
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   809
(** generic transformation **)
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   810
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   811
fun eta_expand query =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   812
  let
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   813
    fun eta_app ((f, ty), es) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   814
      let
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   815
        val delta = query f - length es;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   816
        val add_n = if delta < 0 then 0 else delta;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   817
        val add_vars =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   818
          invent_var_e_names es add_n [] "x" ~~ Library.drop (length es, (fst o unfold_fun) ty);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   819
      in
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   820
        Library.foldr IAbs (add_vars, IConst (f, ty) `$$ es `$$ (map IVarE add_vars))
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   821
      end;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   822
    fun eta_iexpr' e = map_iexpr I I eta_iexpr e
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   823
    and eta_iexpr (IConst (f, ty)) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   824
          eta_app ((f, ty), [])
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   825
      | eta_iexpr (e as IApp _) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   826
          (case (unfold_app e)
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   827
           of (IConst (f, ty), es) =>
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   828
                eta_app ((f, ty), map eta_iexpr es)
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   829
            | _ => eta_iexpr' e)
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   830
      | eta_iexpr e = eta_iexpr' e;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   831
  in map_defs (map_def_fun I eta_iexpr) end;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   832
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   833
fun connect_datatypes_clsdecls module =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   834
  let
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   835
    fun extract_dep (name, Datatypecons (dtname, _)) = 
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   836
          [(dtname, name)]
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   837
      | extract_dep (name, Classmember (cls, _, _)) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   838
          [(cls, name)]
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   839
      | extract_dep (name, def) = []
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   840
  in add_deps extract_dep module end;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   841
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   842
fun tupelize_cons module =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   843
  let
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   844
    fun replace_def (_, (def as Datatypecons (_, []))) acc =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   845
          (def, acc)
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   846
      | replace_def (_, (def as Datatypecons (_, [_]))) acc =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   847
          (def, acc)
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   848
      | replace_def (name, (Datatypecons (tyco, tys))) acc =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   849
          (Datatypecons (tyco,
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   850
            [foldl' (op xx) (NONE, tys)]), name::acc)
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   851
      | replace_def (_, def) acc = (def, acc);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   852
    fun replace_app cs ((f, ty), es) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   853
      if member (op =) cs f
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   854
      then
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   855
        let
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   856
          val (tys, ty') = unfold_fun ty
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   857
        in IConst (f, foldr' (op xx) (tys, NONE) `-> ty') `$ foldl' (op **) (NONE, es) end
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   858
      else IConst (f, ty) `$$ es;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   859
    fun replace_iexpr cs (IConst (f, ty)) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   860
          replace_app cs ((f, ty), [])
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   861
      | replace_iexpr cs (e as IApp _) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   862
          (case unfold_app e
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   863
           of (IConst fty, es) => replace_app cs (fty, map (replace_iexpr cs) es)
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   864
            | _ => map_iexpr I I (replace_iexpr cs) e)
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   865
      | replace_iexpr cs e = map_iexpr I I (replace_iexpr cs) e;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   866
    fun replace_ipat cs (p as ICons ((c, ps), ty)) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   867
          if member (op =) cs c then
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   868
            ICons ((c, [(foldl' (op &&) (NONE, map (replace_ipat cs) ps))]), ty)
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   869
          else map_ipat I (replace_ipat cs) p
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   870
      | replace_ipat cs p = map_ipat I (replace_ipat cs) p;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   871
  in
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   872
    transform_defs replace_def replace_ipat replace_iexpr [cons_cons] module
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   873
  end;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   874
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   875
fun eliminate_dtconstr module =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   876
  let
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   877
    fun replace_def (name, (Datatype (vs, cs, is))) acc =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   878
          (Datatype (map (fn (v, _) => (v, [])) vs, cs, is), (name, vs)::acc)
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   879
      | replace_def (_, def) acc = (def, acc);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   880
    fun constrain (ty as IType _, _) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   881
          ty
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   882
      | constrain (IVarT (v, sort1), (_, sort2)) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   883
          IVarT (v, gen_union (op =) (sort1, sort2));
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   884
    fun replace_ty tycos (ty as (IType (tyco, tys))) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   885
          (case AList.lookup (op =) tycos tyco
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   886
           of NONE => ty
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   887
            | SOME vs => IType (tyco, map2 constrain (tys, vs)))
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   888
      | replace_ty tycos ty =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   889
          map_itype (replace_ty tycos) ty;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   890
  in
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   891
    transform_defs replace_def
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   892
      (*! HIER FEHLT NOCH: ÄNDERN VON TYP UND SORTCTXT BEI FUNS !*)
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   893
      (fn tycos => map_ipat (replace_ty tycos) I)
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   894
      (fn tycos => map_iexpr (replace_ty tycos) (map_ipat (replace_ty tycos) I) I) [] module
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   895
  end;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   896
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   897
fun eliminate_classes module =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   898
  let
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   899
    fun mk_cls_typ_map memberdecls ty_inst =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   900
      map (fn (memname, (v, ty)) =>
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   901
        (memname, ty |> instant_itype (v, ty_inst))) memberdecls;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   902
    fun transform_dicts (Class (supcls, members, insts)) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   903
          let
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   904
            val memberdecls = AList.make
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   905
              ((fn Classmember (_, v, ty) => (v, ty)) o get_def module) members;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   906
            val varname_cls = invent_var_t_names (map (snd o snd) memberdecls) 1 [] "a" |> hd;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   907
          in
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   908
            Typesyn ([(varname_cls, [])], IDictT (mk_cls_typ_map memberdecls (IVarT (varname_cls, []))))
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   909
          end
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   910
      | transform_dicts (Classinst (tyco, (cls, arity), memdefs)) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   911
          let
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   912
            val Class (_, members, _) = get_def module cls;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   913
            val memberdecls = AList.make
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   914
              ((fn Classmember (_, v, ty) => (v, ty)) o get_def module) members;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   915
            val ty_arity = tyco `%% map IVarT (invent_var_t_names (map (snd o snd) memberdecls)
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   916
              (length arity) [] "a" ~~ arity);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   917
            val inst_typ_map = mk_cls_typ_map memberdecls ty_arity;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   918
            val memdefs_ty = map (fn (memname, memprim) =>
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   919
              (memname, (memprim, (the o AList.lookup (op =) inst_typ_map) memname))) memdefs;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   920
          in
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   921
            Fun ([([], IDictE (map (apsnd IConst) memdefs_ty))],
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   922
              ([], IDictT inst_typ_map))
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   923
          end
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   924
      | transform_dicts d = d
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   925
    fun transform_defs (Fun (ds, (sortctxt, ty))) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   926
          let
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   927
            fun reduce f xs = foldl' f (NONE, xs);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   928
            val varnames_ctxt =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   929
              sortctxt
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   930
              |> length o Library.flat o map snd
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   931
              |> (fn used => invent_var_e_names (map snd ds) used ((vars_of_ipats o fst o hd) ds) "d")
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   932
              |> unflat (map snd sortctxt);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   933
            val vname_alist = map2 (fn ((vt, sort), vs) => (vt, vs ~~ sort)) (sortctxt, varnames_ctxt);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   934
            fun add_typarms ty =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   935
              map (reduce (op xx) o (fn (vt, vss) => map (fn (_, cls) => cls `%% [IVarT (vt, [])]) vss)) vname_alist
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   936
                `--> ty;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   937
            fun add_parms ps =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   938
              map (reduce (op &&) o (fn (vt, vss) => map (fn (v, cls) => IVarP (v, cls `%% [IVarT (vt, [])])) vss)) vname_alist
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   939
                @ ps;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   940
            fun transform_itype (IVarT (v, s)) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   941
                  IVarT (v, [])
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   942
              | transform_itype ty =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   943
                  map_itype transform_itype ty;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   944
            fun transform_ipat p =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   945
                  map_ipat transform_itype transform_ipat p;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   946
            fun transform_lookup (ClassPackage.Instance ((cdict, idict), ls)) = 
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   947
                  ls
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   948
                  |> transform_lookups
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   949
                  |-> (fn ty =>
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   950
                        curry mk_apps (IConst (idict, cdict `%% ty))
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   951
                        #> pair (cdict `%% ty))
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   952
              | transform_lookup (ClassPackage.Lookup (deriv, (v, i))) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   953
                  let
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   954
                    val (v', cls) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   955
                      (nth o the oo AList.lookup (op =)) vname_alist v i;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   956
                    fun mk_parm tyco = tyco `%% [IVarT (v, [])];
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   957
                  in (mk_parm (hd (deriv)), ILookup (rev deriv, v')) end
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   958
            and transform_lookups lss =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   959
                  map_yield (map_yield transform_lookup
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   960
                       #> apfst (reduce (op xx))
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   961
                       #> apsnd (reduce (op **))) lss;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   962
            fun transform_iexpr (IInst (e, ls)) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   963
                  transform_iexpr e `$$ (snd o transform_lookups) ls
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   964
              | transform_iexpr e = 
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   965
                  map_iexpr transform_itype transform_ipat transform_iexpr e;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   966
            fun transform_rhs (ps, rhs) = (add_parms ps, transform_iexpr rhs)
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   967
          in Fun (map transform_rhs ds, ([], add_typarms ty)) end
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   968
      | transform_defs d = d
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   969
  in
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   970
    module
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   971
    |> map_defs transform_dicts
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   972
    |> map_defs transform_defs
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   973
  end;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   974
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   975
end; (* struct *)
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   976
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   977
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   978
structure CodegenThingol : CODEGEN_THINGOL =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   979
struct
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   980
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   981
open CodegenThingolOp;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   982
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   983
end; (* struct *)