type_io.icl 24.6 KB
Newer Older
1
2
implementation module type_io

Martijn Vervoort's avatar
Martijn Vervoort committed
3
4
5
// WARNING: It is essential to report changes in this module to martijnv@cs.kun.nl
//			because the binary format for type-files is used by the dynamic run-time
//			system.
6

Martijn Vervoort's avatar
Martijn Vervoort committed
7
import StdEnv, compare_constructor
8
9
import scanner, general, Heap, typeproperties, utilities, checksupport

Martijn Vervoort's avatar
Martijn Vervoort committed
10
11
12
13
14
15
// normal form:
// -	type variables in type definitions are normalized by checkTypeDef in the
//		module checktypes.icl. The position of a type variable in the left-hand
//		side of a type constructor is used.
// -	algebraic datatypes; constructors are alphabetically ordered in this 
//		module
16
//
Martijn Vervoort's avatar
Martijn Vervoort committed
17
18
19
// unsupported:
// - 	type synonyms
// - 	ADTs
20

Martijn Vervoort's avatar
Martijn Vervoort committed
21
22
//import DebugUtilities;
F a b :== b;
Martijn Vervoort's avatar
Martijn Vervoort committed
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

class NormaliseTypeDef a 
where
	normalise_type_def :: a -> a
	
import RWSDebug

instance NormaliseTypeDef TypeRhs
where
	normalise_type_def (AlgType defined_symbols)
		// algebraic data types are further normalized by an alphabetical sort on the
		// constructor names.
		= AlgType (sortBy (\{ds_ident={id_name=id_name1}} {ds_ident={id_name=id_name2}} -> id_name1 < id_name2) defined_symbols)
	normalise_type_def i
		= i
38
39

//1.3		
Martijn Vervoort's avatar
Martijn Vervoort committed
40
instance NormaliseTypeDef TypeDef rhs | NormaliseTypeDef rhs
41
42
43
44
//3.1
/*2.0
instance NormaliseTypeDef (TypeDef rhs) | NormaliseTypeDef rhs
0.2*/
Martijn Vervoort's avatar
Martijn Vervoort committed
45
46
47
where
	normalise_type_def type_def=:{td_args,td_arity}
		= type_def
Martijn Vervoort's avatar
Martijn Vervoort committed
48
				
Martijn Vervoort's avatar
Martijn Vervoort committed
49
	
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
class WriteTypeInfo a 
where
	write_type_info :: a !*File -> !*File
	
instance WriteTypeInfo CommonDefs
where 
	write_type_info {com_type_defs,com_cons_defs} tcl_file
		# tcl_file
			= write_type_info com_type_defs tcl_file
		# tcl_file
 			= write_type_info com_cons_defs tcl_file
		= tcl_file
	
instance WriteTypeInfo ConsDef
where 
Martijn Vervoort's avatar
Martijn Vervoort committed
65
	write_type_info {cons_symb,cons_type,cons_arg_vars,cons_priority,cons_index,cons_type_index,cons_exi_vars} tcl_file
66
67
		# tcl_file
			= write_type_info cons_symb tcl_file
Martijn Vervoort's avatar
Martijn Vervoort committed
68
69
		# tcl_file
			= write_type_info cons_type tcl_file
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
120
121
122
		# tcl_file
			= write_type_info cons_arg_vars tcl_file
		# tcl_file
			= write_type_info cons_priority tcl_file

		# tcl_file
			= write_type_info cons_index tcl_file
		# tcl_file
			= write_type_info cons_type_index tcl_file
		# tcl_file
			= write_type_info cons_exi_vars tcl_file
	
		= tcl_file
		
PrioCode	=: toChar 0
NoPrioCode	=: toChar 1

instance WriteTypeInfo Priority
where 
	write_type_info (Prio assoc i) tcl_file
		# tcl_file
			= fwritec PrioCode tcl_file
		# tcl_file
			= write_type_info assoc tcl_file
		# tcl_file
			= write_type_info i tcl_file
		= tcl_file
	write_type_info NoPrio tcl_file
		# tcl_file
			= fwritec NoPrioCode tcl_file
		= tcl_file
		
