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