src/Pure/Tools/codegen_thingol.ML
author aspinall
Wed, 31 Jan 2007 20:06:24 +0100
changeset 22224 6c2373adc7a0
parent 22185 24bf0e403526
child 22305 0e56750a092b
permissions -rw-r--r--
Expose hard-wired string which was changed and broke Proof General.
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
20855
9f60d493c8fe clarified header comments
haftmann
parents: 20835
diff changeset
     5
Intermediate language ("Thin-gol") representing extracted code.
18169
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;
22076
42ae57200d96 changed dictionary representation to explicit classrel witnesses
haftmann
parents: 22062
diff changeset
    19
  datatype dict =
42ae57200d96 changed dictionary representation to explicit classrel witnesses
haftmann
parents: 22062
diff changeset
    20
      DictConst of string * dict list list
42ae57200d96 changed dictionary representation to explicit classrel witnesses
haftmann
parents: 22062
diff changeset
    21
    | DictVar of string list * (vname * (int * int));
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    22
  datatype itype =
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
    23
      `%% of string * itype list
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
    24
    | ITyVar of vname;
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20071
diff changeset
    25
  datatype iterm =
22076
42ae57200d96 changed dictionary representation to explicit classrel witnesses
haftmann
parents: 22062
diff changeset
    26
      IConst of string * (dict list list * itype)
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
    27
    | IVar of vname
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20071
diff changeset
    28
    | `$ of iterm * iterm
454f4be984b7 adaptions in codegen
haftmann
parents: 20071
diff changeset
    29
    | `|-> of (vname * itype) * iterm
21012
haftmann
parents: 20976
diff changeset
    30
    | INum of IntInf.int
haftmann
parents: 20976
diff changeset
    31
    | IChar of string (*length one!*)
haftmann
parents: 20976
diff changeset
    32
    | ICase of (iterm * itype) * (iterm * iterm) list;
haftmann
parents: 20976
diff changeset
    33
        (*(discriminendum term (td), discriminendum type (ty)),
haftmann
parents: 20976
diff changeset
    34
                [(selector pattern (p), body term (t))] (bs))*)
21123
9f7c430cf9ac dropped constructiv `->
haftmann
parents: 21082
diff changeset
    35
  val `-> : itype * itype -> itype;
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
    36
  val `--> : itype list * itype -> itype;
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
    37
  val `$$ : iterm * iterm list -> iterm;
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
    38
  val `|--> : (vname * itype) list * iterm -> iterm;
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
    39
  type typscheme = (vname * sort) list * itype;
19136
00ade10f611d some refinements
haftmann
parents: 19042
diff changeset
    40
end;
00ade10f611d some refinements
haftmann
parents: 19042
diff changeset
    41
00ade10f611d some refinements
haftmann
parents: 19042
diff changeset
    42
signature CODEGEN_THINGOL =
00ade10f611d some refinements
haftmann
parents: 19042
diff changeset
    43
sig
00ade10f611d some refinements
haftmann
parents: 19042
diff changeset
    44
  include BASIC_CODEGEN_THINGOL;