LeftAssocCode	=: toChar 2
RightAssocCode	=: toChar 3
NoAssocCode		=: toChar 4
		
instance WriteTypeInfo Assoc
where 
	write_type_info LeftAssoc tcl_file
		# tcl_file
			= fwritec LeftAssocCode tcl_file
		= tcl_file

	write_type_info RightAssoc tcl_file
		# tcl_file
			= fwritec RightAssocCode tcl_file
		= tcl_file	

	write_type_info NoAssoc tcl_file
		# tcl_file
			= fwritec NoAssocCode tcl_file
		= tcl_file	
		
123
//1.3
124
instance WriteTypeInfo TypeDef TypeRhs
125
126
127
128
//3.1
/*2.0
instance WriteTypeInfo (TypeDef TypeRhs)
0.2*/
129
where 
Martijn Vervoort's avatar
Martijn Vervoort committed
130
131
132
133
	write_type_info /*{td_name,td_arity,td_args,td_rhs}*/ type_def tcl_file
		# {td_name,td_arity,td_args,td_rhs}
			= normalise_type_def type_def
	
134
		|  F ("TypeDef '" +++ td_name.id_name +++ "'") True
135
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
 		#! tcl_file
 			= write_type_info td_name tcl_file
		#! tcl_file
 			= write_type_info td_arity tcl_file
 		#! tcl_file
 			= write_type_info td_args tcl_file
		#! tcl_file
 			= write_type_info td_rhs tcl_file
 		= tcl_file
 		
instance WriteTypeInfo ATypeVar
where 
	write_type_info {atv_annotation,atv_variable} tcl_file
 		#! tcl_file
 			= write_type_info atv_annotation tcl_file
 		#! tcl_file
 			= write_type_info atv_variable tcl_file
 		= tcl_file
 		
instance WriteTypeInfo Annotation
where 
	write_type_info AN_Strict tcl_file	
		= fwritec '!' tcl_file
	write_type_info AN_None tcl_file
		= fwritec ' ' tcl_file
		
instance WriteTypeInfo TypeVar
where
	write_type_info {tv_name} tcl_file
Martijn Vervoort's avatar
Martijn Vervoort committed
164
		// writing tv_name as number suffices
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
		= write_type_info tv_name tcl_file
		
AlgTypeCode			=: (toChar 5)
SynTypeCode 		=: (toChar 6)
RecordTypeCode		=: (toChar 7)
AbstractTypeCode	=: (toChar 8)

instance WriteTypeInfo TypeRhs
where 
	write_type_info (AlgType defined_symbols) tcl_file
 		#! tcl_file
 			= fwritec AlgTypeCode tcl_file;
		
		# tcl_file
			= write_type_info defined_symbols tcl_file

		= tcl_file
		
	write_type_info (SynType _) tcl_file
		#! tcl_file
 			= fwritec SynTypeCode tcl_file;
 			
 		// unimplemented
 		= tcl_file 
		
	write_type_info (RecordType {rt_fields}) tcl_file
 		#! tcl_file
 			= fwritec RecordTypeCode tcl_file;
		= write_type_info rt_fields tcl_file

	write_type_info (AbstractType _) tcl_file
 		#! tcl_file
 			= fwritec AbstractTypeCode tcl_file;
 			
 		// unimplemented
		= tcl_file
		
instance WriteTypeInfo DefinedSymbol 
where
	write_type_info {ds_ident,ds_arity,ds_index} tcl_file
		# tcl_file
			= write_type_info ds_ident tcl_file
		# tcl_file
			= write_type_info ds_arity tcl_file
		# tcl_file
			= write_type_info ds_index tcl_file
		= tcl_file

instance WriteTypeInfo Ident 
where
	write_type_info {id_name} tcl_file
216
217
218
		# tcl_file
			= fwritei (size id_name) tcl_file
		= fwrites id_name tcl_file
219
220
221
222
223
224
225
226
227
228
229
		
instance WriteTypeInfo FieldSymbol
where
	write_type_info {fs_name,fs_var,fs_index} tcl_file
		# tcl_file
			= write_type_info fs_name tcl_file
		# tcl_file
			= write_type_info fs_var tcl_file
		# tcl_file
			= write_type_info fs_index tcl_file
		= tcl_file
