src/Pure/Tools/codegen_thingol.ML
author haftmann
Tue, 19 Sep 2006 15:22:26 +0200
changeset 20600 6d75e02ed285
parent 20466 7c20ddbd911b
child 20709 645236e80885
permissions -rw-r--r--
added codegen_data
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
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
     8
infix 8 `%%;
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
     9
infixr 6 `->;
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
    10
infixr 6 `-->;
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
    11
infix 4 `$;
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
    12
infix 4 `$$;
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
    13
infixr 3 `|->;
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
    14
infixr 3 `|-->;
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
    15
19136
00ade10f611d some refinements
haftmann
parents: 19042
diff changeset
    16
signature BASIC_CODEGEN_THINGOL =
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    17
sig
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    18
  type vname = string;
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
    19
  datatype inst =
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
    20
      Instance of string * inst list list
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
    21
    | Context of class list * (vname * int);
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    22
  datatype itype =
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
    23
      `%% of string * itype list
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
    24
    | `-> of itype * itype
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
    25
    | ITyVar of vname;
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20071
diff changeset
    26
  datatype iterm =
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
    27
      IConst of string * (inst list list * itype)
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
    28
    | IVar of vname
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20071
diff changeset
    29
    | `$ of iterm * iterm
454f4be984b7 adaptions in codegen
haftmann
parents: 20071
diff changeset
    30
    | `|-> of (vname * itype) * iterm
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20466
diff changeset
    31
    | INum of IntInf.int * iterm
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20071
diff changeset
    32
    | IChar of string (*length one!*) * iterm
454f4be984b7 adaptions in codegen
haftmann
parents: 20071
diff changeset
    33
    | ICase of ((iterm * itype) * (iterm * iterm) list) * iterm;
20439
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
    34
        (*((discriminendum term (td), discriminendum type (ty)),
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
    35
                [(selector pattern (p), body term (t))] (bs)),
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
    36
                pure term (t0))*)
19136
00ade10f611d some refinements
haftmann
parents: 19042
diff changeset
    37
end;
00ade10f611d some refinements
haftmann
parents: 19042
diff changeset
    38
00ade10f611d some refinements
haftmann
parents: 19042
diff changeset
    39
signature CODEGEN_THINGOL =
00ade10f611d some refinements
haftmann
parents: 19042
diff changeset
    40
sig
00ade10f611d some refinements
haftmann
parents: 19042
diff changeset
    41
  include BASIC_CODEGEN_THINGOL;
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
    42
  val `--> : itype list * itype -> itype;
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20071
diff changeset
    43
  val `$$ : iterm * iterm list -> iterm;
454f4be984b7 adaptions in codegen
haftmann
parents: 20071
diff changeset
    44
  val `|--> : (vname * itype) list * iterm -> iterm;
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    45
  val pretty_itype: itype -> Pretty.T;
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20071
diff changeset
    46
  val pretty_iterm: iterm -> Pretty.T;