18216
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
    45
  val unfoldl: ('a -> ('a * 'b) option) -> 'a -> 'a * 'b list;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
    46
  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
    47
  val unfold_fun: itype -> itype list * itype;
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20071
diff changeset
    48
  val unfold_app: iterm -> iterm * iterm list;
22062
f4cfc4101c8f more term inspection
haftmann
parents: 22037
diff changeset
    49
  val split_abs: iterm -> (((vname * iterm option) * itype) * iterm) option;
21012
haftmann
parents: 20976
diff changeset
    50
  val unfold_abs: iterm -> ((vname * iterm option) * itype) list * iterm;
22062
f4cfc4101c8f more term inspection
haftmann
parents: 22037
diff changeset
    51
  val split_let: iterm -> (((iterm * itype) * iterm) * iterm) option;
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20071
diff changeset
    52
  val unfold_let: iterm -> ((iterm * itype) * iterm) list * iterm;
454f4be984b7 adaptions in codegen
haftmann
parents: 20071
diff changeset
    53
  val unfold_const_app: iterm ->
22076
42ae57200d96 changed dictionary representation to explicit classrel witnesses
haftmann
parents: 22062
diff changeset
    54
    ((string * (dict list list * itype)) * iterm list) option;
42ae57200d96 changed dictionary representation to explicit classrel witnesses
haftmann
parents: 22062
diff changeset
    55
  val eta_expand: (string * (dict list list * itype)) * iterm list -> int -> iterm;
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
    56
  val fold_constnames: (string -> 'a -> 'a) -> iterm -> 'a -> 'a;
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
    57
  val fold_varnames: (string -> 'a -> 'a) -> iterm -> 'a -> 'a;
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
    58
  val fold_unbound_varnames: (string -> 'a -> 'a) -> iterm -> 'a -> 'a;
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    59
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    60
  datatype def =
19816
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
    61
      Bot
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
    62
    | Fun of (iterm list * iterm) list * typscheme
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
    63
    | Datatype of (vname * sort) list * (string * itype list) list
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
    64
    | Datatypecons of string
22076
42ae57200d96 changed dictionary representation to explicit classrel witnesses
haftmann
parents: 22062
diff changeset
    65
    | Class of (class * string) list * (vname * (string * itype) list)
21082
82460fa3340d final Haskell serializer
haftmann
parents: 21012
diff changeset
    66
    | Classop of class
22076
42ae57200d96 changed dictionary representation to explicit classrel witnesses
haftmann
parents: 22062
diff changeset
    67
    | Classrel of class * class
20389
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
    68
    | Classinst of (class * (string * (vname * sort) list))
22076
42ae57200d96 changed dictionary representation to explicit classrel witnesses
haftmann
parents: 22062
diff changeset
    69
          * ((class * (string * (string * dict list list))) list
20389
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
    70
        * (string * iterm) list);
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
    71
  type code = def Graph.T;
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    72
  type transact;
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
    73
  val empty_code: code;
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
    74
  val get_def: code -> string -> def;
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
    75
  val merge_code: code * code -> code;
21012
haftmann
parents: 20976
diff changeset
    76
  val project_code: string list (*hidden*) -> string list option (*selected*)
haftmann
parents: 20976
diff changeset
    77
    -> code -> code;
21160
haftmann
parents: 21123
diff changeset
    78
  val add_eval_def: string (*bind name*) * iterm -> code -> code;
21012
haftmann
parents: 20976
diff changeset
    79
22037
fbf0a12d053f proper node names
haftmann
parents: 21937
diff changeset
    80
  val ensure_def: (string -> string) -> (transact -> def * code) -> bool -> string
19884
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
    81
    -> string -> transact -> transact;
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
    82
  val succeed: 'a -> transact -> 'a * code;
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
    83
  val fail: string -> transact -> 'a * code;
19884
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
    84
  val message: string -> (transact -> 'a) -> transact -> 'a;
22185
haftmann
parents: 22076
diff changeset
    85
  val start_transact: (transact -> 'a * transact) -> code -> 'a * code;
18216
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
    86
20835
haftmann
parents: 20709
diff changeset
    87
  val trace: bool ref;
haftmann
parents: 20709
diff changeset
    88
  val tracing: ('a -> string) -> 'a -> 'a;
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    89
end;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    90
18850
92ef83e5eaea various improvements
haftmann
parents: 18812
diff changeset
    91
structure CodegenThingol: CODEGEN_THINGOL =
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    92
struct
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    93
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    94
(** auxiliary **)
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    95
20835
haftmann
parents: 20709
diff changeset
    96
val trace = ref false;
haftmann
parents: 20709
diff changeset
    97
fun tracing f x = (if !trace then Output.tracing (f x) else (); x);
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    98
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    99
fun unfoldl dest x =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   100
  case dest x
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   101
   of NONE => (x, [])
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   102
    | SOME (x1, x2) =>
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   103
        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
   104
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   105
fun unfoldr dest x =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   106
  case dest x
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   107
   of NONE => ([], x)
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   108
    | SOME (x1, x2) =>
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   109
        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
   110
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   111
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   112
(** language core - types, pattern, expressions **)
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   113
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   114
(* language representation *)
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   115
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   116
type vname = string;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   117
22076
42ae57200d96 changed dictionary representation to explicit classrel witnesses
haftmann
parents: 22062
diff changeset
   118
datatype dict =
42ae57200d96 changed dictionary representation to explicit classrel witnesses
haftmann
parents: 22062
diff changeset
   119
    DictConst of string * dict list list
42ae57200d96 changed dictionary representation to explicit classrel witnesses
haftmann
parents: 22062
diff changeset
   120
  | DictVar of string list * (vname * (int * int));
18885
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   121
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   122
datatype itype =
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   123
    `%% of string * itype list
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   124
  | ITyVar of vname;
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   125
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20071
diff changeset
   126
datatype iterm =
22076
42ae57200d96 changed dictionary representation to explicit classrel witnesses
haftmann
parents: 22062
diff changeset
   127
    IConst of string * (dict list list * itype)
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   128
  | IVar of vname
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20071
diff changeset
   129
  | `$ of iterm * iterm
454f4be984b7 adaptions in codegen
haftmann
parents: 20071
diff changeset
   130
  | `|-> of (vname * itype) * iterm
21012
haftmann
parents: 20976
diff changeset
   131
  | INum of IntInf.int
haftmann
parents: 20976
diff changeset
   132
  | IChar of string
haftmann
parents: 20976
diff changeset
   133
  | ICase of (iterm * itype) * (iterm * iterm) list;
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   134
    (*see also signature*)
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   135
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   136
(*
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   137
  variable naming conventions
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   138
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   139
  bare names:
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   140
    variable names          v
20439
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   141
    class names             class
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   142
    type constructor names  tyco
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   143
    datatype names          dtco
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   144
    const names (general)   c
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   145
    constructor names       co
20439
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   146
    class operation names   clsop (op)
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   147
    arbitrary name          s
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   148
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   149
    v, c, co, clsop also annotated with types etc.
20439
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   150
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   151
  constructs:
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   152
    sort                    sort
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   153
    type parameters         vs
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   154
    type                    ty
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   155
    type schemes            tysm
20439
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   156
    term                    t
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   157
    (term as pattern)       p
21838
f9243336f54e fixed type
haftmann
parents: 21160
diff changeset
   158
    instance (class, tyco)  inst
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   159
 *)
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   160
21123
9f7c430cf9ac dropped constructiv `->
haftmann
parents: 21082
diff changeset
   161
fun ty1 `-> ty2 = "fun" `%% [ty1, ty2];
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   162
val op `--> = Library.foldr (op `->);
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   163
val op `$$ = Library.foldl (op `$);
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   164
val op `|--> = Library.foldr (op `|->);
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   165
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   166
val unfold_fun = unfoldr
21123
9f7c430cf9ac dropped constructiv `->
haftmann
parents: 21082
diff changeset
   167
  (fn "fun" `%% [ty1, ty2] => SOME (ty1, ty2)
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   168
    | _ => NONE);
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   170
val unfold_app = unfoldl
20439
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   171
  (fn op `$ t => SOME t
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   172
    | _ => NONE);
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   173
22062
f4cfc4101c8f more term inspection
haftmann
parents: 22037
diff changeset
   174
val split_abs =
21012
haftmann
parents: 20976
diff changeset
   175
  (fn (v, ty) `|-> (t as ICase ((IVar w, _), [(p, t')])) =>
haftmann
parents: 20976
diff changeset
   176
        if v = w then SOME (((v, SOME p), ty), t') else SOME (((v, NONE), ty), t)
haftmann
parents: 20976
diff changeset
   177
    | (v, ty) `|-> t => SOME (((v, NONE), ty), t)
22062
f4cfc4101c8f more term inspection
haftmann
parents: 22037
diff changeset
   178
    | _ => NONE);
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   179
22062
f4cfc4101c8f more term inspection
haftmann
parents: 22037
diff changeset
   180
val unfold_abs = unfoldr split_abs;
f4cfc4101c8f more term inspection
haftmann
parents: 22037
diff changeset
   181
f4cfc4101c8f more term inspection
haftmann
parents: 22037
diff changeset
   182
val split_let = 
21012
haftmann
parents: 20976
diff changeset
   183
  (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
   184
    | _ => NONE);
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   185
22062
f4cfc4101c8f more term inspection
haftmann
parents: 22037
diff changeset
   186
val unfold_let = unfoldr split_let;
f4cfc4101c8f more term inspection
haftmann
parents: 22037
diff changeset
   187
20439
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   188
fun unfold_const_app t =
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   189
 case unfold_app t
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   190
  of (IConst c, ts) => SOME (c, ts)
18865
31aed965135c minor cleanups
haftmann
parents: 18852
diff changeset
   191
   | _ => NONE;
31aed965135c minor cleanups
haftmann
parents: 18852
diff changeset
   192
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   193
fun fold_aiterms f (t as IConst _) =
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   194
      f t
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   195
  | fold_aiterms f (t as IVar _) =
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   196
      f t
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   197
  | fold_aiterms f (t1 `$ t2) =
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   198
      fold_aiterms f t1 #> fold_aiterms f t2
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   199
  | fold_aiterms f (t as _ `|-> t') =
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   200
      f t #> fold_aiterms f t'
21012
haftmann
parents: 20976
diff changeset
   201
  | fold_aiterms f (t as INum _) =
haftmann
parents: 20976
diff changeset
   202
      f t
haftmann
parents: 20976
diff changeset
   203
  | fold_aiterms f (t as IChar _) =
haftmann
parents: 20976
diff changeset
   204
      f t
haftmann
parents: 20976
diff changeset
   205
  | fold_aiterms f (ICase ((td, _), bs)) =
haftmann
parents: 20976
diff changeset
   206
      fold_aiterms f td #> fold (fn (p, t) => fold_aiterms f p #> fold_aiterms f t) bs;
19202
0b9eb4b0ad98 substantial improvement in codegen iml
haftmann
parents: 19167
diff changeset
   207
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   208
fun fold_constnames f =
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   209
  let
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   210
    fun add (IConst (c, _)) = f c
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   211
      | add _ = I;
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   212
  in fold_aiterms add end;
18885
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   213
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   214
fun fold_varnames f =
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   215
  let
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   216
    fun add (IVar v) = f v
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   217
      | add ((v, _) `|-> _) = f v
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   218
      | add _ = I;
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   219
  in fold_aiterms add end;
20709
645236e80885 some cleanup
haftmann
parents: 20600
diff changeset
   220
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   221
fun fold_unbound_varnames f =
18885
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   222
  let
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   223
    fun add _ (IConst _) =
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   224
          I
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   225
      | add vs (IVar v) =
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   226
          if not (member (op =) vs v) then f v else I
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   227
      | add vs (t1 `$ t2) =
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   228
          add vs t1 #> add vs t2
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   229
      | add vs ((v, _) `|-> t) =
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   230
          add (insert (op =) v vs) t
21012
haftmann
parents: 20976
diff changeset
   231
      | add vs (INum _) =
haftmann
parents: 20976
diff changeset
   232
          I
haftmann
parents: 20976
diff changeset
   233
      | add vs (IChar _) =
haftmann
parents: 20976
diff changeset
   234
          I
haftmann
parents: 20976
diff changeset
   235
      | add vs (ICase ((td, _), bs)) =
haftmann
parents: 20976
diff changeset
   236
          add vs td #> fold (fn (p, t) => add vs p #> add vs t) bs;
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   237
  in add [] end;
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20071
diff changeset
   238
20439
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   239
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
   240
  let
20439
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   241
    val j = length ts;
19607
07eeb832f28d introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents: 19597
diff changeset
   242
    val l = k - j;
07eeb832f28d introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents: 19597
diff changeset
   243
    val tys = (curry Library.take l o curry Library.drop j o fst o unfold_fun) ty;
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   244
    val ctxt = (fold o fold_varnames) Name.declare ts Name.context;
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   245
    val vs_tys = Name.names ctxt "a" tys;
20439
1bf42b262a38 code refinements
haftmann
parents: 20428
diff changeset
   246
  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
   247
18304
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   248
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   249
(** definitions, transactions **)
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   250
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   251
(* type definitions *)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   252
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   253
type typscheme = (vname * sort) list * itype;
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   254
datatype def =
19816
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
   255
    Bot
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   256
  | Fun of (iterm list * iterm) list * typscheme
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   257
  | Datatype of (vname * sort) list * (string * itype list) list
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   258
  | Datatypecons of string
22076
42ae57200d96 changed dictionary representation to explicit classrel witnesses
haftmann
parents: 22062
diff changeset
   259
  | Class of (class * string) list * (vname * (string * itype) list)
21082
82460fa3340d final Haskell serializer
haftmann
parents: 21012
diff changeset
   260
  | Classop of class
22076
42ae57200d96 changed dictionary representation to explicit classrel witnesses
haftmann
parents: 22062
diff changeset
   261
  | Classrel of class * class
20389
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   262
  | Classinst of (class * (string * (vname * sort) list))
22076
42ae57200d96 changed dictionary representation to explicit classrel witnesses
haftmann
parents: 22062
diff changeset
   263
        * ((class * (string * (string * dict list list))) list
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   264
      * (string * iterm) list);
19597
8ced57ffc090 major refinement of codegen_theorems.ML
haftmann
parents: 19482
diff changeset
   265
val eq_def = (op =) : def * def -> bool;
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   266
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   267
type code = def Graph.T;
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   268
type transact = Graph.key option * code;
21012
haftmann
parents: 20976
diff changeset
   269
exception FAIL of string list;
18360
a2c9506b62a7 improved class handling
haftmann
parents: 18335
diff changeset
   270
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   271
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   272
(* abstract code *)
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   273
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   274
val empty_code = Graph.empty : code; (*read: "depends on"*)
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   275
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   276
val get_def = Graph.get_node;
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   277
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   278
fun ensure_bot name = Graph.default_node (name, Bot);
19816
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
   279
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   280
fun add_def_incr strict (name, Bot) code =
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   281
      (case the_default Bot (try (get_def code) name)
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   282
       of Bot => if strict then error "Attempted to add Bot to code"
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   283
            else Graph.map_node name (K Bot) code
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   284
        | _ => code)
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   285
  | add_def_incr _ (name, def) code =
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   286
      (case try (get_def code) name
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   287
       of NONE => Graph.new_node (name, def) code
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   288
        | SOME Bot => Graph.map_node name (K def) code
19816
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
   289
        | SOME def' => if eq_def (def, def')
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   290
            then code
20389
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   291
            else error ("Tried to overwrite definition " ^ quote name));
19816
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
   292
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   293
fun add_dep (dep as (name1, name2)) =
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   294
  if name1 = name2 then I else Graph.add_edge dep;
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   295
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   296
val merge_code = Graph.join (fn _ => fn def12 => if eq_def def12 then fst def12 else Bot);
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   297
21012
haftmann
parents: 20976
diff changeset
   298
fun project_code hidden raw_selected code =
20466
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   299
  let
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   300
    fun is_bot name = case get_def code name
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   301
     of Bot => true
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   302
      | _ => false;
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   303
    val names = subtract (op =) hidden (Graph.keys code);
21012
haftmann
parents: 20976
diff changeset
   304
    val deleted = Graph.all_preds code (filter is_bot names);
haftmann
parents: 20976
diff changeset
   305
    val selected = case raw_selected
haftmann
parents: 20976
diff changeset
   306
     of NONE => names |> subtract (op =) deleted 
haftmann
parents: 20976
diff changeset
   307
      | SOME sel => sel
haftmann
parents: 20976
diff changeset
   308
          |> subtract (op =) deleted
haftmann
parents: 20976
diff changeset
   309
          |> subtract (op =) hidden
haftmann
parents: 20976
diff changeset
   310
          |> Graph.all_succs code
haftmann
parents: 20976
diff changeset
   311
          |> subtract (op =) deleted
haftmann
parents: 20976
diff changeset
   312
          |> subtract (op =) hidden;
20191
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   313
  in
21012
haftmann
parents: 20976
diff changeset
   314
    code
21937
4ba9531c60eb renamed Graph.project to Graph.subgraph;
wenzelm
parents: 21911
diff changeset
   315
    |> Graph.subgraph (member (op =) selected)
20191
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   316
  end;
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20175
diff changeset
   317
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   318
fun check_samemodule names =
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   319
  fold (fn name =>
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   320
    let
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   321
      val module_name = (NameSpace.qualifier o NameSpace.qualifier) name
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   322
    in
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   323
     fn NONE => SOME module_name
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   324
      | SOME module_name' => if module_name = module_name' then SOME module_name
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   325
          else error ("Inconsistent name prefix for simultanous names: " ^ commas_quote names)
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   326
    end
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   327
  ) names NONE;
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   328
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   329
fun check_funeqs eqs =
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   330
  (fold (fn (pats, _) =>
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   331
    let
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   332
      val l = length pats
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   333
    in
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   334
     fn NONE => SOME l
18850
92ef83e5eaea various improvements
haftmann
parents: 18812
diff changeset
   335
      | SOME l' => if l = l' then SOME l
20389
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   336
          else error "Function definition with different number of arguments"
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   337
    end
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   338
  ) eqs NONE; eqs);
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   339
21082
82460fa3340d final Haskell serializer
haftmann
parents: 21012
diff changeset
   340
fun check_prep_def code Bot =
19816
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
   341
      Bot
21082
82460fa3340d final Haskell serializer
haftmann
parents: 21012
diff changeset
   342
  | check_prep_def code (Fun (eqs, d)) =
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   343
      Fun (check_funeqs eqs, d)
21082
82460fa3340d final Haskell serializer
haftmann
parents: 21012
diff changeset
   344
  | check_prep_def code (d as Datatype _) =
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 19025
diff changeset
   345
      d
21082
82460fa3340d final Haskell serializer
haftmann
parents: 21012
diff changeset
   346
  | check_prep_def code (Datatypecons dtco) =
22185
haftmann
parents: 22076
diff changeset
   347
      error "Attempted to add bare term constructor"
21082
82460fa3340d final Haskell serializer
haftmann
parents: 21012
diff changeset
   348
  | check_prep_def code (d as Class _) =
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 19025
diff changeset
   349
      d
21082
82460fa3340d final Haskell serializer
haftmann
parents: 21012
diff changeset
   350
  | check_prep_def code (Classop _) =
22076
42ae57200d96 changed dictionary representation to explicit classrel witnesses
haftmann
parents: 22062
diff changeset
   351
      error "Attempted to add bare class operation"
42ae57200d96 changed dictionary representation to explicit classrel witnesses
haftmann
parents: 22062
diff changeset
   352
  | check_prep_def code (Classrel _) =
42ae57200d96 changed dictionary representation to explicit classrel witnesses
haftmann
parents: 22062
diff changeset
   353
      error "Attempted to add bare class relation"
42ae57200d96 changed dictionary representation to explicit classrel witnesses
haftmann
parents: 22062
diff changeset
   354
  | check_prep_def code (d as Classinst ((class, (tyco, arity)), (_, inst_classops))) =
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   355
      let
22076
42ae57200d96 changed dictionary representation to explicit classrel witnesses
haftmann
parents: 22062
diff changeset
   356
        val Class (_, (_, classops)) = get_def code class;
42ae57200d96 changed dictionary representation to explicit classrel witnesses
haftmann
parents: 22062
diff changeset
   357
        val _ = if length inst_classops > length classops
42ae57200d96 changed dictionary representation to explicit classrel witnesses
haftmann
parents: 22062
diff changeset
   358
          then error "Too many class operations given"
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   359
          else ();
22076
42ae57200d96 changed dictionary representation to explicit classrel witnesses
haftmann
parents: 22062
diff changeset
   360
        fun check_classop (f, _) =
42ae57200d96 changed dictionary representation to explicit classrel witnesses
haftmann
parents: 22062
diff changeset
   361
          if AList.defined (op =) inst_classops f
42ae57200d96 changed dictionary representation to explicit classrel witnesses
haftmann
parents: 22062
diff changeset
   362
          then () else error ("Missing definition for class operation " ^ quote f);
42ae57200d96 changed dictionary representation to explicit classrel witnesses
haftmann
parents: 22062
diff changeset
   363
        val _ = map check_classop classops;
42ae57200d96 changed dictionary representation to explicit classrel witnesses
haftmann
parents: 22062
diff changeset
   364
      in d end;
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   365
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 19025
diff changeset
   366
fun postprocess_def (name, Datatype (_, constrs)) =
22076
42ae57200d96 changed dictionary representation to explicit classrel witnesses
haftmann
parents: 22062
diff changeset
   367
      tap (fn _ => check_samemodule (name :: map fst constrs))
42ae57200d96 changed dictionary representation to explicit classrel witnesses
haftmann
parents: 22062
diff changeset
   368
      #> fold (fn (co, _) =>
19816
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
   369
        add_def_incr true (co, Datatypecons name)
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   370
        #> add_dep (co, name)
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   371
        #> add_dep (name, co)
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   372
      ) constrs
22076
42ae57200d96 changed dictionary representation to explicit classrel witnesses
haftmann
parents: 22062
diff changeset
   373
  | postprocess_def (name, Class (classrels, (_, classops))) =
42ae57200d96 changed dictionary representation to explicit classrel witnesses
haftmann
parents: 22062
diff changeset
   374
      tap (fn _ => check_samemodule (name :: map fst classops @ map snd classrels))
42ae57200d96 changed dictionary representation to explicit classrel witnesses
haftmann
parents: 22062
diff changeset
   375
      #> fold (fn (f, _) =>
42ae57200d96 changed dictionary representation to explicit classrel witnesses
haftmann
parents: 22062
diff changeset
   376
        add_def_incr true (f, Classop name)
42ae57200d96 changed dictionary representation to explicit classrel witnesses
haftmann
parents: 22062
diff changeset
   377
        #> add_dep (f, name)
42ae57200d96 changed dictionary representation to explicit classrel witnesses
haftmann
parents: 22062
diff changeset
   378
        #> add_dep (name, f)
42ae57200d96 changed dictionary representation to explicit classrel witnesses
haftmann
parents: 22062
diff changeset
   379
      ) classops
42ae57200d96 changed dictionary representation to explicit classrel witnesses
haftmann
parents: 22062
diff changeset
   380
      #> fold (fn (superclass, classrel) =>
42ae57200d96 changed dictionary representation to explicit classrel witnesses
haftmann
parents: 22062
diff changeset
   381
        add_def_incr true (classrel, Classrel (name, superclass))
42ae57200d96 changed dictionary representation to explicit classrel witnesses
haftmann
parents: 22062
diff changeset
   382
        #> add_dep (classrel, name)
42ae57200d96 changed dictionary representation to explicit classrel witnesses
haftmann
parents: 22062
diff changeset
   383
        #> add_dep (name, classrel)
42ae57200d96 changed dictionary representation to explicit classrel witnesses
haftmann
parents: 22062
diff changeset
   384
      ) classrels
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   385
  | postprocess_def _ =
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   386
      I;
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   387
19884
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   388
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   389
(* transaction protocol *)
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   390
22037
fbf0a12d053f proper node names
haftmann
parents: 21937
diff changeset
   391
fun ensure_def labelled_name defgen strict msg name (dep, code) =
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   392
  let
19816
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
   393
    val msg' = (case dep
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   394
     of NONE => msg
22037
fbf0a12d053f proper node names
haftmann
parents: 21937
diff changeset
   395
      | SOME dep => msg ^ ", required for " ^ labelled_name dep)