Martijn Vervoort's avatar
Martijn Vervoort committed
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
		
// NEW ->
instance WriteTypeInfo SymbolType
where
	write_type_info {st_vars,st_args,st_arity,st_result} tcl_file
		# tcl_file
			= write_type_info st_vars tcl_file
		# tcl_file
			= write_type_info st_args tcl_file
		# tcl_file
			= write_type_info st_arity tcl_file
		# tcl_file
			= write_type_info st_result tcl_file
		= tcl_file
		
instance WriteTypeInfo AType
where
247
	write_type_info {at_annotation,at_type} tcl_file
Martijn Vervoort's avatar
Martijn Vervoort committed
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
370
371
372
373
374
375
376
377
378
379
380
		# tcl_file
			= write_type_info at_annotation tcl_file
		# tcl_file
			= write_type_info at_type tcl_file
		= tcl_file

TypeTACode			=: (toChar 9)		// TA
TypeArrowCode 		=: (toChar 10)		// -->
TypeConsApplyCode	=: (toChar 11)		// :@:
TypeTBCode			=: (toChar 12)		// TB
TypeGTVCode			=: (toChar 13)		// GTV
TypeTVCode			=: (toChar 14)		// TV
TypeTQVCode			=: (toChar 15)		// TempTQV
TypeTECode			=: (toChar 16)		// TE

BT_IntCode			=: (toChar 17)	
BT_CharCode			=: (toChar 18)
BT_RealCode			=: (toChar 19)
BT_BoolCode			=: (toChar 20)
BT_DynamicCode		=: (toChar 21)
BT_FileCode			=: (toChar 22)
BT_WorldCode		=: (toChar 23)
BT_StringCode		=: (toChar 24)

instance WriteTypeInfo Type
where
	write_type_info (TA type_symb_ident atypes) tcl_file
		# tcl_file
			= fwritec TypeTACode tcl_file
		# tcl_file
			= write_type_info type_symb_ident tcl_file
		# tcl_file
			= write_type_info atypes tcl_file
		= tcl_file

	write_type_info (atype1 --> atype2) tcl_file
		# tcl_file
			= fwritec TypeArrowCode tcl_file
		# tcl_file
			= write_type_info atype1 tcl_file
		# tcl_file
			= write_type_info atype2 tcl_file
		= tcl_file
		
	write_type_info (cons_variable :@: atypes) tcl_file
		# tcl_file
			= fwritec TypeConsApplyCode tcl_file
		# tcl_file
			= write_type_info cons_variable tcl_file
		# tcl_file
			= write_type_info atypes tcl_file
		= tcl_file
		
	write_type_info tb=:(TB basic_type) tcl_file
		# tcl_file
			= case basic_type of
				BT_Int		-> fwritec BT_IntCode tcl_file
				BT_Char		-> fwritec BT_CharCode tcl_file
				BT_Real		-> fwritec BT_RealCode tcl_file
				BT_Bool		-> fwritec BT_BoolCode tcl_file
				BT_Dynamic	-> fwritec BT_DynamicCode tcl_file
				BT_File		-> fwritec BT_FileCode tcl_file
				BT_World	-> fwritec BT_WorldCode tcl_file
				BT_String type
					# tcl_file
						= fwritec BT_StringCode tcl_file
					# tcl_file
						= write_type_info type tcl_file
					-> tcl_file
		= tcl_file
	
	write_type_info (GTV type_var) tcl_file
		# tcl_file
			= fwritec TypeGTVCode tcl_file
		# tcl_file
			= write_type_info type_var tcl_file
		= tcl_file

	write_type_info (TV type_var) tcl_file
		# tcl_file
			= fwritec TypeTVCode tcl_file
		# tcl_file
			= write_type_info type_var tcl_file
		= tcl_file
		
	write_type_info (TQV type_var) tcl_file
		# tcl_file
			= fwritec TypeTQVCode tcl_file
		# tcl_file
			= write_type_info type_var tcl_file
		= tcl_file	

	write_type_info TE tcl_file
		# tcl_file
			= fwritec TypeTECode tcl_file
		= tcl_file	
	
	
