type.icl 133 KB
Newer Older
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1
2
3
implementation module type

import StdEnv
4
import syntax, typesupport, check, analtypes, overloading, unitype, refmark, predef, utilities, compare_constructor // , RWSDebug
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
5
6

::	TypeInput =
7
8
9
	{	ti_common_defs	:: !{# CommonDefs }
	,	ti_functions	:: !{# {# FunType }}
	,	ti_main_dcl_module_n :: !Int
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
10
11
12
13
14
15
16
17
18
19
20
	}

::	TypeState =
	{	ts_fun_env		:: !.{! FunctionType}
	,	ts_var_store	:: !Int
	,	ts_attr_store	:: !Int
	,	ts_var_heap		:: !.VarHeap 
	,	ts_type_heaps	:: !.TypeHeaps
	,	ts_expr_heap	:: !.ExpressionHeap 
	,	ts_td_infos		:: !.TypeDefInfos
	,	ts_error		:: !.ErrorAdmin
21
	,	ts_out			:: !.File // MW4++
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
22
23
24
25
26
27
28
29
30
31
	}

::	TypeCoercion =
	{	tc_demanded		:: !AType
	,	tc_offered		:: !AType
	,	tc_position		:: !CoercionPosition
	,	tc_coercible	:: !Bool
	}

::	SharedAttribute = 
Sjaak Smetsers's avatar
Sjaak Smetsers committed
32
	{	sa_attr_nr	:: !Int
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
33
34
35
36
37
38
	,	sa_position	:: !Expression
	}

::	Requirements =
	{	req_overloaded_calls	:: ![ExprInfoPtr]
	,	req_type_coercions		:: ![TypeCoercion]
39
	,	req_type_coercion_groups:: ![TypeCoercionGroup] // MW4++
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
40
41
42
43
	,	req_attr_coercions		:: ![AttrCoercion]
	,	req_cons_variables		:: ![[TempVarId]]
	,	req_case_and_let_exprs	:: ![ExprInfoPtr]
	}
44
45
46
47
48
49
50
51

// MW4 added..
::	TypeCoercionGroup =
	{	tcg_type_coercions	:: ![TypeCoercion]
	,	tcg_position		:: !Position
	}
// ..MW4

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
52
53
54
55
instance toString BoundVar
where
	toString varid = varid.var_name.id_name

56
57
class arraySubst type :: !type !u:{!Type} -> (!type, !u:{! Type})
/*
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
58
59
60
61
62
63
64
65
66
67
68
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
instance arraySubst AType
where
	arraySubst atype=:{at_type} subst
		# (at_type, subst) = arraySubst at_type subst
		= ({ atype & at_type = at_type }, subst)
		
instance arraySubst Type
where
	arraySubst tv=:(TempV tv_number) subst
		#! type = subst.[tv_number]
		= case type of
			TE	-> (tv, subst)
			_	-> arraySubst type subst
	arraySubst (arg_type --> res_type) subst
		# (arg_type, subst) = arraySubst arg_type subst
		  (res_type, subst) = arraySubst res_type subst
		= (arg_type --> res_type, subst)
	arraySubst (TA cons_id cons_args) subst
		# (cons_args, subst) = arraySubst cons_args subst
		= (TA cons_id cons_args, subst) 
	arraySubst (TempCV tv_number :@: types) subst
		#! type = subst.[tv_number]
		= case type of
			TE
				# (types, subst) = arraySubst types subst
				-> (TempCV tv_number :@: types, subst)
			_
				# (type, subst) = arraySubst type subst
				  (types, subst) = arraySubst types subst
				-> (simplify_type_appl type types, subst)
	where
		simplify_type_appl :: !Type ![AType] -> Type
		simplify_type_appl (TA type_cons=:{type_arity} cons_args) type_args
			= TA { type_cons & type_arity = type_arity + length type_args } (cons_args ++ type_args)
		simplify_type_appl (cons_var :@: types) type_args
			= cons_var :@: (types ++ type_args)
		simplify_type_appl (TempV tv_number) type_args
			= TempCV tv_number :@: type_args
		simplify_type_appl (TempQV tv_number) type_args
			= TempQCV tv_number :@: type_args
	arraySubst type subst
		= (type, subst)

instance arraySubst [a] | arraySubst a
where
	arraySubst l subst
		= mapSt arraySubst l subst

instance arraySubst TempSymbolType
where
	arraySubst tst=:{tst_args,tst_result,tst_context} subst
		# (tst_args, subst) = arraySubst tst_args subst
		  (tst_result, subst) = arraySubst tst_result subst
		  (tst_context, subst) = arraySubst tst_context subst
		= ({tst & tst_args = tst_args,tst_result = tst_result,tst_context = tst_context}, subst)

instance arraySubst TypeContext
where
	arraySubst tc=:{tc_types} subst
		# (tc_types, subst) = arraySubst tc_types subst
		= ({ tc & tc_types = tc_types}, subst)

120
121
122
123
124
125
126
	/*
	instance arraySubst OverloadedCall
	where
		arraySubst oc=:{oc_context} subst
			# (oc_context, subst) = arraySubst oc_context subst
			= ({ oc & oc_context = oc_context },  subst)
	*/
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
127
128
129
130
131
132
133
134
135

instance arraySubst CaseType
where
	arraySubst ct=:{ct_pattern_type,ct_result_type,ct_cons_types} subst
		# (ct_pattern_type, subst) = arraySubst ct_pattern_type subst
		  (ct_result_type, subst) = arraySubst ct_result_type subst
		  (ct_cons_types, subst) = arraySubst ct_cons_types subst
		= ({ ct & ct_pattern_type = ct_pattern_type, ct_result_type = ct_result_type, ct_cons_types = ct_cons_types }, subst)

136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
*/

instance arraySubst AType
where
	arraySubst atype=:{at_type} subst
		# (changed,at_type, subst) = arraySubst2 at_type subst
		| changed
			= ({ atype & at_type = at_type }, subst)
			= (atype, subst)
		
instance arraySubst Type
where
	arraySubst tv=:(TempV tv_number) subst
		#! type = subst.[tv_number]
		= case type of
			TE	-> (tv, subst)
			_	-> arraySubst type subst
	arraySubst type=:(arg_type0 --> res_type0) subst
		# (changed,arg_type, subst) = arraySubst2 arg_type0 subst
		| changed
			#  (changed,res_type, subst) = arraySubst2 res_type0 subst
			| changed
				= (arg_type --> res_type, subst)
				= (arg_type --> res_type0, subst)
			#  (changed,res_type, subst) = arraySubst2 res_type0 subst
			| changed
				= (arg_type0 --> res_type, subst)
				= (type, subst)
	arraySubst type=:(TA cons_id cons_args) subst
		# (changed,cons_args, subst) = arraySubst2 cons_args subst
		| changed
			= (TA cons_id cons_args, subst) 
			= (type, subst) 
	arraySubst tcv=:(TempCV tv_number :@: types) subst
		#! type = subst.[tv_number]
		= case type of
			TE
				# (changed,types, subst) = arraySubst2 types subst
				| changed
					-> (TempCV tv_number :@: types, subst)
					-> (tcv, subst)
			_
				# (type, subst) = arraySubst type subst
				  (types, subst) = arraySubst types subst
				-> (simplify_type_appl type types, subst)
	where
		simplify_type_appl :: !Type ![AType] -> Type
		simplify_type_appl (TA type_cons=:{type_arity} cons_args) type_args
			= TA { type_cons & type_arity = type_arity + length type_args } (cons_args ++ type_args)
		simplify_type_appl (cons_var :@: types) type_args
			= cons_var :@: (types ++ type_args)
		simplify_type_appl (TempV tv_number) type_args
			= TempCV tv_number :@: type_args
		simplify_type_appl (TempQV tv_number) type_args
			= TempQCV tv_number :@: type_args
	arraySubst type subst
		= (type, subst)

instance arraySubst [a] | arraySubst2 a
where
	arraySubst [] subst
		= ([],subst)
	arraySubst t=:[type0:types0] subst
		# (changed,types,subst) = arraySubst2 types0 subst
		| changed
			# (changed,type,subst) = arraySubst2 type0 subst
			| changed
				= ([type:types],subst)
				= ([type0:types],subst)
			# (changed,type,subst) = arraySubst2 type0 subst
			| changed
				= ([type:types0],subst)
				= (t,subst)
	
instance arraySubst TempSymbolType
where
	arraySubst tst=:{tst_args,tst_result,tst_context} subst
		# (changed,tst_args, subst) = arraySubst2 tst_args subst
		| changed
			# (changed,tst_result, subst) = arraySubst2 tst_result subst
			# (changed,tst_context, subst) = arraySubst2 tst_context subst
			= ({tst & tst_args = tst_args,tst_result = tst_result,tst_context = tst_context}, subst)
			# (changed,tst_result, subst) = arraySubst2 tst_result subst
			| changed
				# (changed,tst_context, subst) = arraySubst2 tst_context subst
				= ({tst & tst_result = tst_result,tst_context = tst_context}, subst)
				# (changed,tst_context, subst) = arraySubst2 tst_context subst
				| changed
					= ({tst & tst_context = tst_context}, subst)
					= (tst, subst)

instance arraySubst TypeContext
where
	arraySubst tc=:{tc_types} subst
		# (changed,tc_types, subst) = arraySubst2 tc_types subst
		| changed
			= ({ tc & tc_types = tc_types}, subst)
			= ( tc, subst)

instance arraySubst CaseType
where
	arraySubst ct=:{ct_pattern_type,ct_result_type,ct_cons_types} subst
		# (changed,ct_pattern_type, subst) = arraySubst2 ct_pattern_type subst
		| changed
			# (changed,ct_result_type, subst) = arraySubst2 ct_result_type subst
			#  (changed,ct_cons_types, subst) = arraySubst2 ct_cons_types subst
			= ({ ct & ct_pattern_type = ct_pattern_type, ct_result_type = ct_result_type, ct_cons_types = ct_cons_types }, subst)
			# (changed,ct_result_type, subst) = arraySubst2 ct_result_type subst
			| changed
				# (changed,ct_cons_types, subst) = arraySubst2 ct_cons_types subst
				= ({ ct & ct_result_type = ct_result_type, ct_cons_types = ct_cons_types }, subst)
				# (changed,ct_cons_types, subst) = arraySubst2 ct_cons_types subst
				| changed
					= ({ ct & ct_cons_types = ct_cons_types }, subst)
					= (ct, subst)

class arraySubst2 type :: !type !u:{!Type} -> (!Bool,!type, !u:{! Type})

instance arraySubst2 AType
where
	arraySubst2 atype=:{at_type} subst
		# (changed,at_type, subst) = arraySubst2 at_type subst
		| changed
			= (True,{ atype & at_type = at_type }, subst)
			= (False,atype, subst)
		
instance arraySubst2 Type
where
	arraySubst2 tv=:(TempV tv_number) subst
		#! type = subst.[tv_number]
		= case type of
			TE	-> (False,tv, subst)
			_
				# (t,s) = arraySubst type subst
				-> (True,t,s)
	arraySubst2 type=:(arg_type0 --> res_type0) subst
		# (changed,arg_type, subst) = arraySubst2 arg_type0 subst
		| changed
			#  (changed,res_type, subst) = arraySubst2 res_type0 subst
			| changed
				= (True,arg_type --> res_type, subst)
				= (True,arg_type --> res_type0, subst)
			#  (changed,res_type, subst) = arraySubst2 res_type0 subst
			| changed
				= (True,arg_type0 --> res_type, subst)
				= (False,type, subst)
	arraySubst2 type=:(TA cons_id cons_args) subst
		# (changed,cons_args, subst) = arraySubst2 cons_args subst
		| changed
			= (True,TA cons_id cons_args, subst) 
			= (False,type, subst) 
	arraySubst2 tcv=:(TempCV tv_number :@: types) subst
		#! type = subst.[tv_number]
		= case type of
			TE
				# (changed,types, subst) = arraySubst2 types subst
				| changed
					-> (True,TempCV tv_number :@: types, subst)
					-> (False,tcv, subst)
			_
				# (type, subst) = arraySubst type subst
				  (types, subst) = arraySubst types subst
				-> (True,simplify_type_appl type types, subst)
	where
		simplify_type_appl :: !Type ![AType] -> Type
		simplify_type_appl (TA type_cons=:{type_arity} cons_args) type_args
			= TA { type_cons & type_arity = type_arity + length type_args } (cons_args ++ type_args)
		simplify_type_appl (cons_var :@: types) type_args
			= cons_var :@: (types ++ type_args)
		simplify_type_appl (TempV tv_number) type_args
			= TempCV tv_number :@: type_args
		simplify_type_appl (TempQV tv_number) type_args
			= TempQCV tv_number :@: type_args
	arraySubst2 type subst
		= (False,type, subst)

instance arraySubst2 [a] | arraySubst2 a
where
	arraySubst2 [] subst
		= (False,[],subst)
	arraySubst2 t=:[type0:types0] subst
		# (changed,types,subst) = arraySubst2 types0 subst
		| changed
			# (changed,type,subst) = arraySubst2 type0 subst
			| changed
				= (True,[type:types],subst)
				= (True,[type0:types],subst)
			# (changed,type,subst) = arraySubst2 type0 subst
			| changed
				= (True,[type:types0],subst)
				= (False,t,subst)
	
instance arraySubst2 TempSymbolType
where
	arraySubst2 tst=:{tst_args,tst_result,tst_context} subst
		# (changed,tst_args, subst) = arraySubst2 tst_args subst
		| changed
			# (changed,tst_result, subst) = arraySubst2 tst_result subst
			# (changed,tst_context, subst) = arraySubst2 tst_context subst
			= (True,{tst & tst_args = tst_args,tst_result = tst_result,tst_context = tst_context}, subst)
			# (changed,tst_result, subst) = arraySubst2 tst_result subst
			| changed
				# (changed,tst_context, subst) = arraySubst2 tst_context subst
				= (True,{tst & tst_result = tst_result,tst_context = tst_context}, subst)
				# (changed,tst_context, subst) = arraySubst2 tst_context subst
				| changed
					= (True,{tst & tst_context = tst_context}, subst)
					= (False,tst, subst)

instance arraySubst2 TypeContext
where
	arraySubst2 tc=:{tc_types} subst
		# (changed,tc_types, subst) = arraySubst2 tc_types subst
		| changed
			= (True,{ tc & tc_types = tc_types}, subst)
			= (False, tc, subst)

instance arraySubst2 CaseType
where
	arraySubst2 ct=:{ct_pattern_type,ct_result_type,ct_cons_types} subst
		# (changed,ct_pattern_type, subst) = arraySubst2 ct_pattern_type subst
		| changed
			# (changed,ct_result_type, subst) = arraySubst2 ct_result_type subst
			#  (changed,ct_cons_types, subst) = arraySubst2 ct_cons_types subst
			= (True,{ ct & ct_pattern_type = ct_pattern_type, ct_result_type = ct_result_type, ct_cons_types = ct_cons_types }, subst)
			# (changed,ct_result_type, subst) = arraySubst2 ct_result_type subst
			| changed
				# (changed,ct_cons_types, subst) = arraySubst2 ct_cons_types subst
				= (True,{ ct & ct_result_type = ct_result_type, ct_cons_types = ct_cons_types }, subst)
				# (changed,ct_cons_types, subst) = arraySubst2 ct_cons_types subst
				| changed
					= (True,{ ct & ct_cons_types = ct_cons_types }, subst)
					= (False,ct, subst)

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
class contains_var a :: !Int !a -> Bool

instance contains_var [a] | contains_var a
where
	contains_var var_id [elem:list]
		= contains_var var_id elem || contains_var var_id list	
	contains_var var_id []
		= False

instance contains_var AType
where
	contains_var var_id {at_type} = contains_var var_id at_type

instance contains_var Type
where
	contains_var var_id (TempV tv_number) 
		= var_id == tv_number
	contains_var var_id (arg_type --> res_type) 
		= contains_var var_id arg_type || contains_var var_id res_type
	contains_var var_id (TA cons_id cons_args)
		= contains_var var_id cons_args
	contains_var var_id (type :@: types)
		= contains_var var_id type || contains_var var_id types
	contains_var _ _ 
		= False

instance contains_var ConsVariable
where
	contains_var var_id (TempCV tv_number) 
		= var_id == tv_number
	contains_var var_id _
		= False

403
404
405
406
type_error =: "Type error" // MW4++
type_error_format =: { form_properties = cNoProperties, form_attr_position = No } // MW4++

/* MW4 was:
407
408
cannotUnify t1 t2 position err 
	# err = errorHeading "Type error" err
409
	  format = { form_properties = cNoProperties, form_attr_position = No }
Martin Wierich's avatar
Martin Wierich committed
410
411
	= { err & ea_file = err.ea_file <<< optionalFrontPosition position <<< " cannot unify " <:: (format, t1) 
							<<< " with " <:: (format, t2) <<< position <<< '\n' }
412
*/
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
413

414
cannotUnify t1 t2 position=:(CP_Expression expr) err=:{ea_loc=[ip:_]} 
415
416
417
418
	= case tryToOptimizePosition expr of
// MW0		Yes ident_pos
		Yes (id_name, line)
			# err = pushErrorAdmin { ip & ip_ident.id_name = id_name, ip_line = line } err
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
			  err = errorHeading type_error err
			  err = popErrorAdmin err
			-> { err & ea_file = err.ea_file <<< " cannot unify " <:: (type_error_format, t1, Yes initialTypeVarBeautifulizer) 
											<<< " with " <:: (type_error_format, t2, Yes initialTypeVarBeautifulizer) <<< '\n' }
		_
			-> cannot_unify t1 t2 position err 
cannotUnify t1 t2 position err 
	= cannot_unify t1 t2 position err 

cannot_unify t1 t2 position err
	# (err=:{ea_file}) = errorHeading type_error err
	  ea_file = case position of
				CP_FunArg _ _
					-> ea_file <<< "\"" <<< position <<< "\""
				_
					-> ea_file
	  ea_file = ea_file <<< " cannot unify " <:: (type_error_format, t1, Yes initialTypeVarBeautifulizer) 
						<<< " with " <:: (type_error_format, t2, Yes initialTypeVarBeautifulizer)
	  ea_file = case position of
	  			CP_FunArg _ _
	  				-> ea_file
  				_
	  				-> ea_file <<< " near " <<< position
	= { err & ea_file = ea_file <<< '\n' }

// MW4..
445
446
447
448
449
450
451
tryToOptimizePosition (Case {case_ident=Yes {id_name}})
	= optBeautifulizeIdent id_name
tryToOptimizePosition (App {app_symb={symb_name}})
	= optBeautifulizeIdent symb_name.id_name
tryToOptimizePosition (fun @ _)
	= tryToOptimizePosition fun
tryToOptimizePosition _
Martin Wierich's avatar
Martin Wierich committed
452
	= No
453

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
454
455
456
457
458
459
460
461
462
463
464
465
class unify a :: !a !a !TypeInput !*{! Type} !*TypeHeaps -> (!Bool, !*{! Type}, !*TypeHeaps)

instance unify (a, b) | unify, arraySubst a & unify, arraySubst b
where
	unify (t1x, t1y) (t2x, t2y) modules subst heaps
		# (succ, subst, heaps) =  unify t1y t2y modules subst heaps
		| succ
	      # (t1x, subst) = arraySubst t1x subst
	        (t2x, subst) = arraySubst t2x subst
	      = unify t1x t2x modules subst heaps
	      = (False, subst, heaps)

466
467
//instance unify [a] | unify, arraySubst a
instance unify [a] | unify, arraySubst, arraySubst2 a
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
where
	unify [t1 : ts1] [t2 : ts2] modules subst heaps
		= unify (t1,ts1) (t2,ts2) modules subst heaps
	unify [] [] modules subst heaps
		= (True, subst, heaps)
	unify _ _ modules subst heaps
		= (False, subst, heaps)

instance unify AType
where
	unify t1 t2 modules subst heaps = unifyTypes t1.at_type t1.at_attribute t2.at_type t2.at_attribute modules subst heaps


unifyTypes :: !Type !TypeAttribute !Type !TypeAttribute !TypeInput !*{! Type} !*TypeHeaps -> (!Bool, !*{! Type}, !*TypeHeaps)
unifyTypes (TempV tv_number1) attr1 tv=:(TempV tv_number2) attr2 modules subst heaps
	= unifyTempVarIds tv_number1 tv_number2 subst heaps
unifyTypes tv=:(TempV tv_number) attr1 type attr2 modules subst heaps
	| contains_var tv_number type
		= (False, subst, heaps)
		= (True, { subst & [tv_number] = type}, heaps)
unifyTypes type attr1 tv=:(TempV _) attr2 modules subst heaps
	= unifyTypes tv attr2 type attr1 modules subst heaps
unifyTypes t1=:(TB tb1) attr1 t2=:(TB tb2) attr2 modules subst heaps
	| tb1 == tb2
		= (True, subst, heaps)
		= (False, subst, heaps)
unifyTypes (arg_type1 --> res_type1) attr1 (arg_type2 --> res_type2) attr2 modules subst heaps
	= unify (arg_type1,res_type1) (arg_type2,res_type2) modules subst heaps
unifyTypes t1=:(TA cons_id1 cons_args1) attr1 t2=:(TA cons_id2 cons_args2) attr2 modules subst heaps
	| cons_id1 == cons_id2
		= unify cons_args1 cons_args2 modules subst heaps
499
500
		# (succ1, t1, heaps) = tryToExpand t1 attr1 modules heaps
		  (succ2, t2, heaps) = tryToExpand t2 attr2 modules heaps
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
501
502
503
504
		| succ1 || succ2
			= unifyTypes t1 attr1 t2 attr2 modules subst heaps
			= (False, subst, heaps)
unifyTypes (cons_var :@: types) attr1 type2 attr2 modules subst heaps
505
	# (_, type2, heaps) = tryToExpand type2 attr2 modules heaps
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
506
507
	= unifyTypeApplications cons_var types type2 modules subst heaps
unifyTypes type1 attr1 (cons_var :@: types) attr2 modules subst heaps
508
	# (_, type1, heaps) = tryToExpand type1 attr1 modules heaps
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
509
510
511
512
513
514
515
516
	= unifyTypeApplications cons_var types type1 modules subst heaps
unifyTypes t1=:(TempQV qv_number1) attr1 t2=:(TempQV qv_number2) attr2 modules subst heaps
	= (qv_number1 == qv_number2, subst, heaps)
unifyTypes (TempQV qv_number) attr1 type attr2 modules subst heaps
	= (False, subst, heaps)
unifyTypes type attr1 (TempQV qv_number1) attr2 modules subst heaps
	= (False, subst, heaps)
unifyTypes type1 attr1 type2 attr2 modules subst heaps
517
518
	# (succ1, type1, heaps) = tryToExpand type1 attr1 modules heaps
	  (succ2, type2, heaps) = tryToExpand type2 attr2 modules heaps
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
519
520
521
522
	| succ1 || succ2
		= unifyTypes type1 attr1 type2 attr2 modules subst heaps
		= (False, subst, heaps)

523
tryToExpand type=:(TA {type_index={glob_object,glob_module}} type_args) type_attr {ti_common_defs} type_heaps
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
524
525
526
527
528
529
530
	#! type_def = ti_common_defs.[glob_module].com_type_defs.[glob_object]
	= case type_def.td_rhs of
		SynType {at_type}
			# (res_type, type_heaps) = expandTypeApplication type_def.td_args type_def.td_attribute at_type type_args type_attr type_heaps
			-> (True, res_type, type_heaps)
		_
			-> (False, type, type_heaps)
531
tryToExpand type type_attr modules type_heaps
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
	= (False, type, type_heaps)

unifyConsVariables (TempCV tv_number1) (TempCV tv_number2) subst heaps
	= unifyTempVarIds tv_number1 tv_number2 subst heaps
unifyConsVariables (TempCV tv_number1) (TempQCV tv_number2) subst heaps
	= (True, { subst & [tv_number1] = TempQV tv_number2}, heaps)
unifyConsVariables (TempQCV tv_number1) (TempCV tv_number2) subst heaps
	= (True, { subst & [tv_number2] = TempQV tv_number1}, heaps)
unifyConsVariables (TempQCV tv_number1) (TempQCV tv_number2) subst heaps
	= (tv_number1 == tv_number2, subst, heaps)

unifyTempVarIds tv_number1 tv_number2 subst heaps
	| tv_number1 == tv_number2
		= (True, subst, heaps)
		= (True, { subst & [tv_number1] = TempV tv_number2}, heaps)

constructorVariableToTypeVariable (TempCV temp_var_id)
	= TempV temp_var_id
constructorVariableToTypeVariable (TempQCV temp_var_id)
	= TempQV temp_var_id

unifyTypeApplications cons_var type_args type=:(TA type_cons cons_args) modules subst heaps
	# diff = type_cons.type_arity - length type_args
	| diff >= 0 
		# (succ, subst, heaps) = unify type_args (drop diff cons_args) modules subst heaps
		| succ
			# (rest_args, subst) = arraySubst (take diff cons_args) subst
			= unifyTypes (constructorVariableToTypeVariable cons_var) TA_Multi (TA { type_cons & type_arity = diff } rest_args) TA_Multi modules subst heaps
		    = (False, subst, heaps)
		= (False, subst, heaps)
unifyTypeApplications cons_var1 type_args type=:(cons_var2 :@: types) modules subst heaps
	# arity1 = length type_args
	  arity2 = length types
	  diff = arity1 - arity2
	| diff == 0
		# (succ, subst, heaps) = unifyConsVariables cons_var1 cons_var2 subst heaps
		| succ
		    # (type_args, subst) = arraySubst type_args subst
		      (types, subst) = arraySubst types subst
		    = unify type_args types modules subst heaps
			= (False, subst, heaps)
	| diff < 0
		# diff = 0 - diff
		  (succ, subst, heaps) = unifyTypes (constructorVariableToTypeVariable cons_var1) TA_Multi (cons_var2 :@: take diff types) TA_Multi modules subst heaps
		| succ
		    # (type_args, subst) = arraySubst type_args subst
		      (types, subst) = arraySubst (drop diff types) subst
		    = unify type_args types modules subst heaps
			= (False, subst, heaps)
581
//	| otherwise
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
582
583
584
585
586
587
588
589
590
591
592
		# (succ, subst, heaps) = unifyTypes (cons_var1 :@: take diff type_args) TA_Multi (constructorVariableToTypeVariable cons_var2) TA_Multi modules subst heaps
		| succ
		    # (type_args, subst) = arraySubst (drop diff type_args) subst
		      (types, subst) = arraySubst types subst
		    = unify type_args types modules subst heaps
			= (False, subst, heaps)
unifyTypeApplications cons_var type_args type modules subst heaps
	= (False, subst, heaps)


::	CopyState =
Sjaak Smetsers's avatar
Sjaak Smetsers committed
593
	{	copy_heaps		:: !.TypeHeaps
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
	}
	
instance fromInt TypeAttribute
where
	fromInt AttrUni		= TA_Unique
	fromInt AttrMulti	= TA_Multi
	fromInt av_number	= TA_TempVar av_number

class freshCopy a :: !a !*CopyState -> (!a, !*CopyState)

instance freshCopy [a] | freshCopy a
where
	freshCopy l ls = mapSt freshCopy l ls

/*
cDoExtendAttrEnv	:== True
cDontExtendAttrEnv	:== False

freshCopies :: !Bool ![a] !{# CommonDefs } !*CopyState -> (![a], !*CopyState) | freshCopy a
freshCopies extend_env [] modules cs
	= ([], [], cs)
freshCopies extend_env [t:ts] modules cs
	# (t, prop, cs) = freshCopy extend_env t modules cs
	  (ts, props, cs) = freshCopies extend_env ts modules cs
	= ([t:ts], [prop:props], cs)
*/

freshCopyOfAttributeVar {av_name,av_info_ptr} attr_var_heap
	# (av_info, attr_var_heap) = readPtr av_info_ptr attr_var_heap
	= case av_info of
		AVI_Attr attr
			-> (attr, attr_var_heap)
		_
			-> abort ("freshCopyOfAttributeVar (type,icl)" ---> av_name)

Sjaak Smetsers's avatar
Sjaak Smetsers committed
629

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
630
631
freshCopyOfTypeAttribute (TA_Var avar) attr_var_heap
	= freshCopyOfAttributeVar avar attr_var_heap
Sjaak Smetsers's avatar
Sjaak Smetsers committed
632
633
634
635

/* A temporary hack to handle the new Object IO lib */
/* Should be removed !!!!!!!!!! */

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
636
freshCopyOfTypeAttribute (TA_RootVar avar) attr_var_heap
Sjaak Smetsers's avatar
Sjaak Smetsers committed
637
	= PA_BUG (TA_TempExVar, attr_var_heap) (freshCopyOfAttributeVar avar attr_var_heap)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
638
639
640
641
642
643
644
freshCopyOfTypeAttribute TA_None attr_var_heap
	= (TA_Multi, attr_var_heap)
freshCopyOfTypeAttribute TA_Unique attr_var_heap
	= (TA_Unique, attr_var_heap)
freshCopyOfTypeAttribute attr attr_var_heap
	= (attr, attr_var_heap)

Sjaak Smetsers's avatar
Sjaak Smetsers committed
645

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
646
647
648
649
cIsExistential 		:== True
cIsNotExistential	:== False

freshCopyOfTypeVariable {tv_name,tv_info_ptr} cs=:{copy_heaps}
650
651
652
	# (TVI_Type fresh_var, th_vars)	= readPtr tv_info_ptr copy_heaps.th_vars
//	= (fresh_var, { cs & copy_heaps.th_vars = th_vars } ) // 2.0
	= (fresh_var, { cs & copy_heaps = { copy_heaps & th_vars = th_vars }})
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
653
654

freshConsVariable {tv_info_ptr} type_var_heap
655
	# (tv_info, type_var_heap) = readPtr tv_info_ptr type_var_heap
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
	= (to_constructor_variable tv_info, type_var_heap)
	where
		to_constructor_variable (TVI_Type (TempV temp_var_id))
			= TempCV temp_var_id
		to_constructor_variable (TVI_Type (TempQV temp_var_id))
			= TempQCV temp_var_id

instance freshCopy AType
where 	
	freshCopy type=:{at_type = CV tv :@: types, at_attribute}  cs=:{copy_heaps}
		# (fresh_cons_var, th_vars)		= freshConsVariable tv copy_heaps.th_vars
		  (fresh_attribute, th_attrs)	= freshCopyOfTypeAttribute at_attribute copy_heaps.th_attrs
		  (types, cs)					= freshCopy types { cs & copy_heaps = { copy_heaps & th_attrs = th_attrs, th_vars = th_vars }}
		= ({type & at_type = fresh_cons_var :@: types, at_attribute = fresh_attribute }, cs)
	freshCopy type=:{at_type, at_attribute} cs=:{copy_heaps}
		# (fresh_attribute, th_attrs)	= freshCopyOfTypeAttribute at_attribute copy_heaps.th_attrs
		  (fresh_type, cs)				= freshCopy at_type { cs & copy_heaps = { copy_heaps & th_attrs = th_attrs }}
		= ({ type & at_type = fresh_type, at_attribute = fresh_attribute }, cs)
		
instance freshCopy Type
where
	freshCopy (TV tv) cs=:{copy_heaps}
		= freshCopyOfTypeVariable tv cs
	freshCopy (TA cons_id=:{type_index={glob_object,glob_module}} cons_args)  cs
		# (cons_args, cs) = freshCopy cons_args cs
		= (TA cons_id cons_args, cs)
	freshCopy (arg_type --> res_type) cs
		# (arg_type, cs) = freshCopy arg_type cs
		  (res_type, cs) = freshCopy res_type cs
		= (arg_type --> res_type, cs)
	freshCopy type cs
		= (type, cs)

freshExistentialVariables type_variables state
	= foldSt fresh_existential_variable type_variables state 
where
	fresh_existential_variable {atv_variable={tv_info_ptr}} (var_heap, var_store)
		= (var_heap <:= (tv_info_ptr, TVI_Type (TempQV var_store)), inc var_store)

freshAlgebraicType :: !(Global Int) ![AlgebraicPattern] !{#CommonDefs} !*TypeState -> (![[AType]],!AType,![AttrCoercion],!*TypeState)
freshAlgebraicType {glob_module, glob_object} patterns common_defs ts=:{ts_var_store,ts_attr_store,ts_type_heaps,ts_td_infos}
	# {td_rhs,td_args,td_attrs,td_name,td_attribute} = common_defs.[glob_module].com_type_defs.[glob_object]
	# (th_vars, ts_var_store)		= fresh_type_variables td_args (ts_type_heaps.th_vars, ts_var_store)
	  (th_attrs, ts_attr_store)		= fresh_attributes td_attrs (ts_type_heaps.th_attrs, ts_attr_store)
Sjaak Smetsers's avatar
Sjaak Smetsers committed
700
701
702
703
	  copy_heaps = { ts_type_heaps & th_vars = th_vars, th_attrs = th_attrs }
	  (cons_types, alg_type, ts_var_store, attr_env, copy_heaps)
	  		= fresh_symbol_types patterns common_defs.[glob_module].com_cons_defs ts_var_store copy_heaps
	= (cons_types, alg_type, attr_env, { ts & ts_var_store = ts_var_store, ts_attr_store = ts_attr_store, ts_type_heaps = copy_heaps })
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
704
705
//		---> ("freshAlgebraicType", alg_type, cons_types)
where
Sjaak Smetsers's avatar
Sjaak Smetsers committed
706
	fresh_symbol_types [{ap_symbol={glob_object}}] cons_defs var_store copy_heaps
707
		# {cons_type = {st_args,st_attr_env,st_result}, cons_index, cons_exi_vars} = cons_defs.[glob_object.ds_index]
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
708
709
		  (th_vars, var_store)		= freshExistentialVariables  cons_exi_vars (copy_heaps.th_vars, var_store)
	  	  (attr_env, th_attrs) 		= fresh_environment st_attr_env ([], copy_heaps.th_attrs)
Sjaak Smetsers's avatar
Sjaak Smetsers committed
710
	  	  (result_type, cs)			= freshCopy st_result { copy_heaps = { copy_heaps & th_attrs = th_attrs, th_vars = th_vars } }
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
711
	  	  (fresh_args, cs)			= freshCopy st_args cs
Sjaak Smetsers's avatar
Sjaak Smetsers committed
712
713
714
715
		= ([fresh_args], result_type, var_store, attr_env, cs.copy_heaps)
	fresh_symbol_types [{ap_symbol={glob_object}} : patterns] cons_defs var_store copy_heaps
		# (cons_types, result_type, var_store, attr_env, copy_heaps)
				= fresh_symbol_types patterns cons_defs var_store copy_heaps
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
716
717
718
		  {cons_type = {st_args,st_attr_env}, cons_index, cons_exi_vars} = cons_defs.[glob_object.ds_index]
		  (th_vars, var_store)		= freshExistentialVariables cons_exi_vars (copy_heaps.th_vars, var_store)
		  (attr_env, th_attrs) 		= fresh_environment st_attr_env (attr_env, copy_heaps.th_attrs)
Sjaak Smetsers's avatar
Sjaak Smetsers committed
719
720
	  	  (fresh_args, cs) 			= freshCopy st_args  { copy_heaps = { copy_heaps & th_attrs = th_attrs, th_vars = th_vars }}
		= ([fresh_args : cons_types], result_type, var_store, attr_env, cs.copy_heaps)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750

	
	fresh_type_variables type_variables state
		= foldSt (\{atv_variable={tv_info_ptr}} (var_heap, var_store) -> (var_heap <:= (tv_info_ptr, TVI_Type (TempV var_store)), inc var_store))
						type_variables state 
	fresh_attributes attributes state
		= foldSt (\{av_info_ptr} (attr_heap, attr_store) -> (attr_heap <:= (av_info_ptr, AVI_Attr (TA_TempVar attr_store)), inc attr_store))
						attributes state
	fresh_environment inequalities (attr_env, attr_heap)
		= foldSt fresh_inequality inequalities (attr_env, attr_heap)

	fresh_inequality {ai_demanded,ai_offered} (attr_env, attr_heap)
		# (AVI_Attr dem_temp_attr, attr_heap) = readPtr ai_demanded.av_info_ptr attr_heap
		  (AVI_Attr off_temp_attr, attr_heap) = readPtr ai_offered.av_info_ptr attr_heap
		= case dem_temp_attr of
			TA_TempVar dem_attr_var
				-> case off_temp_attr of
					TA_TempVar off_attr_var
						| is_new_ineqality  dem_attr_var off_attr_var attr_env
							-> ([{ac_demanded = dem_attr_var, ac_offered = off_attr_var} : attr_env ], attr_heap)
							-> (attr_env, attr_heap)
					_
						-> (attr_env, attr_heap)
			_
				-> (attr_env, attr_heap)
		
	is_new_ineqality dem_attr_var off_attr_var [{ac_demanded, ac_offered} : attr_env]
		= (dem_attr_var <> ac_demanded || off_attr_var <> ac_offered) && is_new_ineqality dem_attr_var off_attr_var  attr_env
	is_new_ineqality dem_attr_var off_attr_var []
		= True
Sjaak Smetsers's avatar
Sjaak Smetsers committed
751
752
753
754
755
756

cWithFreshContextVars 		:== True		
cWithoutFreshContextVars 	:== False		

freshSymbolType fresh_context_vars st=:{st_vars,st_args,st_result,st_context,st_attr_vars,st_attr_env,st_arity} common_defs
				ts=:{ts_var_store,ts_attr_store,ts_type_heaps,ts_td_infos,ts_var_heap}
Sjaak Smetsers's avatar
Sjaak Smetsers committed
757
758
	# (th_vars, ts_var_store)		= fresh_type_variables st_vars (ts_type_heaps.th_vars, ts_var_store)
	  (th_attrs, ts_attr_store)	= fresh_attributes st_attr_vars (ts_type_heaps.th_attrs, ts_attr_store)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
759
760
761
762
	  (attr_env, th_attrs)		= freshEnvironment st_attr_env th_attrs 
	  cs = { copy_heaps = { ts_type_heaps & th_vars = th_vars, th_attrs = th_attrs }}
	  (tst_args, cs)				= freshCopy st_args  cs
	  (tst_result, cs)				= freshCopy st_result cs
Sjaak Smetsers's avatar
Sjaak Smetsers committed
763
	  (tst_context, ({copy_heaps}, ts_var_heap)) 	= freshTypeContexts fresh_context_vars st_context (cs, ts_var_heap)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
764
765
	  cons_variables				= foldSt (collect_cons_variables_in_tc common_defs) tst_context []
	= ({ tst_args = tst_args, tst_result = tst_result, tst_context = tst_context, tst_attr_env = attr_env, tst_arity = st_arity, tst_lifted = 0 }, cons_variables,
Sjaak Smetsers's avatar
Sjaak Smetsers committed
766
767
	   { ts & ts_var_store = ts_var_store, ts_attr_store = ts_attr_store, ts_type_heaps = copy_heaps, ts_var_heap = ts_var_heap})
//		---> ("freshSymbolType", tst_args, tst_result, tst_context)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
768
	where
769
		fresh_type_variables :: .[TypeVar] *(*Heap TypeVarInfo,.Int) -> (!.Heap TypeVarInfo,!Int);
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
770
771
772
		fresh_type_variables type_variables state
			= foldr (\{tv_info_ptr} (var_heap, var_store) -> (writePtr tv_info_ptr (TVI_Type (TempV var_store)) var_heap, inc var_store))
							state type_variables
773
774

		fresh_attributes :: .[AttributeVar] *(*Heap AttrVarInfo,.Int) -> (!.Heap AttrVarInfo,!Int);
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
		fresh_attributes attributes state
			= foldr (\{av_info_ptr} (attr_heap, attr_store) -> (writePtr av_info_ptr (AVI_Attr (TA_TempVar attr_store)) attr_heap, inc attr_store))
							state attributes
		
		collect_cons_variables_in_tc common_defs tc=:{tc_class={glob_module,glob_object={ds_index}}, tc_types} collected_cons_vars
			# {class_cons_vars} = common_defs.[glob_module].com_class_defs.[ds_index]
			= collect_cons_variables tc_types class_cons_vars collected_cons_vars
		
		collect_cons_variables [] class_cons_vars collected_cons_vars
			= collected_cons_vars
		collect_cons_variables [type : tc_types] class_cons_vars collected_cons_vars
			| class_cons_vars bitand 1 == 0
				= collect_cons_variables tc_types (class_cons_vars >> 1) collected_cons_vars
				= case type of
					TempV temp_var_id 
						-> collect_cons_variables tc_types (class_cons_vars >> 1) (add_variable temp_var_id collected_cons_vars)
//							---> ("collect_cons_variables", temp_var_id)
					_
						-> collect_cons_variables tc_types (class_cons_vars >> 1) collected_cons_vars
						
		add_variable new_var_id []
			= [new_var_id]
		add_variable new_var_id vars=:[var_id : var_ids]
			| new_var_id == var_id
				= vars
				= [var_id : add_variable new_var_id var_ids]
801
802
803
	
// JVG: added type:
freshInequality :: AttrInequality *(Heap AttrVarInfo) -> (!AttrCoercion,!.Heap AttrVarInfo);
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
804
805
806
807
808
809
810
811
812
813
814
815
816
817
freshInequality {ai_demanded,ai_offered} attr_heap
	# (av_dem_info, attr_heap) = readPtr ai_demanded.av_info_ptr attr_heap
	  (av_off_info, attr_heap) = readPtr ai_offered.av_info_ptr attr_heap
	  (AVI_Attr (TA_TempVar dem_attr_var)) = av_dem_info
	  (AVI_Attr (TA_TempVar off_attr_var)) = av_off_info
	= ({ac_demanded = dem_attr_var, ac_offered = off_attr_var}, attr_heap) // <<- (av_dem_info,av_off_info)
	
freshEnvironment [ineq : ineqs] attr_heap
	# (fresh_ineq, attr_heap) = freshInequality ineq attr_heap
	  (fresh_env, attr_heap) = freshEnvironment ineqs attr_heap
	= ([fresh_ineq : fresh_env], attr_heap)
freshEnvironment [] attr_heap
	= ([], attr_heap)

Sjaak Smetsers's avatar
Sjaak Smetsers committed
818
819
freshTypeContexts fresh_context_vars tcs cs_and_var_heap
	= mapSt (fresh_type_context fresh_context_vars) tcs cs_and_var_heap
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
820
where	
Sjaak Smetsers's avatar
Sjaak Smetsers committed
821
	fresh_type_context fresh_context_vars tc=:{tc_types} (cs, var_heap)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
822
		# (tc_types, cs) = mapSt fresh_context_type tc_types cs
Sjaak Smetsers's avatar
Sjaak Smetsers committed
823
824
825
826
		| fresh_context_vars
			# (new_info_ptr, var_heap) = newPtr VI_Empty var_heap
			= ({ tc & tc_types = tc_types, tc_var = new_info_ptr }, (cs, var_heap))
			= ({ tc & tc_types = tc_types}, (cs, var_heap))
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858

	fresh_context_type (CV tv :@: types) cs=:{copy_heaps}
		# (fresh_cons_var, th_vars)		= freshConsVariable tv copy_heaps.th_vars
		  (types, cs) = freshCopy types { cs & copy_heaps = { copy_heaps & th_vars = th_vars }}
		= (fresh_cons_var :@: types, cs)
	fresh_context_type type cs
		= freshCopy type cs

freshAttributedVariable :: !u:TypeState -> (!AType, !u:TypeState)
freshAttributedVariable ts=:{ts_var_store,ts_attr_store}
	= ({ at_attribute = TA_TempVar ts_attr_store, at_annotation = AN_None, at_type = TempV ts_var_store },
	     {ts & ts_var_store = inc ts_var_store, ts_attr_store = inc ts_attr_store})

freshNonUniqueVariable :: !u:TypeState -> (!AType, !u:TypeState)
freshNonUniqueVariable ts=:{ts_var_store}
	= ({ at_attribute = TA_Multi, at_annotation = AN_None, at_type = TempV ts_var_store },
	     {ts & ts_var_store = inc ts_var_store})

freshAttribute :: !u:TypeState -> (!TypeAttribute, !u:TypeState)
freshAttribute ts=:{ts_attr_store}
	= (TA_TempVar ts_attr_store, {ts & ts_attr_store = inc ts_attr_store})


::	PropState =
	{	prop_type_heaps	:: !.TypeHeaps
	,	prop_td_infos	:: !.TypeDefInfos
	,	prop_attr_vars	:: ![AttributeVar]
	,	prop_attr_env	:: ![AttrInequality]
	,	prop_error		:: !.ErrorAdmin
	}

attribute_error type_attr err
859
860
	# err = errorHeading "Type error" err
	= { err & ea_file = err.ea_file <<< "* attribute expected instead of " <<< type_attr <<< '\n' }
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
861

862
863
864
addPropagationAttributesToAType :: {#CommonDefs} !AType !*PropState -> *(!AType,Int,!*PropState);
//addPropagationAttributesToAType modules type=:{at_type = TA cons_id=:{type_index={glob_object,glob_module}} cons_args, at_attribute} ps
addPropagationAttributesToAType modules type=:{at_type = TA cons_id=:{type_index={glob_object,glob_module},type_name} cons_args, at_attribute} ps
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
865
866
867
868
869
	# (cons_args, props, ps=:{prop_td_infos,prop_type_heaps,prop_attr_vars,prop_attr_env,prop_error})
			= add_propagation_attributes_to_atypes modules cons_args ps
	  (prop_class, th_vars, prop_td_infos) = propClassification glob_object glob_module props modules prop_type_heaps.th_vars prop_td_infos
	  (at_attribute, prop_class, th_attrs, prop_attr_vars, prop_attr_env, prop_error)
	  		= determine_attribute_of_cons modules at_attribute cons_args prop_class prop_type_heaps.th_attrs prop_attr_vars prop_attr_env prop_error
Martin Wierich's avatar
Martin Wierich committed
870
871
872
873
874
875
// MW32..
	  ({tdi_kinds}, prop_td_infos)
	  		= prop_td_infos![glob_module,glob_object]
	  (_, prop_error)
	  		= unsafeFold2St (check_kind type_name modules) tdi_kinds cons_args (1, prop_error)
// ..MW32
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
	= ({ type & at_type = TA cons_id cons_args, at_attribute = at_attribute }, prop_class,  { ps & prop_attr_vars = prop_attr_vars,
			prop_td_infos = prop_td_infos, prop_attr_env = prop_attr_env,
				prop_type_heaps = { prop_type_heaps & th_vars = th_vars, th_attrs = th_attrs}, prop_error = prop_error })
	where
		add_propagation_attributes_to_atypes modules [] ps
			= ([], [], ps) 
		add_propagation_attributes_to_atypes modules [atype : atypes] ps
			# (atype, prop_class, ps) = addPropagationAttributesToAType modules atype ps
			  (atypes, prop_classes, ps) = add_propagation_attributes_to_atypes modules atypes ps
			= ([atype : atypes], [prop_class : prop_classes], ps)
	
		determine_attribute_of_cons modules TA_Unique cons_args prop_class attr_var_heap attr_vars attr_env ps_error
			= (TA_Unique, prop_class >> length cons_args, attr_var_heap, attr_vars, attr_env, ps_error)
		determine_attribute_of_cons modules cons_attr cons_args prop_class attr_var_heap attr_vars attr_env ps_error
			# (cumm_attr, prop_attrs, prop_class) = determine_cummulative_attribute cons_args TA_Multi [] prop_class
			  (comb_attr, attr_var_heap, attr_vars, attr_env, ps_error)
			  		= combine_attributes cons_attr cumm_attr prop_attrs attr_var_heap attr_vars attr_env ps_error
			= (comb_attr, prop_class, attr_var_heap, attr_vars, attr_env, ps_error)
			  
		determine_cummulative_attribute [] cumm_attr attr_vars prop_class
			= (cumm_attr, attr_vars, prop_class)
		determine_cummulative_attribute [{at_attribute} : types ] cumm_attr attr_vars prop_class
			| prop_class bitand 1 == 0
				= determine_cummulative_attribute types cumm_attr attr_vars (prop_class >> 1)
				= case at_attribute of
					TA_Unique
						-> (TA_Unique, [], prop_class >> length types)
					TA_Multi
						-> determine_cummulative_attribute types cumm_attr attr_vars (prop_class >> 1)
					TA_Var attr_var
						-> determine_cummulative_attribute types at_attribute [attr_var : attr_vars] (prop_class >> 1)
		

		combine_attributes (TA_Var attr_var) cumm_attr prop_vars attr_var_heap attr_vars attr_env ps_error
			= case cumm_attr of
				TA_Unique
					-> (TA_Unique, attr_var_heap, attr_vars, attr_env, attribute_error attr_var ps_error)
				
				TA_Multi
					-> (TA_Var attr_var, attr_var_heap, attr_vars, attr_env, ps_error)
				TA_Var _
					-> (TA_Var attr_var, attr_var_heap, attr_vars, foldSt (new_inequality attr_var) prop_vars attr_env, ps_error)
		where
			new_inequality off_attr_var dem_attr_var [] 
				= [{ ai_demanded = dem_attr_var, ai_offered = off_attr_var }]
			new_inequality off_attr_var dem_attr_var ins=:[ inequal : iequals ]
				 | dem_attr_var.av_info_ptr == inequal.ai_demanded.av_info_ptr && off_attr_var.av_info_ptr == inequal.ai_offered.av_info_ptr
				 	= ins
				 	= [ inequal : new_inequality off_attr_var dem_attr_var iequals ]
	
		combine_attributes _ (TA_Var var) prop_vars attr_var_heap attr_vars attr_env ps_error
			# (new_attr_ptr, attr_var_heap) = newPtr AVI_Empty attr_var_heap
			  new_attr_var = { var & av_info_ptr = new_attr_ptr }
			= (TA_Var new_attr_var, attr_var_heap, [new_attr_var : attr_vars],
					mapAppend (\ai_demanded -> { ai_demanded = ai_demanded, ai_offered = new_attr_var }) prop_vars attr_env, ps_error)
		combine_attributes cons_attr TA_Unique _ attr_var_heap attr_vars attr_env ps_error
			= (TA_Unique, attr_var_heap, attr_vars, attr_env, ps_error)
		combine_attributes cons_attr _ _ attr_var_heap attr_vars attr_env ps_error
			= (cons_attr, attr_var_heap, attr_vars, attr_env, ps_error)

Martin Wierich's avatar
Martin Wierich committed
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
// MW32..
		check_kind type_name modules type_kind {at_type} (arg_nr, prop_error)
			# ok
					= kind_is_ok modules (my_kind_to_int type_kind) at_type
			| ok
				= (arg_nr+1, prop_error)
			# prop_error = errorHeading type_error prop_error
			= (arg_nr+1, { prop_error & ea_file = prop_error.ea_file <<< " argument " <<< arg_nr <<< " of type " <<< type_name 
													<<< " expected kind " <<< type_kind <<< "\n" })
		  where
			kind_is_ok modules demanded_kind (TA {type_index={glob_object,glob_module}} args)
				# {td_arity}
						= modules.[glob_module].com_type_defs.[glob_object]
				= demanded_kind == td_arity-length args
			kind_is_ok modules 0 (_ --> _)
				= True
			kind_is_ok modules _ (_ :@: _)
				= True
			kind_is_ok modules 0 (TB _)
				= True
			kind_is_ok modules _ (GTV _)
				= True
			kind_is_ok modules _ (TV _)
				= True
			kind_is_ok modules _ (TQV _)
				= True
			kind_is_ok modules _ _
				= False
		
			my_kind_to_int KindConst
				= 0
			my_kind_to_int (KindArrow int_kind)
				= int_kind
// ..MW32

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
addPropagationAttributesToAType modules type=:{at_type} ps
	# (at_type, ps) = addPropagationAttributesToType modules at_type ps
	= ({ type & at_type = at_type }, NoPropClass, ps)

addPropagationAttributesToType modules (arg_type --> res_type) ps
	# (arg_type, prop_class, ps) = addPropagationAttributesToAType modules arg_type ps
	  (res_type, prop_class, ps) = addPropagationAttributesToAType modules res_type ps
	= (arg_type --> res_type, ps)
addPropagationAttributesToType modules (type_var :@: types) ps
	# (types, ps) = addPropagationAttributesToATypes modules types ps
	= (type_var :@: types, ps)
addPropagationAttributesToType modules type ps
	= (type, ps)

addPropagationAttributesToATypes modules types ps
	= mapSt (add_propagation_attributes_to_atype modules) types ps
where
	add_propagation_attributes_to_atype modules type ps
		# (type, prop_class, ps) = addPropagationAttributesToAType modules type ps
		= (type, ps)

:: Base :== {! AType}

currySymbolType st=:{tst_args,tst_arity,tst_result,tst_attr_env} req_arity ts=:{ts_attr_store}
	| tst_arity == req_arity
		= (st, ts)
	# (tst_args, rest_args, is_unique) = split_args req_arity tst_args 
	| is_unique
		# (type, _, _) = buildCurriedType rest_args tst_result TA_Unique [] 0
		= ({ st & tst_args = tst_args, tst_arity = req_arity, tst_result = type }, ts)
		# (type, tst_attr_env, ts_attr_store) = buildCurriedType rest_args tst_result (TA_TempVar ts_attr_store)
		  		(build_attr_env ts_attr_store tst_args tst_attr_env) (inc ts_attr_store)
		= ({ st & tst_args = tst_args, tst_arity = req_arity, tst_result = type, tst_attr_env = tst_attr_env }, { ts & ts_attr_store = ts_attr_store })
where
	split_args 0 args = ([], args, False)
	split_args n [atype=:{at_attribute} : args]
		# (left, right, is_unique) = split_args (dec n) args
		= ([ atype : left ], right, is_unique || attr_is_unique at_attribute)
	
	attr_is_unique TA_Unique = True
	attr_is_unique _ = False
	
	build_attr_env cum_attr_var [] attr_env
		= attr_env
	build_attr_env cum_attr_var [{at_attribute=(TA_TempVar attr_var)} : args] attr_env
		= build_attr_env cum_attr_var args [{ ac_demanded = attr_var, ac_offered = cum_attr_var } : attr_env]
	build_attr_env cum_attr_var [_ : args] attr_env
		= build_attr_env cum_attr_var args attr_env


emptyIdent =: { id_name = "", id_info = nilPtr }

buildCurriedType [] type cum_attr attr_env attr_store
	= (type, attr_env, attr_store)
buildCurriedType [at=:{at_attribute}:ats] type cum_attr attr_env attr_store
	# (next_cum_attr, attr_env, attr_store) = combine_attributes at_attribute cum_attr attr_env attr_store
	  (res_type, attr_env, attr_store) = buildCurriedType ats type next_cum_attr attr_env attr_store
	= ({at_annotation = AN_None, at_attribute = cum_attr , at_type = at --> res_type }, attr_env, attr_store)
where
	combine_attributes TA_Unique cum_attr attr_env attr_store
		= (TA_Unique, attr_env, attr_store)
	combine_attributes (TA_TempVar attr_var) (TA_TempVar cum_attr_var) attr_env attr_store
		= (TA_TempVar attr_store, [{ ac_demanded = cum_attr_var,ac_offered = attr_store },{ ac_demanded = attr_var,ac_offered = attr_store }:attr_env], inc attr_store)
	combine_attributes (TA_TempVar _) cum_attr attr_env attr_store
		= (cum_attr, attr_env, attr_store)
	combine_attributes _ (TA_TempVar cum_attr_var) attr_env attr_store
		= (TA_TempVar attr_store, [{ ac_demanded = cum_attr_var,ac_offered = attr_store }:attr_env], inc attr_store)
	combine_attributes _ cum_attr attr_env attr_store
		= (cum_attr, attr_env, attr_store)

determineSymbolTypeOfFunction ident act_arity st=:{st_args,st_result,st_attr_vars,st_attr_env} type_ptr common_defs ts=:{ts_var_heap}
	# (type_info, ts_var_heap) = readPtr type_ptr ts_var_heap
	  ts = { ts & ts_var_heap = ts_var_heap }
	= case type_info of
		VI_PropagationType symb_type
Sjaak Smetsers's avatar
Sjaak Smetsers committed
1046
	   		# (copy_symb_type, cons_variables, ts) = freshSymbolType cWithFreshContextVars symb_type common_defs ts 
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1047
1048
1049
1050
1051
1052
1053
1054
1055
			  (curried_st, ts) = currySymbolType copy_symb_type act_arity ts
			-> (curried_st, cons_variables, ts)
		_	
			# (st_args, ps) = addPropagationAttributesToATypes common_defs st_args
					{ prop_type_heaps = ts.ts_type_heaps, prop_td_infos = ts.ts_td_infos,
					  prop_attr_vars = st_attr_vars, prop_attr_env = st_attr_env, prop_error = ts.ts_error}
			  (st_result, _, {prop_type_heaps,prop_td_infos,prop_attr_vars,prop_error,prop_attr_env})
			  			= addPropagationAttributesToAType common_defs st_result ps
			  st = { st & st_args = st_args, st_result = st_result, st_attr_vars = prop_attr_vars, st_attr_env = prop_attr_env }
Sjaak Smetsers's avatar
Sjaak Smetsers committed
1056
	   		# (copy_symb_type, cons_variables, ts) = freshSymbolType cWithFreshContextVars st common_defs { ts &
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1057
1058
1059
1060
1061
1062
	   										ts_type_heaps = prop_type_heaps, ts_td_infos = prop_td_infos, ts_error = prop_error,
											ts_var_heap = ts.ts_var_heap <:= (type_ptr, VI_PropagationType st) }
			  (curried_st, ts) = currySymbolType copy_symb_type act_arity ts
			-> (curried_st, cons_variables, ts)

standardFieldSelectorType {glob_object={ds_ident,ds_index},glob_module} {ti_common_defs} ts=:{ts_var_store,ts_type_heaps}
1063
	#! {sd_type,sd_exi_vars} = ti_common_defs.[glob_module].com_selector_defs.[ds_index]
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1064
	# (th_vars, ts_var_store) = freshExistentialVariables sd_exi_vars (ts_type_heaps.th_vars, ts_var_store)
Sjaak Smetsers's avatar
Sjaak Smetsers committed
1065
	  (inst, cons_variables, ts) = freshSymbolType cWithFreshContextVars sd_type ti_common_defs { ts & ts_type_heaps = { ts_type_heaps & th_vars = th_vars }, ts_var_store = ts_var_store }
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1066
1067
1068
1069
1070
	= (inst, ts)
//		 ---> ("standardFieldSelectorType", ds_ident, inst)

standardTupleSelectorType {ds_index} arg_nr {ti_common_defs} ts
	#! {cons_type} = ti_common_defs.[cPredefinedModuleIndex].com_cons_defs.[ds_index]
Sjaak Smetsers's avatar
Sjaak Smetsers committed
1071
	   (fresh_type, cons_variables, ts) = freshSymbolType cWithFreshContextVars { cons_type & st_args = [cons_type.st_result], st_result = cons_type.st_args !! arg_nr } ti_common_defs ts
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1072
1073
1074
1075
1076
	= (fresh_type, ts)

standardRhsConstructorType index mod arity {ti_common_defs} ts
	#! {cons_symb, cons_type, cons_exi_vars } = ti_common_defs.[mod].com_cons_defs.[index]
	# cons_type = { cons_type & st_vars = mapAppend (\{atv_variable} -> atv_variable) cons_exi_vars cons_type.st_vars }
Sjaak Smetsers's avatar
Sjaak Smetsers committed
1077
	  (fresh_type, _, ts) = freshSymbolType cWithFreshContextVars cons_type ti_common_defs ts
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1078
1079
1080
1081
1082
1083
	= currySymbolType fresh_type arity ts
//		 ---> ("standardRhsConstructorType", cons_symb, fresh_type)

standardLhsConstructorType index mod arity {ti_common_defs} ts=:{ts_var_store,ts_type_heaps}
	#! {cons_symb, cons_type, cons_exi_vars } = ti_common_defs.[mod].com_cons_defs.[index]
	# (th_vars, ts_var_store) = freshExistentialVariables cons_exi_vars (ts_type_heaps.th_vars, ts_var_store)
Sjaak Smetsers's avatar
Sjaak Smetsers committed
1084
	  (fresh_type, _, ts) = freshSymbolType cWithFreshContextVars cons_type ti_common_defs { ts & ts_type_heaps = { ts_type_heaps & th_vars = th_vars }, ts_var_store = ts_var_store }
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1085
1086
1087
1088
1089
1090
1091
1092
1093
	= (fresh_type, ts)
//		 ---> ("standardLhsConstructorType", cons_symb, fresh_type)

:: ReferenceMarking :== Bool

cIsRecursive :== True
cIsNotRecursive :== False

storeAttribute (Yes expt_ptr) type_attribute symbol_heap
1094
	= symbol_heap <:=  (expt_ptr, EI_Attribute (toInt type_attribute))
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1095
1096
1097
storeAttribute No type_attribute symbol_heap
	= symbol_heap

1098
1099
1100
1101
getSymbolType ti=:{ti_functions,ti_common_defs,ti_main_dcl_module_n} {symb_kind = SK_Function {glob_module,glob_object}, symb_arity, symb_name} ts
	| glob_module == ti_main_dcl_module_n
		| glob_object>=size ts.ts_fun_env
			= abort symb_name.id_name;
1102
		# (fun_type, ts) = ts!ts_fun_env.[glob_object]
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1103
1104
		= case fun_type of
			UncheckedType fun_type
1105
1106
				# (fun_type_copy, ts) = currySymbolType fun_type symb_arity ts
				-> (fun_type_copy, [], [], ts)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1107
			SpecifiedType fun_type lifted_arg_types _ 
Sjaak Smetsers's avatar
Sjaak Smetsers committed
1108
1109
1110
				# (fun_type_copy=:{tst_args,tst_arity}, cons_variables, ts) = freshSymbolType cWithoutFreshContextVars fun_type ti_common_defs ts
				  (fun_type_copy, ts) = currySymbolType { fun_type_copy & tst_args = lifted_arg_types ++ fun_type_copy.tst_args,
				  										  tst_arity = tst_arity + length lifted_arg_types } symb_arity ts
1111
				-> (fun_type_copy, cons_variables, [], ts)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1112
			CheckedType fun_type
Sjaak Smetsers's avatar
Sjaak Smetsers committed
1113
				# (fun_type_copy, cons_variables, ts) = freshSymbolType cWithFreshContextVars fun_type ti_common_defs ts
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1114
				  (fun_type_copy,ts) = currySymbolType fun_type_copy symb_arity ts
1115
				-> (fun_type_copy, cons_variables, [], ts)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1116
			_
1117
1118
				-> abort ("getSymbolType "+++toString symb_name+++toString glob_object)
//				-> abort "getSymbolType (type.icl)" ---> (symb_name, glob_object, fun_type)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1119
		# {ft_type,ft_type_ptr,ft_specials} = ti_functions.[glob_module].[glob_object]
1120
1121
1122
		| glob_module>=size ti_functions || glob_object>=size ti_functions.[glob_module]
			= abort (toString glob_module+++" "+++toString glob_object+++" "+++toString ti_main_dcl_module_n+++" "+++symb_name.id_name);
		# (fun_type_copy, cons_variables, ts) = determineSymbolTypeOfFunction symb_name symb_arity ft_type ft_type_ptr ti_common_defs ts
1123
1124
1125
1126
1127
1128
1129
		= (fun_type_copy, cons_variables, get_specials ft_specials, ts)
	where
		get_specials (SP_ContextTypes specials) = specials
		get_specials SP_None 					= []
getSymbolType ti {symb_kind = SK_Constructor {glob_module,glob_object}, symb_arity} ts
	# (fresh_cons_type, ts) = standardRhsConstructorType glob_object glob_module symb_arity ti ts
	= (fresh_cons_type, [], [], ts) 
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
getSymbolType ti=:{ti_functions,ti_common_defs,ti_main_dcl_module_n} {symb_kind = SK_LocalMacroFunction glob_object, symb_arity, symb_name} ts
	| glob_object>=size ts.ts_fun_env
		= abort symb_name.id_name;
	# (fun_type, ts) = ts!ts_fun_env.[glob_object]
	= case fun_type of
		UncheckedType fun_type
			# (fun_type_copy, ts) = currySymbolType fun_type symb_arity ts
			-> (fun_type_copy, [], [], ts)
		SpecifiedType fun_type lifted_arg_types _ 
			# (fun_type_copy=:{tst_args,tst_arity}, cons_variables, ts) = freshSymbolType cWithoutFreshContextVars fun_type ti_common_defs ts
			  (fun_type_copy, ts) = currySymbolType { fun_type_copy & tst_args = lifted_arg_types ++ fun_type_copy.tst_args,
			  										  tst_arity = tst_arity + length lifted_arg_types } symb_arity ts
			-> (fun_type_copy, cons_variables, [], ts)
		CheckedType fun_type
			# (fun_type_copy, cons_variables, ts) = freshSymbolType cWithFreshContextVars fun_type ti_common_defs ts
			  (fun_type_copy,ts) = currySymbolType fun_type_copy symb_arity ts
			-> (fun_type_copy, cons_variables, [], ts)
		_
1148
1149
			-> abort ("getSymbolType "+++toString symb_name+++toString glob_object)
//			-> abort "getSymbolType (type.icl)" ---> (symb_name, glob_object, fun_type)
1150
1151
1152
1153
1154
1155
1156
1157
getSymbolType ti=:{ti_common_defs} {symb_kind = SK_OverloadedFunction {glob_module,glob_object}, symb_arity} ts
	# {me_symb, me_type,me_type_ptr} = ti_common_defs.[glob_module].com_member_defs.[glob_object]
	  (fun_type_copy, cons_variables, ts) = determineSymbolTypeOfFunction me_symb symb_arity me_type me_type_ptr ti_common_defs ts
	= (fun_type_copy, cons_variables, [], ts)

class requirements a :: !TypeInput !a !(!u:Requirements, !*TypeState) -> (!AType, !Optional ExprInfoPtr, !(!u:Requirements, !*TypeState))

instance requirements BoundVar
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1158
where
Sjaak Smetsers's avatar
Sjaak Smetsers committed
1159
1160
1161
	requirements ti {var_name,var_info_ptr,var_expr_ptr} (reqs, ts)
		# (var_info, ts_var_heap) = readPtr var_info_ptr ts.ts_var_heap
		  ts = { ts & ts_var_heap = ts_var_heap }
1162
		= (case var_info of
Martin Wierich's avatar
Martin Wierich committed
1163
			VI_Type type _
1164
1165
				-> type
			_
1166
				-> abort "requirements BoundVar" // ---> (var_name <<- var_info))
1167
			, Yes var_expr_ptr, (reqs, ts))
1168

1169
instance requirements App
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1170
where
1171
	requirements ti {app_symb,app_args,app_info_ptr} (reqs=:{req_cons_variables, req_attr_coercions}, ts)
Sjaak Smetsers's avatar
Sjaak Smetsers committed
1172
		# (tst=:{tst_attr_env,tst_args,tst_result,tst_context}, cons_variables, specials, ts) = getSymbolType ti app_symb ts
1173
	  	  reqs = { reqs & req_attr_coercions = tst_attr_env ++ req_attr_coercions, req_cons_variables = [cons_variables : req_cons_variables] }
Martin Wierich's avatar
Martin Wierich committed
1174
	      (reqs, ts) = requirements_of_args ti app_symb.symb_name 1 app_args tst_args (reqs, ts)
1175
1176
1177
1178
1179
1180
		| isEmpty tst_context
			= (tst_result, No, (reqs, ts))
			= (tst_result, No, ({ reqs & req_overloaded_calls = [app_info_ptr : reqs.req_overloaded_calls ]}, 
					{ ts & ts_expr_heap = ts.ts_expr_heap <:= (app_info_ptr,
							EI_Overloaded { oc_symbol = app_symb, oc_context = tst_context, oc_specials = specials })}))
	where
Martin Wierich's avatar
Martin Wierich committed
1181
1182
		requirements_of_args :: !TypeInput !Ident !Int ![Expression] ![AType] !(!u:Requirements, !*TypeState) -> (!u:Requirements, !*TypeState)
		requirements_of_args ti _ _ [] [] reqs_ts
1183
			= reqs_ts
Martin Wierich's avatar
Martin Wierich committed
1184
		requirements_of_args ti fun_ident arg_nr [expr:exprs] [lt:lts] reqs_ts
1185
			# (e_type, opt_expr_ptr, (reqs, ts)) = requirements ti expr reqs_ts
Martin Wierich's avatar
Martin Wierich committed
1186
			  req_type_coercions = [{ tc_demanded = lt, tc_offered = e_type, tc_position = CP_FunArg fun_ident arg_nr, tc_coercible = True } : reqs.req_type_coercions ]
1187
			  ts_expr_heap = storeAttribute opt_expr_ptr lt.at_attribute ts.ts_expr_heap
Martin Wierich's avatar
Martin Wierich committed
1188
			= requirements_of_args ti fun_ident (arg_nr+1) exprs lts ({ reqs & req_type_coercions = req_type_coercions}, { ts & ts_expr_heap = ts_expr_heap })
1189
1190

instance requirements Case
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1191
where
1192
1193
// MW4 was:	requirements ti {case_expr,case_guards,case_default,case_info_ptr} reqs_ts
	requirements ti {case_expr,case_guards,case_default,case_info_ptr, case_default_pos} reqs_ts
1194
1195
1196
		# (expr_type, opt_expr_ptr, (reqs, ts)) = requirements ti case_expr reqs_ts
		  (fresh_v, ts) = freshAttributedVariable ts
		  (cons_types, reqs_ts) = requirements_of_guarded_expressions ti case_guards case_expr expr_type opt_expr_ptr fresh_v (reqs, ts)
1197
1198
// MW4 was:		  (reqs, ts) = requirements_of_default ti case_default fresh_v reqs_ts
		  (reqs, ts) = requirements_of_default ti case_default case_default_pos fresh_v reqs_ts
1199
1200
1201
1202
1203
1204
1205
1206
1207
		  ts_expr_heap =  ts.ts_expr_heap <:= (case_info_ptr, EI_CaseType { ct_pattern_type = expr_type, ct_result_type = fresh_v, ct_cons_types = cons_types })
		= (fresh_v, No, ({ reqs & req_case_and_let_exprs = [case_info_ptr : reqs.req_case_and_let_exprs]},
				{ ts & ts_expr_heap = ts_expr_heap }))
	where
		requirements_of_guarded_expressions ti=:{ti_common_defs} (AlgebraicPatterns alg_type patterns) match_expr pattern_type opt_pattern_ptr
						goal_type (reqs, ts)
			# (cons_types, result_type, new_attr_env, ts) = freshAlgebraicType alg_type patterns ti_common_defs ts
			  (used_cons_types, (reqs, ts)) = requirements_of_algebraic_patterns ti patterns cons_types goal_type [] (reqs, ts)
			  ts_expr_heap = storeAttribute opt_pattern_ptr result_type.at_attribute ts.ts_expr_heap
1208
			  (position, ts_var_heap) = getPositionOfExpr match_expr ts.ts_var_heap
Martin Wierich's avatar
Martin Wierich committed
1209
			= (reverse used_cons_types,  ({ reqs & req_type_coercions = [{tc_demanded = result_type,tc_offered = pattern_type, tc_position = position,
1210
					tc_coercible = True} : reqs.req_type_coercions],
Martin Wierich's avatar
Martin Wierich committed
1211
						req_attr_coercions = new_attr_env ++ reqs.req_attr_coercions }, { ts & ts_expr_heap = ts_expr_heap, ts_var_heap = ts_var_heap }))
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1212
	
1213
1214
1215
1216
		requirements_of_guarded_expressions ti (BasicPatterns bas_type patterns) match_expr pattern_type opt_pattern_ptr goal_type (reqs, ts)
			# (attr_bas_type, ts) = attributedBasicType bas_type ts
			  (reqs, ts) = requirements_of_basic_patterns ti patterns goal_type (reqs, ts)
			  ts_expr_heap = storeAttribute opt_pattern_ptr attr_bas_type.at_attribute ts.ts_expr_heap
Martin Wierich's avatar
Martin Wierich committed
1217
			= ([], ({ reqs & req_type_coercions = [{tc_demanded = attr_bas_type,tc_offered = pattern_type, tc_position = CP_Expression match_expr, tc_coercible = True} :
1218
1219
1220
1221
1222
						reqs.req_type_coercions]}, { ts & ts_expr_heap = ts_expr_heap }))
		requirements_of_guarded_expressions ti (DynamicPatterns dynamic_patterns) match_expr pattern_type opt_pattern_ptr goal_type reqs_ts
			# dyn_type = { at_type = TB BT_Dynamic, at_attribute = TA_Multi, at_annotation = AN_None }
			  (used_dyn_types, (reqs, ts)) = requirements_of_dynamic_patterns ti goal_type dynamic_patterns [] reqs_ts
			  ts_expr_heap = storeAttribute opt_pattern_ptr TA_Multi ts.ts_expr_heap
Martin Wierich's avatar
Martin Wierich committed
1223
			= (reverse used_dyn_types, ({ reqs & req_type_coercions = [{tc_demanded = dyn_type, tc_offered = pattern_type, tc_position = CP_Expression match_expr, tc_coercible = True} :
1224
						reqs.req_type_coercions] }, { ts & ts_expr_heap = ts_expr_heap }))
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1225
	
1226
/* MW4 was:
1227
1228
		requirements_of_algebraic_patterns ti [] cons_types goal_type used_cons_types reqs_ts
			= (used_cons_types, reqs_ts)
Martin Wierich's avatar
Martin Wierich committed
1229
1230
1231
		requirements_of_algebraic_patterns ti=:{ti_common_defs} [{ap_symbol, ap_vars, ap_expr }:gs] [ cons_arg_types : cons_types] goal_type used_cons_types (reqs, ts)
			# (res_type, opt_expr_ptr, (reqs, ts)) 
					= requirements ti ap_expr (reqs, { ts & ts_var_heap = makeBase ap_symbol.glob_object.ds_ident 1 ap_vars cons_arg_types ts.ts_var_heap})
1232
1233
			  ts_expr_heap = storeAttribute opt_expr_ptr res_type.at_attribute ts.ts_expr_heap
			= requirements_of_algebraic_patterns ti gs cons_types goal_type [ cons_arg_types : used_cons_types ]
Martin Wierich's avatar
Martin Wierich committed
1234
				({ reqs & req_type_coercions = [ { tc_demanded = goal_type, tc_offered = res_type, tc_position = CP_Expression ap_expr, tc_coercible = True } : reqs.req_type_coercions] },
1235
					  { ts & ts_expr_heap = ts_expr_heap })
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
*/

		requirements_of_algebraic_patterns ti [] cons_types goal_type used_cons_types reqs_ts
			= (used_cons_types, reqs_ts)
		requirements_of_algebraic_patterns ti [alg_pattern=:{ap_position}:alg_patterns] [ cons_arg_types : cons_types] 
											goal_type used_cons_types reqs_ts
			= requirements_of_algebraic_patterns ti alg_patterns cons_types goal_type [ cons_arg_types : used_cons_types ]
					(possibly_accumulate_reqs_in_new_group 
						ap_position
						(requirements_of_algebraic_pattern ti alg_pattern cons_arg_types goal_type) 
						reqs_ts
					) 

// MW4++..
		requirements_of_algebraic_pattern ti {ap_symbol, ap_vars, ap_expr} cons_arg_types goal_type (reqs, ts)
			# (res_type, opt_expr_ptr, (reqs, ts)) 
					= requirements ti ap_expr (reqs, { ts & ts_var_heap = makeBase ap_symbol.glob_object.ds_ident 1 ap_vars cons_arg_types ts.ts_var_heap})
			  ts_expr_heap = storeAttribute opt_expr_ptr res_type.at_attribute ts.ts_expr_heap
			= ({ reqs & req_type_coercions = [ { tc_demanded = goal_type, tc_offered = res_type, tc_position = CP_Expression ap_expr, tc_coercible = True } : reqs.req_type_coercions] },
					  { ts & ts_expr_heap = ts_expr_heap })
// ..MW4
1257
	
1258
/*
1259
1260
1261
1262
1263
1264
		requirements_of_basic_patterns _ [] goal_type reqs_ts
			= reqs_ts
		requirements_of_basic_patterns ti=:{ti_common_defs} [{bp_expr }:gs] goal_type reqs_ts
		  	# (res_type, opt_expr_ptr, (reqs, ts)) = requirements ti bp_expr reqs_ts
			  ts_expr_heap = storeAttribute opt_expr_ptr res_type.at_attribute ts.ts_expr_heap
			= requirements_of_basic_patterns ti gs goal_type
Martin Wierich's avatar
Martin Wierich committed
1265
				({ reqs & req_type_coercions = [ { tc_demanded = goal_type, tc_offered = res_type, tc_position = CP_Expression bp_expr, tc_coercible = True } : reqs.req_type_coercions] },
1266
						 { ts & ts_expr_heap = ts_expr_heap })
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
*/	
		requirements_of_basic_patterns _ [] goal_type reqs_ts
			= reqs_ts
		requirements_of_basic_patterns ti [{bp_expr, bp_position}:gs] goal_type reqs_ts
			= requirements_of_basic_patterns ti gs goal_type
				(possibly_accumulate_reqs_in_new_group
					bp_position
					(requirements_of_basic_pattern ti bp_expr goal_type)
					reqs_ts
				)

// MW4++..
		requirements_of_basic_pattern ti bp_expr goal_type reqs_ts
		  	# (res_type, opt_expr_ptr, (reqs, ts)) = requirements ti bp_expr reqs_ts
			  ts_expr_heap = storeAttribute opt_expr_ptr res_type.at_attribute ts.ts_expr_heap
			= ({ reqs & req_type_coercions = [ { tc_demanded = goal_type, tc_offered = res_type, tc_position = CP_Expression bp_expr, tc_coercible = True } : reqs.req_type_coercions] },
						 { ts & ts_expr_heap = ts_expr_heap })
// ..MW4
1285
	
1286
/* MW4 was
1287
1288
		requirements_of_dynamic_patterns ti goal_type [{dp_var={fv_info_ptr},dp_type,dp_rhs} : dps] used_dyn_types (reqs, ts=:{ts_expr_heap, ts_var_heap})
			# (EI_TempDynamicPattern _ _ _ _ dyn_type dyn_context dyn_expr_ptr type_code_symbol, ts_expr_heap) = readPtr dp_type ts_expr_heap
Martin Wierich's avatar
Martin Wierich committed
1289
			  ts_var_heap = ts_var_heap <:= (fv_info_ptr, VI_Type dyn_type No)
1290
1291
			  (dp_rhs_type, opt_expr_ptr, (reqs, ts)) = requirements ti dp_rhs (reqs, { ts & ts_expr_heap = ts_expr_heap, ts_var_heap = ts_var_heap })
			  ts_expr_heap = storeAttribute opt_expr_ptr dp_rhs_type.at_attribute ts.ts_expr_heap
Martin Wierich's avatar
Martin Wierich committed
1292
			  type_coercion = { tc_demanded = goal_type, tc_offered = dp_rhs_type, tc_position = CP_Expression dp_rhs, tc_coercible = True }
1293
1294
1295
1296
1297
1298
1299
1300
			| isEmpty dyn_context
				# reqs = {reqs & req_type_coercions = [ type_coercion : reqs.req_type_coercions]}
				= requirements_of_dynamic_patterns ti goal_type dps [ [dyn_type] : used_dyn_types ] (reqs, { ts &  ts_expr_heap = ts_expr_heap })
				# reqs = { reqs & req_type_coercions = [ type_coercion : reqs.req_type_coercions], req_overloaded_calls = [dyn_expr_ptr : reqs.req_overloaded_calls ]}
				= requirements_of_dynamic_patterns ti goal_type dps [ [dyn_type] : used_dyn_types ] (reqs, { ts & ts_expr_heap = ts_expr_heap <:=
						(dyn_expr_ptr,  EI_Overloaded {  oc_symbol = type_code_symbol, oc_context = dyn_context, oc_specials = [] }) })
		requirements_of_dynamic_patterns ti goal_type [] used_dyn_types reqs_ts
			= (used_dyn_types, reqs_ts)
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
*/
	
		requirements_of_dynamic_patterns ti goal_type [] used_dyn_types reqs_ts
			= (used_dyn_types, reqs_ts)
		requirements_of_dynamic_patterns ti goal_type [dp=:{dp_position, dp_type} : dps] used_dyn_types (reqs, ts=:{ts_expr_heap})
			# (EI_TempDynamicPattern _ _ _ _ dyn_type dyn_context dyn_expr_ptr type_code_symbol, ts_expr_heap)
					= readPtr dp_type ts_expr_heap
			  (reqs_ts) 
			  		= possibly_accumulate_reqs_in_new_group
			  				dp_position
							(requirements_of_dynamic_pattern dyn_type dyn_context dyn_expr_ptr type_code_symbol ti goal_type dp)
							(reqs, { ts & ts_expr_heap = ts_expr_heap})
			= requirements_of_dynamic_patterns ti goal_type dps [ [dyn_type] : used_dyn_types ] reqs_ts
	
// MW4++..
		requirements_of_dynamic_pattern dyn_type dyn_context dyn_expr_ptr type_code_symbol 
										ti goal_type {dp_var={fv_info_ptr},dp_rhs} (reqs, ts=:{ts_expr_heap, ts_var_heap})
			# ts_var_heap = ts_var_heap <:= (fv_info_ptr, VI_Type dyn_type No)
			  (dp_rhs_type, opt_expr_ptr, (reqs, ts)) = requirements ti dp_rhs (reqs, { ts & ts_expr_heap = ts_expr_heap, ts_var_heap = ts_var_heap })
			  ts_expr_heap = storeAttribute opt_expr_ptr dp_rhs_type.at_attribute ts.ts_expr_heap
			  type_coercion = { tc_demanded = goal_type, tc_offered = dp_rhs_type, tc_position = CP_Expression dp_rhs, tc_coercible = True }
			| isEmpty dyn_context
				# reqs = {reqs & req_type_coercions = [ type_coercion : reqs.req_type_coercions]}
				= (reqs, { ts &  ts_expr_heap = ts_expr_heap })
				# reqs = { reqs & req_type_coercions = [ type_coercion : reqs.req_type_coercions], req_overloaded_calls = [dyn_expr_ptr : reqs.req_overloaded_calls ]}
				= (reqs, { ts & ts_expr_heap = ts_expr_heap <:=
						(dyn_expr_ptr,  EI_Overloaded {  oc_symbol = type_code_symbol, oc_context = dyn_context, oc_specials = [] }) })
// ..MW4
1329
1330
	
	
1331
/* MW4 was:
1332
1333
1334
		requirements_of_default ti (Yes expr) goal_type reqs_ts
			# (res_type, opt_expr_ptr, (reqs,  ts)) = requirements ti expr reqs_ts
			  ts_expr_heap = storeAttribute opt_expr_ptr res_type.at_attribute ts.ts_expr_heap
Martin Wierich's avatar
Martin Wierich committed
1335
			= ({ reqs & req_type_coercions = [ { tc_demanded = goal_type, tc_offered = res_type, tc_position = CP_Expression expr, tc_coercible = True } : reqs.req_type_coercions] },
1336
1337
1338
					{ ts & ts_expr_heap = ts_expr_heap })
		requirements_of_default ti No goal_type reqs_ts
			= reqs_ts