-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathprelude.alc
1809 lines (1657 loc) · 62.5 KB
/
prelude.alc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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
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
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
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
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
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
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
708
709
710
711
712
713
714
715
716
717
718
719
720
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
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
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
859
860
861
862
863
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
914
915
916
917
918
919
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
966
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
# FIXME: type of universe
# we need to wrap this, and wrap takes a type in star-10
# so we have to settle for star-9
let omega = 9
# FIXME: apparently variables aren't allowed here
#let universe = type_(omega, 1)
let universe = type_(9, 1)
# nor expressions
####let implicit-wrap = lambda_curry ((T : type_(omega + 1, 0)))
lambda (x : T)
wrap T x
let implicit-wrap = lambda_curry ((T : type_(10, 0)))
lambda (x : T)
wrap T x
let implicit-unwrap = lambda_implicit (T : type_(10, 0))
lambda (x : wrapped(T))
unwrap T x
let implicit-unstrict-wrap = lambda_curry ((T : type_(10, 0)))
lambda (x : T)
unstrict-wrap T x
let implicit-unstrict-unwrap = lambda_implicit (T : type_(10, 0))
lambda (x : unstrict-wrapped(T))
unstrict-unwrap T x
let tuple-of-implicit = lambda_implicit(T : type_(10, 0))
lambda_single (xs : T)
xs
let explicit-unwrap = unwrap
let wrap = implicit-wrap
let unwrap = implicit-unwrap
let unstrict-wrap = implicit-unstrict-wrap
let unstrict-unwrap = implicit-unstrict-unwrap
let host-bool-wrap = intrinsic "return terms.value.host_bool_type" : wrapped(host-type)
let host-string-wrap = intrinsic "return terms.value.host_string_type" : wrapped(host-type)
let host-syntax-wrap = intrinsic "return terms.host_syntax_type" : wrapped(host-type)
let host-environment-wrap = intrinsic "return terms.host_environment_type" : wrapped(host-type)
let host-goal-wrap = intrinsic "return terms.host_goal_type" : wrapped(host-type)
let host-inferrable-term-wrap = intrinsic "return terms.host_inferrable_term_type" : wrapped(host-type)
let host-checkable-term-wrap = intrinsic "return terms.host_checkable_term_type" : wrapped(host-type)
let host-lua-error-wrap = intrinsic "return terms.host_lua_error_type" : wrapped(host-type)
let host-bool = unwrap(host-bool-wrap)
let host-string = unwrap(host-string-wrap)
let host-syntax = unwrap(host-syntax-wrap)
let host-environment = unwrap(host-environment-wrap)
let host-goal = unwrap(host-goal-wrap)
let host-inferrable-term = unwrap(host-inferrable-term-wrap)
let host-checkable-term = unwrap(host-checkable-term-wrap)
let host-lua-error = unwrap(host-lua-error-wrap)
let srel =
lambda_implicit (U : type_(10, 0))
unwrap
intrinsic
""""
local string_array = terms_gen.declare_array(terms_gen.builtin_string)
return terms.value.closure(
"#srel-arg",
terms.typed_term.tuple_elim(
string_array("target"),
terms.typed_term.bound_variable(1),
1,
terms.typed_term.srel_type(
terms.typed_term.bound_variable(2)
)
),
terms.runtime_context()
)
:
wrapped (forall ((x : U)) -> (res : U))
let variance =
lambda_implicit (U : type_(10, 0))
unwrap
intrinsic
""""
local string_array = terms_gen.declare_array(terms_gen.builtin_string)
return terms.value.closure(
"#srel-arg",
terms.typed_term.tuple_elim(
string_array("target"),
terms.typed_term.bound_variable(1),
1,
terms.typed_term.variance_type(
terms.typed_term.bound_variable(2)
)
),
terms.runtime_context()
)
:
wrapped (forall ((x : U)) -> (res : U))
let subtyping = intrinsic "return evaluator.UniverseOmegaRelation" : srel(type_(9, 0))
let tuple-desc-relation = intrinsic "return evaluator.TupleDescRelation" : srel(type_(9, 0))
let _|_ =
lambda_implicit (U : type_(10, 0))
unwrap
intrinsic
""""
local string_array = terms_gen.declare_array(terms_gen.builtin_string)
return terms.value.closure(
"#union-args",
terms.typed_term.tuple_elim(
string_array("left", "right"),
terms.typed_term.bound_variable(1),
2,
terms.typed_term.union_type(
terms.typed_term.bound_variable(2),
terms.typed_term.bound_variable(3)
)
),
terms.runtime_context()
)
:
wrapped (forall (a : U, b : U) -> (res : U))
let _&_ =
lambda_implicit (U : type_(10, 0))
unwrap
intrinsic
""""
local string_array = terms_gen.declare_array(terms_gen.builtin_string)
return terms.value.closure(
"#intersection-args",
terms.typed_term.tuple_elim(
string_array("left", "right"),
terms.typed_term.bound_variable(1),
2,
terms.typed_term.intersection_type(
terms.typed_term.bound_variable(2),
terms.typed_term.bound_variable(3)
)
),
terms.runtime_context()
)
:
wrapped (forall (a : U, b : U) -> (res : U))
let covariant =
lambda_curry (U : type_(9, 1), a : U)
unwrap
intrinsic
""""
local string_array = terms_gen.declare_array(terms_gen.builtin_string)
return terms.value.closure(
"#covariant-args",
terms.typed_term.tuple_elim(
string_array("base"),
terms.typed_term.bound_variable(1),
1,
terms.typed_term.variance_cons(
terms.typed_term.literal(
terms.value.host_value(true)
),
terms.typed_term.bound_variable(2)
)
),
terms.runtime_context()
)
:
wrapped (forall ((rel : srel(a))) -> (res : variance(a)))
let contravariant =
lambda_curry (U : type_(9, 1), a : U)
unwrap
intrinsic
""""
local string_array = terms_gen.declare_array(terms_gen.builtin_string)
return terms.value.closure(
"#contravariant-args",
terms.typed_term.tuple_elim(
string_array("base"),
terms.typed_term.bound_variable(1),
1,
terms.typed_term.variance_cons(
terms.typed_term.literal(
terms.value.host_value(false)
),
terms.typed_term.bound_variable(2)
)
),
terms.runtime_context()
)
:
wrapped (forall ((rel : srel(a))) -> (res : variance(a)))
let host-family-sig-variances = lambda (signature : type_(1, 0))
let inner = intrinsic
""""
local typed_array = terms_gen.declare_array(terms.typed_term)
local value_array = terms_gen.declare_array(terms.value)
local string_array = terms_gen.declare_array(terms_gen.builtin_string)
local function convert_desc(desc)
local constructor, arg = desc:unwrap_enum_value()
if constructor == terms.DescCons.empty then
return desc
elseif constructor == terms.DescCons.cons then
local elements = arg:unwrap_tuple_value()
local next_desc, type_fun = elements[1], elements[2]
local convert_next = convert_desc(next_desc)
local convert_type =
terms.value.variance_type(
evaluator.apply_value(
type_fun,
terms.value.neutral(
terms.neutral_value.free(terms.free.unique{})
)
)
)
local convert_type_fun = terms.value.closure(
"#tuple-prefix",
terms.typed_term.literal(convert_type),
terms.runtime_context())
return terms.value.enum_value(
terms.DescCons.cons,
terms.value.tuple_value(
value_array(
convert_next, convert_type_fun
)
)
)
else
error "unknown tuple desc constructor"
end
end
local function convert_sig(sig)
local param_type, _, _, _ = sig:unwrap_pi()
local param_desc = param_type:unwrap_tuple_type()
return terms.value.tuple_type(convert_desc(param_desc))
end
return convert_sig
:
host-func-type ((signature : wrapped(type_(1, 0)))) -> ((srel_type : wrapped(type_(1, 0))))
let (srels-t) = inner(wrap(signature))
unwrap(srels-t)
let host-string-concat = intrinsic "return function(a, b) return a .. b end" :
host-func-type (a : host-string, b : host-string) -> ((c : host-string))
# TODO: now that we have effectful programs, work to switch this into using an effect to serialize the operations
# lmao bootstrapping problem
let host-unique-id-wrap = intrinsic
""""
local id = { name = "unique_id" }
evaluator.register_host_srel(id, evaluator.IndepTupleRelation())
return terms.value.host_user_defined_type(id, terms_gen.declare_array(terms.value)())
:
wrapped(host-type)
let host-unique-id = unwrap(host-unique-id-wrap)
let new-host-unique-id = lambda (name : host-string)
let source0 = "return { name = \""
let (source1) = host-string-concat(source0, name)
let (source2) = host-string-concat(source1, "\" }")
intrinsic source2 : host-unique-id
let new-host-type-family = lambda (unique-id : host-unique-id, signature : type_(1, 0), variance : host-family-sig-variances(signature))
let inner = intrinsic
""""
local typed_array = terms_gen.declare_array(terms.typed_term)
local string_array = terms_gen.declare_array(terms_gen.builtin_string)
local function length(desc, len)
len = len or 0
local constructor, arg = desc:unwrap_enum_value()
if constructor == terms.DescCons.empty then
return len
elseif constructor == terms.DescCons.cons then
local elements = arg:unwrap_tuple_value()
local next_desc = elements[1]
return length(next_desc, len + 1)
else
error("unknown tuple desc constructor")
end
end
local function new_host_type_family(unique_id, sig, variance)
local param_type, _, _, _ = sig:unwrap_pi()
local param_desc = param_type:unwrap_tuple_type()
local nparams = length(param_desc)
local variance_elems = variance:unwrap_tuple_value()
local variances = {}
for i, v in variance_elems:ipairs() do
variances[i] = v:unwrap_host_value()
end
local tunpack = table.unpack or unpack
local srel = evaluator.IndepTupleRelation(tunpack(variances))
evaluator.register_host_srel(unique_id, srel)
local params = typed_array()
local param_names = string_array()
for i = 1, nparams do
params:append(terms.typed_term.bound_variable(i + 1))
param_names:append("#type-family-A-" .. tostring(i))
end
local body = terms.typed_term.tuple_elim(
param_names,
terms.typed_term.bound_variable(1),
nparams,
terms.typed_term.host_user_defined_type_cons(
unique_id,
params
)
)
return terms.value.closure("#type-family-B", body, terms.runtime_context())
end
return new_host_type_family
:
host-func-type (
unique-id : host-unique-id,
signature_ : wrapped(type_(1, 0)),
variance_ : wrapped(host-family-sig-variances(unwrap(signature_))))
->
((family : wrapped(unwrap(signature_))))
let (family) = inner(unique-id, wrap(signature), wrap(variance))
unwrap(family)
let new-host-type = lambda (unique-id : host-unique-id)
let Tfam = new-host-type-family unique-id
forall () -> (T : host-type)
tuple-of-implicit()
Tfam()
let host-array-type = new-host-type-family new-host-unique-id("array")
forall ((T : host-type)) -> (T : host-type)
tuple-of-implicit covariant(subtyping)
let host-array-new = lambda (T : host-type)
let inner = intrinsic
""""
local function array_new()
return {}
end
return array_new
:
host-func-type () -> ((arr : host-array-type(T)))
let (arr) = inner()
arr
let host-array-set = lambda_implicit (T : host-type)
lambda (arr : host-array-type(T), index : host-number, elem : T)
let inner = intrinsic
""""
local function array_set(array, index, elem)
-- we have to clone because can't guarantee input array isn't reused
-- Yet. growth mindset.
cloned = {}
for i, v in ipairs(array) do
cloned[i] = v
end
cloned[index] = elem
return cloned
end
return array_set
:
host-func-type (arr : host-array-type(T), index : host-number, elem : T) -> ((arr : host-array-type(T)))
let (arr) = inner(arr, index, elem)
arr
let host-array-get = lambda_implicit (T : host-type)
lambda (arr : host-array-type(T), index : host-number)
let inner = intrinsic
""""
local function array_get(array, index)
return array[index]
end
return array_get
:
host-func-type (arr : host-array-type(T), index : host-number) -> ((elem : T))
let (elem) = inner(arr, index)
elem
let terms-gen-array = new-host-type(new-host-unique-id("terms-gen-array"))
let void =
unwrap
intrinsic
""""
local desc = terms.empty
local basetype = terms.value.enum_type(desc)
return basetype
:
wrapped type
let host-unit = new-host-type(new-host-unique-id("host-unit"))
let host-nil = intrinsic "return nil" : host-unit
let only-accept-host-tuples-inner-host =
intrinsic
""""
local function check_host_tuple(subject, consequent, alternate)
if subject:is_host_tuple_type() then
return consequent
else
return alternate
end
end
return check_host_tuple
:
host-func-type (subject : wrapped(type), consequent : wrapped(host-type), alternate : wrapped(host-type)) -> ((result : wrapped(host-type)))
let only-accept-host-tuples-inner =
lambda (subject : wrapped(type), consequent : host-type, alternate : host-type)
let (res) =
only-accept-host-tuples-inner-host
subject
wrap consequent
wrap alternate
unwrap res
let only-accept-host-tuples =
lambda (subject : wrapped(type))
only-accept-host-tuples-inner
subject
host-unit
wrapped void
let only-accept-prog-host-tuples-inner-host =
intrinsic
""""
local function check_prog_host_tuple(subject, consequent, alternate)
if not subject:is_program_type() then
return alternate
end
local effects, base = subject:unwrap_program_type()
if base:is_host_tuple_type() then
return consequent
else
return alternate
end
end
return check_prog_host_tuple
:
host-func-type (subject : wrapped(type), consequent : wrapped(host-type), alternate : wrapped(host-type)) -> ((result : wrapped(host-type)))
let only-accept-prog-host-tuples-inner =
lambda (subject : wrapped(type), consequent : host-type, alternate : host-type)
let (res) =
only-accept-prog-host-tuples-inner-host
subject
wrap consequent
wrap alternate
unwrap res
let only-accept-prog-host-tuples =
lambda (subject : wrapped(type))
only-accept-host-tuples-inner
subject
host-unit
wrapped void
let host-tuple-type-to-tuple-type-inner =
intrinsic
""""
local function host_tuple_to_tuple(host_tuple_type)
local desc = host_tuple_type:unwrap_host_tuple_type()
-- this conversion happens to work since the eliminator for host tuples and tuples is the same term
local newbasetype = terms.value.tuple_type(desc)
return newbasetype
end
return host_tuple_to_tuple
:
host-func-type (t : wrapped(type), valid : only-accept-host-tuples(t)) -> ((res : wrapped(type)))
let host-tuple-type-to-tuple-type =
lambda (t : wrapped(type), valid : only-accept-host-tuples(t))
let (res) = host-tuple-type-to-tuple-type-inner(t, valid)
res
let extract-prog-host-tuple-type-inner =
intrinsic
""""
return function(prog_type)
local effect, base = prog_type:unwrap_program_type()
return base
end
:
host-func-type (t : wrapped(type), valid : only-accept-prog-host-tuples(t)) -> (res : wrapped(type), valid : only-accept-host-tuples(res))
let rebuild-prog-type =
intrinsic
""""
return function(prog_type, valid, new_base)
local effect, base = prog_type:unwrap_program_type()
return terms.value.program_type(effect, new_base)
end
:
host-func-type (t : wrapped(type), valid : only-accept-prog-host-tuples(t), b : wrapped(type)) -> ((res : wrapped(type)))
let host-tuple-to-tuple-inner =
intrinsic
""""
return function(_type, _valid, val)
local elems = val:unwrap_host_tuple_value()
local vals = terms_gen.delcare_array(terms.value)()
for _, v in ipairs(elems) do
vals:append(terms.value.host_value(v))
end
return terms.value.tuple_value(vals)
end
:
host-func-type (
t : wrapped(type),
valid : only-accept-host-tuples(t),
val : wrapped(unwrap(t)))
->
((res : wrapped(unwrap(host-tuple-type-to-tuple-type(t, valid)))))
let host-tuple-to-tuple =
lambda (t : wrapped(type), valid : only-accept-host-tuples(t), val : unwrap(t))
let (res) = host-tuple-to-tuple-inner(t, valid, wrap(val))
unwrap(res)
let tuple-to-host-tuple-inner =
intrinsic
""""
return function(_type, _valid, val)
local elems = val:unwrap_tuple_value()
local leading = terms_gen.declare_array(terms_gen.any_lua_type)()
local stuck = false
local stuck_elem = nil
local trailing = terms_gen.declare_array(terms.value)()
for _, v in ipairs(elems) do
if stuck then
trailing:append(v)
elseif v:is_host_value() then
leading:append(v:unwrap_host_value())
elseif v:is_neutral() then
stuck, stuck_elem = true, v:unwrap_neutral()
else
error "found an element in a tuple being converted to host-tuple that was neither host nor neutral"
end
end
if not stuck then
return terms.value.host_tuple_value(leading)
else
return terms.value.neutral(terms.neutral_value.host_tuple_stuck(leading, stuck_elem, trailing))
end
end
:
host-func-type (
t : wrapped(type),
valid : only-accept-host-tuples(t),
val : wrapped(unwrap(host-tuple-type-to-tuple-type(t, valid))))
->
((res : wrapped(unwrap(t))))
let tuple-to-host-tuple =
lambda (t : wrapped(type), valid : only-accept-host-tuples(t), val : unwrap(host-tuple-type-to-tuple-type(t, valid)))
let (res) = tuple-to-host-tuple-inner(t, valid, wrap(val))
unwrap(res)
let only-accept-host-funcs-inner-host =
intrinsic
""""
local function check_host_func(subject, consequent, alternate)
if subject:is_host_function_type() then
local param, result, info = subject:unwrap_host_function_type()
if not info:is_result_info() then
error "stuck result info? broken function type?"
end
local info_inner = info:unwrap_result_info()
if info_inner.purity:is_pure() then
return consequent
else
return alternate
end
else
return alternate
end
end
return check_host_func
:
host-func-type (subject : wrapped(type), consequent : wrapped(host-type), alternate : wrapped(host-type)) -> ((result : wrapped(host-type)))
let only-accept-host-funcs-inner =
lambda (subject : wrapped(type), consequent : host-type, alternate : host-type)
let (res) =
only-accept-host-funcs-inner-host
subject
wrap consequent
wrap alternate
unwrap res
let only-accept-host-funcs =
lambda (subject : wrapped(type))
only-accept-host-funcs-inner
subject
host-unit
wrapped void
let only-accept-host-funcprogs-inner-host =
intrinsic
""""
local function check_host_func(subject, consequent, alternate)
if subject:is_host_function_type() then
local param, result, info = subject:unwrap_host_function_type()
if not info:is_result_info() then
error "stuck result info? broken function type?"
end
local info_inner = info:unwrap_result_info()
if info_inner.purity:is_effectful() then
return consequent
else
return alternate
end
else
return alternate
end
end
return check_host_func
:
host-func-type (subject : wrapped(type), consequent : wrapped(host-type), alternate : wrapped(host-type)) -> ((result : wrapped(host-type)))
let only-accept-host-funcprogs-inner =
lambda (subject : wrapped(type), consequent : host-type, alternate : host-type)
let (res) =
only-accept-host-funcprogs-inner-host
subject
wrap consequent
wrap alternate
unwrap res
let only-accept-host-funcprogs =
lambda (subject : wrapped(type))
only-accept-host-funcprogs-inner
subject
host-unit
wrapped void
let only-accept-funcs-inner-host =
intrinsic
""""
local function check_host_func(subject, consequent, alternate)
if subject:is_pi() then
return consequent
else
return alternate
end
end
return check_host_func
:
host-func-type (subject : wrapped(type), consequent : wrapped(host-type), alternate : wrapped(host-type)) -> ((result : wrapped(host-type)))
let only-accept-funcs-inner =
lambda (subject : wrapped(type), consequent : host-type, alternate : host-type)
let (res) =
only-accept-funcs-inner-host
subject
wrap consequent
wrap alternate
unwrap res
let only-accept-funcs =
lambda ((subject : wrapped(type)))
only-accept-funcs-inner
subject
host-unit
wrapped void
let get-host-func-arg-inner =
intrinsic
""""
local function get_host_func_arg(subject, valid)
local param_type, result_type, result_info = subject:unwrap_host_function_type()
return param_type, nil
end
return get_host_func_arg
:
host-func-type (subject : wrapped(type), valid : only-accept-host-funcs(subject)) -> (result : wrapped(type), valid : only-accept-host-tuples(result))
let get-host-funcprog-arg-inner =
intrinsic
""""
local function get_host_func_arg(subject, valid)
local param_type, result_type, result_info = subject:unwrap_host_function_type()
return param_type, nil
end
return get_host_func_arg
:
host-func-type (subject : wrapped(type), valid : only-accept-host-funcprogs(subject)) -> (result : wrapped(type), valid : only-accept-host-tuples(result))
let just-args =
lambda (subject : wrapped(type), valid : only-accept-host-funcs(subject))
let (result, valid) = get-host-func-arg-inner(subject, valid)
result
let func-result-info = new-host-type(new-host-unique-id("func-result-info"))
let get-host-func-result-info-inner =
intrinsic
""""
local function get_host_func_arg(subject, valid)
local param_type, result_type, result_info = subject:unwrap_host_function_type()
return result_info
end
return get_host_func_arg
:
host-func-type (subject : wrapped(type), valid : only-accept-host-funcs(subject)) -> ((result : wrapped(func-result-info)))
let set-func-result-info-inner =
intrinsic
""""
local function get_host_func_arg(subject, valid, new_result_info)
local param_type, param_info, result_type, result_info = subject:unwrap_pi()
return terms.value.pi(param_type, param_info, result_type, new_result_info)
end
return get_host_func_arg
:
host-func-type (subject : wrapped(type), valid : only-accept-funcs(subject), resinf : wrapped(func-result-info)) -> ((result : wrapped(type)))
let func-conv-res-type =
lambda (argtype : wrapped(type))
forall (arg : unwrap(argtype)) -> (res : wrapped(type), valid : only-accept-host-tuples(res))
let funcprog-conv-res-type =
lambda (argtype : wrapped(type))
forall (arg : unwrap(argtype)) -> (res : wrapped(type), valid : only-accept-prog-host-tuples(res))
let get-host-func-res-inner =
intrinsic
""""
local function get_host_func_res(subject, valid)
local param_type, result_type, result_info = subject:unwrap_host_function_type()
local typed_array = terms_gen.declare_array(terms.typed_term)
local tuple_build = terms.typed_term.tuple_cons(
typed_array(
terms.typed_term.host_wrap(
terms.typed_term.application(
terms.typed_term.bound_variable(1),
terms.typed_term.bound_variable(2)
)
),
terms.typed_term.literal(terms.value.host_value(nil))
)
)
local ctx = terms.runtime_context():append(result_type)
return terms.value.closure("#TEST-1", tuple_build, ctx)
end
return get_host_func_res
:
host-func-type (subject : wrapped(type), valid : only-accept-host-funcs(subject)) -> ((results : wrapped(func-conv-res-type(just-args(subject, valid)))))
let get-host-funcprog-res-inner =
intrinsic
""""
local function get_host_func_res(subject, valid)
local param_type, result_type, result_info = subject:unwrap_host_function_type()
local typed_array = terms_gen.declare_array(terms.typed_term)
local tuple_build = terms.typed_term.tuple_cons(
typed_array(
terms.typed_term.host_wrap(
terms.typed_term.application(
terms.typed_term.bound_variable(1),
terms.typed_term.bound_variable(2)
)
),
terms.typed_term.literal(terms.value.host_value(nil))
)
)
local ctx = terms.runtime_context():append(result_type)
return terms.value.closure("#TEST-1", tuple_build, ctx)
end
return get_host_func_res
:
host-func-type (subject : wrapped(type), valid : only-accept-host-funcprogs(subject)) -> ((results : wrapped(funcprog-conv-res-type(just-args(subject, valid)))))
let host-func-type-to-func-type =
lambda (T : type, valid : only-accept-host-funcs(wrap(T)))
let (oldargs oldargs-valid) = get-host-func-arg-inner(wrap(T), valid)
let (newargs) = host-tuple-type-to-tuple-type-inner(oldargs, oldargs-valid)
let (orig-results-wrapped) = get-host-func-res-inner(wrap(T), valid)
let orig-results = unwrap(orig-results-wrapped)
let (orig-result-info-wrapped) = get-host-func-result-info-inner(wrap(T), valid)
let new-results =
lambda (args : unwrap(newargs))
let ptuple = tuple-to-host-tuple(oldargs, oldargs-valid, args)
let (oldres oldres-valid) = apply(orig-results, ptuple)
let (newres) = host-tuple-type-to-tuple-type-inner(oldres, oldres-valid)
newres
let new-func-type = forall (x : unwrap(newargs)) -> (y : unwrap(new-results(x)))
let (final-func-type-wrapped) = set-func-result-info-inner(wrap(new-func-type), host-nil, orig-result-info-wrapped)
unwrap(final-func-type-wrapped)
let host-funcprog-type-to-funcprog-type =
lambda (T : type, valid : only-accept-host-funcprogs(wrap(T)))
let (oldargs oldargs-valid) = get-host-funcprog-arg-inner(wrap(T), valid)
let (newargs) = host-tuple-type-to-tuple-type-inner(oldargs, oldargs-valid)
let (orig-results-wrapped) = get-host-funcprog-res-inner(wrap(T), valid)
let orig-results = unwrap(orig-results-wrapped)
let (orig-result-info-wrapped) = get-host-func-result-info-inner(wrap(T), valid)
let new-results =
lambda (args : unwrap(newargs))
let ptuple = tuple-to-host-tuple(oldargs, oldargs-valid, args)
let (oldres oldres-valid) = apply(orig-results, ptuple)
let (extractres extractres-valid) = extract-prog-host-tuple-type-inner(oldres, oldres-valid)
let (newres) = host-tuple-type-to-tuple-type-inner(extractres, extractres-valid)
let (rebuilt) = rebuild-prog-type(oldres, oldres-valid, newres)
rebuilt
let new-func-type = forall (x : unwrap(newargs)) -> (y : unwrap(new-results(x)))
let (final-func-type-wrapped) = set-func-result-info-inner(wrap(new-func-type), host-nil, orig-result-info-wrapped)
unwrap(final-func-type-wrapped)
let func-to-host-func-inner =
intrinsic
""""
local tunpack = unpack or table.unpack
return function(_type, _valid, afn)
return function(...)
local args = table.pack(...)
local conv_args = terms_gen.declare_array(terms.value)()
for i = 1, args.n do
conv_args:append(terms.value.host_value(args[i]))
end
local res = evaluator.apply_value(afn, terms.value.tuple_value(conv_args))
if not res:is_tuple_value() then
error "alicorn function converted to native function has failed to create a real value"
end
local elems = {}
for i, v in res:unwrap_tuple_value():ipairs() do
elems[i] = v:unwrap_host_value()
end
return tunpack(elems)
end
end
:
host-func-type (T : wrapped(host-type), valid : only-accept-host-funcs(T), fn : wrapped(host-func-type-to-func-type(unwrap(T), valid))) -> ((res-fn : unwrap(T)))
let func-to-host-func =
lambda (T : host-type, valid : only-accept-host-funcs(wrap(T)), fn : host-func-type-to-func-type(T, valid))
let (res) = func-to-host-func-inner(wrap(T), valid, wrap(fn))
res #TODO figure out why `unwrap(res) here doesn't make a nice type error but fails
let funcprog-to-host-funcprog-inner =
intrinsic
""""
local tunpack = unpack or table.unpack
return function(_type, _valid, afn)
return function(...)
local args = {...}
local nargs = select("#", ...)
local conv_args = terms_gen.declare_array(terms.value)()
for i = 1, nargs do
conv_args:append(terms.value.host_value(args[i]))
end
local res = evaluator.execute_program(evaluator.apply_value(afn, terms.value.tuple_value(conv_args)))
if not res:is_tuple_value() then
print(res)
error "alicorn function converted to native function has failed to create a real value"
end
local elems = {}
for i, v in res:unwrap_tuple_value():ipairs() do
elems[i] = v:unwrap_host_value()
end
return tunpack(elems)
end
end
:
host-func-type (T : wrapped(host-type), valid : only-accept-host-funcprogs(T), fn : wrapped(host-funcprog-type-to-funcprog-type(unwrap(T), valid))) -> ((res-fn : unwrap(T)))
let funcprog-to-host-funcprog =
lambda (T : host-type, valid : only-accept-host-funcprogs(wrap(T)), fn : host-funcprog-type-to-funcprog-type(T, valid))
let (res) = funcprog-to-host-funcprog-inner(wrap(T), valid, wrap(fn))
res #TODO figure out why `unwrap(res) here doesn't make a nice type error but fails
let host-if-type =
lambda (T : type_(9, 0))
forall (subject : host-bool, consequent : T, alternate : T) -> (res : T)
let host-if =
lambda_implicit (T : type_(9, 0))
let inner =
intrinsic
""""
local typed = terms.typed_term
local string_array = terms_gen.declare_array(terms_gen.builtin_string)
return terms.value.closure(
"#host-if-param",
typed.tuple_elim(
string_array(
"#host-if-subject",
"#host-if-consequent",
"#host-if-alternate"
),
typed.bound_variable(1),
3,
typed.host_if(
typed.bound_variable(2),
typed.bound_variable(3),
typed.bound_variable(4)
)
),
terms.runtime_context()
)
:
wrapped(host-if-type(T))
unwrap(inner)
let tuple-desc-type-inner = intrinsic "return terms.value.tuple_desc_type" :
host-func-type ((U : unstrict-wrapped(universe))) -> ((T : unstrict-wrapped(host-type)))
let tuple-desc-type = lambda (U : universe)
let (T) = tuple-desc-type-inner(unstrict-wrap(U))
unstrict-unwrap(T)
let tuple-type-explicit = lambda (U : universe, desc : tuple-desc-type(U))
let inner = intrinsic "return terms.value.tuple_type" :
host-func-type ((desc : wrapped(tuple-desc-type(U)))) -> ((T : wrapped(U)))
let (T) = inner(wrap(desc))
unwrap(T)
let tuple-type = lambda_implicit (U : universe)
lambda (desc : tuple-desc-type(U))
let inner = intrinsic "return terms.value.tuple_type" :
host-func-type ((desc : wrapped(tuple-desc-type(U)))) -> ((T : wrapped(U)))
let (T) = inner(wrap(desc))
unwrap(T)
let host-tuple-type = lambda_implicit (U : universe)
lambda (desc : tuple-desc-type(U))
let inner = intrinsic "return terms.value.host_tuple_type" :
host-func-type ((desc : wrapped(tuple-desc-type(U)))) -> ((T : wrapped(host-type)))
let (T) = inner(wrap(desc))
unwrap(T)
let tuple-desc-empty = lambda (U : universe)
let T = tuple-desc-type(U)
let empty = intrinsic "return terms.empty" : wrapped(T)
unwrap(empty)
let tuple-desc-elem-explicit = lambda (
U : universe,
desc : tuple-desc-type(U),
elem : (forall (rest : tuple-type(desc)) -> (next : U)))
let inner = intrinsic "return terms.cons" :
host-func-type (
desc_ : wrapped(tuple-desc-type(U)), # aaaa shadowing
elem : (wrapped (forall (rest : tuple-type(desc)) -> (next : U))))
->
((T : wrapped(tuple-desc-type(U))))
let (T) =
inner
wrap(desc)
wrap(elem)
unwrap(T)
let tuple-desc-elem-implicit = lambda_curry ((U : universe))
lambda (desc : tuple-desc-type(U), elem : (forall (rest : tuple-type(desc)) -> (next : U)))
let inner = intrinsic "return terms.cons" :
host-func-type (
desc_ : wrapped(tuple-desc-type(U)), # aaaa shadowing
elem : (wrapped (forall (rest : tuple-type(desc)) -> (next : U))))
->
((T : wrapped(tuple-desc-type(U))))
let (T) =
inner
wrap(desc)
wrap(elem)
unwrap(T)
# FIXME: we're comparing metavariables against placeholders AGAIN AAAA
# let tuple-desc-elem-implicit2 = lambda_curry (U : universe, desc : tuple-desc-type(U))
# lambda (elem : (forall (rest : tuple-type(desc)) -> (next : U)))
# let inner = intrinsic "return terms.cons" :
# host-func-type (
# desc_ : wrapped(tuple-desc-type(U)), # aaaa shadowing
# elem : (wrapped (forall (rest : tuple-type(desc)) -> (next : U))))
# ->
# ((T : wrapped(tuple-desc-type(U))))
# let (T) =
# inner
# wrap(desc)
# wrap(elem)
# unwrap(T)
let tuple-of = lambda (U : universe, desc : tuple-desc-type(U))
lambda_single (t : tuple-type(desc))
t
let host-tuple-of = lambda (U : universe, desc : tuple-desc-type(U))
intrinsic "return function(...) return ... end" :
host-func-type (t : host-tuple-type(desc)) -> (t : host-tuple-type(desc))
let tuple-desc-singleton = lambda (U : universe, T : U)
tuple-desc-elem-explicit U
tuple-desc-empty U
lambda ()
T
# FIXME: ditto a few lines above
# let tuple-of-imp = lambda_implicit (U : universe)
# lambda (desc : tuple-desc-type(U))
# # ATTN: single parens here means bare lambda syntax
# lambda_single (t : tuple-type(desc))
# t
# let host-tuple-of-imp = lambda_implicit (U : universe)
# lambda (desc : tuple-desc-type(U))
# intrinsic "return function(...) return ... end" :
# host-func-type (t : host-tuple-type(desc)) -> (t : host-tuple-type(desc))
let tuple-desc-singleton = lambda (U : universe, T : U)
tuple-desc-elem-explicit U
tuple-desc-empty U
lambda ()
T
let tuple-desc-concat = lambda (U : universe, head : tuple-desc-type(U), tail : tuple-desc-type(U))
# woah huge intrinsic
let inner = intrinsic