ConsVariableCVCode		=: (toChar 25)	
ConsVariableTempCVCode	=: (toChar 26)
ConsVariableTempQCVCode	=: (toChar 27)

instance WriteTypeInfo ConsVariable
where
	write_type_info (CV type_var) tcl_file
		# tcl_file
			= fwritec ConsVariableCVCode tcl_file
		# tcl_file
			= write_type_info type_var tcl_file
		= tcl_file	

	write_type_info (TempCV temp_var_id) tcl_file
		# tcl_file
			= fwritec ConsVariableTempCVCode tcl_file
		# tcl_file
			= write_type_info temp_var_id tcl_file
		= tcl_file	
		
	write_type_info (TempQCV temp_var_id) tcl_file
		# tcl_file
			= fwritec ConsVariableTempQCVCode tcl_file
		# tcl_file
			= write_type_info temp_var_id tcl_file
		= tcl_file	

instance WriteTypeInfo TypeSymbIdent
where
	write_type_info {type_name,type_arity} tcl_file
		# tcl_file
			= write_type_info type_name tcl_file
		# tcl_file
			= write_type_info type_arity tcl_file
		= tcl_file
381
382
383
384
385
386
387
388
389
390
391
392
		
/*2.0
instance WriteTypeInfo String
where
	write_type_info s tcl_file
		# tcl_file
			= fwritei (size s) tcl_file
		= fwrites s tcl_file
	// warning:
	// Should be identical to the code in Ident

0.2*/
Martijn Vervoort's avatar
Martijn Vervoort committed
393

394
395
396
397
398
399
// basic and structural write_type_info's
instance WriteTypeInfo Int 
where
	write_type_info i tcl_file
		= fwritei i tcl_file