19816
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
   396
      ^ (if strict then " (strict)" else " (non-strict)");
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   397
    fun add_dp NONE = I
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   398
      | add_dp (SOME dep) =
22037
fbf0a12d053f proper node names
haftmann
parents: 21937
diff changeset
   399
          tracing (fn _ => "adding dependency " ^ labelled_name dep
fbf0a12d053f proper node names
haftmann
parents: 21937
diff changeset
   400
            ^ " -> " ^ labelled_name name)
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   401
          #> add_dep (dep, name);
21082
82460fa3340d final Haskell serializer
haftmann
parents: 21012
diff changeset
   402
    fun prep_def def code =
82460fa3340d final Haskell serializer
haftmann
parents: 21012
diff changeset
   403
      (check_prep_def code def, code);
82460fa3340d final Haskell serializer
haftmann
parents: 21012
diff changeset
   404
    fun invoke_generator name defgen code =
82460fa3340d final Haskell serializer
haftmann
parents: 21012
diff changeset
   405
      defgen (SOME name, code)
21012
haftmann
parents: 20976
diff changeset
   406
        handle FAIL msgs =>
haftmann
parents: 20976
diff changeset
   407
          if strict then raise FAIL (msg' :: msgs)
21082
82460fa3340d final Haskell serializer
haftmann
parents: 21012
diff changeset
   408
          else (Bot, code);
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   409
  in
21082
82460fa3340d final Haskell serializer
haftmann
parents: 21012
diff changeset
   410
    code
82460fa3340d final Haskell serializer
haftmann
parents: 21012
diff changeset
   411
    |> (if can (get_def code) name
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   412
        then
22037
fbf0a12d053f proper node names
haftmann
parents: 21937
diff changeset
   413
          tracing (fn _ => "asserting definition " ^ labelled_name name)
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   414
          #> add_dp dep
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   415
        else
22037
fbf0a12d053f proper node names
haftmann
parents: 21937
diff changeset
   416
          tracing (fn _ => "allocating definition " ^ labelled_name name
fbf0a12d053f proper node names
haftmann
parents: 21937
diff changeset
   417
            ^ (if strict then " (strict)" else " (non-strict)"))
19816
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
   418
          #> ensure_bot name
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   419
          #> add_dp dep
22037
fbf0a12d053f proper node names
haftmann
parents: 21937
diff changeset
   420
          #> tracing (fn _ => "creating definition " ^ labelled_name name)
19816
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
   421
          #> invoke_generator name defgen
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   422
          #-> (fn def => prep_def def)
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   423
          #-> (fn def =>
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   424
             tracing (fn _ => "adding")
19816
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19785
diff changeset
   425
          #> add_def_incr strict (name, def)
20835
haftmann
parents: 20709
diff changeset
   426
          #> tracing (fn _ => "postprocessing")
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   427
          #> postprocess_def (name, def)
20835
haftmann
parents: 20709
diff changeset
   428
          #> tracing (fn _ => "adding done")
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   429
       ))
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   430
    |> pair dep
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   431
  end;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   432
