src/Pure/Tools/codegen_thingol.ML
author haftmann
Tue, 27 Dec 2005 15:24:40 +0100
changeset 18515 1cad5c2b2a0b
parent 18454 6720b5010a57
child 18516 4424e2bce9af
permissions -rw-r--r--
substantial improvements in code generating
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
     1
(*  Title:      Pure/Tools/codegen_thingol.ML
18169
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 mk_funs: itype list * itype -> itype;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    29
  val mk_apps: iexpr * iexpr list -> iexpr;
18247
b17724cae935 code generator: case expressions, improved name resolving
haftmann
parents: 18231
diff changeset
    30
  val mk_abss: (vname * itype) list * iexpr -> iexpr;
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    31
  val pretty_itype: itype -> Pretty.T;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    32
  val pretty_ipat: ipat -> Pretty.T;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    33
  val pretty_iexpr: iexpr -> Pretty.T;
18216
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
    34
  val unfoldl: ('a -> ('a * 'b) option) -> 'a -> 'a * 'b list;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
    35
  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
    36
  val unfold_fun: itype -> itype list * itype;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    37
  val unfold_app: iexpr -> iexpr * iexpr list;
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
    38
  val unfold_abs: iexpr -> (vname * itype) list * iexpr;
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    39
  val unfold_let: iexpr -> (ipat * iexpr) list * iexpr;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    40
  val itype_of_iexpr: iexpr -> itype;
18385
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
    41
  val itype_of_ipat: ipat -> itype;
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    42
  val ipat_of_iexpr: iexpr -> ipat;
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
    43
  val eq_itype: itype * itype -> bool;
18304
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
    44
  val tvars_of_itypes: itype list -> string list;
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
    45
  val vars_of_ipats: ipat list -> string list;
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
    46
  val vars_of_iexprs: iexpr list -> string list;
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    47
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    48
  datatype def =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    49
      Nop
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    50
    | Fun of (ipat list * iexpr) list * (ClassPackage.sortcontext * itype)
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
    51
    | Typesyn of (vname * string list) list * itype
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
    52
    | Datatype of (vname * string list) list * (string * itype list) list * string list
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
    53
    | Datatypecons of string
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
    54
    | Class of class list * vname * (string * (ClassPackage.sortcontext * itype)) list * string list
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
    55
    | Classmember of class
18515
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18454
diff changeset
    56
    | Classinst of (class * (string * (vname * sort) list))
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18454
diff changeset
    57
        * ((string * (string * ClassPackage.sortlookup list list)) list
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18454
diff changeset
    58
          * (string * (string * ClassPackage.sortlookup list list)) list);
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    59
  type module;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    60
  type transact;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    61
  type 'dst transact_fin;
18304
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
    62
  type ('src, 'dst) gen_exprgen = 'src -> transact -> 'dst transact_fin;
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    63
  type gen_defgen = string -> transact -> (def * string list) transact_fin;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    64
  val pretty_def: def -> Pretty.T;
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
    65
  val pretty_module: module -> Pretty.T; 
18360
a2c9506b62a7 improved class handling
haftmann
parents: 18335
diff changeset
    66
  val pretty_deps: module -> Pretty.T;
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    67
  val empty_module: module;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    68
  val get_def: module -> string -> def;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    69
  val merge_module: module * module -> module;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    70
  val partof: string list -> module -> module;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    71
  val succeed: 'a -> transact -> 'a transact_fin;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    72
  val fail: string -> transact -> 'a transact_fin;
18304
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
    73
  val gen_invoke: (string * ('src, 'dst) gen_exprgen) list -> string
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    74
    -> 'src -> transact -> 'dst * transact;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    75
  val gen_ensure_def: (string * gen_defgen) list -> string
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    76
    -> string -> transact -> transact;
18216
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
    77
  val start_transact: (transact -> 'a * transact) -> module -> 'a * module;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
    78
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
    79
  val class_eq: string;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
    80
  val type_bool: string;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
    81
  val type_pair: string;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
    82
  val type_list: string;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
    83
  val type_integer: string;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
    84
  val cons_pair: string;
18335
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
    85
  val fun_eq: string;
18216
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
    86
  val fun_fst: string;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
    87
  val fun_snd: string;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
    88
  val Type_integer: itype;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
    89
  val Cons_true: iexpr;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
    90
  val Cons_false: iexpr;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
    91
  val Cons_pair: iexpr;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
    92
  val Cons_nil: iexpr;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
    93
  val Cons_cons: iexpr;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
    94
  val Fun_eq: iexpr;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
    95
  val Fun_not: iexpr;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
    96
  val Fun_and: iexpr;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
    97
  val Fun_or: iexpr;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
    98
  val Fun_if: iexpr;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
    99
  val Fun_fst: iexpr;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
   100
  val Fun_snd: iexpr;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
   101
  val Fun_0: iexpr;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
   102
  val Fun_1: iexpr;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
   103
  val Fun_add: iexpr;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
   104
  val Fun_mult: iexpr;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
   105
  val Fun_minus: iexpr;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
   106
  val Fun_lt: iexpr;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
   107
  val Fun_le: iexpr;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
   108
  val Fun_wfrec: iexpr;
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   109
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   110
  val prims: string list;
18385
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
   111
  val invoke_eq: ('a -> transact -> itype * transact)
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
   112
    -> (string * (def * (string * sort) list) -> transact -> transact)
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
   113
    -> 'a -> transact -> bool * transact;
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   114
  val extract_defs: iexpr -> string list;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   115
  val eta_expand: (string -> int) -> module -> module;
18216
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
   116
  val eta_expand_poly: module -> module;
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   117
  val tupelize_cons: module -> module;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   118
  val eliminate_classes: module -> module;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   119
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   120
  val debug_level : int ref;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   121
  val debug : int -> ('a -> string) -> 'a -> 'a;
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   122
  val soft_exc: bool ref;
18216
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
   123
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
   124
  val serialize:
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   125
    ((string -> string) -> (string * def) list -> Pretty.T option)
18216
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
   126
    -> (string * Pretty.T list -> Pretty.T)
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
   127
    -> (string -> string option)
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
   128
    -> string list list -> string -> module -> Pretty.T
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18441
diff changeset
   129
6720b5010a57 slight improvements in name handling
haftmann
parents: 18441
diff changeset
   130
  val get_prefix: ('a * 'a -> bool) -> 'a list * 'a list -> 'a list * ('a list * 'a list)
18169
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
signature CODEGEN_THINGOL_OP =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   134
sig
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   135
  include CODEGEN_THINGOL;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   136
  val `%% : string * itype list -> itype;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   137
  val `-> : itype * itype -> itype;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   138
  val `--> : itype list * itype -> itype;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   139
  val `$ : iexpr * iexpr -> iexpr;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   140
  val `$$ : iexpr * iexpr list -> iexpr;
18247
b17724cae935 code generator: case expressions, improved name resolving
haftmann
parents: 18231
diff changeset
   141
  val `|-> : (vname * itype) * iexpr -> iexpr;
b17724cae935 code generator: case expressions, improved name resolving
haftmann
parents: 18231
diff changeset
   142
  val `|--> : (vname * itype) list * iexpr -> iexpr;
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   143
end;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   144
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   145
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   146
structure CodegenThingolOp: CODEGEN_THINGOL_OP =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   147
struct
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   148
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   149
(** auxiliary **)
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   150
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   151
val debug_level = ref 0;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   152
fun debug d f x = (if d <= !debug_level then Output.debug (f x) else (); x);
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   153
val soft_exc = ref true;
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   154
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   155
fun unfoldl dest x =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   156
  case dest x
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   157
   of NONE => (x, [])
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   158
    | SOME (x1, x2) =>
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   159
        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
   160
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   161
fun unfoldr dest x =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   162
  case dest x
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   163
   of NONE => ([], x)
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   164
    | SOME (x1, x2) =>
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   165
        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
   166
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   167
fun map_yield f [] = ([], [])
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   168
  | map_yield f (x::xs) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   169
      let
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   170
        val (y, x') = f x
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   171
        val (ys, xs') = map_yield f xs
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   172
      in (y::ys, x'::xs') end;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   173
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18441
diff changeset
   174
fun get_prefix eq ([], ys) = ([], ([], ys))
6720b5010a57 slight improvements in name handling
haftmann
parents: 18441
diff changeset
   175
  | get_prefix eq (xs, []) = ([], (xs, []))
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   176
  | get_prefix eq (xs as x::xs', ys as y::ys') =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   177
      if eq (x, y) then
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18441
diff changeset
   178
        let val (ps', xys'') = get_prefix eq (xs', ys')
6720b5010a57 slight improvements in name handling
haftmann
parents: 18441
diff changeset
   179
        in (x::ps', xys'') end
6720b5010a57 slight improvements in name handling
haftmann
parents: 18441
diff changeset
   180
      else ([], (xs, ys));
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   181
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   182
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   183
(** language core - types, pattern, expressions **)
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   184
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   185
(* language representation *)
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   186
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   187
infix 8 `%%;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   188
infixr 6 `->;
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
infix 4 `$;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   191
infix 4 `$$;
18247
b17724cae935 code generator: case expressions, improved name resolving
haftmann
parents: 18231
diff changeset
   192
infixr 5 `|->;
b17724cae935 code generator: case expressions, improved name resolving
haftmann
parents: 18231
diff changeset
   193
infixr 5 `|-->;
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   194
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   195
type vname = string;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   196
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   197
datatype itype =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   198
    IType of string * itype list
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   199
  | IFun of itype * itype
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   200
  | IVarT of vname * sort
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   201
    (*ML auxiliary*)
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   202
  | IDictT of (string * itype) list;
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   203
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   204
datatype ipat =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   205
    ICons of (string * ipat list) * itype
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   206
  | IVarP of vname * itype;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   207
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   208
datatype iexpr =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   209
    IConst of string * itype
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   210
  | IVarE of vname * itype
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   211
  | IApp of iexpr * iexpr
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   212
  | IInst of iexpr * ClassPackage.sortlookup list list
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   213
  | IAbs of (vname * itype) * iexpr
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   214
  | ICase of iexpr * (ipat * iexpr) list
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   215
    (*ML auxiliary*)
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   216
  | IDictE of (string * iexpr) list
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   217
  | ILookup of (string list * vname);
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   218
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   219
(*
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   220
  variable naming conventions
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   221
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   222
  bare names:
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   223
    variable names          v
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   224
    class names             cls
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   225
    type constructor names  tyco
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   226
    datatype names          dtco
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   227
    const names (general)   c
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   228
    constructor names       co
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   229
    class member names      m
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   230
    arbitrary name          s
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   231
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   232
  constructs:
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   233
    sort                    sort
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   234
    type                    ty
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   235
    expression              e
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   236
    pattern                 p
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   237
    instance (cls, tyco)    inst
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   238
    variable (v, ty)        var
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   239
    class member (m, ty)    membr
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   240
    constructors (co, tys)  constr
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   241
 *)
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   242
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   243
val mk_funs = Library.foldr IFun;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   244
val mk_apps = Library.foldl IApp;
18247
b17724cae935 code generator: case expressions, improved name resolving
haftmann
parents: 18231
diff changeset
   245
val mk_abss = Library.foldr IAbs;
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   246
18247
b17724cae935 code generator: case expressions, improved name resolving
haftmann
parents: 18231
diff changeset
   247
val op `%% = IType;
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   248
val op `-> = IFun;
18247
b17724cae935 code generator: case expressions, improved name resolving
haftmann
parents: 18231
diff changeset
   249
val op `$ = IApp;
b17724cae935 code generator: case expressions, improved name resolving
haftmann
parents: 18231
diff changeset
   250
val op `|-> = IAbs;
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   251
val op `--> = mk_funs;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   252
val op `$$ = mk_apps;
18247
b17724cae935 code generator: case expressions, improved name resolving
haftmann
parents: 18231
diff changeset
   253
val op `|--> = mk_abss;
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   254
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   255
val unfold_fun = unfoldr
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   256
  (fn IFun t => SOME t
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   257
    | _ => NONE);
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   258
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   259
val unfold_app = unfoldl
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   260
  (fn IApp e => SOME e
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   261
    | _ => NONE);
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   262
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   263
val unfold_abs = unfoldr
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   264
  (fn IAbs b => SOME b
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   265
    | _ => NONE)
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   266
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   267
val unfold_let = unfoldr
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   268
  (fn ICase (e, [(p, e')]) => SOME ((p, e), e')
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   269
    | _ => NONE);
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   270
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   271
fun map_itype f_itype (IType (tyco, tys)) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   272
      tyco `%% map f_itype tys
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   273
  | map_itype f_itype (IFun (t1, t2)) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   274
      f_itype t1 `-> f_itype t2
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   275
  | map_itype _ (ty as IVarT _) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   276
      ty;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   277
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   278
fun map_ipat f_itype f_ipat (ICons ((c, ps), ty)) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   279
      ICons ((c, map f_ipat ps), f_itype ty)
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   280
  | map_ipat _ _ (p as IVarP _) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   281
      p;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   282
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   283
fun map_iexpr f_itype f_ipat f_iexpr (IApp (e1, e2)) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   284
      f_iexpr e1 `$ f_iexpr e2
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   285
  | map_iexpr f_itype f_ipat f_iexpr (IInst (e, c)) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   286
      IInst (f_iexpr e, c)
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   287
  | map_iexpr f_itype f_ipat f_iexpr (IAbs (v, e)) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   288
      IAbs (v, f_iexpr e)
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   289
  | map_iexpr f_itype f_ipat f_iexpr (ICase (e, ps)) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   290
      ICase (f_iexpr e, map (fn (p, e) => (f_ipat p, f_iexpr e)) ps)
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   291
  | map_iexpr _ _ _ (e as IConst _) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   292
      e
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   293
  | map_iexpr _ _ _ (e as IVarE _) =
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   294
      e
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   295
  | map_iexpr f_itype f_ipat f_iexpr (IDictE ms) =
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   296
      IDictE (map (apsnd f_iexpr) ms)
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   297
  | map_iexpr _ _ _ (e as ILookup _) =
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   298
      e ;
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   299
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   300
fun fold_itype f_itype (IFun (t1, t2)) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   301
      f_itype t1 #> f_itype t2
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   302
  | fold_itype _ (ty as IType _) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   303
      I
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   304
  | fold_itype _ (ty as IVarT _) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   305
      I;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   306
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   307
fun fold_ipat f_itype f_ipat (ICons ((_, ps), ty)) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   308
      f_itype ty #> fold f_ipat ps
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   309
  | fold_ipat f_itype f_ipat (p as IVarP _) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   310
      I;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   311
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   312
fun fold_iexpr f_itype f_ipat f_iexpr (IApp (e1, e2)) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   313
      f_iexpr e1 #> f_iexpr e2
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   314
  | fold_iexpr f_itype f_ipat f_iexpr (IInst (e, c)) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   315
      f_iexpr e
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   316
  | fold_iexpr f_itype f_ipat f_iexpr (IAbs (v, e)) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   317
      f_iexpr e
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   318
  | fold_iexpr f_itype f_ipat f_iexpr (ICase (e, ps)) =
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   319
      f_iexpr e #> fold (fn (p, e) => f_ipat p #> f_iexpr e) ps
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   320
  | fold_iexpr _ _ _ (e as IConst _) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   321
      I
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   322
  | fold_iexpr _ _ _ (e as IVarE _) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   323
      I;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   324
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   325
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   326
(* simple type matching *)
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   327
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   328
fun eq_itype (ty1, ty2) =
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   329
  let
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   330
    exception NO_MATCH;
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   331
    fun eq (IVarT (v1, sort1)) (IVarT (v2, sort2)) subs =
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   332
          if sort1 <> sort2
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   333
          then raise NO_MATCH
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   334
          else
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   335
            (case AList.lookup (op =) subs v1
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   336
             of NONE => subs |> AList.update (op =) (v1, v2)
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   337
              | (SOME v1') =>
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   338
                  if v1' <> v2
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   339
                  then raise NO_MATCH
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   340
                  else subs)
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   341
      | eq (IType (tyco1, tys1)) (IType (tyco2, tys2)) subs =
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   342
          if tyco1 <> tyco2
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   343
          then raise NO_MATCH
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   344
          else subs |> fold2 eq tys1 tys2
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   345
      | eq (IFun (ty11, ty12)) (IFun (ty21, ty22)) subs =
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   346
          subs |> eq ty11 ty21 |> eq ty12 ty22
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   347
      | eq _ _ _ = raise NO_MATCH;
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   348
  in
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   349
    (eq ty1 ty2 []; true)
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   350
    handle NO_MATCH => false
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   351
  end;
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   352
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   353
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   354
(* simple diagnosis *)
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   355
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   356
fun pretty_itype (IType (tyco, tys)) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   357
      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
   358
  | pretty_itype (IFun (ty1, ty2)) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   359
      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
   360
  | pretty_itype (IVarT (v, sort)) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   361
      Pretty.str (v ^ enclose "|" "|" (space_implode "|" sort))
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   362
  | pretty_itype (IDictT _) =
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   363
      Pretty.str "<DictT>";
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   364
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   365
fun pretty_ipat (ICons ((cons, ps), ty)) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   366
      Pretty.gen_list " " "(" ")"
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   367
        (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
   368
  | pretty_ipat (IVarP (v, ty)) =
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   369
      Pretty.block [Pretty.str ("?" ^ v ^ "::"), pretty_itype ty];
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   370
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   371
fun pretty_iexpr (IConst (f, ty)) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   372
      Pretty.block [Pretty.str (f ^ "::"), pretty_itype ty]
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   373
  | pretty_iexpr (IVarE (v, ty)) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   374
      Pretty.block [Pretty.str ("?" ^ v ^ "::"), pretty_itype ty]
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   375
  | pretty_iexpr (IApp (e1, e2)) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   376
      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
   377
  | pretty_iexpr (IInst (e, c)) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   378
      pretty_iexpr e
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   379
  | pretty_iexpr (IAbs ((v, ty), e)) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   380
      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
   381
  | pretty_iexpr (ICase (e, cs)) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   382
      Pretty.enclose "(" ")" [
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   383
        Pretty.str "case ",
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   384
        pretty_iexpr e,
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   385
        Pretty.enclose "(" ")" (map (fn (p, e) =>
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   386
          Pretty.block [
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   387
            pretty_ipat p,
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   388
            Pretty.str " => ",
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   389
            pretty_iexpr e
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   390
          ]
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   391
        ) cs)
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   392
      ]
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   393
  | pretty_iexpr (IDictE _) =
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   394
      Pretty.str "<DictE>"
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   395
  | pretty_iexpr (ILookup (ls, v)) =
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   396
      Pretty.str ("<Lookup: " ^ commas ls ^ " in " ^ v ^ ">");
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   397
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   398
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   399
(* language auxiliary *)
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   400
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   401
fun itype_of_iexpr (IConst (_, ty)) = ty
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   402
  | itype_of_iexpr (IVarE (_, ty)) = ty
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   403
  | 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
   404
      of (IFun (ty2, ty')) =>
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   405
            if ty2 = itype_of_iexpr e2
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   406
            then ty'
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   407
            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
   408
              ^ ", " ^ (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
   409
       | _ => error ("expression is not a function: " ^ Pretty.output (pretty_iexpr e1)))
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   410
  | itype_of_iexpr (IInst (e, cs)) = itype_of_iexpr e
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   411
  | 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
   412
  | itype_of_iexpr (ICase ((_, [(_, e)]))) = itype_of_iexpr e;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   413
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   414
fun itype_of_ipat (ICons (_, ty)) = ty
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   415
  | itype_of_ipat (IVarP (_, ty)) = ty;
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   416
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   417
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
   418
  | ipat_of_iexpr (IVarE v) = IVarP v
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   419
  | ipat_of_iexpr (e as IApp _) =
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   420
      (case unfold_app e
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   421
        of (IConst (f, ty), es) =>
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   422
              ICons ((f, map ipat_of_iexpr es), (snd o unfold_fun) ty)
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   423
         | (IInst (IConst (f, ty), _), es) =>
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   424
              ICons ((f, map ipat_of_iexpr es), (snd o unfold_fun) ty)
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   425
         | _ => error ("illegal expression for pattern: " ^ (Pretty.output o pretty_iexpr) e))
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   426
  | ipat_of_iexpr e =
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   427
      error ("illegal expression for pattern: " ^ (Pretty.output o pretty_iexpr) e);
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   428
18304
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   429
fun tvars_of_itypes tys =
18216
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
   430
  let
18304
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   431
    fun vars (IType (_, tys)) =
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   432
          fold vars tys
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   433
      | vars (IFun (ty1, ty2)) =
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   434
          vars ty1 #> vars ty2
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   435
      | vars (IVarT (v, _)) =
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   436
          insert (op =) v
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   437
  in fold vars tys [] end;
18216
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
   438
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   439
fun vars_of_ipats ps =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   440
  let
18304
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   441
    fun vars (ICons ((_, ps), _)) =
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   442
          fold vars ps
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   443
      | vars (IVarP (v, _)) =
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   444
          insert (op =) v
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   445
  in fold vars ps [] end;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   446
18304
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   447
fun vars_of_iexprs es =
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   448
  let
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   449
    fun vars (IConst (f, _)) =
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   450
          I
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   451
      | vars (IVarE (v, _)) =
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   452
          insert (op =) v
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   453
      | vars (IApp (e1, e2)) =
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   454
          vars e1 #> vars e2
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   455
      | vars (IAbs ((v, _), e)) =
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   456
          insert (op =) v
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   457
          #> vars e
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   458
      | vars (ICase (e, cs)) =
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   459
          vars e
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   460
          #> fold (fn (p, e) => fold (insert (op =)) (vars_of_ipats [p]) #> vars e) cs
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   461
      | vars (IInst (e, lookup)) =
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   462
          vars e
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   463
      | vars (IDictE ms) =
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   464
          fold (vars o snd) ms
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   465
      | vars (ILookup (_, v)) =
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   466
          cons v
18304
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   467
  in fold vars es [] end;
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   468
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   469
fun instant_itype (v, sty) ty =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   470
  let
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   471
    fun instant (IType (tyco, tys)) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   472
          tyco `%% map instant tys
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   473
      | instant (IFun (ty1, ty2)) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   474
          instant ty1 `-> instant ty2
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   475
      | instant (w as (IVarT (u, _))) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   476
          if v = u then sty else w
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   477
  in instant ty end;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   478
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   479
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   480
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   481
(** language module system - definitions, modules, transactions **)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   482
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   483
(* type definitions *)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   484
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   485
datatype def =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   486
    Nop
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   487
  | Fun of (ipat list * iexpr) list * (ClassPackage.sortcontext * itype)
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   488
  | Typesyn of (vname * string list) list * itype
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   489
  | Datatype of (vname * string list) list * (string * itype list) list * string list
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   490
  | Datatypecons of string
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   491
  | Class of class list * vname * (string * (ClassPackage.sortcontext * itype)) list * string list
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   492
  | Classmember of class
18515
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18454
diff changeset
   493
  | Classinst of (class * (string * (vname * sort) list))
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18454
diff changeset
   494
      * ((string * (string * ClassPackage.sortlookup list list)) list
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18454
diff changeset
   495
        * (string * (string * ClassPackage.sortlookup list list)) list);
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   496
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   497
datatype node = Def of def | Module of node Graph.T;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   498
type module = node Graph.T;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   499
type transact = Graph.key list * module;
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   500
datatype 'dst transact_res = Succeed of 'dst | Fail of string list * exn option;
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   501
type 'dst transact_fin = 'dst transact_res * transact;
18304
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   502
type ('src, 'dst) gen_exprgen = 'src -> transact -> 'dst transact_fin;
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   503
type gen_defgen = string -> transact -> (def * string list) transact_fin;
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   504
exception FAIL of string list * exn option;
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   505
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   506
val eq_def = (op =);
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   507
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   508
(* simple diagnosis *)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   509
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   510
fun pretty_def Nop =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   511
      Pretty.str "<NOP>"
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   512
  | pretty_def (Fun (eqs, (_, ty))) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   513
      Pretty.gen_list " |" "" "" (
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   514
        map (fn (ps, body) =>
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   515
          Pretty.block [
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   516
            Pretty.gen_list "," "[" "]" (map pretty_ipat ps),
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   517
            Pretty.str " |->",
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   518
            Pretty.brk 1,
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   519
            pretty_iexpr body,
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   520
            Pretty.str "::",
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   521
            pretty_itype ty
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   522
          ]) eqs
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   523
        )
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   524
  | pretty_def (Typesyn (vs, ty)) =
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   525
      Pretty.block [
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   526
        Pretty.list "(" ")" (map (pretty_itype o IVarT) vs),
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   527
        Pretty.str " |=> ",
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   528
        pretty_itype ty
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   529
      ]
18335
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   530
  | pretty_def (Datatype (vs, cs, insts)) =
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   531
      Pretty.block [
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   532
        Pretty.list "(" ")" (map (pretty_itype o IVarT) vs),
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   533
        Pretty.str " |=> ",
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   534
        Pretty.gen_list " |" "" ""
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   535
          (map (fn (c, tys) => (Pretty.block o Pretty.breaks) (Pretty.str c :: map pretty_itype tys)) cs),
18335
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   536
        Pretty.str ", instances ",
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   537
        Pretty.gen_list "," "[" "]" (map Pretty.str insts)
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   538
      ]
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   539
  | pretty_def (Datatypecons dtname) =
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   540
      Pretty.str ("cons " ^ dtname)
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   541
  | pretty_def (Class (supcls, v, mems, insts)) =
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   542
      Pretty.block [
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   543
        Pretty.str ("class var " ^ v ^ "extending "),
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   544
        Pretty.gen_list "," "[" "]" (map Pretty.str supcls),
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   545
        Pretty.str " with ",
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   546
        Pretty.gen_list "," "[" "]"
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   547
          (map (fn (m, (_, ty)) => Pretty.block [Pretty.str (m ^ "::"), pretty_itype ty]) mems),
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   548
        Pretty.str " instances ",
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   549
        Pretty.gen_list "," "[" "]" (map Pretty.str insts)
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   550
      ]
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   551
  | pretty_def (Classmember clsname) =
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   552
      Pretty.block [
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   553
        Pretty.str "class member belonging to ",
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   554
        Pretty.str clsname
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   555
      ]
18515
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18454
diff changeset
   556
  | pretty_def (Classinst ((clsname, (tyco, arity)), _)) =
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   557
      Pretty.block [
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   558
        Pretty.str "class instance (",
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   559
        Pretty.str clsname,
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   560
        Pretty.str ", (",
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   561
        Pretty.str tyco,
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   562
        Pretty.str ", ",
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   563
        Pretty.gen_list "," "[" "]" (map (Pretty.gen_list "," "{" "}" o map Pretty.str o snd) arity),
18515
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18454
diff changeset
   564
        Pretty.str "))"
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   565
      ];
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   566
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   567
fun pretty_module modl =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   568
  let
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   569
    fun pretty (name, Module modl) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   570
          Pretty.block (
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   571
            Pretty.str ("module " ^ name ^ " {")
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   572
            :: Pretty.brk 1
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   573
            :: Pretty.chunks (map pretty (AList.make (Graph.get_node modl)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   574
                 (Graph.strong_conn modl |> List.concat |> rev)))
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   575
            :: Pretty.str "}" :: nil
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   576
          )
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   577
      | pretty (name, Def def) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   578
          Pretty.block [Pretty.str name, Pretty.str " :=", Pretty.brk 1, pretty_def def]
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   579
  in pretty ("//", Module modl) end;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   580
18360
a2c9506b62a7 improved class handling
haftmann
parents: 18335
diff changeset
   581
fun pretty_deps modl =
a2c9506b62a7 improved class handling
haftmann
parents: 18335
diff changeset
   582
  let
a2c9506b62a7 improved class handling
haftmann
parents: 18335
diff changeset
   583
    fun one_node key =
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   584
      let
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   585
        val preds_ = Graph.imm_preds modl key;
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   586
        val succs_ = Graph.imm_succs modl key;
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   587
        val mutbs = gen_inter (op =) (preds_, succs_);
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   588
        val preds = fold (remove (op =)) mutbs preds_;
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   589
        val succs = fold (remove (op =)) mutbs succs_;
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   590
      in
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   591
        (Pretty.block o Pretty.fbreaks) (
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   592
          Pretty.str key
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   593
          :: map (fn s => Pretty.str ("<-> " ^ s)) mutbs
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   594
          @ map (fn s => Pretty.str ("<-- " ^ s)) preds
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   595
          @ map (fn s => Pretty.str ("--> " ^ s)) succs
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   596
          @ (the_list oo Option.mapPartial) ((fn Module modl' => SOME (pretty_deps modl') | _ => NONE) o Graph.get_node modl) (SOME key)
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   597
        )
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   598
      end
18360
a2c9506b62a7 improved class handling
haftmann
parents: 18335
diff changeset
   599
  in
a2c9506b62a7 improved class handling
haftmann
parents: 18335
diff changeset
   600
    modl
a2c9506b62a7 improved class handling
haftmann
parents: 18335
diff changeset
   601
    |> Graph.strong_conn
a2c9506b62a7 improved class handling
haftmann
parents: 18335
diff changeset
   602
    |> List.concat
a2c9506b62a7 improved class handling
haftmann
parents: 18335
diff changeset
   603
    |> rev
a2c9506b62a7 improved class handling
haftmann
parents: 18335
diff changeset
   604
    |> map one_node
a2c9506b62a7 improved class handling
haftmann
parents: 18335
diff changeset
   605
    |> Pretty.chunks
a2c9506b62a7 improved class handling
haftmann
parents: 18335
diff changeset
   606
  end;
a2c9506b62a7 improved class handling
haftmann
parents: 18335
diff changeset
   607
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   608
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   609
(* name handling *)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   610
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   611
fun dest_name name =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   612
  let
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   613
    val name' = NameSpace.unpack name
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   614
    val (name'', name_base) = split_last name'
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   615
    val (modl, shallow) = split_last name''
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   616
  in (modl, NameSpace.pack [shallow, name_base]) end
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   617
  handle Empty => error ("not a qualified name: " ^ quote name);
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   618
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   619
fun dest_modl (Module m) = m;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   620
fun dest_def (Def d) = d;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   621
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   622
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   623
(* modules *)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   624
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   625
val empty_module = Graph.empty; (*read: "depends on"*)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   626
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   627
fun get_def modl name =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   628
  case dest_name name
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   629
   of (modlname, base) =>
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   630
        let
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   631
          fun get (Module node) [] =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   632
                (dest_def o Graph.get_node node) base
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   633
            | get (Module node) (m::ms) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   634
                get (Graph.get_node node m) ms
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   635
        in get (Module modl) modlname end;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   636
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   637
fun add_def (name, def) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   638
  let
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   639
    val (modl, base) = dest_name name;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   640
    fun add [] =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   641
          Graph.new_node (base, Def def)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   642
      | add (m::ms) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   643
          Graph.default_node (m, Module empty_module)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   644
          #> Graph.map_node m (Module o add ms o dest_modl)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   645
  in add modl end;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   646
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   647
fun map_def name f =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   648
  let
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   649
    val (modl, base) = dest_name name;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   650
    fun mapp [] =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   651
          Graph.map_node base (Def o f o dest_def)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   652
      | mapp (m::ms) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   653
          Graph.map_node m (Module o mapp ms o dest_modl)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   654
  in mapp modl end;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   655
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   656
fun add_dep (name1, name2) modl =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   657
  if name1 = name2 then modl
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   658
  else
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   659
    let
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   660
      val m1 = dest_name name1 |> apsnd single |> (op @);
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   661
      val m2 = dest_name name2 |> apsnd single |> (op @);
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18441
diff changeset
   662
      val (ms, (r1, r2)) = get_prefix (op =) (m1, m2);
6720b5010a57 slight improvements in name handling
haftmann
parents: 18441
diff changeset
   663
      val (ms, (s1::r1, s2::r2)) = get_prefix (op =) (m1, m2);
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   664
      val add_edge =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   665
        if null r1 andalso null r2
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   666
        then Graph.add_edge
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   667
        else Graph.add_edge_acyclic
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   668
      fun add [] node =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   669
            node
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   670
            |> add_edge (s1, s2)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   671
        | add (m::ms) node =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   672
            node
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   673
            |> Graph.map_node m (Module o add ms o dest_modl);
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   674
    in add ms modl end;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   675
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   676
fun map_defs f =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   677
  let
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   678
    fun mapp (Def def) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   679
          (Def o f) def
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   680
      | mapp (Module modl) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   681
          (Module o Graph.map_nodes mapp) modl
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   682
  in dest_modl o mapp o Module end;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   683
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   684
fun fold_defs f =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   685
  let
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   686
    fun fol prfix (name, Def def) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   687
          f (NameSpace.pack (prfix @ [name]), def)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   688
      | fol prfix (name, Module modl) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   689
          Graph.fold_nodes (fol (prfix @ [name])) modl
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   690
  in Graph.fold_nodes (fol []) end;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   691
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   692
fun add_deps f modl =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   693
  modl
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   694
  |> fold add_dep ([] |> fold_defs (append o f) modl);
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   695
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   696
fun fold_map_defs f =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   697
  let
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   698
    fun foldmap prfix (name, Def def) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   699
          apfst Def o f (NameSpace.pack (prfix @ [name]), def)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   700
      | foldmap prfix (name, Module modl) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   701
          apfst Module o Graph.fold_map_nodes (foldmap (prfix @ [name])) modl
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   702
  in Graph.fold_map_nodes (foldmap []) end;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   703
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   704
fun map_def_fun f_ipat f_iexpr (Fun (eqs, cty)) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   705
      Fun (map (fn (ps, rhs) => (map f_ipat ps, f_iexpr rhs)) eqs, cty)
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   706
  | map_def_fun _ _ def = def;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   707
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   708
fun transform_defs f_def f_ipat f_iexpr s modl =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   709
  let
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   710
    val (modl', s') = fold_map_defs f_def modl s
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   711
  in
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   712
    modl'
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   713
    |> map_defs (map_def_fun (f_ipat s') (f_iexpr s'))
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   714
  end;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   715
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   716
fun merge_module modl12 =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   717
  let
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   718
    fun join_module (Module m1, Module m2) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   719
          (SOME o Module) (merge_module (m1, m2))
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   720
      | join_module (Def d1, Def d2) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   721
          if eq_def (d1, d2) then (SOME o Def) d1 else NONE
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   722
      | join_module _ =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   723
          NONE
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   724
  in Graph.join (K join_module) modl12 end;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   725
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   726
fun partof names modl =
18335
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   727
  let
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   728
    datatype pathnode = PN of (string list * (string * pathnode) list);
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   729
    fun mk_ipath ([], base) (PN (defs, modls)) =
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   730
          PN (base :: defs, modls)
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   731
      | mk_ipath (n::ns, base) (PN (defs, modls)) =
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   732
          modls
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   733
          |> AList.default (op =) (n, PN ([], []))
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   734
          |> AList.map_entry (op =) n (mk_ipath (ns, base))
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   735
          |> (pair defs #> PN);
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   736
    fun select (PN (defs, modls)) (Module module) =
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   737
      module
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   738
      |> Graph.subgraph (Graph.all_succs module (defs @ map fst modls))
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   739
      |> fold (fn (name, modls) => Graph.map_node name (select modls)) modls
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   740
      |> Module;
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   741
  in
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   742
    Module modl
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   743
    |> select (fold (mk_ipath o dest_name) (filter NameSpace.is_qualified names) (PN ([], [])))
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   744
    |> dest_modl
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   745
  end;
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   746
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   747
fun (*add_check_transform (name, (Datatypecons dtname)) =
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   748
      (debug 7 (fn _ => "transformation for datatype constructor " ^ quote name
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   749
        ^ " of datatype " ^ quote dtname) ();
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   750
      ([([dtname],
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   751
          fn [Datatype (_, _, [])] => NONE
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   752
            | _ => "attempted to add constructor to already instantiating datatype" |> SOME)],
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   753
       [(dtname,
18335
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   754
          fn Datatype (vs, cs, insts) => Datatype (vs, name::cs, insts)
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   755
           | def => "attempted to add datatype constructor to non-datatype: "
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   756
              ^ (Pretty.output o pretty_def) def |> error)])
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   757
      )
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   758
  | add_check_transform (name, Classmember (clsname, v, ty)) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   759
      let
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   760
        val _ = debug 7 (fn _ => "transformation for class member " ^ quote name
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   761
        ^ " of class " ^ quote clsname) ();
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   762
        fun check_var (IType (tyco, tys)) s =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   763
              fold check_var tys s
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   764
          | check_var (IFun (ty1, ty2)) s =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   765
              s
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   766
              |> check_var ty1
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   767
              |> check_var ty2
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   768
          | check_var (IVarT (w, sort)) s =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   769
              if v = w
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   770
              andalso member (op =) sort clsname
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   771
              then "additional class appears at type variable" |> SOME
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   772
              else NONE
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   773
      in
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   774
        ([([], fn [] => check_var ty NONE),
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   775
          ([clsname],
18304
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   776
             fn [Class (_, _, _, [])] => NONE
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   777
              | _ => "attempted to add class member to witnessed class" |> SOME)],
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   778
         [(clsname,
18304
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   779
             fn Class (supcs, v, mems, insts) => Class (supcs, v, name::mems, insts)
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   780
              | def => "attempted to add class member to non-class"
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   781
                 ^ (Pretty.output o pretty_def) def |> error)])
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   782
      end
18515
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18454
diff changeset
   783
  | *) add_check_transform (name, Classinst ((clsname, (tyco, arity)), (_, memdefs))) =
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   784
      let
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   785
        val _ = debug 7 (fn _ => "transformation for class instance " ^ quote tyco
18515
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18454
diff changeset
   786
          ^ " of class " ^ quote clsname) ();
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   787
        (* fun check [Classmember (_, v, mtyp_c), Fun (_, (_, mtyp_i))] =
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   788
              let
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   789
                val mtyp_i' = instant_itype (v, tyco `%% map IVarT arity) mtyp_c;
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   790
              in if eq_itype (mtyp_i', mtyp_i)
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   791
                then NONE
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   792
                else "wrong type signature for class member: "
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   793
                  ^ (Pretty.output o pretty_itype) mtyp_i' ^ " expected, "
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   794
                  ^ (Pretty.output o pretty_itype) mtyp_i ^ " given" |> SOME
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   795
              end
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   796
          | check defs =
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   797
              "non-well-formed definitions encountered for classmembers: "
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   798
              ^ (commas o map (quote o Pretty.output o pretty_def)) defs |> SOME *)
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   799
      in
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   800
        ((* map (fn (memname, memprim) => ([memname, memprim], check)) memdefs*) [],
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   801
          [(clsname,
18304
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   802
              fn Class (supcs, v, mems, insts) => Class (supcs, v, mems, name::insts)
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   803
               | def => "attempted to add class instance to non-class"
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   804
                  ^ (Pretty.output o pretty_def) def |> error),
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   805
           (tyco,
18335
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   806
              fn Datatype (vs, cs, insts) => Datatype (vs, cs, name::insts)
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   807
               | Nop => Nop
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   808
               | def => "attempted to instantiate non-type to class instance"
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   809
                  ^ (Pretty.output o pretty_def) def |> error)])
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   810
      end
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   811
  | add_check_transform _ = ([], []);
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   812
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   813
(* checks to be implemented here lateron:
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   814
    - well-formedness of function equations
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   815
    - only possible to add defined constructors and class members
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   816
    - right type abstraction with class members
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   817
    - correct typing of instance definitions
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   818
*)
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   819
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   820
fun succeed some = pair (Succeed some);
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   821
fun fail msg = pair (Fail ([msg], NONE));
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   822
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   823
fun check_fail _ (Succeed dst, trns) = (dst, trns)
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   824
  | check_fail msg (Fail (msgs, e), _) = raise FAIL (msg::msgs, e);
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   825
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   826
fun select_generator _ _ [] modl =
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   827
      ([], modl) |> fail ("no code generator available")
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   828
  | select_generator mk_msg src gens modl =
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   829
      let
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   830
        fun handle_fail msgs f =
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   831
          let
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   832
            in
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   833
              if ! soft_exc
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   834
              then
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   835
                ([], modl) |> f
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   836
                handle FAIL exc => (Fail exc, ([], modl))
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   837
                     | e => (Fail (msgs, SOME e), ([], modl))
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   838
              else
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   839
                ([], modl) |> f
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   840
                handle FAIL exc => (Fail exc, ([], modl))
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   841
            end;
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   842
        fun select msgs [(gname, gen)] =
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   843
          handle_fail (msgs @ [mk_msg gname]) (gen src)
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   844
        fun select msgs ((gname, gen)::gens) =
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   845
          let
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   846
            val msgs' = msgs @ [mk_msg gname]
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   847
          in case handle_fail msgs' (gen src)
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   848
          of (Fail (_, NONE), _) =>
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   849
               select msgs' gens
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   850
           | result =>
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   851
               result
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   852
          end;
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   853
      in select [] gens end;
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   854
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   855
fun gen_invoke codegens msg src (deps, modl) =
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   856
  modl
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   857
  |> select_generator (fn gname => "trying code generator " ^ gname ^ " for source " ^ quote msg)
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   858
       src codegens
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   859
  |> check_fail msg
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   860
  ||> (fn (deps', modl') => (append deps' deps, modl'));
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   861
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   862
fun gen_ensure_def defgens msg name (deps, modl) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   863
  let
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   864
    fun add (name, def) (deps, modl) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   865
      let
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   866
        val (checks, trans) = add_check_transform (name, def);
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   867
        fun check (check_defs, checker) modl =
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   868
          let
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   869
            fun get_def' s =
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   870
              if NameSpace.is_qualified s
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   871
              then get_def modl s
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   872
              else Nop
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   873
            val defs =
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   874
              check_defs
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   875
              |> map get_def';
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   876
          in
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   877
            case checker defs
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   878
             of NONE => modl
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   879
              | SOME msg => raise FAIL ([msg], NONE)
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   880
          end;
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   881
        fun transform (name, f) modl =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   882
          modl
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   883
          |> debug 9 (fn _ => "transforming node " ^ name)
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   884
          |> (if NameSpace.is_qualified name then map_def name f else I);
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   885
      in
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   886
        modl
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   887
        |> debug 10 (fn _ => "considering addition of " ^ name
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   888
             ^ " := " ^ (Pretty.output o pretty_def) def)
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   889
        |> debug 10 (fn _ => "consistency checks")
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   890
        |> fold check checks
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   891
        |> debug 10 (fn _ => "dependencies")
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   892
        |> fold (curry add_dep name) deps
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   893
        |> debug 10 (fn _ => "adding")
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   894
        |> map_def name (fn _ => def)
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   895
        |> debug 10 (fn _ => "transforming")
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   896
        |> fold transform trans
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   897
        |> debug 10 (fn _ => "adding done")
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   898
      end;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   899
    fun ensure_node name modl =
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   900
      (debug 9 (fn _ => "testing node " ^ quote name) ();
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   901
      if can (get_def modl) name
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   902
      then
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   903
        modl
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   904
        |> debug 9 (fn _ => "asserting node " ^ quote name)
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   905
        |> pair [name]
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   906
      else
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   907
        modl
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   908
        |> debug 9 (fn _ => "creating node " ^ quote name)
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   909
        |> add_def (name, Nop)
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18441
diff changeset
   910
        |> debug 9 (fn _ => "checking creation of node " ^ quote name)
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   911
        |> select_generator (fn gname => "trying code generator " ^ gname ^ " for definition of " ^ quote name)
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   912
             name defgens
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18441
diff changeset
   913
        |> debug 9 (fn _ => "checking creation of node " ^ quote name)
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   914
        |> check_fail msg
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   915
        |-> (fn (def, names') =>
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   916
           add (name, def)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   917
           #> fold_map ensure_node names')
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   918
        |-> (fn names' => pair (name :: Library.flat names'))
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   919
      )
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   920
  in
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   921
    modl
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   922
    |> ensure_node name
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   923
    |-> (fn names => pair (names@deps))
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   924
  end;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   925
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   926
fun start_transact f modl =
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   927
  let
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   928
    fun handle_fail f modl =
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   929
      ((([], modl) |> f)
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   930
      handle FAIL (msgs, NONE) =>
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   931
        (error o cat_lines) ("code generation failed, while:" :: msgs))
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   932
      handle FAIL (msgs, SOME e) =>
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   933
        ((writeln o cat_lines) ("code generation failed, while:" :: msgs); raise e);
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   934
  in
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   935
    modl
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   936
    |> handle_fail f
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   937
    |-> (fn x => fn (_, module) => (x, module))
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   938
  end;
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   939
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   940
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   941
(** primitive language constructs **)
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   942
18304
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   943
val class_eq = "Eq"; (*defined for all primitve types and extensionally for all datatypes*)
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   944
val type_bool = "Bool";
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   945
val type_integer = "Integer"; (*infinite!*)
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   946
val type_float = "Float";
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   947
val type_pair = "Pair";
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   948
val type_list = "List";
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   949
val cons_true = "True";
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   950
val cons_false = "False";
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   951
val cons_not = "not";
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   952
val cons_pair = "Pair";
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   953
val cons_nil = "Nil";
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   954
val cons_cons = "Cons";
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   955
val fun_eq = "eq"; (*to class eq*)
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   956
val fun_not = "not";
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   957
val fun_and = "and";
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   958
val fun_or = "or";
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   959
val fun_if = "if";
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   960
val fun_fst = "fst";
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   961
val fun_snd = "snd";
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   962
val fun_add = "add";
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   963
val fun_mult = "mult";
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   964
val fun_minus = "minus";
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   965
val fun_lt = "lt";
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   966
val fun_le = "le";
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   967
val fun_wfrec = "wfrec";
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   968
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   969
local
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   970
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   971
val A = IVarT ("a", []);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   972
val B = IVarT ("b", []);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   973
val E = IVarT ("e", [class_eq]);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   974
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   975
in
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   976
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   977
val Type_bool = type_bool `%% [];
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   978
val Type_integer = type_integer `%% [];
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   979
val Type_float = type_float `%% [];
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   980
fun Type_pair a b = type_pair `%% [a, b];
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   981
fun Type_list a = type_list `%% [a];
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   982
val Cons_true = IConst (cons_true, Type_bool);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   983
val Cons_false = IConst (cons_false, Type_bool);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   984
val Cons_pair = IConst (cons_pair, A `-> B `-> Type_pair A B);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   985
val Cons_nil = IConst (cons_nil, Type_list A);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   986
val Cons_cons = IConst (cons_cons, A `-> Type_list A `-> Type_list A);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   987
val Fun_eq = IConst (fun_eq, E `-> E `-> Type_bool);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   988
val Fun_not = IConst (fun_not, Type_bool `-> Type_bool);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   989
val Fun_and = IConst (fun_and, Type_bool `-> Type_bool `-> Type_bool);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   990
val Fun_or = IConst (fun_or, Type_bool `-> Type_bool `-> Type_bool);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   991
val Fun_if = IConst (fun_if, Type_bool `-> A `-> A `-> A);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   992
val Fun_fst = IConst (fun_fst, Type_pair A B `-> A);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   993
val Fun_snd = IConst (fun_snd, Type_pair A B `-> B);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   994
val Fun_0 = IConst ("0", Type_integer);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   995
val Fun_1 = IConst ("1", Type_integer);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   996
val Fun_add = IConst (fun_add, Type_integer `-> Type_integer `-> Type_integer);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   997
val Fun_mult = IConst (fun_mult, Type_integer `-> Type_integer `-> Type_integer);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   998
val Fun_minus = IConst (fun_minus, Type_integer `-> Type_integer);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   999
val Fun_lt = IConst (fun_lt, Type_integer `-> Type_integer `-> Type_bool);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1000
val Fun_le = IConst (fun_le, Type_integer `-> Type_integer `-> Type_bool);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1001
val Fun_wfrec = IConst (fun_wfrec, ((A `-> B) `-> A `-> B) `-> A `-> B);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1002
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1003
fun foldl1 f (x::xs) =
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1004
  Library.foldl f (x, xs);
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1005
val ** = foldl1 (uncurry Type_pair);
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1006
val XXp = foldl1 (fn (a, b) =>
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1007
  let
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1008
    val ty_a = itype_of_ipat a;
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1009
    val ty_b = itype_of_ipat b;
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1010
  in ICons ((cons_pair, [a, b]), Type_pair ty_a ty_b) end);
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1011
val XXe = foldl1 (fn (a, b) =>
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1012
  let
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1013
    val ty_a = itype_of_iexpr a;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1014
    val ty_b = itype_of_iexpr b;
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1015
  in IConst (cons_pair, ty_a `-> ty_b `-> Type_pair ty_a ty_b) `$ a `$ b end);
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1016
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1017
end; (* local *)
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1018
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1019
val prims = [class_eq, type_bool, type_integer, type_float, type_pair, type_list,
18385
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1020
  cons_true, cons_false, cons_pair, cons_nil, cons_cons, fun_eq, fun_not, fun_and,
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1021
  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
  1022
18335
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
  1023
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
  1024
(** equality handling **)
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
  1025
18385
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1026
fun invoke_eq gen_ty gen_eq x (trns as (_ , modl)) =
18335
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
  1027
  let
18385
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1028
    fun mk_eqpred dtname =
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1029
      let
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1030
        val (vs, cons, _) = case get_def modl dtname of Datatype info => info;
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1031
        val arity = map (rpair [class_eq] o fst) vs
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1032
        val ty = IType (dtname, map IVarT arity);
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1033
        fun mk_eq (c, []) =
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1034
              ([ICons ((c, []), ty), ICons ((c, []), ty)], Cons_true)
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1035
          | mk_eq (c, tys) =
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1036
              let
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1037
                val vars1 = Term.invent_names [] "a" (length tys);
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1038
                val vars2 = Term.invent_names vars1 "b" (length tys);
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1039
                fun mk_eq_cons ty' (v1, v2) =
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1040
                  IConst (fun_eq, ty' `-> ty' `-> Type_bool) `$ IVarE (v1, ty) `$ IVarE (v2, ty)
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1041
                fun mk_conj (e1, e2) =
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1042
                  Fun_and `$ e1 `$ e2;
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1043
              in
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1044
                ([ICons ((c, map2 (curry IVarP) vars1 tys), ty),
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1045
                  ICons ((c, map2 (curry IVarP) vars2 tys), ty)],
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1046
                  foldr1 mk_conj (map2 mk_eq_cons tys (vars1 ~~ vars2)))
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1047
              end;
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1048
        val eqs = map mk_eq cons @ [([IVarP ("_", ty), IVarP ("_", ty)], Cons_false)];
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1049
      in
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1050
        (Fun (eqs, (arity, ty `-> ty `-> Type_bool)), arity)
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1051
      end;
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1052
    fun invoke' (IType (tyco, tys)) trns =
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1053
          trns
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1054
          |> fold_map invoke' tys
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1055
          |-> (fn is_eq =>
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1056
                if forall I is_eq
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1057
                  then if NameSpace.is_qualified tyco
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1058
                  then
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1059
                    gen_eq (tyco, mk_eqpred tyco)
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1060
                    #> pair true
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1061
                  else
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1062
                    pair true
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1063
                else
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1064
                  pair false)
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1065
      | invoke' (IFun _) trns =
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1066
          trns 
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1067
          |> pair false
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1068
      | invoke' (IVarT (_, sort)) trns =
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1069
          trns 
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1070
          |> pair (member (op =) sort class_eq)
18335
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
  1071
  in
18385
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1072
    trns
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1073
    |> gen_ty x
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1074
    |-> (fn ty => invoke' ty)
18335
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
  1075
  end;
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
  1076
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
  1077
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
  1078
(** generic transformation **)
18304
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
  1079
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1080
fun extract_defs e =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1081
  let
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1082
    fun extr_itype (ty as IType (tyco, _)) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1083
          cons tyco #> fold_itype extr_itype ty
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1084
      | extr_itype ty =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1085
          fold_itype extr_itype ty
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1086
    fun extr_ipat (p as ICons ((c, _), _)) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1087
          cons c #> fold_ipat extr_itype extr_ipat p
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1088
      | extr_ipat p =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1089
          fold_ipat extr_itype extr_ipat p
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1090
    fun extr_iexpr (e as IConst (f, _)) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1091
          cons f #> fold_iexpr extr_itype extr_ipat extr_iexpr e
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1092
      | extr_iexpr e =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1093
          fold_iexpr extr_itype extr_ipat extr_iexpr e
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1094
  in extr_iexpr e [] end;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1095
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1096
fun eta_expand query =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1097
  let
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1098
    fun eta_app ((f, ty), es) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1099
      let
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1100
        val delta = query f - length es;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1101
        val add_n = if delta < 0 then 0 else delta;
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
  1102
        val tys =
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
  1103
          (fst o unfold_fun) ty
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
  1104
          |> curry Library.drop (length es)
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
  1105
          |> curry Library.take add_n
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1106
        val add_vars =
18304
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
  1107
          Term.invent_names (vars_of_iexprs es) "x" add_n ~~ tys;
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1108
      in
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1109
        Library.foldr IAbs (add_vars, IConst (f, ty) `$$ es `$$ (map IVarE add_vars))
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1110
      end;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1111
    fun eta_iexpr' e = map_iexpr I I eta_iexpr e
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1112
    and eta_iexpr (IConst (f, ty)) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1113
          eta_app ((f, ty), [])
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1114
      | eta_iexpr (e as IApp _) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1115
          (case (unfold_app e)
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1116
           of (IConst (f, ty), es) =>
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1117
                eta_app ((f, ty), map eta_iexpr es)
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1118
            | _ => eta_iexpr' e)
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1119
      | eta_iexpr e = eta_iexpr' e;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1120
  in map_defs (map_def_fun I eta_iexpr) end;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1121
18216
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1122
val eta_expand_poly =
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1123
  let
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1124
    fun map_def_fun (def as Fun ([([], e)], cty as (sortctxt, (ty as IFun (ty1, ty2))))) =
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1125
          if (not o null) sortctxt
18304
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
  1126
            orelse (null o tvars_of_itypes) [ty]
18216
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1127
          then def
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1128
          else
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1129
            let
18304
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
  1130
              val add_var = (hd (Term.invent_names (vars_of_iexprs [e]) "x" 1), ty1)
18216
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1131
            in (Fun ([([IVarP add_var], IAbs (add_var, e))], cty)) end
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1132
      | map_def_fun def = def;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1133
  in map_defs map_def_fun end;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1134
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1135
fun tupelize_cons module =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1136
  let
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1137
    fun replace_cons (cons as (_, [])) =
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1138
          pair cons
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1139
      | replace_cons (cons as (_, [_])) =
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1140
          pair cons
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1141
      | replace_cons (con, tys) =
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1142
          cons con
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1143
          #> pair (con, [** tys])
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1144
    fun replace_def (_, (def as Datatype (vs, cs, insts))) =
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1145
          fold_map replace_cons cs
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1146
          #-> (fn cs => pair (Datatype (vs, cs, insts)))
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1147
      | replace_def (_, def) =
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1148
          pair def
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1149
    fun replace_app cs ((f, ty), es) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1150
      if member (op =) cs f
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1151
      then
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1152
        let
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1153
          val (tys, ty') = unfold_fun ty
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1154
        in IConst (f, ** tys `-> ty') `$ XXe es end
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1155
      else IConst (f, ty) `$$ es;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1156
    fun replace_iexpr cs (IConst (f, ty)) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1157
          replace_app cs ((f, ty), [])
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1158
      | replace_iexpr cs (e as IApp _) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1159
          (case unfold_app e
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1160
           of (IConst fty, es) => replace_app cs (fty, map (replace_iexpr cs) es)
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1161
            | _ => map_iexpr I I (replace_iexpr cs) e)
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1162
      | replace_iexpr cs e = map_iexpr I I (replace_iexpr cs) e;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1163
    fun replace_ipat cs (p as ICons ((c, ps), ty)) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1164
          if member (op =) cs c then
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1165
            ICons ((c, [XXp (map (replace_ipat cs) ps)]), ty)
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1166
          else map_ipat I (replace_ipat cs) p
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1167
      | replace_ipat cs p = map_ipat I (replace_ipat cs) p;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1168
  in
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1169
    transform_defs replace_def replace_ipat replace_iexpr [cons_cons] module
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1170
  end;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1171
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1172
fun eliminate_classes module =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1173
  let
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1174
    fun transform_itype (IVarT (v, s)) =
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1175
          IVarT (v, [])
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1176
      | transform_itype (ty as IDictT _) =
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1177
          ty
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1178
      | transform_itype ty =
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1179
          map_itype transform_itype ty;
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1180
    fun transform_ipat p =
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1181
          map_ipat transform_itype transform_ipat p;
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1182
    fun transform_iexpr vname_alist (IInst (e, ls)) =
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1183
          let
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
  1184
            fun transform_lookup (ClassPackage.Instance ((cdict, idict), ls)) =
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1185
                  ls
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1186
                  |> transform_lookups
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
  1187
                  |-> (fn tys =>
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
  1188
                        curry mk_apps (IConst (idict, cdict `%% tys))
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
  1189
                        #> pair (cdict `%% tys))
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1190
              | transform_lookup (ClassPackage.Lookup (deriv, (v, i))) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1191
                  let
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1192
                    val (v', cls) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1193
                      (nth o the oo AList.lookup (op =)) vname_alist v i;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1194
                    fun mk_parm tyco = tyco `%% [IVarT (v, [])];
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1195
                  in (mk_parm cls, ILookup (deriv, v')) end
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1196
            and transform_lookups lss =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1197
                  map_yield (map_yield transform_lookup
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1198
                       #> apfst **
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1199
                       #> apsnd XXe) lss
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1200
          in transform_iexpr vname_alist e `$$ (snd o transform_lookups) ls end
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1201
      | transform_iexpr vname_alist e =
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1202
          map_iexpr transform_itype transform_ipat (transform_iexpr vname_alist) e;
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1203
    fun elim_sorts (Fun (eqs, ([], ty))) =
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1204
          Fun (map (fn (ps, rhs) => (map transform_ipat ps, transform_iexpr [] rhs)) eqs,
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18441
diff changeset
  1205
            ([], transform_itype ty))
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1206
      | elim_sorts (Fun (eqs, (sortctxt, ty))) =
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1207
          let
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1208
            val varnames_ctxt =
18441
7488d8ea61bc removed infix prefix, introduces burrow
haftmann
parents: 18385
diff changeset
  1209
              burrow
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1210
                (Term.invent_names ((vars_of_iexprs o map snd) eqs @
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1211
                  (vars_of_ipats o Library.flat o map fst) eqs) "d" o length)
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1212
                (map snd sortctxt);
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1213
            val vname_alist = map2 (fn (vt, sort) => fn vs => (vt, vs ~~ sort))
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18441
diff changeset
  1214
              sortctxt varnames_ctxt;
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1215
            val ty' = map (op ** o (fn (vt, vss) => map (fn (_, cls) =>
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1216
              cls `%% [IVarT (vt, [])]) vss)) vname_alist
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1217
              `--> transform_itype ty;
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1218
            val ps_add = map (XXp o (fn (vt, vss) => map (fn (v, cls) =>
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1219
              IVarP (v, cls `%% [IVarT (vt, [])])) vss)) vname_alist;
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18441
diff changeset
  1220
          in Fun (map (fn (ps, rhs) => (ps_add @ map transform_ipat ps, transform_iexpr vname_alist rhs)) eqs, ([], ty')) end
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1221
      | elim_sorts (Datatype (vars, constrs, insts)) =
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1222
          Datatype (map (fn (v, _) => (v, [])) vars, map (apsnd (map transform_itype)) constrs, insts)
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1223
      | elim_sorts (Typesyn (vars, ty)) =
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1224
          Typesyn (map (fn (v, _) => (v, [])) vars, transform_itype ty)
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1225
      | elim_sorts d = d;
18515
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18454
diff changeset
  1226
    fun mk_cls_typ_map v (supclss, membrs) ty_inst =
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18454
diff changeset
  1227
      (map (fn class => (class, IType (class, [ty_inst]))) supclss,
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18454
diff changeset
  1228
        map (fn (m, (mctxt, ty)) =>
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18454
diff changeset
  1229
        (m, ty |> instant_itype (v, ty_inst))) membrs);
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18454
diff changeset
  1230
    fun extract_members (cls, Class (supclss, v, membrs, _)) =
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18454
diff changeset
  1231
          let
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18454
diff changeset
  1232
            val ty_cls = cls `%% [IVarT (v, [])];
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18454
diff changeset
  1233
            val w = "d";
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18454
diff changeset
  1234
            val add_supclss = if null supclss then I else cons (v, supclss);
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18454
diff changeset
  1235
            fun mk_fun (m, (mctxt, ty)) = (m, Fun ([([IVarP (w, ty_cls)], ILookup ([m], w))],
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18454
diff changeset
  1236
              (add_supclss mctxt, ty `-> ty_cls)));
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18454
diff changeset
  1237
          in fold (cons o mk_fun) membrs end
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18454
diff changeset
  1238
      | extract_members _ = I;
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18454
diff changeset
  1239
    fun introduce_dicts (Class (supclss, v, membrs, insts)) =
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18454
diff changeset
  1240
          let
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18454
diff changeset
  1241
            val varname_cls = Term.invent_names (tvars_of_itypes (map (snd o snd) membrs)) "a" 1 |> hd
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18454
diff changeset
  1242
          in
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18454
diff changeset
  1243
            Typesyn ([(varname_cls, supclss)], IDictT ((op @) (mk_cls_typ_map v (supclss, membrs) (IVarT (varname_cls, [])))))
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18454
diff changeset
  1244
          end
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18454
diff changeset
  1245
      | introduce_dicts (Classinst ((clsname, (tyco, arity)), (supinsts, memdefs))) =
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18454
diff changeset
  1246
          let
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18454
diff changeset
  1247
            val Class (supclss, v, members, _) =
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18454
diff changeset
  1248
              if clsname = class_eq
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18454
diff changeset
  1249
              then
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18454
diff changeset
  1250
                Class ([], "a", [(fun_eq, ([], IVarT ("a", []) `-> IVarT ("a", []) `-> Type_bool))], [])
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18454
diff changeset
  1251
              else
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18454
diff changeset
  1252
                get_def module clsname;
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18454
diff changeset
  1253
            val ty = tyco `%% map IVarT arity;
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18454
diff changeset
  1254
            val (supinst_typ_map, mem_typ_map) = mk_cls_typ_map v (supclss, members) ty;
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18454
diff changeset
  1255
            fun mk_meminst (m, ty) =
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18454
diff changeset
  1256
              let
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18454
diff changeset
  1257
                val (instname, instlookup) = (the o AList.lookup (op =) memdefs) m;
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18454
diff changeset
  1258
              in
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18454
diff changeset
  1259
                IInst (IConst (instname, ty), instlookup)
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18454
diff changeset
  1260
                |> pair m
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18454
diff changeset
  1261
              end;
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18454
diff changeset
  1262
            val memdefs_ty = map mk_meminst mem_typ_map;
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18454
diff changeset
  1263
            fun mk_supinst (supcls, dictty) =
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18454
diff changeset
  1264
              let
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18454
diff changeset
  1265
                val (instname, instlookup) = (the o AList.lookup (op =) supinsts) supcls;
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18454
diff changeset
  1266
              in
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18454
diff changeset
  1267
                IInst (IConst (instname, dictty), instlookup)
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18454
diff changeset
  1268
                |> pair supcls
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18454
diff changeset
  1269
              end;
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18454
diff changeset
  1270
            val instdefs_ty = map mk_supinst supinst_typ_map;
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18454
diff changeset
  1271
          in
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18454
diff changeset
  1272
            Fun ([([], IDictE (instdefs_ty @ memdefs_ty))],
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18454
diff changeset
  1273
              (arity, IType (clsname, [ty])))
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18454
diff changeset
  1274
          end
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18454
diff changeset
  1275
      | introduce_dicts d = d;
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1276
  in
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1277
    module
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1278
    |> `(fn module => fold_defs extract_members module [])
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1279
    |-> (fn membrs => fold (fn (name, f) => map_def name (K f)) membrs)
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1280
    |> map_defs introduce_dicts
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1281
    |> map_defs elim_sorts
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1282
  end;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1283
18216
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1284
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1285
(** generic serialization **)
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1286
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1287
(* resolving *)
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1288
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1289
fun mk_resolvtab nspgrp validate module =
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1290
  let
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1291
    fun ensure_unique prfix prfix' name name' (locals, tab) =
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1292
      let
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1293
        fun uniquify name n =
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1294
          let
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1295
            val name' = if n = 0 then name else name ^ "_" ^ string_of_int n
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1296
          in
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1297
            if member (op =) locals name'
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1298
            then uniquify name (n+1)
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1299
            else case validate name
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1300
              of NONE => name'
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1301
               | SOME name' => uniquify name' n
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1302
          end;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1303
        val name'' = uniquify name' 0;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1304
      in
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1305
        (locals, tab)
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1306
        |> apsnd (Symtab.update_new
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1307
             (NameSpace.pack (prfix @ [name]), NameSpace.pack (prfix' @ [name''])))
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1308
        |> apfst (cons name'')
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1309
        |> pair name''
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1310
      end;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1311
    fun fill_in prfix prfix' node tab =
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1312
      let
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1313
        val keys = Graph.keys node;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1314
        val nodes = AList.make (Graph.get_node node) keys;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1315
        val (mods, defs) =
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1316
          nodes
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1317
          |> List.partition (fn (_, Module _) => true | _ => false)
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1318
          |> apfst (map (fn (name, Module m) => (name, m)))
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1319
          |> apsnd (map fst)
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1320
        fun modl_validate (name, modl) (locals, tab) =
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1321
          (locals, tab)
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1322
          |> ensure_unique prfix prfix' name name
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1323
          |-> (fn name' => apsnd (fill_in (prfix @ [name]) (prfix @ [name']) modl))
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1324
        fun ensure_unique_sidf sidf =
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1325
          let
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1326
            val [shallow, name] = NameSpace.unpack sidf;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1327
          in
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1328
            nspgrp
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1329
            |> get_first
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1330
                (fn grp => if member (op =) grp shallow
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1331
                  then grp |> remove (op =) shallow |> SOME else NONE)
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1332
            |> these
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1333
            |> map (fn s => NameSpace.pack [s, name])
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1334
            |> exists (member (op =) defs)
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1335
            |> (fn b => if b then sidf else name)
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1336
          end;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1337
        fun def_validate sidf (locals, tab) =
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1338
          (locals, tab)
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1339
          |> ensure_unique prfix prfix' sidf (ensure_unique_sidf sidf)
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1340
          |> snd
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1341
      in
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1342
        ([], tab)
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1343
        |> fold modl_validate mods
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1344
        |> fold def_validate defs
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1345
        |> snd
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1346
      end;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1347
  in
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1348
    Symtab.empty
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1349
    |> fill_in [] [] module
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1350
  end;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1351
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
  1352
fun mk_resolv tab =
18216
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1353
  let
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1354
    fun resolver modl name =
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1355
      if NameSpace.is_qualified name then
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1356
        let
18247
b17724cae935 code generator: case expressions, improved name resolving
haftmann
parents: 18231
diff changeset
  1357
          val _ = debug 12 (fn name' => "resolving " ^ quote name ^ " in " ^ (quote o NameSpace.pack) modl) ();
18216
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1358
          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
  1359
          val name' = (NameSpace.unpack o the o Symtab.lookup tab) name
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1360
        in
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18441
diff changeset
  1361
          (NameSpace.pack o snd o snd o get_prefix (op =)) (modl', name')
18247
b17724cae935 code generator: case expressions, improved name resolving
haftmann
parents: 18231
diff changeset
  1362
          |> debug 12 (fn name' => "resolving " ^ quote name ^ " to " ^ quote name' ^ " in " ^ (quote o NameSpace.pack) modl)
18216
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1363
        end
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1364
      else name
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1365
  in resolver end;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1366
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1367
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1368
(* serialization *)
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1369
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1370
fun serialize s_def s_module validate nspgrp name_root module =
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1371
  let
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1372
    val resolvtab = mk_resolvtab nspgrp validate module;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1373
    val resolver = mk_resolv resolvtab;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1374
    fun seri prfx ([(name, Module module)]) =
18247
b17724cae935 code generator: case expressions, improved name resolving
haftmann
parents: 18231
diff changeset
  1375
          s_module (resolver prfx (prfx @ [name] |> NameSpace.pack),
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1376
            List.mapPartial (seri (prfx @ [name]))
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1377
              ((map (AList.make (Graph.get_node module)) o rev o Graph.strong_conn) module))
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18441
diff changeset
  1378
          |> SOME
18216
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1379
      | seri prfx ds =
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1380
          s_def (resolver prfx) (map
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18441
diff changeset
  1381
            (fn (name, Def def) => (resolver prfx (prfx @ [name] |> NameSpace.pack), def)) ds)
18216
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1382
  in
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1383
    setmp print_mode [] (fn _ => s_module (name_root, (List.mapPartial (seri [])
18361
3126d01e9e35 improved serialization of classes to haskell
haftmann
parents: 18360
diff changeset
  1384
      ((map (AList.make (Graph.get_node module)) o rev o Graph.strong_conn) module)))) ()
18216
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1385
  end;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1386
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
  1387
end; (* struct *)
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
  1388
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
  1389
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
  1390
structure CodegenThingol : CODEGEN_THINGOL =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
  1391
struct
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
  1392
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
  1393
open CodegenThingolOp;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
  1394
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
  1395
end; (* struct *)