400
401
402
403
404
405
//1.3
instance WriteTypeInfo {#b} | select_u, size_u, WriteTypeInfo b
//3.1
/*2.0
instance WriteTypeInfo {#b} | WriteTypeInfo b & Array {#} b
0.2*/
406
407
408
409
410
411
412
413
414
where
	write_type_info unboxed_array tcl_file
		# s_unboxed_array
			= size unboxed_array
		# tcl_file
			= fwritei s_unboxed_array tcl_file
			
		= write_type_info_loop 0 s_unboxed_array tcl_file
	where 
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
		write_type_info_loop i limit tcl_file
			| i == limit
				= tcl_file
			# tcl_file
				= write_type_info unboxed_array.[i] tcl_file
			= write_type_info_loop (inc i) limit tcl_file
			
instance WriteTypeInfo [a] | WriteTypeInfo a
where
	write_type_info l tcl_file
		# tcl_file
			= fwritei (length l) tcl_file
		= write_type_info_loop l tcl_file
	where
		write_type_info_loop []	tcl_file
			= tcl_file
		write_type_info_loop [x:xs] tcl_file
			# tcl_file
				= write_type_info x tcl_file
			= write_type_info_loop xs tcl_file
			
instance WriteTypeInfo Char
where
	write_type_info c tcl_file
		# tcl_file
			= fwritec c tcl_file;
		= tcl_file;
443

444
445
446
447
448
449
450
451
452
453
454
// read
class ReadTypeInfo a
where
	read_type_info :: !*File -> (!Bool,a,!*File)
	
instance ReadTypeInfo CommonDefs
where
	read_type_info tcl_file
		# (ok1,com_type_defs,tcl_file)
			= read_type_info tcl_file
		# (ok2,com_cons_defs,tcl_file)
455
			= read_type_info tcl_file
456
457
458
459
460
461
462
463
464
465
466
467
			
		# common_defs
			= { CommonDefs |
				com_type_defs		= com_type_defs
			,	com_cons_defs		= com_cons_defs
			,	com_selector_defs	= {}
			,	com_class_defs		= {}
			,	com_member_defs		= {}
			,	com_instance_defs	= {}
			}
		= (ok1&&ok2,common_defs,tcl_file)

468
//1.3
469
instance ReadTypeInfo TypeDef TypeRhs
470
471
472
473
//3.1
/*2.0
instance ReadTypeInfo (TypeDef a) | ReadTypeInfo a & DefaultElem a
0.2*/
474
where
475
	read_type_info tcl_file	
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
		// td_name
		#! (ok1,td_name,tcl_file)
			= read_type_info tcl_file
		| F ("TypeDef '" +++ td_name.id_name +++ "'") not ok1
			= (False,default_elem,tcl_file)
			
		// td_arity
		#! (ok2,td_arity,tcl_file)
			= read_type_info tcl_file
		| not ok2
			= (False,default_elem,tcl_file)

		// td_args
		#! (ok2,td_args,tcl_file)
			= read_type_info tcl_file
		| not ok2
			= (False,default_elem,tcl_file)

494

495
496
497
498
499
		// td_rhs
		#! (ok2,td_rhs,tcl_file)
			= read_type_info tcl_file
		| not ok2
			= (False,default_elem,tcl_file)
500

501
502
	
		# type_def
503
			= updateTypeDefRhs { default_elem &
504
505
506
				td_name 	= td_name
			,	td_arity	= td_arity
			,	td_args		= td_args
507
			} td_rhs
508
509
		= (ok1,type_def,tcl_file)

510
511
512
513
updateTypeDefRhs :: (TypeDef a) a -> (TypeDef a)
updateTypeDefRhs type_def rhs
	=	{type_def & td_rhs = rhs}

514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
instance ReadTypeInfo TypeRhs
where
	read_type_info tcl_file
		# (ok1,c,tcl_file)
			= freadc tcl_file
		| not ok1
			= (False,default_elem,tcl_file)
			
		| c == AlgTypeCode
			# (ok,defined_symbols,tcl_file)
				= read_type_info tcl_file
			= (ok,AlgType defined_symbols,tcl_file)
			
		| c == SynTypeCode
			= (True,UnknownType,tcl_file)
		| c == RecordTypeCode
530
531
532
533
534
535
536
537
538
			# (ok,rt_fields,tcl_file)
				= read_type_info tcl_file
				
			# record_type
				= { default_elem &
					rt_fields	= rt_fields
				};
			= (True,RecordType record_type,tcl_file)
			
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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
		| c == AbstractTypeCode
			= (True,UnknownType,tcl_file)
			
instance ReadTypeInfo Priority
where
	read_type_info tcl_file
		# (ok1,p,tcl_file)
			= freadc tcl_file
		| not ok1
			= (False,default_elem,tcl_file)
			
		| p == PrioCode
			# (ok1,assoc,tcl_file)
				= read_type_info tcl_file
			# (ok2,i,tcl_file)
				= read_type_info tcl_file
			
			# prio
				= Prio assoc i
			= (ok1&&ok2,prio,tcl_file)	
			
		| p == NoPrioCode
			= (ok1,NoPrio,tcl_file)
			
instance ReadTypeInfo Assoc
where
	read_type_info tcl_file
		# (ok1,a,tcl_file)
			= freadc tcl_file
		| not ok1
			= (False,default_elem,tcl_file)	
			
		| a == LeftAssocCode
			= (ok1,LeftAssoc,tcl_file)
		| a == RightAssocCode
			= (ok1,RightAssoc,tcl_file)
		| a == NoAssocCode
			= (ok1,NoAssoc,tcl_file)
						
instance ReadTypeInfo DefinedSymbol
where
	read_type_info tcl_file
		// ds_ident
		# (ok1,ds_ident,tcl_file)
			= read_type_info tcl_file
		| not ok1
			= (False,default_elem,tcl_file)
			
		// ds_arity
		# (ok2,ds_arity,tcl_file)
			= read_type_info tcl_file
		| not ok2
			= (False,default_elem,tcl_file)
		
		// ds_index
		# (ok3,ds_index,tcl_file)
			= read_type_info tcl_file
		
		# defined_symbol
			= { default_elem &
				ds_ident	= ds_ident
			,	ds_arity	= ds_arity
			,	ds_index	= ds_index
			}
		= (ok3,defined_symbol,tcl_file)
		

instance ReadTypeInfo ConsDef 
where
	read_type_info tcl_file
		# (ok1,cons_symb,tcl_file)
			= read_type_info tcl_file
Martijn Vervoort's avatar
Martijn Vervoort committed
611
612
613
614
615
616
		  ok2 = True
		  cons_type = undef
//		# (ok2,cons_type,tcl_file)
//			= read_type_info tcl_file
			
		# (ok3,cons_arg_vars,tcl_file)
617
			= read_type_info tcl_file
Martijn Vervoort's avatar
Martijn Vervoort committed
618
		# (ok4,cons_priority,tcl_file)
619
620
			= read_type_info tcl_file

Martijn Vervoort's avatar
Martijn Vervoort committed
621
		# (ok5,cons_index,tcl_file)
622
			= read_type_info tcl_file
Martijn Vervoort's avatar
Martijn Vervoort committed
623
		# (ok6,cons_type_index,tcl_file)
624
			= read_type_info tcl_file
Martijn Vervoort's avatar
Martijn Vervoort committed
625
		# (ok7,cons_exi_vars,tcl_file)
626
627
628
629
630
			= read_type_info tcl_file
			
		# consdef
			= { default_elem &
				cons_symb			= cons_symb
Martijn Vervoort's avatar
Martijn Vervoort committed
631
			,	cons_type			= cons_type
632
633
634
635
636
637
638
			,	cons_arg_vars		= cons_arg_vars
			,	cons_priority		= cons_priority
			
			,	cons_index			= cons_index
			,	cons_type_index		= cons_type_index
			,	cons_exi_vars		= cons_exi_vars
			}
Martijn Vervoort's avatar
Martijn Vervoort committed
639
		= (ok1&&ok2&&ok3&&ok4&&ok5&&ok6&&ok7,consdef,tcl_file)
640
641
642
643
644
		
instance ReadTypeInfo Char
where
	read_type_info :: !*File -> (!Bool,Char,!*File)
	read_type_info tcl_file
645
		= freadc tcl_file
646
647
648
649
650
651
652
653
654
655

instance ReadTypeInfo Ident
where 
	read_type_info :: !*File -> (!Bool,Ident,!*File)
	read_type_info tcl_file
		# (ok1,i,tcl_file)
			= freadi tcl_file
		# (id_name,tcl_file)
			= freads tcl_file i;
		| F ("Ident " +++ toString i +++ " - " +++ id_name) True
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
700
701
702
703
704
705
706
707
		# ident
			= { default_elem &
				id_name		= id_name
			,	id_info		= nilPtr
			}
		= (ok1,ident,tcl_file)

instance ReadTypeInfo ATypeVar
where
	read_type_info tcl_file
		// atv_annotation
		# (ok1,atv_annotation,tcl_file)
			= read_type_info tcl_file
		| not ok1
			= (False,default_elem,tcl_file)
			
		// atv_variable
		# (ok2,atv_variable,tcl_file)
			= read_type_info tcl_file
		| not ok2
			= (False,default_elem,tcl_file)
			
		# atypevar
			= { default_elem &
				atv_annotation	= atv_annotation
			,	atv_variable	= atv_variable
			}
		= (True,atypevar,tcl_file)
		
instance ReadTypeInfo TypeVar
where
	read_type_info tcl_file
		# (ok1,tv_name,tcl_file)
			= read_type_info tcl_file
		
		# typevar
			= { default_elem &
				tv_name		= tv_name
			}
		= (ok1,typevar,tcl_file)
			
instance ReadTypeInfo Annotation
where
	read_type_info tcl_file
		#! (ok1,c,tcl_file)
			= freadc tcl_file
		
		# annotation
			= if (c == '!') AN_Strict AN_None
		= (ok1,annotation,tcl_file)
		
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
instance ReadTypeInfo FieldSymbol
where
	read_type_info tcl_file
		# (ok1,fs_name,tcl_file)
			= read_type_info tcl_file
		# (ok2,fs_var,tcl_file)	
			= read_type_info tcl_file
		# (ok3,fs_index,tcl_file)
			= read_type_info tcl_file
		
		# field_symbol
			= { FieldSymbol | 
				fs_name			= fs_name
			,	fs_var			= fs_var
			,	fs_index		= fs_index
			}
		= (ok1&&ok2&&ok3,field_symbol,tcl_file)		
Martijn Vervoort's avatar
Martijn Vervoort committed
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
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
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
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
	
instance ReadTypeInfo SymbolType
where 
	read_type_info tcl_file
		# (ok1,st_vars,tcl_file)
			= read_type_info tcl_file
		# (ok2,st_args,tcl_file)
			= read_type_info tcl_file
		# (ok3,st_arity,tcl_file)
			= read_type_info tcl_file
		# (ok4,st_result,tcl_file)
			= read_type_info tcl_file
			
		# symbol_type
			= { default_elem &
				st_vars			= st_vars
			,	st_args			= st_args
			,	st_arity		= st_arity
			,	st_result		= st_result
			}
		= (ok1&&ok2&&ok3&&ok4,symbol_type,tcl_file)
		
instance ReadTypeInfo AType
where 
	read_type_info tcl_file
		# (ok1,at_annotation,tcl_file)
			= read_type_info tcl_file
		# (ok2,at_type,tcl_file)
			= read_type_info tcl_file
			
		# atype
			= { default_elem &
				at_annotation	= at_annotation
			,	at_type			= at_type
			}
		= (ok1&&ok2,atype,tcl_file)
		
	
instance ReadTypeInfo Type
where 
	read_type_info tcl_file
		# (ok,c,tcl_file)
			= freadc tcl_file
		| not ok
			= (False,default_elem,tcl_file)
			
		| c == TypeTACode
			# (ok1,type_symb_ident,tcl_file)
				= read_type_info tcl_file
			# (ok2,atypes,tcl_file)
				= read_type_info tcl_file
			= (ok1&&ok2,TA type_symb_ident atypes,tcl_file)
			
		| c == TypeArrowCode
			# (ok1,atype1,tcl_file)
				= read_type_info tcl_file
			# (ok2,atype2,tcl_file)
				= read_type_info tcl_file
			= (ok1&&ok2,atype1 --> atype2,tcl_file)
			
		| c == TypeConsApplyCode
			# (ok1,cons_variable,tcl_file)
				= read_type_info tcl_file
			# (ok2,atypes,tcl_file)
				= read_type_info tcl_file
			= (ok1&&ok2,cons_variable :@: atypes,tcl_file)
			
		// TB BasicType
		| c == BT_IntCode
			= (True,TB BT_Int,tcl_file);
		| c == BT_CharCode
			= (True,TB BT_Char,tcl_file);
		| c == BT_RealCode
			= (True,TB BT_Real,tcl_file);
		| c == BT_BoolCode
			= (True,TB BT_Bool,tcl_file);
		| c == BT_DynamicCode
			= (True,TB BT_Dynamic,tcl_file);
		| c == BT_FileCode
			= (True,TB BT_File,tcl_file);
		| c == BT_WorldCode
			= (True,TB BT_World,tcl_file);
		| c == BT_StringCode
			# (ok,type,tcl_file)
				= read_type_info tcl_file		
			= (ok,TB (BT_String type),tcl_file);
			
		| c == TypeGTVCode
			# (ok,type_var,tcl_file)
				= read_type_info tcl_file		
			= (ok,GTV type_var,tcl_file);
			
		| c == TypeTVCode
			# (ok,type_var,tcl_file)
				= read_type_info tcl_file		
			= (ok,TV type_var,tcl_file)
			
		| c == TypeTQVCode
			# (ok,type_var,tcl_file)
				= read_type_info tcl_file		
			= (ok,TQV type_var,tcl_file)
			
		| c == TypeTECode
			= (True,TE,tcl_file)
			
830
831
832
833
instance ReadTypeInfo ConsVariable
where
	read_type_info tcl_file
		= abort "instance ReadTypeInfo ConsVariable"
834
		
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
instance ReadTypeInfo TypeSymbIdent
where
	read_type_info tcl_file
		# (ok1,type_name,tcl_file)
			= read_type_info tcl_file
		# (ok2,type_arity,tcl_file)
			= read_type_info tcl_file
			
		# type_symb_ident
			= { default_elem &
				type_name	= type_name
			,	type_arity	= type_arity
			}
			
		= (ok1&&ok2,type_symb_ident,tcl_file)
					
851
852
853
854
855
// basic and structural write_type_info's
instance ReadTypeInfo Int
where 
	read_type_info :: !*File -> (!Bool,Int,!*File)
	read_type_info tcl_file
856
		= freadi tcl_file			
857
	
858
//1.3
859
instance ReadTypeInfo {#b} | ReadTypeInfo b & DefaultElem b & ArrayElem b
860
861
862
863
//3.1
/*2.0
instance ReadTypeInfo {#b} | ReadTypeInfo b & DefaultElem b & Array {#} b
0.2*/
864
865
866
867
868
869
870
871
872
873
874
875
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
where 
	read_type_info tcl_file

		# (ok,s_unboxed_array,tcl_file)
			= freadi tcl_file
		| F ("s_unboxed_array: " +++ toString s_unboxed_array) not ok
			= (False,{default_elem},tcl_file)
			
		# unboxed_array
			= { default_elem \\ i <- [1..s_unboxed_array] }
		= read_type_info_loop 0 s_unboxed_array tcl_file unboxed_array

	where
		read_type_info_loop i limit tcl_file unboxed_array
			| F ("  " +++ toString i) i == limit
				= (True,unboxed_array,tcl_file)
				
			# (ok,elem,tcl_file)
				= read_type_info tcl_file
			| not ok
				= (False,unboxed_array,tcl_file)
				
				= read_type_info_loop (inc i) limit tcl_file {unboxed_array & [i] = elem}
	
		
instance ReadTypeInfo [a] | ReadTypeInfo a
where
	read_type_info tcl_file
		# (ok1,limit,tcl_file)
			= freadi tcl_file
		| not ok1
			= (False,[],tcl_file)
			
		= read_type_info_loop 0 limit tcl_file []
	where
		read_type_info_loop i limit tcl_file elems
			| i == limit
				= (True,reverse elems,tcl_file)
				
			# (ok,elem,tcl_file)
				= read_type_info tcl_file
			| not ok 
				= (False,[],tcl_file)
				= read_type_info_loop (inc i) limit tcl_file [elem:elems]
	
// defaults
class DefaultElem a
where
	default_elem :: a
	
914
//1.3
915
instance DefaultElem (TypeDef TypeRhs)
916
917
918
919
//3.1
/*2.0
instance DefaultElem (TypeDef a) | DefaultElem a
0.2*/
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
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
where 	
	default_elem
	 	= { TypeDef |
	 		td_name			= default_elem
		,	td_index		= default_elem
		,	td_arity		= default_elem
		,	td_args			= default_elem
		,	td_attrs		= default_elem
		,	td_context		= default_elem
		,	td_rhs			= default_elem
		,	td_attribute	= default_elem
		,	td_pos			= default_elem
		}
		
instance DefaultElem Position
where
	default_elem
		= NoPos
		
instance DefaultElem TypeAttribute
where
	default_elem
		= TA_None
		
instance DefaultElem TypeRhs
where
	default_elem
		= UnknownType
		
instance DefaultElem ATypeVar
where
	default_elem
		= {	ATypeVar |
			atv_attribute		= TA_None
		,	atv_annotation		= AN_None
		,	atv_variable		= default_elem
		}	

instance DefaultElem TypeVar
where
	default_elem
		= { TypeVar |
			tv_name				= default_elem
		,	tv_info_ptr			= default_elem
		}
	
966
instance DefaultElem (Ptr a)
967
968
969
970
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
where
	default_elem
		= nilPtr
		
instance DefaultElem Ident
where 
	default_elem
		= { Ident |
		 	id_name		= {}
		,	id_info 	= default_elem
		}
		
		
instance DefaultElem TypeVarInfo
where
	default_elem
		= TVI_Empty
		
instance DefaultElem SymbolTableEntry
where
	default_elem
		= { SymbolTableEntry |
			ste_kind		= STE_Empty
		,	ste_index		= NoIndex
		,	ste_def_level	= NotALevel
		,	ste_previous	= abort "instance DefaultElem SymboltableEntry"
		}
		
instance DefaultElem [a]
where
	default_elem
		= []
		
instance DefaultElem Int
For faster browsing, not all history is shown. View entire blame