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