21082
82460fa3340d final Haskell serializer
haftmann
parents: 21012
diff changeset
   433
fun succeed some (_, code) = (some, code);
19884
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   434
21082
82460fa3340d final Haskell serializer
haftmann
parents: 21012
diff changeset
   435
fun fail msg (_, code) = raise FAIL [msg];
19884
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   436
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   437
fun message msg f trns =
21012
haftmann
parents: 20976
diff changeset
   438
  f trns handle FAIL msgs =>
haftmann
parents: 20976
diff changeset
   439
    raise FAIL (msg :: msgs);
19884
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   440
22185
haftmann
parents: 22076
diff changeset
   441
fun start_transact f code =
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   442
  let
18963
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18919
diff changeset
   443
    fun handle_fail f x =
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18919
diff changeset
   444
      (f x
21012
haftmann
parents: 20976
diff changeset
   445
      handle FAIL msgs =>
20389
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   446
        (error o cat_lines) ("Code generation failed, while:" :: msgs))
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   447
  in
22185
haftmann
parents: 22076
diff changeset
   448
    (NONE, code)
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   449
    |> handle_fail f
21082
82460fa3340d final Haskell serializer
haftmann
parents: 21012
diff changeset
   450
    |-> (fn x => fn (_, code) => (x, code))
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   451
  end;
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   452
21160
haftmann
parents: 21123
diff changeset
   453
fun add_eval_def (name, t) code =
haftmann
parents: 21123
diff changeset
   454
  code
haftmann
parents: 21123
diff changeset
   455
  |> Graph.new_node (name, Fun ([([], t)], ([("_", [])], ITyVar "_")))
haftmann
parents: 21123
diff changeset
   456
  |> fold (curry Graph.add_edge name) (Graph.keys code);
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   457
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   458
end; (*struct*)
18885
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   459
18216
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
   460
19300
7689f81f8996 subtract (op =);
wenzelm
parents: 19253
diff changeset
   461
structure BasicCodegenThingol: BASIC_CODEGEN_THINGOL = CodegenThingol;