18216
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
    47
  val unfoldl: ('a -> ('a * 'b) option) -> 'a -> 'a * 'b list;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
    48
  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
    49
  val unfold_fun: itype -> itype list * itype;
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20071
diff changeset
    50
  val unfold_app: iterm -> iterm * iterm list;
454f4be984b7 adaptions in codegen
haftmann
parents: 20071
diff changeset
    51
  val unfold_abs: iterm -> (iterm * itype) list * iterm;
454f4be984b7 adaptions in codegen
haftmann
parents: 20071
diff changeset
    52
  val unfold_let: iterm -> ((iterm * itype) * iterm) list * iterm;
454f4be984b7 adaptions in codegen
haftmann
parents: 20071
diff changeset
    53
  val unfold_const_app: iterm ->
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
    54
    ((string * (inst list list * itype)) * iterm list) option;
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20071
diff changeset
    55
  val add_constnames: iterm -> string list -> string list;
454f4be984b7 adaptions in codegen
haftmann
parents: 20071
diff changeset
    56
  val add_varnames: iterm -> string list -> string list;
454f4be984b7 adaptions in codegen
haftmann
parents: 20071
diff changeset
    57
  val is_pat: (string -> bool) -> iterm -> bool;
454f4be984b7 adaptions in codegen
haftmann
parents: 20071
diff changeset
    58
  val vars_distinct: iterm list -> bool;
454f4be984b7 adaptions in codegen
haftmann
parents: 20071
diff changeset
    59
  val map_pure: (iterm -> 'a) -> iterm -> 'a;
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
    60
  val eta_expand: (string * (inst list list * itype)) * iterm list -> int -> iterm;
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20071
diff changeset
    61
  val resolve_tycos: (string -> string) -> itype * iterm list -> itype * iterm list;
454f4be984b7 adaptions in codegen
haftmann
parents: 20071
diff changeset
    62
  val resolve_consts: (string -> string) -> iterm -> iterm;
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    63
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
    64
  type typscheme = (vname * sort) list * itype;
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    65
  datatype def =
19816
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
    66
      Bot
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
    67
    | Fun of (iterm list * iterm) list * typscheme
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
    68
    | Typesyn of typscheme
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
    69
    | Datatype of (vname * sort) list * (string * itype list) list
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
    70
    | Datatypecons of string
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
    71
    | Class of class list * (vname * (string * itype) list)
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
    72
    | Classmember of class
20389
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
    73
    | Classinst of (class * (string * (vname * sort) list))
20466
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
    74
          * ((class * (string * inst list list)) list
20389
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
    75
        * (string * iterm) list);
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    76
  type module;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    77
  type transact;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    78
  type 'dst transact_fin;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    79
  val pretty_def: def -> Pretty.T;
20191
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
    80
  val pretty_module: module -> Pretty.T;
18360
a2c9506b62a7 improved class handling
haftmann
parents: 18335
diff changeset
    81
  val pretty_deps: module -> Pretty.T;
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    82
  val empty_module: module;
19341
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19300
diff changeset
    83
  val get_def: module -> string -> def;
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    84
  val merge_module: module * module -> module;
19042
630b8dd0b31a exported some interfaces useful for other code generator approaches
haftmann
parents: 19038
diff changeset
    85
  val diff_module: module * module -> (string * def) list;
19341
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19300
diff changeset
    86
  val project_module: string list -> module -> module;
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19300
diff changeset
    87
  val purge_module: string list -> module -> module;
20428
67fa1c6ba89e refinements
haftmann
parents: 20405
diff changeset
    88
(*   val flat_funs_datatypes: module -> (string * def) list;  *)
20216
f30b73385060 added eval_term
haftmann
parents: 20192
diff changeset
    89
  val add_eval_def: string (*shallow name space*) * iterm -> module -> string * module;
20191
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
    90
  val delete_garbage: string list (*hidden definitions*) -> module -> module;
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
    91
  val has_nsp: string -> string -> bool;
19884
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
    92
  val ensure_def: (string -> transact -> def transact_fin) -> bool -> string
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
    93
    -> string -> transact -> transact;
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    94
  val succeed: 'a -> transact -> 'a transact_fin;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    95
  val fail: string -> transact -> 'a transact_fin;
19884
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
    96
  val message: string -> (transact -> 'a) -> transact -> 'a;
18963
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18919
diff changeset
    97
  val start_transact: string option -> (transact -> 'a * transact) -> module -> 'a * module;
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
    98
  val elim_classes: module -> (iterm list * iterm) list * typscheme -> (iterm list * iterm) list * itype;
18216
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
    99
19341
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19300
diff changeset
   100
  val debug: bool ref;
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19300
diff changeset
   101
  val debug_msg: ('a -> string) -> 'a -> 'a;
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   102
  val soft_exc: bool ref;
18216
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
   103
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
   104
  val serialize:
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 19025
diff changeset
   105
    ((string -> string -> string) -> string -> (string * def) list -> 'a option)
19341
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19300
diff changeset
   106
    -> ((string -> string) -> string list -> (string * string) * 'a list -> 'a option)
18216
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
   107
    -> (string -> string option)
18919
haftmann
parents: 18918
diff changeset
   108
    -> (string * string -> string)
18850
92ef83e5eaea various improvements
haftmann
parents: 18812
diff changeset
   109
    -> string list list -> string -> module -> 'a option;
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   110
end;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   111
18850
92ef83e5eaea various improvements
haftmann
parents: 18812
diff changeset
   112
structure CodegenThingol: CODEGEN_THINGOL =
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   113
struct
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   114
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   115
(** auxiliary **)
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   116
19341
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19300
diff changeset
   117
val debug = ref false;
20405
8276fd8d1919 tracing
haftmann
parents: 20389
diff changeset
   118
fun debug_msg f x = (if !debug then Output.tracing (f x) else (); x);
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   119
val soft_exc = ref true;
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   120
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   121
fun unfoldl dest x =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   122
  case dest x
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   123
   of NONE => (x, [])
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   124
    | SOME (x1, x2) =>
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   125
        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
   126
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   127
fun unfoldr dest x =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   128
  case dest x
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   129
   of NONE => ([], x)
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   130
    | SOME (x1, x2) =>
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   131
        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
   132
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   133
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   134
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   135
(** language core - types, pattern, expressions **)
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   136
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   137
(* language representation *)
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   138
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   139
type vname = string;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   140
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   141
datatype inst =
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   142
    Instance of string * inst list list
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   143
  | Context of class list * (vname * int);
18885
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   144
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   145
datatype itype =
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   146
    `%% of string * itype list
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   147
  | `-> of itype * itype
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   148
  | ITyVar of vname;
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   149
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20071
diff changeset
   150
datatype iterm =
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   151
    IConst of string * (inst list list * itype)
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   152
  | IVar of vname
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20071
diff changeset
   153
  | `$ of iterm * iterm
454f4be984b7 adaptions in codegen
haftmann
parents: 20071
diff changeset
   154
  | `|-> of (vname * itype) * iterm
454f4be984b7 adaptions in codegen
haftmann
parents: 20071
diff changeset
   155
  | INum of IntInf.int * iterm
454f4be984b7 adaptions in codegen
haftmann
parents: 20071
diff changeset
   156
  | IChar of string * iterm
454f4be984b7 adaptions in codegen
haftmann
parents: 20071
diff changeset
   157
  | ICase of ((iterm * itype) * (iterm * iterm) list) * iterm;
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   158
    (*see also signature*)
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   159
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   160
(*
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   161
  variable naming conventions
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   162
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   163
  bare names:
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   164
    variable names          v
20439
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   165
    class names             class
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   166
    type constructor names  tyco
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   167
    datatype names          dtco
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   168
    const names (general)   c
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   169
    constructor names       co
20439
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   170
    class operation names   clsop (op)
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   171
    arbitrary name          s
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   172
20439
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   173
    v, c, co, clsop also annotated with types usw.
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   174
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   175
  constructs:
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   176
    sort                    sort
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   177
    type parameters         vs
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   178
    type                    ty
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   179
    type schemes            tysm
20439
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   180
    term                    t
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   181
    (term as pattern)       p
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   182
    instance (classs, tyco) inst
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   183
 *)
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   184
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   185
val op `--> = Library.foldr (op `->);
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   186
val op `$$ = Library.foldl (op `$);
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   187
val op `|--> = Library.foldr (op `|->);
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   188
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   189
val pretty_typparms =
19341
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19300
diff changeset
   190
  Pretty.list "(" ")" o Pretty.commas o map (fn (v, sort) => (Pretty.block o Pretty.breaks)
19150
1457d810b408 class package and codegen refinements
haftmann
parents: 19136
diff changeset
   191
    [Pretty.str v, Pretty.str "::", Pretty.enum "&" "" "" (map Pretty.str sort)]);
1457d810b408 class package and codegen refinements
haftmann
parents: 19136
diff changeset
   192
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   193
fun pretty_itype (tyco `%% tys) =
19341
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19300
diff changeset
   194
      Pretty.enum "" "(" ")" (Pretty.str tyco :: map pretty_itype tys)
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   195
  | pretty_itype (ty1 `-> ty2) =
18885
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   196
      Pretty.enum "" "(" ")" [pretty_itype ty1, Pretty.str "->", pretty_itype ty2]
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   197
  | pretty_itype (ITyVar v) =
19150
1457d810b408 class package and codegen refinements
haftmann
parents: 19136
diff changeset
   198
      Pretty.str v;
18885
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   199
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20071
diff changeset
   200
fun pretty_iterm (IConst (c, _)) =
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   201
      Pretty.str c
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20071
diff changeset
   202
  | pretty_iterm (IVar v) =
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   203
      Pretty.str ("?" ^ v)
20439
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   204
  | pretty_iterm (t1 `$ t2) =
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   205
      (Pretty.enclose "(" ")" o Pretty.breaks)
20439
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   206
        [pretty_iterm t1, pretty_iterm t2]
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   207
  | pretty_iterm ((v, ty) `|-> t) =
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20071
diff changeset
   208
      (Pretty.enclose "(" ")" o Pretty.breaks)
20439
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   209
        [Pretty.str v, Pretty.str "::", pretty_itype ty, Pretty.str "|->", pretty_iterm t]
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20071
diff changeset
   210
  | pretty_iterm (INum (n, _)) =
19202
0b9eb4b0ad98 substantial improvement in codegen iml
haftmann
parents: 19167
diff changeset
   211
      (Pretty.str o IntInf.toString) n
20439
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   212
  | pretty_iterm (IChar (h, _)) =
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   213
      (Pretty.str o quote) h
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   214
  | pretty_iterm (ICase (((t, _), bs), _)) =
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   215
      (Pretty.enclose "(" ")" o Pretty.breaks) [
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   216
        Pretty.str "case",
20439
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   217
        pretty_iterm t,
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   218
        Pretty.enclose "(" ")" (map (fn (p, t) =>
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   219
          (Pretty.block o Pretty.breaks) [
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20071
diff changeset
   220
            pretty_iterm p,
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   221
            Pretty.str "=>",
20439
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   222
            pretty_iterm t
18885
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   223
          ]
20439
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   224
        ) bs)
18885
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   225
      ];
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   226
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   227
val unfold_fun = unfoldr
20439
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   228
  (fn op `-> ty => SOME ty
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   229
    | _ => NONE);
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   230
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   231
val unfold_app = unfoldl
20439
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   232
  (fn op `$ t => SOME t
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   233
    | _ => NONE);
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   234
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   235
val unfold_abs = unfoldr
20439
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   236
  (fn (v, ty) `|-> (e as ICase (((IVar w, _), [(p, t)]), _)) =>
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   237
        if v = w then SOME ((p, ty), t) else SOME ((IVar v, ty), t)
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   238
    | (v, ty) `|-> t => SOME ((IVar v, ty), t)
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   239
    | _ => NONE)
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   240
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   241
val unfold_let = unfoldr
20439
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   242
  (fn ICase (((td, ty), [(p, t)]), _) => SOME (((p, ty), td), t)
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   243
    | _ => NONE);
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   244
20439
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   245
fun unfold_const_app t =
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   246
 case unfold_app t
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   247
  of (IConst c, ts) => SOME (c, ts)
18865
31aed965135c minor cleanups
haftmann
parents: 18852
diff changeset
   248
   | _ => NONE;
31aed965135c minor cleanups
haftmann
parents: 18852
diff changeset
   249
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   250
fun map_itype _ (ty as ITyVar _) =
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   251
      ty
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   252
  | map_itype f (tyco `%% tys) =
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   253
      tyco `%% map f tys
20439
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   254
  | map_itype f (ty1 `-> ty2) =
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   255
      f ty1 `-> f ty2;
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   256
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   257
fun eq_ityp ((vs1, ty1), (vs2, ty2)) =
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   258
  let
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   259
    exception NO_MATCH;
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   260
    fun eq_typparms subs vs1 vs2 =
19597
8ced57ffc090 major refinement of codegen_theorems.ML
haftmann
parents: 19482
diff changeset
   261
      map (fn (v : string, sort : string list) => case AList.lookup (op =) subs v
19150
1457d810b408 class package and codegen refinements
haftmann
parents: 19136
diff changeset
   262
       of NONE => raise NO_MATCH
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   263
        | SOME (v' : string) => case AList.lookup (op =) vs2 v'
19150
1457d810b408 class package and codegen refinements
haftmann
parents: 19136
diff changeset
   264
           of NONE => raise NO_MATCH
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   265
            | SOME sort' => if sort <> sort' then raise NO_MATCH else ()) vs1
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   266
    fun eq (ITyVar v1) (ITyVar v2) subs =
19150
1457d810b408 class package and codegen refinements
haftmann
parents: 19136
diff changeset
   267
          (case AList.lookup (op =) subs v1
1457d810b408 class package and codegen refinements
haftmann
parents: 19136
diff changeset
   268
           of NONE => subs |> AList.update (op =) (v1, v2)
1457d810b408 class package and codegen refinements
haftmann
parents: 19136
diff changeset
   269
            | SOME v1' =>
1457d810b408 class package and codegen refinements
haftmann
parents: 19136
diff changeset
   270
                if v1' <> v2
1457d810b408 class package and codegen refinements
haftmann
parents: 19136
diff changeset
   271
                then raise NO_MATCH
1457d810b408 class package and codegen refinements
haftmann
parents: 19136
diff changeset
   272
                else subs)
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   273
      | eq (tyco1 `%% tys1) (tyco2 `%% tys2) subs =
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   274
          if tyco1 <> tyco2
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   275
          then raise NO_MATCH
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   276
          else subs |> fold2 eq tys1 tys2
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   277
      | eq (ty11 `-> ty12) (ty21 `-> ty22) subs =
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   278
          subs |> eq ty11 ty21 |> eq ty12 ty22
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   279
      | eq _ _ _ = raise NO_MATCH;
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   280
  in
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   281
    (eq_typparms vs1 vs2; eq ty1 ty2 []; true)
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   282
    handle NO_MATCH => false
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   283
  end;
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   284
18885
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   285
fun instant_itype f =
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   286
  let
20439
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   287
    fun instant (ITyVar v) = f v
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   288
      | instant ty = map_itype instant ty;
19215
03abed544f1e first running version of type classes
haftmann
parents: 19214
diff changeset
   289
  in instant end;
18885
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   290
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   291
fun is_pat is_cons (IConst (c, _)) = is_cons c
20439
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   292
  | is_pat _ (IVar _) = true
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   293
  | is_pat is_cons (t1 `$ t2) =
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   294
      is_pat is_cons t1 andalso is_pat is_cons t2
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   295
  | is_pat _ (INum _) = true
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   296
  | is_pat _ (IChar _) = true
19953
2f54a51f1801 class package refinements, slight code generation refinements
haftmann
parents: 19937
diff changeset
   297
  | is_pat _ _ = false;
19202
0b9eb4b0ad98 substantial improvement in codegen iml
haftmann
parents: 19167
diff changeset
   298
20439
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   299
fun map_pure f (t as IConst _) =
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   300
      f t
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   301
  | map_pure f (t as IVar _) =
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   302
      f t
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   303
  | map_pure f (t as _ `$ _) =
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   304
      f t
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   305
  | map_pure f (t as _ `|-> _) =
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   306
      f t
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   307
  | map_pure f (INum (_, t0)) =
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   308
      f t0
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   309
  | map_pure f (IChar (_, t0)) =
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   310
      f t0
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   311
  | map_pure f (ICase (_, t0)) =
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   312
      f t0;
18912
dd168daf172d improvement in devarifications
haftmann
parents: 18885
diff changeset
   313
19607
07eeb832f28d introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents: 19597
diff changeset
   314
fun resolve_tycos _ = error "";
07eeb832f28d introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents: 19597
diff changeset
   315
fun resolve_consts _ = error "";
18216
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
   316
19202
0b9eb4b0ad98 substantial improvement in codegen iml
haftmann
parents: 19167
diff changeset
   317
fun add_constnames (IConst (c, _)) =
0b9eb4b0ad98 substantial improvement in codegen iml
haftmann
parents: 19167
diff changeset
   318
      insert (op =) c
0b9eb4b0ad98 substantial improvement in codegen iml
haftmann
parents: 19167
diff changeset
   319
  | add_constnames (IVar _) =
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   320
      I
20439
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   321
  | add_constnames (t1 `$ t2) =
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   322
      add_constnames t1 #> add_constnames t2
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   323
  | add_constnames (_ `|-> t) =
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   324
      add_constnames t
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   325
  | add_constnames (INum (_, t0)) =
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   326
      add_constnames t0
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   327
  | add_constnames (IChar (_, t0)) =
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   328
      add_constnames t0
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   329
  | add_constnames (ICase (_, t0)) =
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   330
      add_constnames t0;
19202
0b9eb4b0ad98 substantial improvement in codegen iml
haftmann
parents: 19167
diff changeset
   331
0b9eb4b0ad98 substantial improvement in codegen iml
haftmann
parents: 19167
diff changeset
   332
fun add_varnames (IConst _) =
0b9eb4b0ad98 substantial improvement in codegen iml
haftmann
parents: 19167
diff changeset
   333
      I
0b9eb4b0ad98 substantial improvement in codegen iml
haftmann
parents: 19167
diff changeset
   334
  | add_varnames (IVar v) =
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   335
      insert (op =) v
20439
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   336
  | add_varnames (t1 `$ t2) =
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   337
      add_varnames t1 #> add_varnames t2
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   338
  | add_varnames ((v, _) `|-> t) =
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   339
      insert (op =) v #> add_varnames t
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   340
  | add_varnames (INum (_, t)) =
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   341
      add_varnames t
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   342
  | add_varnames (IChar (_, t)) =
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   343
      add_varnames t
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   344
  | add_varnames (ICase (((td, _), bs), _)) =
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   345
      add_varnames td #> fold (fn (p, t) => add_varnames p #> add_varnames t) bs;
18885
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   346
20439
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   347
fun vars_distinct ts =
18885
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   348
  let
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20071
diff changeset
   349
    fun distinct _ NONE =
454f4be984b7 adaptions in codegen
haftmann
parents: 20071
diff changeset
   350
          NONE
454f4be984b7 adaptions in codegen
haftmann
parents: 20071
diff changeset
   351
      | distinct (IConst _) x =
454f4be984b7 adaptions in codegen
haftmann
parents: 20071
diff changeset
   352
          x
454f4be984b7 adaptions in codegen
haftmann
parents: 20071
diff changeset
   353
      | distinct (IVar v) (SOME vs) =
454f4be984b7 adaptions in codegen
haftmann
parents: 20071
diff changeset
   354
          if member (op =) vs v then NONE else SOME (v::vs)
20439
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   355
      | distinct (t1 `$ t2) x =
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   356
          x |> distinct t1 |> distinct t2
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   357
      | distinct (_ `|-> t) x =
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   358
          x |> distinct t
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20071
diff changeset
   359
      | distinct (INum _) x =
454f4be984b7 adaptions in codegen
haftmann
parents: 20071
diff changeset
   360
          x
454f4be984b7 adaptions in codegen
haftmann
parents: 20071
diff changeset
   361
      | distinct (IChar _) x =
454f4be984b7 adaptions in codegen
haftmann
parents: 20071
diff changeset
   362
          x
20439
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   363
      | distinct (ICase (((td, _), bs), _)) x =
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   364
          x |> distinct td |> fold (fn (p, t) => distinct p #> distinct t) bs;
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   365
  in is_some (fold distinct ts (SOME [])) end;
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20071
diff changeset
   366
20439
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   367
fun eta_expand (c as (_, (_, ty)), ts) k =
19607
07eeb832f28d introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents: 19597
diff changeset
   368
  let
20439
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   369
    val j = length ts;
19607
07eeb832f28d introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents: 19597
diff changeset
   370
    val l = k - j;
07eeb832f28d introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents: 19597
diff changeset
   371
    val tys = (curry Library.take l o curry Library.drop j o fst o unfold_fun) ty;
20439
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   372
    val vs_tys = Name.names (fold Name.declare (fold add_varnames ts []) Name.context) "a" tys;
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   373
  in vs_tys `|--> IConst c `$$ ts @ map (fn (v, _) => IVar v) vs_tys end;
19607
07eeb832f28d introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents: 19597
diff changeset
   374
18304
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   375
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   376
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   377
(** language module system - definitions, modules, transactions **)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   378
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   379
(* type definitions *)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   380
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   381
type typscheme = (vname * sort) list * itype;
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   382
datatype def =
19816
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
   383
    Bot
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   384
  | Fun of (iterm list * iterm) list * typscheme
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   385
  | Typesyn of typscheme
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   386
  | Datatype of (vname * sort) list * (string * itype list) list
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   387
  | Datatypecons of string
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   388
  | Class of class list * (vname * (string * itype) list)
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   389
  | Classmember of class
20389
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   390
  | Classinst of (class * (string * (vname * sort) list))
20466
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   391
        * ((class * (string * inst list list)) list
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   392
      * (string * iterm) list);
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   393
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   394
datatype node = Def of def | Module of node Graph.T;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   395
type module = node Graph.T;
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   396
type transact = Graph.key option * module;
19816
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
   397
type 'dst transact_fin = 'dst * module;
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   398
exception FAIL of string list * exn option;
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   399
19597
8ced57ffc090 major refinement of codegen_theorems.ML
haftmann
parents: 19482
diff changeset
   400
val eq_def = (op =) : def * def -> bool;
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   401
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   402
(* simple diagnosis *)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   403
19816
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
   404
fun pretty_def Bot =
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
   405
      Pretty.str "<Bot>"
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   406
  | pretty_def (Fun (eqs, (vs, ty))) =
18812
a4554848b59e renamed Pretty.gen_list to Pretty.enum;
wenzelm
parents: 18756
diff changeset
   407
      Pretty.enum " |" "" "" (
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   408
        map (fn (ps, body) =>
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   409
          Pretty.block [
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20071
diff changeset
   410
            Pretty.enum "," "[" "]" (map pretty_iterm ps),
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   411
            Pretty.str " |->",
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   412
            Pretty.brk 1,
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20071
diff changeset
   413
            pretty_iterm body,
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   414
            Pretty.str "::",
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   415
            pretty_typparms vs,
19341
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19300
diff changeset
   416
            Pretty.str "/",
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   417
            pretty_itype ty
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   418
          ]) eqs
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   419
        )
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   420
  | pretty_def (Typesyn (vs, ty)) =
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   421
      Pretty.block [
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   422
        pretty_typparms vs,
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   423
        Pretty.str " |=> ",
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   424
        pretty_itype ty
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   425
      ]
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 19025
diff changeset
   426
  | pretty_def (Datatype (vs, cs)) =
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   427
      Pretty.block [
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   428
        pretty_typparms vs,
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   429
        Pretty.str " |=> ",
18852
f1e2602ca7ba replaced gen_list by enum
haftmann
parents: 18850
diff changeset
   430
        Pretty.enum " |" "" ""
18850
92ef83e5eaea various improvements
haftmann
parents: 18812
diff changeset
   431
          (map (fn (c, tys) => (Pretty.block o Pretty.breaks)
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 19025
diff changeset
   432
            (Pretty.str c :: map pretty_itype tys)) cs)
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   433
      ]
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   434
  | pretty_def (Datatypecons dtname) =
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   435
      Pretty.str ("cons " ^ dtname)
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 19025
diff changeset
   436
  | pretty_def (Class (supcls, (v, mems))) =
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   437
      Pretty.block [
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   438
        Pretty.str ("class var " ^ v ^ " extending "),
18812
a4554848b59e renamed Pretty.gen_list to Pretty.enum;
wenzelm
parents: 18756
diff changeset
   439
        Pretty.enum "," "[" "]" (map Pretty.str supcls),
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   440
        Pretty.str " with ",
18852
f1e2602ca7ba replaced gen_list by enum
haftmann
parents: 18850
diff changeset
   441
        Pretty.enum "," "[" "]"
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   442
          (map (fn (m, ty) => Pretty.block
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 19025
diff changeset
   443
            [Pretty.str (m ^ "::"), pretty_itype ty]) mems)
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   444
      ]
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   445
  | pretty_def (Classmember clsname) =
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   446
      Pretty.block [
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   447
        Pretty.str "class member belonging to ",
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   448
        Pretty.str clsname
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   449
      ]
20389
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   450
  | pretty_def (Classinst ((clsname, (tyco, arity)), _)) =
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   451
      Pretty.block [
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   452
        Pretty.str "class instance (",
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   453
        Pretty.str clsname,
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   454
        Pretty.str ", (",
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   455
        Pretty.str tyco,
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   456
        Pretty.str ", ",
18852
f1e2602ca7ba replaced gen_list by enum
haftmann
parents: 18850
diff changeset
   457
        Pretty.enum "," "[" "]" (map (Pretty.enum "," "{" "}" o
18850
92ef83e5eaea various improvements
haftmann
parents: 18812
diff changeset
   458
          map Pretty.str o snd) arity),
18515
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18454
diff changeset
   459
        Pretty.str "))"
20389
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   460
      ];
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   461
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   462
fun pretty_module modl =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   463
  let
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   464
    fun pretty (name, Module modl) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   465
          Pretty.block (
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   466
            Pretty.str ("module " ^ name ^ " {")
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   467
            :: Pretty.brk 1
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   468
            :: Pretty.chunks (map pretty (AList.make (Graph.get_node modl)
19482
9f11af8f7ef9 tuned basic list operators (flat, maps, map_filter);
wenzelm
parents: 19466
diff changeset
   469
                 (Graph.strong_conn modl |> flat |> rev)))
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   470
            :: Pretty.str "}" :: nil
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   471
          )
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   472
      | pretty (name, Def def) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   473
          Pretty.block [Pretty.str name, Pretty.str " :=", Pretty.brk 1, pretty_def def]
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   474
  in pretty ("//", Module modl) end;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   475
18360
a2c9506b62a7 improved class handling
haftmann
parents: 18335
diff changeset
   476
fun pretty_deps modl =
a2c9506b62a7 improved class handling
haftmann
parents: 18335
diff changeset
   477
  let
a2c9506b62a7 improved class handling
haftmann
parents: 18335
diff changeset
   478
    fun one_node key =
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   479
      let
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   480
        val preds_ = Graph.imm_preds modl key;
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   481
        val succs_ = Graph.imm_succs modl key;
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   482
        val mutbs = gen_inter (op =) (preds_, succs_);
19300
7689f81f8996 subtract (op =);
wenzelm
parents: 19253
diff changeset
   483
        val preds = subtract (op =) mutbs preds_;
7689f81f8996 subtract (op =);
wenzelm
parents: 19253
diff changeset
   484
        val succs = subtract (op =) mutbs succs_;
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   485
      in
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   486
        (Pretty.block o Pretty.fbreaks) (
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   487
          Pretty.str key
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   488
          :: map (fn s => Pretty.str ("<-> " ^ s)) mutbs
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   489
          @ map (fn s => Pretty.str ("<-- " ^ s)) preds
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   490
          @ map (fn s => Pretty.str ("--> " ^ s)) succs
18850
92ef83e5eaea various improvements
haftmann
parents: 18812
diff changeset
   491
          @ (the_list oo Option.mapPartial)
92ef83e5eaea various improvements
haftmann
parents: 18812
diff changeset
   492
            ((fn Module modl' => SOME (pretty_deps modl')
92ef83e5eaea various improvements
haftmann
parents: 18812
diff changeset
   493
               | _ => NONE) o Graph.get_node modl) (SOME key)
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   494
        )
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   495
      end
18360
a2c9506b62a7 improved class handling
haftmann
parents: 18335
diff changeset
   496
  in
a2c9506b62a7 improved class handling
haftmann
parents: 18335
diff changeset
   497
    modl
a2c9506b62a7 improved class handling
haftmann
parents: 18335
diff changeset
   498
    |> Graph.strong_conn
19482
9f11af8f7ef9 tuned basic list operators (flat, maps, map_filter);
wenzelm
parents: 19466
diff changeset
   499
    |> flat
18360
a2c9506b62a7 improved class handling
haftmann
parents: 18335
diff changeset
   500
    |> rev
a2c9506b62a7 improved class handling
haftmann
parents: 18335
diff changeset
   501
    |> map one_node
a2c9506b62a7 improved class handling
haftmann
parents: 18335
diff changeset
   502
    |> Pretty.chunks
a2c9506b62a7 improved class handling
haftmann
parents: 18335
diff changeset
   503
  end;
a2c9506b62a7 improved class handling
haftmann
parents: 18335
diff changeset
   504
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   505
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   506
(* name handling *)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   507
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   508
fun dest_name name =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   509
  let
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   510
    val name' = NameSpace.unpack name
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   511
    val (name'', name_base) = split_last name'
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   512
    val (modl, shallow) = split_last name''
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   513
  in (modl, NameSpace.pack [shallow, name_base]) end
20389
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   514
  handle Empty => error ("Not a qualified name: " ^ quote name);
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   515
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   516
fun has_nsp name shallow =
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   517
  NameSpace.is_qualified name
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   518
  andalso let
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   519
    val name' = NameSpace.unpack name
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   520
    val (name'', _) = split_last name'
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   521
    val (_, shallow') = split_last name''
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   522
  in shallow' = shallow end;
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   523
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   524
fun dest_modl (Module m) = m;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   525
fun dest_def (Def d) = d;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   526
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   527
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   528
(* modules *)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   529
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   530
val empty_module = Graph.empty; (*read: "depends on"*)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   531
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   532
fun get_def modl name =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   533
  case dest_name name
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   534
   of (modlname, base) =>
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   535
        let
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   536
          fun get (Module node) [] =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   537
                (dest_def o Graph.get_node node) base
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   538
            | get (Module node) (m::ms) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   539
                get (Graph.get_node node m) ms
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   540
        in get (Module modl) modlname end;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   541
19816
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
   542
fun is_def modl name =
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
   543
  case try (get_def modl) name
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
   544
   of NONE => false
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
   545
    | SOME Bot => false
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
   546
    | _ => true;
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
   547
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   548
fun add_def (name, def) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   549
  let
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   550
    val (modl, base) = dest_name name;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   551
    fun add [] =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   552
          Graph.new_node (base, Def def)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   553
      | add (m::ms) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   554
          Graph.default_node (m, Module empty_module)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   555
          #> Graph.map_node m (Module o add ms o dest_modl)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   556
  in add modl end;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   557
19816
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
   558
fun map_def name f =
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
   559
  let
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
   560
    val (modl, base) = dest_name name;
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
   561
    fun mapp [] =
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
   562
          Graph.map_node base (Def o f o dest_def)
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
   563
      | mapp (m::ms) =
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
   564
          Graph.map_node m (Module o mapp ms o dest_modl)
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
   565
  in mapp modl end;
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
   566
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
   567
fun ensure_bot name =
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
   568
  let
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
   569
    val (modl, base) = dest_name name;
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
   570
    fun ensure [] module =
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
   571
          (case try (Graph.get_node module) base
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
   572
           of NONE =>
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
   573
                module
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
   574
                |> Graph.new_node (base, Def Bot)
20389
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   575
            | SOME (Module _) => error ("Module already present: " ^ quote name)
19816
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
   576
            | _ => module)
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
   577
      | ensure (m::ms) module =
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
   578
          module
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
   579
          |> Graph.default_node (m, Module empty_module)
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
   580
          |> Graph.map_node m (Module o ensure ms o dest_modl)
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
   581
  in ensure modl end;
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
   582
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
   583
fun add_def_incr strict (name, Bot) module =
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
   584
      (case try (get_def module) name
20389
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   585
       of NONE => if strict then error "Attempted to add Bot to module"
19816
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
   586
            else map_def name (K Bot) module
20389
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   587
        | SOME Bot => if strict then error "Attempted to add Bot to module"
19816
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
   588
            else map_def name (K Bot) module
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
   589
        | SOME _ => module)
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
   590
  | add_def_incr _ (name, def) module =
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
   591
      (case try (get_def module) name
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
   592
       of NONE => add_def (name, def) module
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
   593
        | SOME Bot => map_def name (K def) module
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
   594
        | SOME def' => if eq_def (def, def')
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
   595
            then module
20389
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   596
            else error ("Tried to overwrite definition " ^ quote name));
19816
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
   597
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   598
fun add_dep (name1, name2) modl =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   599
  if name1 = name2 then modl
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   600
  else
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   601
    let
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   602
      val m1 = dest_name name1 |> apsnd single |> (op @);
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   603
      val m2 = dest_name name2 |> apsnd single |> (op @);
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20071
diff changeset
   604
      val (ms, (r1, r2)) = chop_prefix (op =) (m1, m2);
454f4be984b7 adaptions in codegen
haftmann
parents: 20071
diff changeset
   605
      val (ms, (s1::r1, s2::r2)) = chop_prefix (op =) (m1, m2);
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   606
      val add_edge =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   607
        if null r1 andalso null r2
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   608
        then Graph.add_edge
19785
52d71ee5c8a8 bugfixes
haftmann
parents: 19616
diff changeset
   609
        else fn edge => fn gr => (Graph.add_edge_acyclic edge gr
19884
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   610
          handle Graph.CYCLES _ =>
20389
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   611
            error ("Adding dependency "
19884
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   612
              ^ quote name1 ^ " -> " ^ quote name2 ^ " would result in module dependency cycle"))
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   613
      fun add [] node =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   614
            node
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   615
            |> add_edge (s1, s2)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   616
        | add (m::ms) node =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   617
            node
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   618
            |> Graph.map_node m (Module o add ms o dest_modl);
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   619
    in add ms modl end;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   620
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   621
fun merge_module modl12 =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   622
  let
19025
596fb1eb7856 simplified TableFun.join;
wenzelm
parents: 18963
diff changeset
   623
    fun join_module _ (Module m1, Module m2) =
596fb1eb7856 simplified TableFun.join;
wenzelm
parents: 18963
diff changeset
   624
          Module (merge_module (m1, m2))
596fb1eb7856 simplified TableFun.join;
wenzelm
parents: 18963
diff changeset
   625
      | join_module name (Def d1, Def d2) =
19884
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   626
          if eq_def (d1, d2) then Def d1 else Def Bot
19025
596fb1eb7856 simplified TableFun.join;
wenzelm
parents: 18963
diff changeset
   627
      | join_module name _ = raise Graph.DUP name
596fb1eb7856 simplified TableFun.join;
wenzelm
parents: 18963
diff changeset
   628
  in Graph.join join_module modl12 end;
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   629
19042
630b8dd0b31a exported some interfaces useful for other code generator approaches
haftmann
parents: 19038
diff changeset
   630
fun diff_module modl12 =
630b8dd0b31a exported some interfaces useful for other code generator approaches
haftmann
parents: 19038
diff changeset
   631
  let
20191
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   632
    fun diff_entry prefix modl2 (name, Def def1) =
19042
630b8dd0b31a exported some interfaces useful for other code generator approaches
haftmann
parents: 19038
diff changeset
   633
          let
630b8dd0b31a exported some interfaces useful for other code generator approaches
haftmann
parents: 19038
diff changeset
   634
            val e2 = try (Graph.get_node modl2) name
630b8dd0b31a exported some interfaces useful for other code generator approaches
haftmann
parents: 19038
diff changeset
   635
          in if is_some e2 andalso eq_def (def1, (dest_def o the) e2)
630b8dd0b31a exported some interfaces useful for other code generator approaches
haftmann
parents: 19038
diff changeset
   636
            then I
630b8dd0b31a exported some interfaces useful for other code generator approaches
haftmann
parents: 19038
diff changeset
   637
            else cons (NameSpace.pack (prefix @ [name]), def1)
630b8dd0b31a exported some interfaces useful for other code generator approaches
haftmann
parents: 19038
diff changeset
   638
          end
630b8dd0b31a exported some interfaces useful for other code generator approaches
haftmann
parents: 19038
diff changeset
   639
      | diff_entry prefix modl2 (name, Module modl1) =
630b8dd0b31a exported some interfaces useful for other code generator approaches
haftmann
parents: 19038
diff changeset
   640
          diff_modl (prefix @ [name]) (modl1,
630b8dd0b31a exported some interfaces useful for other code generator approaches
haftmann
parents: 19038
diff changeset
   641
            (the_default empty_module o Option.map dest_modl o try (Graph.get_node modl2)) name)
630b8dd0b31a exported some interfaces useful for other code generator approaches
haftmann
parents: 19038
diff changeset
   642
    and diff_modl prefix (modl1, modl2) =
630b8dd0b31a exported some interfaces useful for other code generator approaches
haftmann
parents: 19038
diff changeset
   643
      fold (diff_entry prefix modl2)
19466
wenzelm
parents: 19341
diff changeset
   644
        ((AList.make (Graph.get_node modl1) o flat o Graph.strong_conn) modl1)
19042
630b8dd0b31a exported some interfaces useful for other code generator approaches
haftmann
parents: 19038
diff changeset
   645
  in diff_modl [] modl12 [] end;
630b8dd0b31a exported some interfaces useful for other code generator approaches
haftmann
parents: 19038
diff changeset
   646
19884
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   647
fun project_module names modl =
18335
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   648
  let
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   649
    datatype pathnode = PN of (string list * (string * pathnode) list);
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   650
    fun mk_ipath ([], base) (PN (defs, modls)) =
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   651
          PN (base :: defs, modls)
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   652
      | mk_ipath (n::ns, base) (PN (defs, modls)) =
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   653
          modls
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   654
          |> AList.default (op =) (n, PN ([], []))
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   655
          |> AList.map_entry (op =) n (mk_ipath (ns, base))
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   656
          |> (pair defs #> PN);
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   657
    fun select (PN (defs, modls)) (Module module) =
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   658
      module
20191
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   659
      |> Graph.project (member (op =) ((*!*) Graph.all_succs module (defs @ map fst modls)))
18335
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   660
      |> fold (fn (name, modls) => Graph.map_node name (select modls)) modls
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   661
      |> Module;
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   662
  in
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   663
    Module modl
18850
92ef83e5eaea various improvements
haftmann
parents: 18812
diff changeset
   664
    |> select (fold (mk_ipath o dest_name)
92ef83e5eaea various improvements
haftmann
parents: 18812
diff changeset
   665
         (filter NameSpace.is_qualified names) (PN ([], [])))
18335
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   666
    |> dest_modl
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   667
  end;
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   668
19884
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   669
fun purge_module names modl =
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   670
  let
19884
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   671
    fun split_names names =
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   672
      fold
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   673
        (fn ([], name) => apfst (cons name)
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   674
          | (m::ms, name) => apsnd (AList.default (op =) (m : string, [])
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   675
              #> AList.map_entry (op =) m (cons (ms, name))))
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   676
        names ([], []);
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   677
    fun purge names (Module modl) =
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   678
      let
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   679
        val (ndefs, nmodls) = split_names names;
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   680
      in
20191
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   681
        modl
19884
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   682
        |> Graph.del_nodes (Graph.all_preds modl ndefs)
20191
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   683
        |> Graph.del_nodes ndefs
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   684
        |> Graph.del_nodes (Graph.all_preds modl (map fst nmodls))
19884
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   685
        |> fold (fn (nmodl, names') => Graph.map_node nmodl (purge names')) nmodls
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   686
        |> Module
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   687
      end;
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   688
  in
19884
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   689
    Module modl
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   690
    |> purge (map dest_name names)
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   691
    |> dest_modl
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   692
  end;
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   693
20428
67fa1c6ba89e refinements
haftmann
parents: 20405
diff changeset
   694
fun flat_module modl =
67fa1c6ba89e refinements
haftmann
parents: 20405
diff changeset
   695
  maps (
67fa1c6ba89e refinements
haftmann
parents: 20405
diff changeset
   696
   fn (name, Module modl) => map (apfst (NameSpace.append name)) (flat_module modl)
67fa1c6ba89e refinements
haftmann
parents: 20405
diff changeset
   697
    | (name, Def def) => [(name, def)]
67fa1c6ba89e refinements
haftmann
parents: 20405
diff changeset
   698
  ) ((AList.make (Graph.get_node modl) o flat o Graph.strong_conn) modl)
67fa1c6ba89e refinements
haftmann
parents: 20405
diff changeset
   699
67fa1c6ba89e refinements
haftmann
parents: 20405
diff changeset
   700
(*
20466
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   701
(*FIXME: graph-based approach is better.
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   702
* build graph
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   703
* implement flat_classops on sort level, not class level
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   704
* flat_instances bleibt wie es ist
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   705
*)
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   706
fun flat_classops modl =
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   707
  let
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   708
    fun add_ancestry class anc =
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   709
      let
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   710
        val SOME (Class (super_classes, (v, ops))) = AList.lookup (op =) modl class
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   711
        val super_classees' = filter (not o member (fn (c', (c, _)) => c = c') anc) super_classes;
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   712
      in
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   713
        [(class, ops)] @ anc
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   714
        |> fold add_ancestry super_classees'
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   715
      end;
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   716
  in
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   717
    Symtab.empty
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   718
    |> fold (
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   719
         fn (class, Class _) =>
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   720
              Symtab.update_new (class, maps snd (add_ancestry class []))
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   721
           | _ => I
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   722
       ) modl
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   723
    |> the oo Symtab.lookup
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   724
  end;
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   725
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   726
fun flat_instances modl =
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   727
  let
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   728
    fun add_ancestry instance instsss anc =
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   729
      let
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   730
        val SOME (Classinst (_, (super_instances, ops))) = AList.lookup (op =) modl instance;
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   731
        val super_instances' = filter (not o member (eq_fst (op =)) anc) super_instances;
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   732
        val ops' = map (apsnd (rpair instsss)) ops;
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   733
        (*FIXME: build types*)
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   734
      in
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   735
        [(instance, ops')] @ anc
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   736
        |> fold (fn (_, (instance, instss)) => add_ancestry instance (instss :: instsss)) super_instances'
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   737
      end;
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   738
  in
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   739
    Symtab.empty
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   740
    |> fold (
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   741
         fn (instance, Classinst _) =>
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   742
              Symtab.update_new (instance, maps snd (add_ancestry instance [] []))
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   743
           | _ => I
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   744
       ) modl
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   745
    |> the oo Symtab.lookup
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   746
  end;
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   747
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   748
fun flat_fundef classops instdefs is_classop (eqs, (vs, ty)) =
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   749
  let
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   750
    fun fold_map_snd' f (x, ys) = fold_map (f x) ys;
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   751
    fun fold_map_snd f (x, ys) = fold_map f ys #-> (fn zs => pair (x, zs));
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   752
    val names =
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   753
      Name.context
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   754
      |> fold Name.declare
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   755
           (fold (fn (rhs, lhs) => fold add_varnames rhs #> add_varnames lhs) eqs []);
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   756
    val opmap = [] : (string * (string * (string * itype) list) list) list;
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   757
    val (params, tys) = (split_list o maps snd o maps snd) opmap;
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   758
    (*fun name_ops v' class = 
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   759
      (fold_map o fold_map_snd')
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   760
        (fn (class, v) => fn (c, ty) => Name.variants [c] #-> (fn [p] =>
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   761
          pair (class, v') (c, (ty, p))))
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   762
          (classops class);
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   763
    val (opsmap, _) = (fold_map o fold_map_snd') name_ops vs names;
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   764
    (* --> (iterm * itype) list *)*)
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   765
    fun flat_inst (Instance (instance, instss)) =
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   766
          let
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   767
            val xs : (string * (iterm * (itype * inst list list list))) list = instdefs instance
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   768
            fun mk_t (t, (ty, instsss)) =
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   769
              (Library.foldl (fn (t, instss) => t `$$ map (fst o snd) ((maps o maps) flat_inst instss))
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   770
                (t, instss :: instsss), ty)
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   771
          in
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   772
            map (apsnd mk_t) xs
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   773
          end
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   774
      | flat_inst (Context (classes, (v, k))) =
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   775
          let
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   776
            val _ : 'a = classops (hd classes);
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   777
          in
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   778
            []
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   779
          end
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   780
          (*
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   781
            val parm_map = nth ((the o AList.lookup (op =) octxt) v)
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   782
              (if k = ~1 then 0 else k);
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   783
          in map (apfst IVar o swap o snd) (case classes
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   784
           of class::_ => (the o AList.lookup (op =) parm_map) class
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   785
            | _ => (snd o hd) parm_map)*)
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   786
    and flat_iterm (e as IConst (c, (lss, ty))) =
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   787
          if is_classop c then let
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   788
            val tab = (maps o maps) flat_inst lss;
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   789
            val SOME (t, _) = AList.lookup (op =) tab c;
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   790
          in t end else let
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   791
            val (es, tys) = (split_list o map snd) ((maps o maps) flat_inst lss)
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   792
          in IConst (c, (replicate (length lss) [], tys `--> ty)) `$$ es end
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   793
      | flat_iterm (e as IVar _) =
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   794
          e
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   795
      | flat_iterm (e1 `$ e2) =
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   796
          flat_iterm e1 `$ flat_iterm e2
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   797
      | flat_iterm (v_ty `|-> e) =
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   798
          v_ty `|-> flat_iterm e
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   799
      | flat_iterm (INum (k, e)) =
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   800
          INum (k, flat_iterm e)
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   801
      | flat_iterm (IChar (s, e)) =
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   802
          IChar (s, flat_iterm e)
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   803
      | flat_iterm (ICase (((de, dty), es), e)) =
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   804
          ICase (((flat_iterm de, dty), map (pairself flat_iterm) es), flat_iterm e);
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   805
    fun flat_eq (lhs, rhs) = (map IVar params @ lhs, flat_iterm rhs);
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   806
  in (map flat_eq eqs, (map (apsnd (K [])) vs, tys `--> ty)) end;
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   807
20428
67fa1c6ba89e refinements
haftmann
parents: 20405
diff changeset
   808
fun flat_funs_datatypes modl =
20466
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   809
  let
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   810
    val modl = flat_module modl;
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   811
    val classops = flat_classops modl;
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   812
    val instdefs = flat_instances modl;
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   813
    val is_classop = is_some o AList.lookup (op =) modl;
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   814
  in map_filter (
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   815
   fn def as (_, Datatype _) => SOME def
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   816
    | (name, Fun funn) => SOME (name, (Fun (flat_fundef classops instdefs is_classop funn)))
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   817
    | _ => NONE
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   818
  ) end;
20428
67fa1c6ba89e refinements
haftmann
parents: 20405
diff changeset
   819
*)
67fa1c6ba89e refinements
haftmann
parents: 20405
diff changeset
   820
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   821
val add_deps_of_typparms =
20191
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   822
  fold (fn (v : vname, sort : sort) => fold (insert (op =)) sort);
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   823
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20466
diff changeset
   824
fun add_deps_of_classlookup (Instance (inst, lss)) =
6d75e02ed285 added codegen_data
haftmann
parents: 20466
diff changeset
   825
      insert (op =) inst
20191
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   826
      #> (fold o fold) add_deps_of_classlookup lss
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   827
  | add_deps_of_classlookup (Context (clss, _)) =
20191
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   828
      fold (insert (op =)) clss;
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   829
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   830
fun add_deps_of_type (tyco `%% tys) =
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   831
      insert (op =) tyco
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   832
      #> fold add_deps_of_type tys
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   833
  | add_deps_of_type  (ty1 `-> ty2) =
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   834
      add_deps_of_type ty1
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   835
      #> add_deps_of_type ty2
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   836
  | add_deps_of_type (ITyVar v) =
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   837
      I;
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   838
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   839
fun add_deps_of_term (IConst (c, (lss, ty))) =
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   840
      insert (op =) c
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   841
      #> add_deps_of_type ty
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   842
      #> (fold o fold) add_deps_of_classlookup lss
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   843
  | add_deps_of_term (IVar _) =
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   844
      I
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   845
  | add_deps_of_term (e1 `$ e2) =
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   846
      add_deps_of_term e1 #> add_deps_of_term e2
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   847
  | add_deps_of_term ((_, ty) `|-> e) =
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   848
      add_deps_of_type ty
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   849
      #> add_deps_of_term e
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   850
  | add_deps_of_term (INum _) =
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   851
      I
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   852
  | add_deps_of_term (IChar (_, e)) =
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   853
      add_deps_of_term e
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   854
  | add_deps_of_term (ICase (_, e)) =
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   855
      add_deps_of_term e;
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   856
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   857
fun deps_of Bot =
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   858
      []
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   859
  | deps_of (Fun (eqs, (vs, ty))) =
20191
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   860
      []
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   861
      |> add_deps_of_typparms vs
20191
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   862
      |> add_deps_of_type ty
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   863
      |> fold (fn (lhs, rhs) => fold add_deps_of_term lhs #> add_deps_of_term rhs) eqs
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   864
  | deps_of (Typesyn (vs, ty)) =
20191
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   865
      []
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   866
      |> add_deps_of_typparms vs
20191
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   867
      |> add_deps_of_type ty
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   868
  | deps_of (Datatype (vs, cos)) =
20191
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   869
      []
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   870
      |> add_deps_of_typparms vs
20191
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   871
      |> fold (fn (c, tys) => insert (op =) c #> fold add_deps_of_type tys) cos
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   872
  | deps_of (Datatypecons dtco) =
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   873
      [dtco]
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   874
  | deps_of (Class (supclss, (_, memdecls))) =
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   875
      []
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   876
      |> fold (insert (op =)) supclss
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   877
      |> fold (fn (name, ty) =>
20191
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   878
            insert (op =) name
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   879
            #> add_deps_of_type ty
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   880
      ) memdecls
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   881
  | deps_of (Classmember class) =
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   882
      [class]
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   883
  | deps_of (Classinst ((class, (tyco, vs)), (suparities, memdefs))) =
20191
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   884
      []
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   885
      |> insert (op =) class
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   886
      |> insert (op =) tyco
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   887
      |> add_deps_of_typparms vs
20466
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   888
      |> fold (fn (supclass, (supinst, lss)) =>
20191
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   889
            insert (op =) supclass
20466
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   890
            #> insert (op =) supinst
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   891
            #> (fold o fold) add_deps_of_classlookup lss
20191
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   892
      ) suparities
20389
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   893
      |> fold (fn (name, e) =>
20191
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   894
            insert (op =) name
20389
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   895
            #> add_deps_of_term e
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   896
      ) memdefs;
20191
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   897
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   898
fun delete_garbage hidden modl =
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   899
  let
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   900
    fun allnames modl =
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   901
      let
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   902
        val entries = AList.make (Graph.get_node modl) (Graph.keys modl)
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   903
        fun is_def (name, Module _) = NONE
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   904
          | is_def (name, _) = SOME name;
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   905
        fun is_modl (name, Module modl) = SOME (name, modl)
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   906
          | is_modl (name, _) = NONE;
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   907
        val defs = map_filter is_def entries;
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   908
        val modls = map_filter is_modl entries;
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   909
      in
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   910
        defs
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   911
        @ maps (fn (name, modl) => map (NameSpace.append name) (allnames modl)) modls
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   912
      end;
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   913
    fun alldeps modl =
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   914
      let
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   915
        val entries = AList.make (Graph.get_node modl) (Graph.keys modl)
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   916
        fun is_def (name, Module _) = NONE
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   917
          | is_def (name, _) = SOME name;
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   918
        fun is_modl (name, Module modl) = SOME (name, modl)
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   919
          | is_modl (name, _) = NONE;
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   920
        val defs = map_filter is_def entries;
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   921
        val modls = map_filter is_modl entries;
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   922
      in
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   923
        maps (fn name => map (pair (name)) (Graph.imm_succs modl name)) defs
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   924
        @ maps (fn (name, modl) => (map o pairself) (NameSpace.append name) (alldeps modl)) modls
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   925
      end;
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   926
    val names = subtract (op =) hidden (allnames modl);
20353
d73e49780ef2 code generator refinements
haftmann
parents: 20216
diff changeset
   927
(*     val _ = writeln "HIDDEN";  *)
d73e49780ef2 code generator refinements
haftmann
parents: 20216
diff changeset
   928
(*     val _ = (writeln o commas) hidden;  *)
d73e49780ef2 code generator refinements
haftmann
parents: 20216
diff changeset
   929
(*     val _ = writeln "NAMES";  *)
d73e49780ef2 code generator refinements
haftmann
parents: 20216
diff changeset
   930
(*     val _ = (writeln o commas) names;  *)
20191
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   931
    fun is_bot name =
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   932
      case get_def modl name of Bot => true | _ => false;
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   933
    val bots = filter is_bot names;
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   934
    val defs = filter (not o is_bot) names;
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   935
    val expldeps =
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   936
      Graph.empty
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   937
      |> fold (fn name => Graph.new_node (name, ())) names
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   938
      |> fold (fn name => fold (curry Graph.add_edge name)
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   939
            (deps_of (get_def modl name) |> subtract (op =) hidden)) names
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   940
    val bots' = fold (insert op =) bots (Graph.all_preds expldeps bots);
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   941
    val selected = subtract (op =) bots' names;
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   942
(*     val deps = filter (fn (x, y) => member (op =) selected x andalso member (op =) selected y)  *)
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   943
    val adddeps = maps (fn (n, ns) => map (pair n) ns) (expldeps |> Graph.del_nodes bots' |> Graph.dest);
20353
d73e49780ef2 code generator refinements
haftmann
parents: 20216
diff changeset
   944
(*    val _ = writeln "SELECTED";
20191
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   945
    val _ = (writeln o commas) selected;
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   946
    val _ = writeln "DEPS";
20353
d73e49780ef2 code generator refinements
haftmann
parents: 20216
diff changeset
   947
    val _ = (writeln o cat_lines o map (fn (x, y) => x ^ " -> " ^ y)) adddeps; *)
20191
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   948
  in
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   949
    empty_module
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   950
    |> fold (fn name => add_def (name, get_def modl name)) selected
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   951
(*     |> fold ensure_bot (hidden @ bots')  *)
20216
f30b73385060 added eval_term
haftmann
parents: 20192
diff changeset
   952
    |> fold (fn (x, y) => ((*writeln ("adding " ^ x ^ " -> " ^ y);*) add_dep (x, y))) adddeps
20191
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   953
  end;
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   954
19884
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   955
fun allimports_of modl =
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   956
  let
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   957
    fun imps_of prfx (Module modl) imps tab =
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   958
          let
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   959
            val this = NameSpace.pack prfx;
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   960
            val name_con = (rev o Graph.strong_conn) modl;
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   961
          in
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   962
            tab
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   963
            |> pair []
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   964
            |> fold (fn names => fn (imps', tab) =>
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   965
                tab
20191
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   966
                |> fold_map (fn name =>
19884
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   967
                     imps_of (prfx @ [name]) (Graph.get_node modl name) (imps' @ imps)) names
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   968
                |-> (fn imps'' => pair (flat imps'' @ imps'))) name_con
20191
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   969
            |-> (fn imps' =>
19884
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   970
               Symtab.update_new (this, imps' @ imps)
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   971
            #> pair (this :: imps'))
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   972
          end
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   973
      | imps_of prfx (Def _) imps tab =
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   974
          ([], tab);
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   975
  in snd (imps_of [] (Module modl) [] Symtab.empty) end;
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   976
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   977
fun check_samemodule names =
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   978
  fold (fn name =>
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   979
    let
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   980
      val modn = (fst o dest_name) name
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   981
    in
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   982
     fn NONE => SOME modn
18850
92ef83e5eaea various improvements
haftmann
parents: 18812
diff changeset
   983
      | SOME mod' => if modn = mod' then SOME modn
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   984
          else error ("Inconsistent name prefix for simultanous names: " ^ commas_quote names)
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   985
    end
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   986
  ) names NONE;
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   987
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   988
fun check_funeqs eqs =
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   989
  (fold (fn (pats, _) =>
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   990
    let
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   991
      val l = length pats
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   992
    in
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   993
     fn NONE => SOME l
18850
92ef83e5eaea various improvements
haftmann
parents: 18812
diff changeset
   994
      | SOME l' => if l = l' then SOME l
20389
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   995
          else error "Function definition with different number of arguments"
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   996
    end
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   997
  ) eqs NONE; eqs);
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   998
19816
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
   999
fun check_prep_def modl Bot =
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
  1000
      Bot
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1001
  | check_prep_def modl (Fun (eqs, d)) =
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1002
      Fun (check_funeqs eqs, d)
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1003
  | check_prep_def modl (d as Typesyn _) =
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1004
      d
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 19025
diff changeset
  1005
  | check_prep_def modl (d as Datatype _) =
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 19025
diff changeset
  1006
      d
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1007
  | check_prep_def modl (Datatypecons dtco) =
20389
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
  1008
      error "Attempted to add bare datatype constructor"
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 19025
diff changeset
  1009
  | check_prep_def modl (d as Class _) =
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 19025
diff changeset
  1010
      d
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1011
  | check_prep_def modl (Classmember _) =
20389
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
  1012
      error "Attempted to add bare class member"
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
  1013
  | check_prep_def modl (d as Classinst ((class, (tyco, arity)), (_, memdefs))) =
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
  1014
      let
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 19025
diff changeset
  1015
        val Class (_, (v, membrs)) = get_def modl class;
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1016
        val _ = if length memdefs > length memdefs
20389
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
  1017
          then error "Too many member definitions given"
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1018
          else ();
20389
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
  1019
        fun check_memdef (m, _) =
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
  1020
          if AList.defined (op =) memdefs m
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
  1021
          then () else error ("Missing definition for member " ^ quote m);
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
  1022
        val _ = map check_memdef memdefs;
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
  1023
      in d end
19213
ee83040c3c84 first running version of type classes
haftmann
parents: 19202
diff changeset
  1024
  | check_prep_def modl Classinstmember =
20389
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
  1025
      error "Attempted to add bare class instance member";
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
  1026
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 19025
diff changeset
  1027
fun postprocess_def (name, Datatype (_, constrs)) =
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1028
      (check_samemodule (name :: map fst constrs);
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1029
      fold (fn (co, _) =>
19816
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
  1030
        add_def_incr true (co, Datatypecons name)
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1031
        #> add_dep (co, name)
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1032
        #> add_dep (name, co)
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1033
      ) constrs
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1034
      )
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 19025
diff changeset
  1035
  | postprocess_def (name, Class (_, (_, membrs))) =
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1036
      (check_samemodule (name :: map fst membrs);
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1037
      fold (fn (m, _) =>
19816
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
  1038
        add_def_incr true (m, Classmember name)
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1039
        #> add_dep (m, name)
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1040
        #> add_dep (name, m)
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1041
      ) membrs
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1042
      )
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1043
  | postprocess_def _ =
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1044
      I;
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1045
19884
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
  1046
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
  1047
(* transaction protocol *)
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
  1048
19816
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
  1049
fun ensure_def defgen strict msg name (dep, modl) =
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
  1050
  let
20389
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
  1051
    (*FIXME represent dependencies as tuple (name, name -> string), for better error msgs*)
19816
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
  1052
    val msg' = (case dep
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1053
     of NONE => msg
19884
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
  1054
      | SOME dep => msg ^ ", required for " ^ quote dep)
19816
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
  1055
      ^ (if strict then " (strict)" else " (non-strict)");
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1056
    fun add_dp NONE = I
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1057
      | add_dp (SOME dep) =
19341
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19300
diff changeset
  1058
          debug_msg (fn _ => "adding dependency " ^ quote dep ^ " -> " ^ quote name)
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1059
          #> add_dep (dep, name);
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1060
    fun prep_def def modl =
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1061
      (check_prep_def modl def, modl);
19816
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
  1062
    fun invoke_generator name defgen modl =
20191
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
  1063
      if ! soft_exc (*that "!" isn't a "not"...*)
19956
f992e507020e slight improvements in code generation
haftmann
parents: 19953
diff changeset
  1064
      then defgen name (SOME name, modl)
f992e507020e slight improvements in code generation
haftmann
parents: 19953
diff changeset
  1065
        handle FAIL (msgs, exc) =>
f992e507020e slight improvements in code generation
haftmann
parents: 19953
diff changeset
  1066
                if strict then raise FAIL (msg' :: msgs, exc)
f992e507020e slight improvements in code generation
haftmann
parents: 19953
diff changeset
  1067
                else (Bot, modl)
20191
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
  1068
             | e => raise
19956
f992e507020e slight improvements in code generation
haftmann
parents: 19953
diff changeset
  1069
                FAIL (["definition generator for " ^ quote name, msg'], SOME e)
f992e507020e slight improvements in code generation
haftmann
parents: 19953
diff changeset
  1070
      else defgen name (SOME name, modl)
f992e507020e slight improvements in code generation
haftmann
parents: 19953
diff changeset
  1071
        handle FAIL (msgs, exc) =>
19884
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
  1072
              if strict then raise FAIL (msg' :: msgs, exc)
19956
f992e507020e slight improvements in code generation
haftmann
parents: 19953
diff changeset
  1073
              else (Bot, modl);
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
  1074
  in
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
  1075
    modl
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1076
    |> (if can (get_def modl) name
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1077
        then
19341
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19300
diff changeset
  1078
          debug_msg (fn _ => "asserting node " ^ quote name)
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1079
          #> add_dp dep
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1080
        else
19816
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
  1081
          debug_msg (fn _ => "allocating node " ^ quote name ^ (if strict then " (strict)" else " (non-strict)"))
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
  1082
          #> ensure_bot name
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1083
          #> add_dp dep
19341
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19300
diff changeset
  1084
          #> debug_msg (fn _ => "creating node " ^ quote name)
19816
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
  1085
          #> invoke_generator name defgen
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1086
          #-> (fn def => prep_def def)
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1087
          #-> (fn def =>
19341
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19300
diff changeset
  1088
             debug_msg (fn _ => "addition of " ^ name ^ " := " ^ (Pretty.output o pretty_def) def)
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19300
diff changeset
  1089
          #> debug_msg (fn _ => "adding")
19816
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
  1090
          #> add_def_incr strict (name, def)
19341
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19300
diff changeset
  1091
          #> debug_msg (fn _ => "postprocessing")
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1092
          #> postprocess_def (name, def)
19341
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19300
diff changeset
  1093
          #> debug_msg (fn _ => "adding done")
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1094
       ))
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1095
    |> pair dep
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
  1096
  end;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
  1097
19884
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
  1098
fun succeed some (_, modl) = (some, modl);
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
  1099
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
  1100
fun fail msg (_, modl) = raise FAIL ([msg], NONE);
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
  1101
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
  1102
fun message msg f trns =
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
  1103
  f trns handle FAIL (msgs, exc) =>
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
  1104
    raise FAIL (msg :: msgs, exc);
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
  1105
18963
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18919
diff changeset
  1106
fun start_transact init f modl =
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
  1107
  let
18963
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18919
diff changeset
  1108
    fun handle_fail f x =
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18919
diff changeset
  1109
      (f x
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
  1110
      handle FAIL (msgs, NONE) =>
20389
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
  1111
        (error o cat_lines) ("Code generation failed, while:" :: msgs))
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
  1112
      handle FAIL (msgs, SOME e) =>
20389
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
  1113
        ((Output.error_msg o cat_lines) ("Code generation failed, while:" :: msgs); raise e);
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
  1114
  in
19884
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
  1115
    modl
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
  1116
    |> (if is_some init then ensure_bot (the init) else I)
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
  1117
    |> pair init
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
  1118
    |> handle_fail f
19884
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
  1119
    |-> (fn x => fn (_, modl) => (x, modl))
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
  1120
  end;
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1121
20216
f30b73385060 added eval_term
haftmann
parents: 20192
diff changeset
  1122
fun add_eval_def (shallow, e) modl =
20191
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
  1123
  let
20216
f30b73385060 added eval_term
haftmann
parents: 20192
diff changeset
  1124
    val name = "VALUE";
f30b73385060 added eval_term
haftmann
parents: 20192
diff changeset
  1125
    val sname = NameSpace.pack [shallow, name];
20191
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
  1126
  in
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
  1127
    modl
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
  1128
    |> add_def (sname, Fun ([([], e)], ([("_", [])], ITyVar "_")))
20216
f30b73385060 added eval_term
haftmann
parents: 20192
diff changeset
  1129
    |> fold (curry add_dep sname) (add_deps_of_term e [])
20191
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
  1130
    |> pair name
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
  1131
  end;
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1132
18335
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
  1133
20389
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
  1134
(** eliminating classes in definitions **)
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
  1135
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
  1136
fun elim_classes modl (eqs, (vs, ty)) =
20389
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
  1137
  let
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
  1138
    fun elim_expr _ = ();
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
  1139
  in (error ""; (eqs, ty)) end;
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
  1140
18216
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1141
(** generic serialization **)
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1142
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1143
(* resolving *)
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1144
18885
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1145
structure NameMangler = NameManglerFun (
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1146
  type ctxt = (string * string -> string) * (string -> string option);
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
  1147
  type src = string * string;
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
  1148
  val ord = prod_ord string_ord string_ord;
18963
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18919
diff changeset
  1149
  fun mk (postprocess, validate) ((shallow, name), 0) =
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18919
diff changeset
  1150
        let
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18919
diff changeset
  1151
          val name' = postprocess (shallow, name);
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18919
diff changeset
  1152
        in case validate name'
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18919
diff changeset
  1153
         of NONE => name'
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18919
diff changeset
  1154
          | _ => mk (postprocess, validate) ((shallow, name), 1)
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18919
diff changeset
  1155
        end
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18919
diff changeset
  1156
    | mk (postprocess, validate) (("", name), i) =
19150
1457d810b408 class package and codegen refinements
haftmann
parents: 19136
diff changeset
  1157
        postprocess ("", name ^ replicate_string i "'")
1457d810b408 class package and codegen refinements
haftmann
parents: 19136
diff changeset
  1158
        |> perhaps validate
1457d810b408 class package and codegen refinements
haftmann
parents: 19136
diff changeset
  1159
    | mk (postprocess, validate) ((shallow, name), 1) =
1457d810b408 class package and codegen refinements
haftmann
parents: 19136
diff changeset
  1160
        postprocess (shallow, shallow ^ "_" ^ name)
18885
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1161
        |> perhaps validate
18963
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18919
diff changeset
  1162
    | mk (postprocess, validate) ((shallow, name), i) =
19150
1457d810b408 class package and codegen refinements
haftmann
parents: 19136
diff changeset
  1163
        postprocess (shallow, name ^ replicate_string i "'")
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
  1164
        |> perhaps validate;
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
  1165
  fun is_valid _ _ = true;
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
  1166
  fun maybe_unique _ _ = NONE;
20389
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
  1167
  fun re_mangle _ dst = error ("No such definition name: " ^ quote dst);
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
  1168
);
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
  1169
18963
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18919
diff changeset
  1170
fun mk_deresolver module nsp_conn postprocess validate =
18885
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1171
  let
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1172
    datatype tabnode = N of string * tabnode Symtab.table option;
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1173
    fun mk module manglers tab =
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1174
      let
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1175
        fun mk_name name =
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1176
          case NameSpace.unpack name
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1177
           of [n] => ("", n)
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1178
            | [s, n] => (s, n);
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1179
        fun in_conn (shallow, conn) =
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1180
          member (op = : string * string -> bool) conn shallow;
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1181
        fun add_name name =
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1182
          let
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1183
            val n as (shallow, _) = mk_name name;
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1184
          in
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1185
            AList.map_entry_yield in_conn shallow (
18963
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18919
diff changeset
  1186
              NameMangler.declare (postprocess, validate) n
18885
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1187
              #-> (fn n' => pair (name, n'))
18963
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18919
diff changeset
  1188
            ) #> apfst the
18885
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1189
          end;
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1190
        val (renamings, manglers') =
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1191
          fold_map add_name (Graph.keys module) manglers;
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1192
        fun extend_tab (n, n') =
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1193
          if (length o NameSpace.unpack) n = 1
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1194
          then
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1195
            Symtab.update_new
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1196
              (n, N (n', SOME (mk ((dest_modl o Graph.get_node module) n) manglers' Symtab.empty)))
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1197
          else
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1198
            Symtab.update_new (n, N (n', NONE));
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1199
      in fold extend_tab renamings tab end;
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1200
    fun get_path_name [] tab =
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1201
          ([], SOME tab)
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1202
      | get_path_name [p] tab =
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1203
          let
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1204
            val SOME (N (p', tab')) = Symtab.lookup tab p
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1205
          in ([p'], tab') end
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1206
      | get_path_name [p1, p2] tab =
18919
haftmann
parents: 18918
diff changeset
  1207
          (case Symtab.lookup tab p1
20191
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
  1208
           of SOME (N (p', SOME tab')) =>
18885
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1209
                let
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1210
                  val (ps', tab'') = get_path_name [p2] tab'
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1211
                in (p' :: ps', tab'') end
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1212
            | NONE =>
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1213
                let
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1214
                  val SOME (N (p', NONE)) = Symtab.lookup tab (NameSpace.pack [p1, p2])
18919
haftmann
parents: 18918
diff changeset
  1215
                in ([p'], NONE) end)
18885
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1216
      | get_path_name (p::ps) tab =
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1217
          let
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1218
            val SOME (N (p', SOME tab')) = Symtab.lookup tab p
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1219
            val (ps', tab'') = get_path_name ps tab'
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1220
          in (p' :: ps', tab'') end;
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1221
    fun deresolv tab prefix name =
19341
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19300
diff changeset
  1222
      let
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20071
diff changeset
  1223
        val (common, (_, rem)) = chop_prefix (op =) (prefix, NameSpace.unpack name);
18885
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1224
        val (_, SOME tab') = get_path_name common tab;
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1225
        val (name', _) = get_path_name rem tab';
20428
67fa1c6ba89e refinements
haftmann
parents: 20405
diff changeset
  1226
      in NameSpace.pack name' end handle BIND => (error ("Missing name: " ^ quote name ^ ", in " ^ quote (NameSpace.pack prefix)));
18885
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1227
  in deresolv (mk module (AList.make (K NameMangler.empty) nsp_conn) Symtab.empty) end;
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
  1228
18216
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1229
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1230
(* serialization *)
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1231
18963
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18919
diff changeset
  1232
fun serialize seri_defs seri_module validate postprocess nsp_conn name_root module =
18216
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1233
  let
19884
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
  1234
    val imptab = allimports_of module;
18963
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18919
diff changeset
  1235
    val resolver = mk_deresolver module nsp_conn postprocess validate;
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 19025
diff changeset
  1236
    fun sresolver s = (resolver o NameSpace.unpack) s
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1237
    fun mk_name prfx name =
18850
92ef83e5eaea various improvements
haftmann
parents: 18812
diff changeset
  1238
      let
92ef83e5eaea various improvements
haftmann
parents: 18812
diff changeset
  1239
        val name_qual = NameSpace.pack (prfx @ [name])
92ef83e5eaea various improvements
haftmann
parents: 18812
diff changeset
  1240
      in (name_qual, resolver prfx name_qual) end;
20191
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
  1241
    fun is_bot (_, (Def Bot)) = true
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
  1242
      | is_bot _ = false;
18756
5eb3df798405 more general serializer
haftmann
parents: 18702
diff changeset
  1243
    fun mk_contents prfx module =
19482
9f11af8f7ef9 tuned basic list operators (flat, maps, map_filter);
wenzelm
parents: 19466
diff changeset
  1244
      map_filter (seri prfx)
18850
92ef83e5eaea various improvements
haftmann
parents: 18812
diff changeset
  1245
        ((map (AList.make (Graph.get_node module)) o rev o Graph.strong_conn) module)
19816
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
  1246
    and seri prfx [(name, Module modl)] =
19884
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
  1247
          seri_module (resolver []) (map (resolver []) ((the o Symtab.lookup imptab) (NameSpace.pack (prfx @ [name]))))
18850
92ef83e5eaea various improvements
haftmann
parents: 18812
diff changeset
  1248
            (mk_name prfx name, mk_contents (prfx @ [name]) modl)
18756
5eb3df798405 more general serializer
haftmann
parents: 18702
diff changeset
  1249
      | seri prfx ds =
20191
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
  1250
          case filter_out is_bot ds
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
  1251
           of [] => NONE
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
  1252
            | ds' => seri_defs sresolver (NameSpace.pack prfx)
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
  1253
                (map (fn (name, Def def) => (fst (mk_name prfx name), def (*|> tap (Pretty.writeln o pretty_def)*))) ds')
18216
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1254
  in
19937
d1b8374d8df7 fixed bug resolving Haskell names
haftmann
parents: 19884
diff changeset
  1255
    seri_module (resolver []) (map (resolver []) ((the o Symtab.lookup imptab) ""))
18850
92ef83e5eaea various improvements
haftmann
parents: 18812
diff changeset
  1256
      (("", name_root), (mk_contents [] module))
18216
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1257
  end;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1258
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
  1259
end; (* struct *)
19136
00ade10f611d some refinements
haftmann
parents: 19042
diff changeset
  1260
19300
7689f81f8996 subtract (op =);
wenzelm
parents: 19253
diff changeset
  1261
structure BasicCodegenThingol: BASIC_CODEGEN_THINGOL = CodegenThingol;