generics1.icl 202 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//**************************************************************************************
// Generic programming features 
//**************************************************************************************

implementation module generics1

import StdEnv
import check
from checktypes import createClassDictionaries
from transform import ::Group
import genericsupport

// Data types

:: FunDefs :== {#FunDef}
:: Modules :== {#CommonDefs}
:: DclModules :== {#DclModule}
:: Groups :== {!Group}
19
20
21
22
23

:: FunsAndGroups= ! {
		fg_fun_index :: !Index,
		fg_group_index :: !Index,
		fg_funs :: ![FunDef],
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
		fg_groups :: ![Group],
		fg_bimap_functions :: !BimapFunctions
	}

:: BimapFunctions = {
		bimap_id_function :: !FunctionIndexAndIdent,
		bimap_fromto_function :: !FunctionIndexAndIdent,
		bimap_tofrom_function :: !FunctionIndexAndIdent,
		bimap_to_function :: !FunctionIndexAndIdent,
		bimap_from_function :: !FunctionIndexAndIdent,
		bimap_arrow_function :: !FunctionIndexAndIdent,
		bimap_arrow_arg_id_function :: !FunctionIndexAndIdent,
		bimap_arrow_res_id_function :: !FunctionIndexAndIdent,
		bimap_from_Bimap_function :: !FunctionIndexAndIdent,
		bimap_PAIR_function :: !FunctionIndexAndIdent,
		bimap_EITHER_function :: !FunctionIndexAndIdent,
		bimap_OBJECT_function :: !FunctionIndexAndIdent,
		bimap_CONS_function :: !FunctionIndexAndIdent,
		bimap_FIELD_function :: !FunctionIndexAndIdent
	}

:: FunctionIndexAndIdent = {	
		fii_index :: !Index,
		fii_ident :: Ident
48
	}
49

50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
:: *GenericState = 
	{ gs_modules :: !*Modules
	, gs_exprh :: !*ExpressionHeap
	, gs_genh :: !*GenericHeap
	, gs_varh :: !*VarHeap
	, gs_tvarh :: !*TypeVarHeap
	, gs_avarh :: !*AttrVarHeap 
	, gs_error :: !*ErrorAdmin
	, gs_symtab :: !*SymbolTable
	, gs_dcl_modules :: !*DclModules
	, gs_td_infos :: !*TypeDefInfos
	, gs_funs :: !*{#FunDef}
	, gs_groups :: {!Group}
	// non-unique, read only
	, gs_predefs :: !PredefinedSymbols
	, gs_main_module :: !Index
	, gs_used_modules :: !NumberSet
	}

69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Exported functions

convertGenerics :: 
		!Int 					// index of the main dcl module
		!NumberSet				// set of used modules
		!{#CommonDefs} 			// common definitions of all modules
		!{!Group} 				// groups of functions
		!*{# FunDef} 			// functions
		!*TypeDefInfos 			// type definition information of all modules
		!*Heaps 				// all heaps
		!*HashTable 			// needed for what creating class dictionaries
		!*PredefinedSymbols 	// predefined symbols
		!u:{# DclModule}		// dcl modules
		!*ErrorAdmin 			// to report errors
	->  ( !{#CommonDefs}		// common definitions of all modules
		, !{!Group}				// groups of functions
		, !*{# FunDef}			// function definitions
		, ![IndexRange]			// index ranges of generated functions
		, !*TypeDefInfos		// type definition infos
		, !*Heaps				// all heaps
		, !*HashTable			// needed for creating class dictinaries
		, !*PredefinedSymbols	// predefined symbols	
		, !u:{# DclModule}		// dcl modules
		, !*ErrorAdmin			// to report errors
		)
94
convertGenerics main_dcl_module_n used_module_numbers modules groups funs td_infos heaps hash_table u_predefs dcl_modules error
95
96
97
98
99
100
	#! modules = {x \\ x <-: modules} 			// unique copy
	#! dcl_modules = { x \\ x <-: dcl_modules } 	// unique copy
	#! size_predefs = size u_predefs
	#! (predefs, u_predefs) = arrayCopyBegin u_predefs size_predefs // non-unique copy

	#! td_infos = clearTypeDefInfos td_infos
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
	#! (modules, heaps) = clearGenericDefs modules heaps

	# {hp_var_heap, hp_generic_heap, hp_type_heaps={th_vars, th_attrs}, hp_expression_heap} = heaps
	# gs = 
		{ gs_modules = modules
		, gs_symtab = hash_table.hte_symbol_heap
		, gs_dcl_modules = dcl_modules
		, gs_td_infos = td_infos
		, gs_exprh = hp_expression_heap	
		, gs_genh = hp_generic_heap	
		, gs_varh = hp_var_heap
		, gs_tvarh = th_vars
		, gs_avarh = th_attrs
		, gs_error = error	
		, gs_funs = funs
		, gs_groups = groups	
		, gs_predefs = predefs
		, gs_main_module = main_dcl_module_n
		, gs_used_modules = used_module_numbers
		} 

	# (generic_ranges, gs) = convert_generics gs

	#	{ 	gs_modules = modules, gs_symtab, gs_dcl_modules = dcl_modules, gs_td_infos = td_infos, 
			gs_genh = hp_generic_heap, gs_varh = hp_var_heap, gs_tvarh = th_vars, gs_avarh = th_attrs, 
			gs_exprh = hp_expression_heap,	
			gs_error = error, gs_funs = funs, gs_groups = groups,
			gs_predefs = predefs, gs_main_module = main_dcl_module_n, gs_used_modules = used_module_numbers} = gs
	#! hash_table = { hash_table & hte_symbol_heap = gs_symtab }
	#! heaps = 
		{ hp_expression_heap = hp_expression_heap
		, hp_var_heap = hp_var_heap
		, hp_generic_heap = hp_generic_heap
		, hp_type_heaps = { th_vars = th_vars, th_attrs = th_attrs }
135
		}
136
	= (modules, groups, funs, generic_ranges, td_infos, heaps, hash_table, u_predefs, dcl_modules, error)
137
where
138
139
	convert_generics :: !*GenericState -> (![IndexRange], !*GenericState)
	convert_generics gs		
140
141
		# (iso_range, bimap_functions, gs) = buildGenericRepresentations gs
		| not gs.gs_error.ea_ok = ([], gs)	
142

143
144
		# gs = buildClasses gs
		| not gs.gs_error.ea_ok = ([], gs)
145

146
147
		# (instance_range, gs) = convertGenericCases bimap_functions gs
		| not gs.gs_error.ea_ok = ([], gs)	
148
149
150

		#! gs = convertGenericTypeContexts gs

151
		= ([/*iso_range,*/instance_range], gs)
152

153
154
155
// clear stuff that might have been left over
// from compilation of other icl modules

156
clearTypeDefInfos :: !*{#*{#TypeDefInfo}} -> *{#*{#TypeDefInfo}}
157
158
159
160
161
162
163
164
clearTypeDefInfos td_infos
	= clear_modules 0 td_infos
where
	clear_modules n td_infos
		| n == size td_infos
			= td_infos
			#! (td_infos1, td_infos) = replace td_infos n {}
			#! td_infos1 = clear_td_infos 0 td_infos1
165
			#! td_infos = {td_infos & [n]=td_infos1}
166
167
168
169
170
171
172
173
174
			= clear_modules (inc n) td_infos 
			
	clear_td_infos n td_infos 			
		| n == size td_infos
			= td_infos
			#! (td_info, td_infos) = td_infos![n]
			#! td_infos = {td_infos & [n] = {td_info & tdi_gen_rep = No}}
			= clear_td_infos (inc n) td_infos 

175
clearGenericDefs :: !*{#CommonDefs} !*Heaps -> (!*{#CommonDefs},!*Heaps)
176
177
178
179
180
181
182
183
184
185
186
clearGenericDefs modules heaps
	= clear_module 0 modules  heaps
where	
	clear_module n modules heaps
		| n == size modules
			= (modules, heaps)
			#! ({com_generic_defs}, modules) = modules![n]
			#! (com_generic_defs, heaps) = updateArraySt clear_generic_def {x\\x<-:com_generic_defs} heaps 			
			#! modules = {modules & [n].com_generic_defs = com_generic_defs}
			= clear_module (inc n) modules heaps
			
187
	clear_generic_def generic_def=:{gen_info_ptr} heaps=:{hp_generic_heap}
188
		#! (gen_info, hp_generic_heap) = readPtr gen_info_ptr hp_generic_heap
189
		#! gen_info = { gen_info & gen_classes = createArray 32 [] }
190
191
192
193
194
195
196
		#! hp_generic_heap = writePtr gen_info_ptr gen_info hp_generic_heap
		= (generic_def, {heaps & hp_generic_heap = hp_generic_heap})
		
//	generic type representation

// generic representation is built for each type argument of
// generic cases of the current module
197
buildGenericRepresentations :: !*GenericState -> (!IndexRange,!BimapFunctions,!*GenericState)
198
199
200
buildGenericRepresentations gs=:{gs_main_module, gs_modules, gs_funs, gs_groups}
	#! (size_funs, gs_funs) = usize gs_funs
	#! size_groups = size gs_groups
201
	#! ({com_gencase_defs}, gs_modules) = gs_modules![gs_main_module]
202
	
203
	#! gs = { gs & gs_modules = gs_modules, gs_funs = gs_funs, gs_groups = gs_groups }
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
	
	# undefined_function_and_ident = {fii_index = -1,fii_ident = undef}
	  bimap_functions = {
				bimap_id_function = undefined_function_and_ident,
				bimap_fromto_function = undefined_function_and_ident,
				bimap_tofrom_function = undefined_function_and_ident,
				bimap_to_function = undefined_function_and_ident,
				bimap_from_function = undefined_function_and_ident,
		  		bimap_arrow_function = undefined_function_and_ident,
		  		bimap_arrow_arg_id_function = undefined_function_and_ident,
		  		bimap_arrow_res_id_function = undefined_function_and_ident,
		  		bimap_from_Bimap_function = undefined_function_and_ident,
		  		bimap_PAIR_function = undefined_function_and_ident,
		  		bimap_EITHER_function = undefined_function_and_ident,
		  		bimap_OBJECT_function = undefined_function_and_ident,
		  		bimap_CONS_function = undefined_function_and_ident,
		  		bimap_FIELD_function = undefined_function_and_ident
	  		}
	  funs_and_groups = {fg_fun_index=size_funs, fg_group_index=size_groups, fg_funs=[], fg_groups=[],fg_bimap_functions= bimap_functions}
223
	#! (funs_and_groups, gs)
224
		= foldArraySt build_generic_representation com_gencase_defs (funs_and_groups, gs)
225

226
	# {fg_fun_index,fg_funs=new_funs,fg_groups=new_groups,fg_bimap_functions} = funs_and_groups 
227
228
229
	# {gs_funs, gs_groups} = gs
	#! gs_funs = arrayPlusRevList gs_funs new_funs
	#! gs_groups = arrayPlusRevList gs_groups new_groups
230

231
	#! range = {ir_from = size_funs, ir_to = fg_fun_index}
232

233
	= (range, fg_bimap_functions, {gs & gs_funs = gs_funs, gs_groups = gs_groups})
234
where
235
	build_generic_representation 
236
237
238
239
240
			{gc_type_cons=TypeConsSymb {type_index={glob_module,glob_object},type_ident},gc_body=GCB_FunIndex fun_index,gc_ident,gc_pos}
			(funs_and_groups, gs)
		# (type_def,gs) = gs!gs_modules.[glob_module].com_type_defs.[glob_object]
		# (td_info, gs) = gs!gs_td_infos.[glob_module,glob_object]
		= case gs.gs_funs.[fun_index].fun_body of
241
242
243
244
245
246
247
			TransformedBody _ 
				// does not need a generic representation
				-> (funs_and_groups, gs)
			GeneratedBody	
				// needs a generic representation
				-> case type_def.td_rhs of
					SynType _
248
						#  gs_error = reportError gc_ident gc_pos ("cannot derive a generic instance for a synonym type " +++ type_def.td_ident.id_name) gs.gs_error
249
250
						-> (funs_and_groups, {gs & gs_error = gs_error})	
					AbstractType _
251
						#  gs_error = reportError gc_ident gc_pos ("cannot derive a generic instance for an abstract type "  +++ type_def.td_ident.id_name) gs.gs_error
252
253
254
						-> (funs_and_groups, {gs & gs_error = gs_error})	
					_ 
						-> case td_info.tdi_gen_rep of
255
256
257
							Yes _
								-> (funs_and_groups, gs)	// generic representation is already built
							No
258
259
								# type_def_gi = {gi_module=glob_module,gi_index=glob_object}
								# (gen_type_rep, funs_and_groups, gs)
260
									= buildGenericTypeRep type_def_gi funs_and_groups gs
261
262
								# td_info = {td_info & tdi_gen_rep = Yes gen_type_rep}
								# gs = {gs & gs_td_infos.[glob_module,glob_object] = td_info}
263
								-> (funs_and_groups, gs)
264
	build_generic_representation _ st = st
265
266

:: ConsInfo = {ci_cons_info :: DefinedSymbol, ci_field_infos :: [DefinedSymbol]}
267

268
buildGenericTypeRep :: !GlobalIndex /*type def index*/ !FunsAndGroups !*GenericState ->	(!GenericTypeRep,!FunsAndGroups,!*GenericState)
269
buildGenericTypeRep type_index funs_and_groups
270
		gs=:{gs_modules, gs_predefs, gs_main_module, gs_error, gs_td_infos, gs_exprh, gs_varh, gs_genh, gs_avarh, gs_tvarh}
271
272
273
274
275
	# heaps = 
		{ hp_expression_heap = gs_exprh
		, hp_var_heap = gs_varh
		, hp_generic_heap = gs_genh
		, hp_type_heaps = { th_vars = gs_tvarh, th_attrs = gs_avarh }
John van Groningen's avatar
John van Groningen committed
276
277
		}

278
279
	# (type_def, gs_modules) = gs_modules![type_index.gi_module].com_type_defs.[type_index.gi_index]

280
	# (type_info, cons_infos, funs_and_groups, gs_modules, heaps, gs_error)
281
282
		= buildTypeDefInfo type_index.gi_module type_def gs_main_module gs_predefs funs_and_groups gs_modules heaps gs_error

Artem Alimarine's avatar
Artem Alimarine committed
283
	# (atype, (gs_modules, gs_td_infos, heaps, gs_error)) 
Artem Alimarine's avatar
Artem Alimarine committed
284
		= buildStructType type_index type_info cons_infos gs_predefs (gs_modules, gs_td_infos, heaps, gs_error)
285
286
287
288
289
290
291
292
293
294
295
	
	# (from_fun_ds, funs_and_groups, heaps, gs_error)
		= buildConversionFrom type_index.gi_module type_def gs_main_module gs_predefs funs_and_groups heaps gs_error

	# (to_fun_ds, funs_and_groups, heaps, gs_error)
		= buildConversionTo type_index.gi_module type_def gs_main_module gs_predefs funs_and_groups heaps gs_error

	# (iso_fun_ds, funs_and_groups, heaps, gs_error)
		= buildConversionIso type_def from_fun_ds to_fun_ds gs_main_module gs_predefs funs_and_groups heaps gs_error

	# {hp_expression_heap, hp_var_heap, hp_generic_heap, hp_type_heaps={th_vars, th_attrs}} = heaps
296
297
298
299
300
301
302
303
304
	# gs = {gs	& gs_modules = gs_modules
				, gs_td_infos = gs_td_infos
				, gs_error = gs_error
				, gs_avarh = th_attrs
				, gs_tvarh = th_vars
				, gs_varh = hp_var_heap
				, gs_genh = hp_generic_heap
				, gs_exprh = hp_expression_heap
		   }
305
	= ({gtr_type=atype,gtr_iso=iso_fun_ds,gtr_to=to_fun_ds,gtr_from=from_fun_ds}, funs_and_groups, gs)
306
307
308
	
//	the structure type

309
convertATypeToGenTypeStruct :: !Ident !Position !PredefinedSymbols !AType (!*Modules, !*TypeDefInfos, !*Heaps, !*ErrorAdmin) 
310
													   -> (GenTypeStruct, (!*Modules, !*TypeDefInfos, !*Heaps, !*ErrorAdmin))
311
convertATypeToGenTypeStruct ident pos predefs type st
312
	= convert type st
313
where	
Artem Alimarine's avatar
Artem Alimarine committed
314
315
316
317
	convert {at_type=TA type_symb args, at_attribute} st
		= convert_type_app type_symb at_attribute args st
	convert {at_type=TAS type_symb args _, at_attribute} st
		= convert_type_app type_symb at_attribute args st
318
319
	convert {at_type=(CV tv) :@: args} st
		#! (args, st) = mapSt convert args st
320
		= (GTSAppVar tv args, st)
321
322
323
	convert {at_type=x --> y} st
		#! (x, st) = convert x st
		#! (y, st) = convert y st
324
		= (GTSArrow x y, st)
325
326
327
328
	convert {at_type=TV tv} st
		= (GTSVar tv, st)  
	convert {at_type=TB _} st
		= (GTSAppCons KindConst [], st)  
329
	convert {at_type=type} (modules, td_infos, heaps, error)
330
		# error = reportError ident pos ("can not build generic representation for this type", type) error
Artem Alimarine's avatar
Artem Alimarine committed
331
332
333
334
335
336
337
338
339
		= (GTSE, (modules, td_infos, heaps, error))

	convert_type_app {type_index} attr args (modules, td_infos, heaps, error)	
		# (type_def, modules) = modules![type_index.glob_module].com_type_defs.[type_index.glob_object]
		= case type_def.td_rhs of 
			SynType atype
				# (expanded_type, th) = expandSynonymType type_def attr args heaps.hp_type_heaps 
				-> convert {at_type = expanded_type, at_attribute = attr} 
					(modules, td_infos, {heaps & hp_type_heaps = th}, error) 
340
			_
341
342
343
344
345
				#! {pds_module, pds_def} = predefs.[PD_UnboxedArrayType]
				| 	type_index.glob_module == pds_module
					&& type_index.glob_object == pds_def
					&& (case args of [{at_type=TB _}] -> True; _ -> False)
					-> (GTSAppCons KindConst [], (modules, td_infos, heaps, error))
346
				| otherwise
347
348
349
350
					#! ({tdi_kinds}, td_infos) = td_infos ! [type_index.glob_module,type_index.glob_object]
					#! kind = if (isEmpty tdi_kinds) KindConst (KindArrow tdi_kinds)
					#! (args, st) = mapSt  convert args (modules, td_infos, heaps, error)
					-> (GTSAppCons kind args, st)  
Artem Alimarine's avatar
Artem Alimarine committed
351

352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
convert_bimap_AType_to_GenTypeStruct :: !AType !Position !PredefinedSymbols (!*Modules, !*TypeDefInfos, !*Heaps, !*ErrorAdmin) 
														 -> (GenTypeStruct, (!*Modules, !*TypeDefInfos, !*Heaps, !*ErrorAdmin))
convert_bimap_AType_to_GenTypeStruct type pos predefs st
	= convert type st
where
	convert {at_type=TA type_symb args, at_attribute} st
		= convert_type_app type_symb at_attribute args st
	convert {at_type=TAS type_symb args _, at_attribute} st
		= convert_type_app type_symb at_attribute args st
	convert {at_type=(CV tv) :@: args} st
		#! (args, st) = mapSt convert args st
		= (GTSAppVar tv args, st)
	convert {at_type=x --> y} st
		#! (x, st) = convert x st
		#! (y, st) = convert y st
		= (GTSArrow x y, st)  
	convert {at_type=TV tv} st
		= (GTSVar tv, st)  
	convert {at_type=TB _} st
		= (GTSAppCons KindConst [], st)  
	convert {at_type=type} (modules, td_infos, heaps, error)
		# error = reportError predefined_idents.[PD_GenericBimap] pos ("can not build generic representation for this type", type) error
		= (GTSE, (modules, td_infos, heaps, error))

376
377
	convert_type_app {type_index=type_index=:{glob_module,glob_object},type_arity} attr args (modules, td_infos, heaps, error)
		# (type_def, modules) = modules![glob_module].com_type_defs.[glob_object]
378
379
		= case type_def.td_rhs of 
			SynType atype
380
				# (expanded_type, th) = expandSynonymType type_def attr args heaps.hp_type_heaps
381
382
				-> convert {at_type = expanded_type, at_attribute = attr} 
					(modules, td_infos, {heaps & hp_type_heaps = th}, error) 
383
			AbstractType _
384
				#! {pds_module, pds_def} = predefs.[PD_UnboxedArrayType]
385
				| glob_module == pds_module && glob_object == pds_def
386
387
					&& (case args of [{at_type=TB _}] -> True; _ -> False)
					-> (GTSAppCons KindConst [], (modules, td_infos, heaps, error))
388
			RecordType _
389
				# {pds_module, pds_def} = predefs.[PD_TypeBimap]
390
				| glob_module == pds_module && glob_object == pds_def
391
					&& case args of [_,_] -> True; _ -> False
392
					#! (tdi_kinds,td_infos) = td_infos![glob_module,glob_object].tdi_kinds
393
					#! kind = if (isEmpty tdi_kinds) KindConst (KindArrow tdi_kinds)
394
					#! (args, st) = convert_args args (modules, td_infos, heaps, error)
395
					-> (GTSAppBimap kind args, st)
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
			AlgType alts
				# n_args = length args
				| n_args>0 && type_arity==n_args
					# (can_generate_bimap_to_or_from,modules,heaps)
						= can_generate_bimap_to_or_from_for_this_type type_def glob_module alts modules heaps
					| can_generate_bimap_to_or_from
						#! (tdi_kinds,td_infos) = td_infos![glob_module,glob_object].tdi_kinds			
						#! (args, st) = convert_args args (modules, td_infos, heaps, error)
						-> (GTSAppConsSimpleType type_index (KindArrow tdi_kinds) args, st)
						-> 	convert_type_app_to_GTSAppCons glob_module glob_object args modules td_infos heaps error
			_
				-> 	convert_type_app_to_GTSAppCons glob_module glob_object args modules td_infos heaps error
	where
		convert_type_app_to_GTSAppCons glob_module glob_object args modules td_infos heaps error
			#! (tdi_kinds,td_infos) = td_infos![glob_module,glob_object].tdi_kinds
			#! kind = if (isEmpty tdi_kinds) KindConst (KindArrow tdi_kinds)
			#! (args, st) = convert_args args (modules, td_infos, heaps, error)
			= (GTSAppCons kind args, st)

	can_generate_bimap_to_or_from_for_this_type :: !CheckedTypeDef !Index ![DefinedSymbol] !*Modules !*Heaps -> (!Bool,!*Modules,!*Heaps)
	can_generate_bimap_to_or_from_for_this_type type_def=:{td_args} type_def_module_n alts modules heaps=:{hp_type_heaps}
		# th_vars = number_type_arguments td_args 0 hp_type_heaps.th_vars
		#! ok = check_constructors alts type_def_module_n modules th_vars
		# th_vars = remove_type_argument_numbers td_args th_vars
		# heaps = {heaps & hp_type_heaps={hp_type_heaps & th_vars=th_vars}}
		= (ok,modules,heaps)
	where
		check_constructors :: ![DefinedSymbol] !Index !Modules !TypeVarHeap -> Bool
		check_constructors [{ds_index}:constructors] type_def_module_n modules th_vars
			# {cons_type,cons_exi_vars} = modules.[type_def_module_n].com_cons_defs.[ds_index]
			= isEmpty cons_exi_vars &&
			  isEmpty cons_type.st_context &&
			  check_constructor cons_type.st_args 0 th_vars &&
			  check_constructors constructors type_def_module_n modules th_vars
		check_constructors [] type_def_module_n modules th_vars
			= True

		check_constructor :: ![AType] !Int !TypeVarHeap -> Bool
		check_constructor [{at_type=TV {tv_info_ptr}}:atypes] used_type_vars th_vars
			= case sreadPtr tv_info_ptr th_vars of
				TVI_GenTypeVarNumber arg_n
					# arg_mask = 1<<arg_n
					| used_type_vars bitand arg_mask<>0
						-> False
						# used_type_vars = used_type_vars bitor arg_mask
						-> check_constructor atypes used_type_vars th_vars
		check_constructor [_:_] used_type_vars th_vars
			= False
		check_constructor [] used_type_vars th_vars
			= True

	convert_args args st
		= mapSt convert args st
449
450

// the structure type of a generic type can often be simplified
Artem Alimarine's avatar
Artem Alimarine committed
451
// because bimaps for types not containing generic variables are indentity bimaps
452
453
simplify_bimap_GenTypeStruct :: ![TypeVar] !GenTypeStruct !*Heaps -> (!GenTypeStruct, !*Heaps)
simplify_bimap_GenTypeStruct gvars type heaps=:{hp_type_heaps=hp_type_heaps=:{th_vars}} 
454
455
456
457
	#! th_vars = foldSt mark_type_var gvars th_vars
	#! (type, th_vars) = simplify type th_vars
	#! th_vars = foldSt clear_type_var gvars th_vars 
	= (type, { heaps & hp_type_heaps = { hp_type_heaps & th_vars = th_vars}})
Artem Alimarine's avatar
Artem Alimarine committed
458
459
460
461
462
463
where
	simplify t=:(GTSAppCons KindConst [])  st
		= (t, st)
	simplify (GTSAppCons kind=:(KindArrow kinds) args) st
		# formal_arity = length kinds
		# actual_arity = length args
464
465
466
467
468
		# contains_gen_vars = occurs_list args st
		| formal_arity == actual_arity && not contains_gen_vars
			= (GTSAppConsBimapKindConst, st)
			# (args, st) = mapSt simplify args st
			= (GTSAppCons kind args, st)
469
470
471
472
473
474
	simplify (GTSAppConsSimpleType type_symbol_n kind args) st
		# contains_gen_vars = occurs_list args st
		| not contains_gen_vars
			= (GTSAppConsBimapKindConst, st)
			# (args, st) = mapSt simplify args st
			= (GTSAppConsSimpleType type_symbol_n kind args, st)
475
476
477
478
479
480
	simplify t=:(GTSAppBimap KindConst [])  st
		= (t, st)
	simplify (GTSAppBimap kind=:(KindArrow kinds) args) st
		# formal_arity = length kinds
		# actual_arity = length args
		# contains_gen_vars = occurs_list args st
Artem Alimarine's avatar
Artem Alimarine committed
481
		| formal_arity == actual_arity && not contains_gen_vars
482
			= (GTSAppConsBimapKindConst, st)
Artem Alimarine's avatar
Artem Alimarine committed
483
			# (args, st) = mapSt simplify args st
484
			= (GTSAppBimap kind args, st)
Artem Alimarine's avatar
Artem Alimarine committed
485
	simplify (GTSArrow x y) st
486
487
488
489
490
491
		# contains_gen_vars = occurs2 x y st
		| not contains_gen_vars
			= (GTSAppConsBimapKindConst, st)
			# (x, st) = simplify x st
			# (y, st) = simplify y st
			= (GTSArrow x y, st)
Artem Alimarine's avatar
Artem Alimarine committed
492
493
494
495
496
	simplify (GTSAppVar tv args) st
		# (args, st) = mapSt simplify args st
		= (GTSAppVar tv args, st)	
	simplify t=:(GTSVar tv) st
		= (t, st)
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
	simplify (GTSPair x y) st
		# (x, st) = simplify x st
		# (y, st) = simplify y st
		= (GTSPair x y, st)
	simplify (GTSEither x y) st
		# (x, st) = simplify x st
		# (y, st) = simplify y st
		= (GTSEither x y, st)
	simplify (GTSCons cons_info_ds x) st
		# (x, st) = simplify x st
		= (GTSCons cons_info_ds x, st)
	simplify (GTSField field_info_ds x) st
		# (x, st) = simplify x st
		= (GTSField field_info_ds x, st)
	simplify (GTSObject type_info_ds x) st
		# (x, st) = simplify x st
		= (GTSObject type_info_ds x, st)
Artem Alimarine's avatar
Artem Alimarine committed
514
515
		
	occurs (GTSAppCons _ args) st 	= occurs_list args st
516
	occurs (GTSAppConsSimpleType _ _ args) st 	= occurs_list args st
517
	occurs (GTSAppBimap _ args) st 	= occurs_list args st
518
	occurs (GTSAppVar tv args) st 	= type_var_occurs tv st || occurs_list args st		
Artem Alimarine's avatar
Artem Alimarine committed
519
	occurs (GTSVar tv) st			= type_var_occurs tv st
520
521
522
	occurs (GTSArrow x y) st 		= occurs2 x y st
	occurs (GTSPair x y) st			= occurs2 x y st
	occurs (GTSEither x y) st		= occurs2 x y st
Artem Alimarine's avatar
Artem Alimarine committed
523
524
525
	occurs (GTSCons _ arg) st 		= occurs arg st	
	occurs (GTSField _ arg) st 		= occurs arg st	
	occurs (GTSObject _ arg) st 	= occurs arg st	
526
	occurs GTSE st 					= False
Artem Alimarine's avatar
Artem Alimarine committed
527

528
529
530
531
532
	occurs2 x y st
		= occurs x st || occurs y st

	occurs_list [] st
		= False
Artem Alimarine's avatar
Artem Alimarine committed
533
	occurs_list [x:xs] st 
534
		= occurs x st || occurs_list xs st
Artem Alimarine's avatar
Artem Alimarine committed
535
536

	type_var_occurs tv th_vars
537
538
539
		= case sreadPtr tv.tv_info_ptr th_vars of
			TVI_Empty = False
			TVI_Used = True
Artem Alimarine's avatar
Artem Alimarine committed
540
541
542
543
544
545

	mark_type_var tv=:{tv_info_ptr} th_vars 
		# (tv_info, th_vars) = readPtr tv_info_ptr th_vars
		= case tv_info of
			TVI_Empty = writePtr tv_info_ptr TVI_Used th_vars 
			_ = abort "type var is not empty"
546

Artem Alimarine's avatar
Artem Alimarine committed
547
548
	clear_type_var {tv_info_ptr} th_vars
		= writePtr tv_info_ptr TVI_Empty th_vars 
549
550

buildStructType ::
Artem Alimarine's avatar
Artem Alimarine committed
551
552
		!GlobalIndex				// type def global index
		!DefinedSymbol 				// type_info
553
		![ConsInfo]					// constructor and field info symbols
554
		!PredefinedSymbols
Artem Alimarine's avatar
Artem Alimarine committed
555
		(!*Modules, !*TypeDefInfos, !*Heaps, !*ErrorAdmin)
556
	-> 	( !GenTypeStruct			// the structure type
Artem Alimarine's avatar
Artem Alimarine committed
557
		, (!*Modules, !*TypeDefInfos, !*Heaps, !*ErrorAdmin)
558
		)
Artem Alimarine's avatar
Artem Alimarine committed
559
buildStructType {gi_module,gi_index} type_info cons_infos predefs (modules, td_infos, heaps, error)
560
	# (type_def=:{td_ident}, modules) = modules![gi_module].com_type_defs.[gi_index]	
Artem Alimarine's avatar
Artem Alimarine committed
561
	= build_type type_def type_info cons_infos (modules, td_infos, heaps, error)	
562
where
Artem Alimarine's avatar
Artem Alimarine committed
563
	build_type {td_rhs=AlgType alts, td_ident, td_pos} type_info cons_infos st
564
		# (cons_args, st) = zipWithSt (build_alt td_ident td_pos) alts cons_infos st
Artem Alimarine's avatar
Artem Alimarine committed
565
		# type = build_sum_type cons_args
566
		= (GTSObject type_info type, st)
567
	build_type
568
			{td_rhs=RecordType {rt_constructor}, td_ident, td_pos} 
Artem Alimarine's avatar
Artem Alimarine committed
569
			type_info  [{ci_cons_info, ci_field_infos}] 
570
571
572
573
			(modules, td_infos, heaps, error)
		# ({cons_type={st_args},cons_exi_vars}, modules) = modules![gi_module].com_cons_defs.[rt_constructor.ds_index]
		| isEmpty cons_exi_vars
			# (args, st) = mapSt (convertATypeToGenTypeStruct td_ident td_pos predefs) st_args (modules, td_infos, heaps, error)		
574
			# args = [GTSField fi arg \\ arg <- args & fi <- ci_field_infos]
575
			# prod_type = build_prod_type args		
576
577
			# type = GTSCons ci_cons_info prod_type
			= (GTSObject type_info type, st)
578
579
			# error = reportError td_ident td_pos "cannot build a generic representation of an existential type" error
			= (GTSE, (modules, td_infos, heaps, error))
Artem Alimarine's avatar
Artem Alimarine committed
580
	build_type {td_rhs=SynType type,td_ident, td_pos} type_info cons_infos (modules, td_infos, heaps, error)
581
		# error = reportError td_ident td_pos "cannot build a generic representation of a synonym type" error
Artem Alimarine's avatar
Artem Alimarine committed
582
		= (GTSE, (modules, td_infos, heaps, error))
Artem Alimarine's avatar
Artem Alimarine committed
583
	build_type td=:{td_rhs=(AbstractType _),td_ident, td_arity, td_args, td_pos} type_info cdis (modules, td_infos, heaps, error)
584
		# error = reportError td_ident td_pos "cannot build a generic representation of an abstract type" error
Artem Alimarine's avatar
Artem Alimarine committed
585
		= (GTSE, (modules, td_infos, heaps, error))
586

587
	build_alt td_ident td_pos cons_def_sym=:{ds_index} {ci_cons_info} (modules, td_infos, heaps, error)
588
589
590
		# ({cons_type={st_args},cons_exi_vars}, modules) = modules![gi_module].com_cons_defs.[ds_index]
		| isEmpty cons_exi_vars
			# (args, st) = mapSt (convertATypeToGenTypeStruct td_ident td_pos predefs) st_args (modules, td_infos, heaps, error)	
591
592
			# prod_type = build_prod_type args
			= (GTSCons ci_cons_info prod_type, st)
593
594
			# error = reportError td_ident td_pos "cannot build a generic representation of an existential type" error
			= (GTSE, (modules, td_infos, heaps, error))
595
596
597
598
599

	build_prod_type :: [GenTypeStruct] -> GenTypeStruct
	build_prod_type types 
		= listToBin build_pair build_unit types	
	where
600
		build_pair x y = GTSPair x y
601
602
603
604
605
606
		build_unit = GTSAppCons KindConst []	
		
	build_sum_type :: [GenTypeStruct] -> GenTypeStruct
	build_sum_type types
		= listToBin build_either build_void types
	where
607
		build_either x y = GTSEither x y
608
		build_void = abort "sanity check: no alternatives in a type\n"		
John van Groningen's avatar
John van Groningen committed
609

610
/*
611
// build a product of types
John van Groningen's avatar
John van Groningen committed
612
buildProductType :: ![AType] !PredefinedSymbols -> AType
613
614
615
616
617
618
619
buildProductType types predefs 
	= listToBin build_pair build_unit types
where
	build_pair x y = buildPredefTypeApp PD_TypePAIR [x, y] predefs	
	build_unit  = buildPredefTypeApp PD_TypeUNIT [] predefs

// build a sum of types		
John van Groningen's avatar
John van Groningen committed
620
buildSumType :: ![AType] !PredefinedSymbols -> AType
621
622
623
624
625
buildSumType types predefs 
	= listToBin build_either build_void types
where
	build_either x y = buildPredefTypeApp PD_TypeEITHER [x, y] predefs	
	build_void  = abort "sum of zero types\n"
John van Groningen's avatar
John van Groningen committed
626
*/
627
628
629
630
631
632
633
634
635
636

// build a binary representation of a list
listToBin :: (a a -> a) a [a] -> a 
listToBin bin tip [] = tip
listToBin bin tip [x] = x
listToBin bin tip xs
	# (l,r) = splitAt ((length xs) / 2) xs
	= bin (listToBin bin tip l) (listToBin bin tip r)

// build application of a predefined type constructor 
John van Groningen's avatar
John van Groningen committed
637
buildPredefTypeApp :: !Int [AType] !PredefinedSymbols -> AType
638
639
640
641
642
643
644
buildPredefTypeApp predef_index args predefs
	# {pds_module, pds_def} = predefs.[predef_index]
	# pds_ident = predefined_idents.[predef_index]
	# global_index = {glob_module = pds_module, glob_object = pds_def}
	# type_symb = MakeTypeSymbIdent global_index pds_ident (length args) 		  
	= makeAType (TA type_symb args) TA_Multi

645
646
647
648
649
650
//	build type infos
buildTypeDefInfo :: 
		!Index 				// type def module
		!CheckedTypeDef		// the type definition
		!Index				// icl module
		!PredefinedSymbols		
651
		!FunsAndGroups !*Modules !*Heaps !*ErrorAdmin
Artem Alimarine's avatar
Artem Alimarine committed
652
653
	-> 	( DefinedSymbol	// type info
		, ![ConsInfo]
654
		, !FunsAndGroups, !*Modules, !*Heaps, !*ErrorAdmin)
655
buildTypeDefInfo td_module td=:{td_rhs = AlgType alts} main_module_index predefs funs_and_groups modules heaps error
656
657
658
	= buildTypeDefInfo1 td_module td alts [] main_module_index predefs funs_and_groups modules heaps error
buildTypeDefInfo td_module td=:{td_rhs = RecordType {rt_constructor, rt_fields}} main_module_index predefs funs_and_groups modules heaps error
	= buildTypeDefInfo1 td_module td [rt_constructor] [x\\x<-:rt_fields] main_module_index predefs funs_and_groups modules heaps error
659
660
buildTypeDefInfo td_module td=:{td_rhs = SynType type, td_ident, td_pos} main_module_index predefs funs_and_groups modules heaps error
	# error = reportError td_ident td_pos "cannot build constructor uinformation for a synonym type" error
661
	= buildTypeDefInfo1 td_module td [] [] main_module_index predefs funs_and_groups modules heaps error
662
663
buildTypeDefInfo td_module td=:{td_rhs = AbstractType _, td_ident, td_pos} main_module_index predefs funs_and_groups modules heaps error
	# error = reportError td_ident td_pos "cannot build constructor uinformation for an abstract type" error
664
	= buildTypeDefInfo1 td_module td [] [] main_module_index predefs funs_and_groups modules heaps error
665

666
667
buildTypeDefInfo1 td_module {td_ident, td_pos, td_arity} alts fields main_module_index predefs
				funs_and_groups=:{fg_fun_index=fun_index,fg_group_index=group_index,fg_funs=funs,fg_groups=groups} modules heaps error
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682

	# num_conses = length alts
	# num_fields = length fields
	# new_group_index = inc group_index

	# type_def_dsc_index = fun_index
	# first_cons_dsc_index = fun_index + 1
	# cons_dsc_indexes = [first_cons_dsc_index .. first_cons_dsc_index + num_conses - 1]
	# first_field_dsc_index = first_cons_dsc_index + num_conses
	# field_dsc_indexes = [first_field_dsc_index .. first_field_dsc_index + num_fields - 1]
	# new_fun_index = first_field_dsc_index + num_fields

	# group = {group_members = [fun_index .. new_fun_index - 1]}
	# new_groups = [group:groups]
	
683
	# type_def_dsc_ds = {ds_arity=0, ds_ident=makeIdent("tdi_"+++td_ident.id_name), ds_index=type_def_dsc_index}
684
685
	# cons_dsc_dss = [ {ds_arity=0, ds_ident=makeIdent("cdi_"+++ds_ident.id_name), ds_index=i} \\ 
		{ds_ident} <- alts & i <- cons_dsc_indexes]
686
687
	# field_dsc_dss = [ {ds_arity=0, ds_ident=makeIdent("fdi_"+++fs_ident.id_name), ds_index=i} \\ 
		{fs_ident} <- fields & i <- field_dsc_indexes]
688

689
690
	# (type_def_dsc_fun, heaps) = build_type_def_dsc group_index cons_dsc_dss type_def_dsc_ds heaps	
	
John van Groningen's avatar
John van Groningen committed
691
	# (cons_dsc_funs, (modules, heaps)) = zipWithSt (build_cons_dsc group_index type_def_dsc_ds field_dsc_dss) cons_dsc_dss alts (modules, heaps)
692

693
694
	# (field_dsc_funs, (modules, heaps)) = zipWithSt (build_field_dsc group_index (hd cons_dsc_dss)) field_dsc_dss fields (modules, heaps)
	 
Artem Alimarine's avatar
Artem Alimarine committed
695
696
	// NOTE: reverse order (new functions are added at the head) 
	# new_funs = (reverse field_dsc_funs) ++ (reverse cons_dsc_funs) ++ [type_def_dsc_fun] ++ funs 
697
698

	# funs_and_groups = {funs_and_groups & fg_fun_index=new_fun_index, fg_group_index=new_group_index, fg_funs=new_funs, fg_groups=new_groups}
Artem Alimarine's avatar
Artem Alimarine committed
699
700
701

	# (type_info_ds, (funs_and_groups, heaps)) 
		= build_type_info type_def_dsc_ds (funs_and_groups, heaps)
702
703
704
705
706
707
708
709
	
	# (cons_info_dss, (funs_and_groups, heaps)) 
		= mapSt build_cons_info cons_dsc_dss (funs_and_groups, heaps)

	# (field_info_dss, (funs_and_groups, heaps)) 
		= mapSt build_field_info field_dsc_dss (funs_and_groups, heaps)

	# cons_infos = case (cons_info_dss, field_info_dss) of
Artem Alimarine's avatar
Artem Alimarine committed
710
		([cons_info_ds], field_infos) -> [{ci_cons_info = cons_info_ds, ci_field_infos = field_infos}] 	 
711
712
713
		(cons_info_dss, []) -> [{ci_cons_info=x,ci_field_infos=[]}\\x<-cons_info_dss]
		_ -> abort "generics.icl sanity check: fields in non-record type\n"

Artem Alimarine's avatar
Artem Alimarine committed
714
	= (type_info_ds, cons_infos, funs_and_groups, modules, heaps, error)
715
where
716
	build_type_def_dsc group_index cons_info_dss {ds_ident} heaps
717
		# td_name_expr = makeStringExpr td_ident.id_name
718
		# td_arity_expr = makeIntExpr td_arity
Artem Alimarine's avatar
Artem Alimarine committed
719
		# num_conses_expr = makeIntExpr (length alts)
720
721
722
723
		# (cons_info_exprs, heaps) = mapSt (\x st->buildFunApp main_module_index x [] st) cons_info_dss heaps
		# (td_conses_expr, heaps) = makeListExpr cons_info_exprs predefs heaps
		
		# (body_expr, heaps) = buildPredefConsApp PD_CGenericTypeDefDescriptor 
Artem Alimarine's avatar
Artem Alimarine committed
724
725
726
727
			[ td_name_expr
			, td_arity_expr
			, num_conses_expr 
			, td_conses_expr
728
			// TODO: module_name_expr
Artem Alimarine's avatar
Artem Alimarine committed
729
			] 
730
731
			predefs heaps

732
		# fun = makeFunction ds_ident group_index [] body_expr No main_module_index td_pos
733
734
735
		= (fun, heaps)

	build_cons_dsc group_index type_def_info_ds field_dsc_dss cons_info_ds cons_ds (modules, heaps)
736
		# ({cons_ident,cons_type,cons_priority,cons_number,cons_exi_vars}, modules)
Artem Alimarine's avatar
Artem Alimarine committed
737
			= modules! [td_module].com_cons_defs.[cons_ds.ds_index]  		
738
		# name_expr 			 = makeStringExpr cons_ident.id_name
739
740
741
		# arity_expr 			 = makeIntExpr cons_type.st_arity
		# (prio_expr, heaps)	 = make_prio_expr cons_priority heaps
		# (type_def_expr, heaps) = buildFunApp main_module_index type_def_info_ds [] heaps
742
		# (type_expr, heaps) 	 = make_type_expr cons_exi_vars cons_type heaps 			
743
744
		# (field_exprs, heaps)   = mapSt (\x st->buildFunApp main_module_index x [] st) field_dsc_dss heaps
		# (fields_expr, heaps)   =  makeListExpr field_exprs predefs heaps 
745
		# cons_index_expr		 = makeIntExpr cons_number
746
747
748
749
750
751
752
753
		# (body_expr, heaps) 
			= buildPredefConsApp PD_CGenericConsDescriptor 
				[ name_expr 
				, arity_expr
				, prio_expr
				, type_def_expr
				, type_expr
				, fields_expr 
Artem Alimarine's avatar
Artem Alimarine committed
754
				, cons_index_expr
755
756
757
				]  
				predefs heaps

758
		# fun = makeFunction cons_info_ds.ds_ident group_index [] body_expr No main_module_index td_pos		
759
760
761
762
763
764
765
766
767
768
769
770
771
		= (fun, (modules, heaps))
	where
		make_prio_expr NoPrio heaps
			= buildPredefConsApp PD_CGenConsNoPrio [] predefs heaps
		make_prio_expr (Prio assoc prio) heaps
			# assoc_predef = case assoc of
				NoAssoc 	-> PD_CGenConsAssocNone 
				LeftAssoc 	-> PD_CGenConsAssocLeft
				RightAssoc 	-> PD_CGenConsAssocRight
			# (assoc_expr, heaps) = buildPredefConsApp assoc_predef [] predefs heaps 	
			# prio_expr = makeIntExpr prio		
			= buildPredefConsApp PD_CGenConsPrio [assoc_expr, prio_expr] predefs heaps 

772
		make_type_expr [] {st_vars, st_args, st_result} heaps=:{hp_type_heaps=type_heaps=:{th_vars}}
773
774
775
			# (_,th_vars) = foldSt (\ {tv_info_ptr} (n, th_vars) -> (n+1, writePtr tv_info_ptr (TVI_GenTypeVarNumber n) th_vars)) st_vars (0,th_vars)
			# heaps = {heaps & hp_type_heaps={type_heaps & th_vars=th_vars}}
			# (arg_exprs, heaps) = mapSt make_expr1 st_args heaps
776
			# (result_expr, heaps) = make_expr1 st_result heaps
777
778
779
780
			# {hp_type_heaps=type_heaps=:{th_vars}} = heaps
			# th_vars = foldSt (\ {tv_info_ptr} th_vars -> writePtr tv_info_ptr TVI_Empty th_vars) st_vars th_vars
			# heaps = {heaps & hp_type_heaps={type_heaps & th_vars=th_vars}}
			= curry arg_exprs result_expr heaps
781
782
783
784
785
786
787
788
789
790
791
792
793
		where
			curry [] result_expr heaps 
				= (result_expr, heaps)
			curry [x:xs] result_expr heaps
				# (y, heaps) = curry xs result_expr heaps
				= make_arrow x y heaps
		
			make_expr1 :: !AType !*Heaps -> (!Expression, !*Heaps)
			make_expr1 {at_type} heaps = make_expr at_type heaps
		
			make_expr :: !Type !*Heaps -> (!Expression, !*Heaps)
			make_expr (TA type_symb arg_types) heaps
				# (arg_exprs, heaps) = mapSt make_expr1 arg_types heaps
794
				# (type_cons, heaps) = make_type_cons type_symb.type_ident.id_name heaps 
795
796
797
				= make_apps type_cons arg_exprs heaps
			make_expr (TAS type_symb arg_types _) heaps
				# (arg_exprs, heaps) = mapSt make_expr1 arg_types heaps
798
				# (type_cons, heaps) = make_type_cons type_symb.type_ident.id_name heaps 
799
800
801
802
803
804
805
806
807
808
809
				= make_apps type_cons arg_exprs heaps
			make_expr (x --> y) heaps
				# (x, heaps) = make_expr1 x heaps
				# (y, heaps) = make_expr1 y heaps				
				= make_arrow x y heaps
			make_expr TArrow heaps 
				= make_type_cons "(->)" heaps
			make_expr (TArrow1 type) heaps
				# (arg_expr, heaps) = make_expr1 type heaps 
				# (arrow_expr, heaps) = make_type_cons "(->)" heaps
				= make_app arrow_expr arg_expr heaps
810
			make_expr (CV {tv_info_ptr} :@: arg_types) heaps
811
				# (arg_exprs, heaps) = mapSt make_expr1 arg_types heaps
812
				# (tv_expr, heaps) = make_type_var tv_info_ptr heaps
813
814
815
				= make_apps tv_expr arg_exprs heaps
			make_expr (TB bt) heaps
				= make_type_cons (toString bt) heaps	
816
817
818
819
820
821
			make_expr (TV {tv_info_ptr}) heaps 
				= make_type_var tv_info_ptr heaps 
			make_expr (GTV {tv_info_ptr}) heaps
				= make_type_var tv_info_ptr heaps 
			make_expr (TQV {tv_info_ptr}) heaps 
				= make_type_var tv_info_ptr heaps
822
			make_expr TE heaps
823
824
825
826
827
				= make_error_type_cons heaps
			make_expr (TFA _ _) heaps
				// error is reported in convertATypeToGenTypeStruct
				= make_error_type_cons heaps
			make_expr _ heaps
828
				= abort "type does not match\n"
829

830
831
832
833
			make_apps x [] heaps 
				= (x, heaps)
			make_apps x [y:ys] heaps
				# (z, heaps) = make_app x y heaps	
834
				= make_apps z ys heaps
835
836
837
838
839
840

			make_type_var tv_info_ptr heaps
				#! type_var_n = case sreadPtr tv_info_ptr heaps.hp_type_heaps.th_vars of
									TVI_GenTypeVarNumber n -> n
				= buildPredefConsApp PD_CGenTypeVar [makeIntExpr type_var_n] predefs heaps									

841
			make_arrow x y heaps = buildPredefConsApp PD_CGenTypeArrow [x, y] predefs heaps
842

843
844
			make_app x y heaps = buildPredefConsApp PD_CGenTypeApp [x, y] predefs heaps 	 

845
846
847
848
849
850
851
852
853
			make_error_type_cons heaps = make_type_cons "<error>" heaps
		make_type_expr [_:_] {st_vars, st_args, st_result} heaps
			// Error "cannot build a generic representation of an existential type" is reported in buildStructType
			= make_type_cons "<error>" heaps

	make_type_cons name heaps
		# name_expr = makeStringExpr name
		= buildPredefConsApp PD_CGenTypeCons [name_expr] predefs heaps

854
855
	build_field_dsc group_index cons_dsc_ds field_dsc_ds {fs_ident, fs_index} (modules, heaps)
		# name_expr = makeStringExpr fs_ident.id_name
856
857
858
		# ({sd_field_nr}, modules)	
			= modules! [td_module].com_selector_defs.[fs_index]  		
		# index_expr = makeIntExpr sd_field_nr
859
860
861
862
863
864
865
866
		# (cons_expr, heaps) = buildFunApp main_module_index cons_dsc_ds [] heaps				
		# (body_expr, heaps) 
			= buildPredefConsApp PD_CGenericFieldDescriptor 
				[ name_expr 
				, index_expr
				, cons_expr
				]  
				predefs heaps
867
		# fun = makeFunction field_dsc_ds.ds_ident group_index [] body_expr No main_module_index td_pos		
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
		= (fun, (modules, heaps))
		
	build_cons_info cons_dsc_ds (funs_and_groups, heaps)
		# ident = makeIdent ("g"+++cons_dsc_ds.ds_ident.id_name)	

		# (cons_dsc_expr, heaps) = buildFunApp main_module_index cons_dsc_ds [] heaps

		# (body_expr, heaps) 
			= buildPredefConsApp PD_GenericConsInfo [cons_dsc_expr] predefs heaps		

		# (def_sym, funs_and_groups) = buildFunAndGroup ident [] body_expr No main_module_index td_pos funs_and_groups
		= (def_sym, (funs_and_groups, heaps))

	build_field_info field_dsc_ds (funs_and_groups, heaps)
		# ident = makeIdent ("g"+++field_dsc_ds.ds_ident.id_name)	

		# (field_dsc_expr, heaps) = buildFunApp main_module_index field_dsc_ds [] heaps

		# (body_expr, heaps) 
			= buildPredefConsApp PD_GenericFieldInfo [field_dsc_expr] predefs heaps		

		# (def_sym, funs_and_groups) = buildFunAndGroup ident [] body_expr No main_module_index td_pos funs_and_groups
		= (def_sym, (funs_and_groups, heaps))

Artem Alimarine's avatar
Artem Alimarine committed
892
893
894
895
896
897
898
899
900
901
902
	build_type_info type_dsc_ds (funs_and_groups, heaps)
		# ident = makeIdent ("g"+++type_dsc_ds.ds_ident.id_name)	

		# (type_dsc_expr, heaps) = buildFunApp main_module_index type_dsc_ds [] heaps

		# (body_expr, heaps) 
			= buildPredefConsApp PD_GenericTypeInfo [type_dsc_expr] predefs heaps		

		# (def_sym, funs_and_groups) = buildFunAndGroup ident [] body_expr No main_module_index td_pos funs_and_groups
		= (def_sym, (funs_and_groups, heaps))

903
904
905
906
907
908
909
910
//	conversions functions

// buildConversionIso
buildConversionIso :: 
		!CheckedTypeDef		// the type definition
		!DefinedSymbol		// from fun
		!DefinedSymbol	 	// to fun
		!Index				// main module
911
912
913
914
915
916
		!PredefinedSymbols
		FunsAndGroups !*Heaps !*ErrorAdmin
	-> (!DefinedSymbol,
		FunsAndGroups,!*Heaps,!*ErrorAdmin)
buildConversionIso type_def=:{td_ident, td_pos} from_fun to_fun
		main_dcl_module_n predefs funs_and_groups heaps error
917
918
	#! (from_expr, heaps) 	= buildFunApp main_dcl_module_n from_fun [] heaps
	#! (to_expr, heaps) 	= buildFunApp main_dcl_module_n to_fun [] heaps	
919
	#! (iso_expr, heaps) 	= build_bimap_record to_expr from_expr predefs heaps
920
	
921
	#! ident = makeIdent ("iso" +++ td_ident.id_name)
922
923
	#! (def_sym, funs_and_groups) = buildFunAndGroup ident [] iso_expr No main_dcl_module_n td_pos funs_and_groups
	= (def_sym, funs_and_groups, heaps, error)
924
925
926

build_bimap_record to_expr from_expr predefs heaps 
	= buildPredefConsApp PD_ConsBimap [to_expr, from_expr] predefs heaps	
927

928
929
930
931
932
933
// conversion from type to generic
buildConversionTo ::
		!Index				// type def module
		!CheckedTypeDef 	// the type def
		!Index 				// main module
		!PredefinedSymbols
934
935
936
		!FunsAndGroups !*Heaps !*ErrorAdmin
	-> 	(!DefinedSymbol,
		 FunsAndGroups,!*Heaps,!*ErrorAdmin)
937
938
buildConversionTo		
		type_def_mod 
939
		type_def=:{td_rhs, td_ident, td_index, td_pos} 
940
		main_module_index predefs funs_and_groups heaps error
941
942
943
	# (arg_expr, arg_var, heaps) = buildVarExpr "x" heaps 
	# (body_expr, heaps, error) = 
		build_expr_for_type_rhs type_def_mod td_index td_rhs arg_expr heaps error
944
	# fun_name = makeIdent ("toGeneric" +++ td_ident.id_name)
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
	| not error.ea_ok
		# (def_sym, funs_and_groups) 
			= (buildFunAndGroup fun_name [] EE No main_module_index td_pos funs_and_groups)
		= (def_sym, funs_and_groups, heaps, error)	
		# (def_sym, funs_and_groups) 
			= (buildFunAndGroup fun_name [arg_var] body_expr No main_module_index td_pos funs_and_groups)
		= (def_sym, funs_and_groups, heaps, error)
where
	// build conversion for type rhs
	build_expr_for_type_rhs :: 
			!Int 				// type def module
			!Int 				// type def index 
			!TypeRhs			// type def rhs 
			!Expression			// expression of the function argument variable   
			!*Heaps 
			!*ErrorAdmin
		-> 	( !Expression		// generated expression
			, !*Heaps	// state
963
			, !*ErrorAdmin)
964
 	build_expr_for_type_rhs type_def_mod type_def_index (AlgType def_symbols) arg_expr heaps error
965
		= build_expr_for_conses False type_def_mod type_def_index def_symbols arg_expr heaps error
966
	build_expr_for_type_rhs type_def_mod type_def_index (RecordType {rt_constructor}) arg_expr heaps error		
967
		= build_expr_for_conses True type_def_mod type_def_index [rt_constructor] arg_expr  heaps error
968
	build_expr_for_type_rhs type_def_mod type_def_index (AbstractType _) arg_expr  heaps error
969
		#! error = checkErrorWithIdentPos (newPosition td_ident td_pos) "cannot build isomorphisms for an abstract type" error
970
971
		= (EE, heaps, error)
	build_expr_for_type_rhs type_def_mod type_def_index (SynType _) arg_expr  heaps error
972
		#! error = checkErrorWithIdentPos (newPosition td_ident td_pos) "cannot build isomorphisms for a synonym type" error
973
		= (EE, heaps, error)
974

975
	// build conversion for constructors of a type def 	
976
	build_expr_for_conses is_record type_def_mod type_def_index cons_def_syms arg_expr heaps error
977
978
		# (case_alts, heaps, error)
			= build_exprs_for_conses is_record 0 (length cons_def_syms) type_def_mod cons_def_syms  heaps error
979
980
		# case_patterns = AlgebraicPatterns {glob_module = type_def_mod, glob_object = type_def_index} case_alts
		# (case_expr, heaps) = buildCaseExpr arg_expr case_patterns heaps
981
		= (case_expr, heaps, error)
982

983
984
	// build conversions for constructors 	
	build_exprs_for_conses :: !Bool !Int !Int !Int ![DefinedSymbol] !*Heaps !*ErrorAdmin
985
		-> ([AlgebraicPattern], !*Heaps, !*ErrorAdmin)
986
987
	build_exprs_for_conses is_record i n type_def_mod [] heaps error
		= ([], heaps, error)
988
989
990
	build_exprs_for_conses is_record i n type_def_mod [cons_def_sym:cons_def_syms] heaps error
		#! (alt, heaps, error) = build_expr_for_cons is_record i n type_def_mod cons_def_sym heaps error
		#! (alts, heaps, error) =  build_exprs_for_conses is_record (i+1) n type_def_mod cons_def_syms heaps error 		
991
		= ([alt:alts], heaps, error)
992

993
	// build conversion for a constructor	
994
	build_expr_for_cons :: !Bool !Int !Int !Int !DefinedSymbol !*Heaps !*ErrorAdmin 
995
		-> (AlgebraicPattern, !*Heaps, !*ErrorAdmin)
996
	build_expr_for_cons is_record i n type_def_mod cons_def_sym=:{ds_ident, ds_arity} heaps error	
997
998
		#! names = ["x" +++ toString (i+1) +++ toString k \\ k <- [1..ds_arity]]
		#! (var_exprs, vars, heaps) = buildVarExprs names heaps 
999

1000
		#! (arg_exprs, heaps) = build_fields is_record var_exprs heaps
1001
1002
			with
				build_fields False var_exprs heaps = (var_exprs, heaps)
1003
				build_fields True var_exprs heaps = mapSdSt build_field var_exprs predefs heaps
1004
1005

		#! (expr, heaps) = build_prod arg_exprs predefs heaps
1006
		#! (expr, heaps) = build_cons expr predefs heaps
1007
1008
		#! (expr, heaps) = build_sum i n expr predefs heaps
				
1009
		#! (expr, heaps) = build_object expr predefs heaps
Artem Alimarine's avatar
Artem Alimarine committed
1010
						
1011
		#! alg_pattern = {
1012
			ap_symbol = {glob_module = type_def_mod, glob_object = cons_def_sym},
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
			ap_vars = vars,
			ap_expr = expr,
			ap_position = NoPos
			}
		= (alg_pattern, heaps, error)	
	
	build_sum :: !Int !Int !Expression !PredefinedSymbols !*Heaps -> (!Expression, !*Heaps)
	build_sum i n expr predefs heaps
		| n == 0 	= abort "build sum of zero elements\n"
		| i >= n	= abort "error building sum"
		| n == 1 	= (expr, heaps)
		| i < (n/2) 
			# (expr, heaps) = build_sum i (n/2) expr predefs heaps
1026
			= build_left expr predefs heaps
1027
1028
		| otherwise
			# (expr, heaps) = build_sum (i - (n/2)) (n - (n/2)) expr predefs heaps
1029
			= build_right expr predefs heaps
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
				
	build_prod :: ![Expression] !PredefinedSymbols !*Heaps -> (!Expression, !*Heaps)
	build_prod [] predefs heaps = build_unit heaps
	where
		build_unit heaps = buildPredefConsApp PD_ConsUNIT [] predefs heaps 	
	build_prod [expr] predefs heaps = (expr, heaps)
	build_prod exprs predefs heaps
		# (lexprs, rexprs) = splitAt ((length exprs)/2) exprs  
		# (lexpr, heaps) = build_prod lexprs predefs heaps
		# (rexpr, heaps) = build_prod rexprs predefs heaps
1040
		= build_pair lexpr rexpr predefs heaps
1041
1042
1043
1044
1045
1046
			
buildConversionFrom	::	
		!Index				// type def module
		!CheckedTypeDef 	// the type def
		!Index 				// main module
		!PredefinedSymbols
1047
1048
1049
1050
		!FunsAndGroups !*Heaps !*ErrorAdmin
	-> (!DefinedSymbol,
		 FunsAndGroups,!*Heaps,!*ErrorAdmin)
buildConversionFrom
1051
		type_def_mod 
1052
		type_def=:{td_rhs, td_ident, td_index, td_pos} 
1053
		main_module_index predefs funs_and_groups heaps error
1054
1055
	# (body_expr, arg_var, heaps, error) = 
		build_expr_for_type_rhs type_def_mod td_rhs heaps error
1056
	# fun_name = makeIdent ("fromGeneric" +++ td_ident.id_name)
1057
1058
1059
	| not error.ea_ok
		# (def_sym, funs_and_groups) 
			= (buildFunAndGroup fun_name [] EE No main_module_index td_pos funs_and_groups)
1060
		= (def_sym, funs_and_groups, heaps, error)
1061
1062
1063
1064
1065
1066
1067
1068
		# (def_sym, funs_and_groups) 
			= (buildFunAndGroup fun_name [arg_var] body_expr No main_module_index td_pos funs_and_groups)
		= (def_sym, funs_and_groups, heaps, error)
where
	// build expression for type def rhs
	build_expr_for_type_rhs :: 
			!Index				// type def module
			!TypeRhs			// type rhs
1069
			!*Heaps !*ErrorAdmin	
1070
1071
		-> 	( !Expression		// body expresssion
			, !FreeVar
1072
			, !*Heaps, !*ErrorAdmin)
1073
	build_expr_for_type_rhs type_def_mod (AlgType def_symbols) heaps error
Artem Alimarine's avatar
Artem Alimarine committed
1074
		#! (expr, var, heaps, error) = build_sum False type_def_mod def_symbols heaps error
1075
		#! (expr, var, heaps) = build_case_object var expr predefs heaps
Artem Alimarine's avatar
Artem Alimarine committed
1076
		= (expr, var, heaps, error)
1077
	build_expr_for_type_rhs type_def_mod (RecordType {rt_constructor}) heaps error				
Artem Alimarine's avatar
Artem Alimarine committed
1078
		# (expr, var, heaps, error) = build_sum True type_def_mod [rt_constructor] heaps	error
1079
		#! (expr, var, heaps) = build_case_object var expr predefs heaps
Artem Alimarine's avatar
Artem Alimarine committed
1080
		= (expr, var, heaps, error)
1081
	build_expr_for_type_rhs type_def_mod (AbstractType _) heaps error
1082
1083
		#! error = reportError td_ident td_pos "cannot build isomorphisms for an abstract type" error
		# dummy_fv = {fv_def_level=(-1), fv_count=0, fv_ident=makeIdent "dummy", fv_info_ptr=nilPtr}
1084
		= (EE, dummy_fv, heaps, error)
1085
	build_expr_for_type_rhs type_def_mod (SynType _) heaps error
1086
1087
		#! error = reportError td_ident td_pos "cannot build isomorphisms for a synonym type" error
		# dummy_fv = {fv_def_level=(-1), fv_count=0, fv_ident=makeIdent "dummy", fv_info_ptr=nilPtr}
1088
		= (EE, dummy_fv, heaps, error)
1089
1090
	
	// build expression for sums
1091
1092
	build_sum ::
			!Bool				// is record 
1093
1094
			!Index 
			![DefinedSymbol] 
1095
			!*Heaps !*ErrorAdmin
1096
1097
		-> 	( !Expression
			, !FreeVar			// top variable
1098
			, !*Heaps, !*ErrorAdmin)
1099
	build_sum is_record type_def_mod [] heaps error
1100
		= abort "algebraic type with no constructors!\n"
1101
	build_sum is_record type_def_mod [def_symbol] heaps error
1102
		#! (cons_app_expr, cons_arg_vars, heaps) = build_cons_app type_def_mod def_symbol heaps
1103
		#! (prod_expr, var, heaps) = build_prod is_record cons_app_expr cons_arg_vars heaps 
1104
		#! (alt_expr, var, heaps) = build_case_cons var prod_expr predefs heaps
1105
		= (alt_expr, var, heaps, error)
1106
	build_sum is_record type_def_mod def_symbols heaps error
1107
1108
		#! (left_def_syms, right_def_syms) = splitAt ((length def_symbols) /2) def_symbols
		#! (left_expr, left_var, heaps, error) 
1109
			= build_sum is_record type_def_mod left_def_syms heaps error
1110
		#! (right_expr, right_var, heaps, error)
1111
			= build_sum is_record type_def_mod right_def_syms heaps error	
1112
		#! (case_expr, var, heaps) = 
1113
			build_case_either left_var left_expr right_var right_expr predefs heaps
1114
1115
1116
1117
		= (case_expr, var, heaps, error)
	
	// build expression for products
	build_prod :: 
1118
			!Bool							// is record
1119
1120
1121
1122
1123
1124
1125
			!Expression   					// result of the case on product
			![FreeVar] 						// list of variables of the constructor pattern
			!*Heaps
		-> 	( !Expression					// generated product
			, !FreeVar						// top variable
			, !*Heaps
			)
1126
	build_prod is_record expr [] heaps
1127
		= build_case_unit expr heaps	
1128
	build_prod is_record expr [cons_arg_var] heaps
1129
1130
1131
		| is_record
			= build_case_field cons_arg_var expr predefs heaps
			= (expr, cons_arg_var, heaps)
1132
	build_prod is_record expr cons_arg_vars heaps
1133
		#! (left_vars, right_vars) = splitAt ((length cons_arg_vars) /2) cons_arg_vars		 
1134
		#! (expr, right_var, heaps) = build_prod is_record expr right_vars heaps
1135
		#! (expr, left_var, heaps) = build_prod is_record expr left_vars heaps
1136
		#! (case_expr, var, heaps) = build_case_pair left_var right_var expr predefs heaps
1137
1138
		= (case_expr, var, heaps) 
	
1139
	// build constructor application expression
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
	build_cons_app :: !Index !DefinedSymbol !*Heaps 
		-> (!Expression, ![FreeVar], !*Heaps)
	build_cons_app cons_mod def_symbol=:{ds_arity} heaps
		#! names = ["x"  +++ toString k \\ k <- [1..ds_arity]]
		#! (var_exprs, vars, heaps) = buildVarExprs names heaps 
		#! (expr, heaps) = buildConsApp cons_mod def_symbol var_exprs heaps
	 	= (expr, vars, heaps)

	build_case_unit body_expr heaps
		# unit_pat = buildPredefConsPattern PD_ConsUNIT [] body_expr predefs
		# {pds_module, pds_def} = predefs.[PD_TypeUNIT]
		# case_patterns = AlgebraicPatterns {glob_module = pds_module, glob_object = pds_def} [unit_pat]
		= build_case_expr case_patterns heaps

1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
build_pair x y predefs heaps
	= buildPredefConsApp PD_ConsPAIR [x, y] predefs heaps

build_left x predefs heaps
	= buildPredefConsApp PD_ConsLEFT [x] predefs heaps

build_right x predefs heaps
	= buildPredefConsApp PD_ConsRIGHT [x] predefs heaps

build_object expr predefs heaps
	= buildPredefConsApp PD_ConsOBJECT [expr] predefs heaps						

build_cons expr predefs heaps
	= buildPredefConsApp PD_ConsCONS [expr] predefs heaps

build_field var_expr predefs heaps
	= buildPredefConsApp PD_ConsFIELD [var_expr] predefs heaps 

build_case_pair var1 var2 body_expr predefs heaps
	# pair_pat = buildPredefConsPattern PD_ConsPAIR [var1, var2] body_expr predefs	
	# {pds_module, pds_def} = predefs.[PD_TypePAIR]
	# case_patterns = AlgebraicPatterns {glob_module = pds_module, glob_object = pds_def} [pair_pat]	
	= build_case_expr case_patterns heaps

build_case_either left_var left_expr right_var right_expr predefs heaps
	# left_pat = buildPredefConsPattern PD_ConsLEFT [left_var] left_expr predefs
	# right_pat = buildPredefConsPattern PD_ConsRIGHT [right_var] right_expr predefs
	# {pds_module, pds_def} = predefs.[PD_TypeEITHER]
	# case_patterns = AlgebraicPatterns {glob_module = pds_module, glob_object = pds_def} [left_pat, right_pat]
	= build_case_expr case_patterns heaps

build_case_object var body_expr predefs heaps
	# pat = buildPredefConsPattern PD_ConsOBJECT [var] body_expr predefs	
	# {pds_module, pds_def} = predefs.[PD_TypeOBJECT]
	# case_patterns = AlgebraicPatterns {glob_module = pds_module, glob_object = pds_def} [pat]
	= build_case_expr case_patterns heaps

build_case_cons var body_expr predefs heaps
	# pat = buildPredefConsPattern PD_ConsCONS [var] body_expr predefs	
	# {pds_module, pds_def} = predefs.[PD_TypeCONS]
	# case_patterns = AlgebraicPatterns {glob_module = pds_module, glob_object = pds_def} [pat]
	= build_case_expr case_patterns heaps 

build_case_field var body_expr predefs heaps
	# pat = buildPredefConsPattern PD_ConsFIELD [var] body_expr predefs	
	# {pds_module, pds_def} = predefs.[PD_TypeFIELD]
	# case_patterns = AlgebraicPatterns {glob_module = pds_module, glob_object = pds_def} [pat]
	= build_case_expr case_patterns heaps 

// case with a variable as the selector expression
build_case_expr case_patterns heaps
	# (var_expr, var, heaps) = buildVarExpr "c" heaps
	# (case_expr, heaps) = buildCaseExpr var_expr case_patterns heaps
	= (case_expr, var, heaps)
1208
1209
1210

// build kind indexed classes 

John van Groningen's avatar
John van Groningen committed
1211
buildClasses :: !*GenericState -> *GenericState
1212
1213
buildClasses gs=:{gs_main_module}
	#! ({com_class_defs,com_member_defs},gs) = gs!gs_modules.[gs_main_module]
1214
1215
1216
	#! num_classes = size com_class_defs
	#! num_members = size com_member_defs

1217
1218
	#! ((classes, members, new_num_classes, new_num_members), gs)
		= build_modules 0 ([], [], num_classes, num_members) gs
1219
1220

	// obtain common definitions again because com_gencase_defs are updated 
1221
	#! (common_defs,gs) = gs!gs_modules.[gs_main_module]
1222
1223
1224
	# common_defs = {common_defs & com_class_defs = arrayPlusRevList com_class_defs classes
								 , com_member_defs = arrayPlusRevList com_member_defs members}

1225
1226
1227
1228
	#! (common_defs, gs)
		= build_class_dictionaries common_defs gs

	= {gs & gs_modules.[gs_main_module] = common_defs}
1229
where
1230
1231
	build_modules :: !Index (![ClassDef], ![MemberDef], !Int, !Int) !*GenericState
		-> ((![ClassDef], ![MemberDef], !Int, !Int), !*GenericState)
1232
	build_modules module_index st gs=:{gs_modules,gs_used_modules}
1233
		| module_index == size gs_modules
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
			= (st, gs)
		| not (inNumberSet module_index gs_used_modules)
			= build_modules (inc module_index) st gs		
			#! ({com_gencase_defs},gs_modules) = gs_modules![module_index] 
			#! (com_gencase_defs, st, gs) 
				= build_module module_index 0 {x\\x<-:com_gencase_defs} st {gs & gs_modules=gs_modules}
			#! gs = {gs & gs_modules.[module_index].com_gencase_defs = com_gencase_defs}
			= build_modules (inc module_index) st gs

	build_module module_index index com_gencase_defs st gs
1244
		| index == size com_gencase_defs
1245
			= (com_gencase_defs, st, gs)
1246
			#! (gencase, com_gencase_defs) = com_gencase_defs ! [index]
1247
			#! (gencase, st, gs) = on_gencase module_index index gencase st gs
1248
			#! com_gencase_defs = {com_gencase_defs & [index] = gencase} 	
1249
			= build_module module_index (inc index) com_gencase_defs st gs
1250

1251
	on_gencase :: !Index !Index
1252
1253
			!GenericCaseDef (![ClassDef], ![MemberDef], !Index, Index) !*GenericState
		-> (!GenericCaseDef,(![ClassDef], ![MemberDef], !Index, Index), !*GenericState)
1254
	on_gencase module_index index
1255
				gencase=:{gc_ident,gc_generic, gc_type_cons} st gs=:{gs_modules, gs_td_infos}
1256
		#! (gen_def, gs_modules) = gs_modules![gc_generic.gi_module].com_generic_defs.[gc_generic.gi_index]
1257
		#! (kind, gs_td_infos) = get_kind_of_type_cons gc_type_cons gs_td_infos
1258

1259
1260
		// To generate all partially applied shorthand instances we need
		// classes for all partial applications of the gc_kind and for
Artem Alimarine's avatar
Artem Alimarine committed
1261
1262
		// all the argument kinds.
		// Additionally, we always need classes for base cases *, *->* and *->*->* 
1263
1264

		#! gs = {gs & gs_modules = gs_modules, gs_td_infos = gs_td_infos}
Artem Alimarine's avatar
Artem Alimarine committed
1265
1266
1267
1268
1269
1270
		#! subkinds = determine_subkinds kind 		
		#! kinds = 
			[ KindConst
			, KindArrow [KindConst]
			, KindArrow [KindConst, KindConst] 
			: subkinds]
1271
		#! (st, gs) = foldSt (build_class_if_needed gen_def) kinds (st, gs)
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
		#! gencase = {gencase & gc_kind = kind}

		#! type_index = index_OBJECT_CONS_FIELD_type gencase.gc_type gs.gs_predefs
		| type_index>=0		
			# ({gc_body = GCB_FunIndex fun_index}) = gencase
			  gen_info_ptr = gen_def.gen_info_ptr

			  fun_ident = genericIdentToFunIdent gc_ident.id_name gc_type_cons
			  ocf_index = {ocf_module=module_index,ocf_index=fun_index,ocf_ident=fun_ident}

			  (gen_info,generic_heap) = readPtr gen_info_ptr gs.gs_genh
			  gen_OBJECT_CONS_FIELD_indices = {gi\\gi<-:gen_info.gen_OBJECT_CONS_FIELD_indices}
			  gen_OBJECT_CONS_FIELD_indices = {gen_OBJECT_CONS_FIELD_indices & [type_index]=ocf_index}
			  gen_info = {gen_info & gen_OBJECT_CONS_FIELD_indices=gen_OBJECT_CONS_FIELD_indices}
			  generic_heap = writePtr gen_info_ptr gen_info generic_heap
			  gs = {gs & gs_genh=generic_heap}
			= (gencase, st, gs)

			= (gencase, st, gs)
1291

1292
1293
1294
1295
	build_class_if_needed :: !GenericDef !TypeKind ((![ClassDef], ![MemberDef], !Index, Index), *GenericState)
		-> ((![ClassDef], ![MemberDef], !Index, Index), *GenericState)		
	build_class_if_needed gen_def kind ((classes, members, class_index, member_index), gs=:{gs_main_module, gs_genh})
		#! (opt_class_info, gs_genh) = lookup_generic_class_info gen_def kind gs_genh
1296
		#! gs = {gs & gs_genh = gs_genh}
1297
		= case opt_class_info of