-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsire-in-sire.sire
2130 lines (1812 loc) · 65.7 KB
/
sire-in-sire.sire
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
#### sire_00_boot
; Copyright 2023 The Plunder Authors
; Use of this source code is governed by a BSD-style license that can be
; found in the LICENSE file.
;;; Primitives ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
= (Pin i) | ##0 i
= (Law n a b) | ##1 n a b
= (Inc m) | ##2 m
= (Case p l a z m o) | ##3 p l a z m o
= (Die x) | ##die x ; Calling a primop above 3 is a crash.
;;; Helpful Wrappers around the Primitives ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
= (PlanCase p l a n x) | Case p l a n _&n x
= (NatCase z p x) | Case _&z (_ _ _)&z (_ _)&z z p x
= (Force x) | Law 0 1 0-x 0
= (Seq x y) | NatCase y _&y x
= (DeepSeq x y) | Seq (Force x) y
= (Trace x y) | DeepSeq x y
= (DeepTrace x y) | DeepSeq x y
= (IsPin x) | PlanCase _&1 (_ _ _)&0 (_ _)&0 0 x
= (IsLaw x) | PlanCase _&0 (_ _ _)&1 (_ _)&0 0 x
= (IsApp x) | PlanCase _&0 (_ _ _)&0 (_ _)&1 0 x
= (IsNat x) | PlanCase _&0 (_ _ _)&0 (_ _)&0 1 x
= (PlanTag x) | PlanCase _&0 (_ _ _)&1 (_ _)&2 3 x
= (PinItem x) | PlanCase i&i (_ _ _)&0 (_ _)&0 0 x
= (LawName x) | PlanCase _&0 (i _ _)&i (_ _)&0 0 x
= (LawArgs x) | PlanCase _&0 (_ i _)&i (_ _)&0 0 x
= (LawBody x) | PlanCase _&0 (_ _ i)&i (_ _)&0 0 x
= (Car x) | PlanCase _&(##0) (n a _)&(##1 n a) (h _)&h 0 x
= (Cdr x) | PlanCase i&i (_ _ b)&b (_ t)&t 0 x
= (Eqz x) | Case _&0 (_ _ _)&0 (_ _)&0 1 _&0 x
= (Eq1 x) | NatCase 0 Eqz x
= (Eq2 x) | NatCase 0 Eq1 x
= (Strict n x) | NatCase x m&(Seq x Strict-m) n
= (traceId x) | Trace x x
= (deepTraceId x) | DeepTrace x x
= (**traced tag x) | Trace (0 tag x) x
= dTrk | DeepTrace
= trk | Trace
;;; Booleans ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(TRUE = 1)(FALSE = 0)
= (If x t e) | NatCase t _&e (Eqz x)
= (Ifz x t e) | If x e t
= (Not x) | If x 0 1
= (Bit x) | If x 1 0
= (And x y) | If x y x
= (Or x y) | If x x y
= (Xor x y) | If x (Not y) y
= (Nand x y) | If x (Not y) 1
= (Nor x y) | If x 0 (Not y)
= (Xnor x y) | If x y (Not y)
= (**else x) | x
;;; Nats ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
= (Nat x) | NatCase 0 Inc x
= (Dec x) | NatCase 0 i&i x
= (Times f z x) | NatCase z (Times f f-z) x
= (Add x y) | Times Inc (Nat x) y
= (Mul x y) | Times (Add x) 0 y
= (Sub x y) | Times Dec (Nat x) y
= (Pow b p) | Times (Mul b) 1 p
= (Bex p) | Pow 2 p
;;; Comparing Nats ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(LT = 0)(EQ = 1)(GT = 2) ; data Ord = LT | EQ | GT
= (OrdWeld x y) | If (Eq1 x) y x
= (natLte x y) | Not (Sub x y)
= (natLth x y) | natLte Inc-x y
= (natGte x y) | natLte y x
= (natGth x y) | natLth y x
= (natEql x y) | And natLte-y-x natLte-x-y
= (natMin x y) | If natLte-x-y x y
= (natMax x y) | If natGte-x-y x y
= (natCmp x y) | natMin 2 (Sub Inc-x y)
;;; Complex Nat Operations ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
= (Div x y) | If (natLth x y) 0 Inc-(Div (Sub x y) y)
= (Mod x y) | Sub x (Mul y | Div x y)
= (DivCeil n m) | Div (Add n | Dec m) m
= (DivMod x y) | 0 (Div x y) (Mod x y)
= (Lsh v n) | Mul (Bex n) v
= (Rsh v n) | Div v (Bex n)
= (Trunc w n) | Mod n Bex-w
= (BitSlice o w n) | Trunc w (Rsh n o)
= (BitIx i n) | BitSlice i 1 n
= (BitSz n) | And n | Inc BitSz-(Div n 2)
= (PopCount n) | And n | Add (Mod n 2) PopCount-(Div n 2)
= (BitSet i n) | If (BitIx i n) n (Add Bex-i n)
= (BitClear i n) | Ifz (BitIx i n) n (Sub n Bex-i)
;;; Arrays ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
= (Sz v) | And IsApp-v Inc-(Sz Car-v)
= (bruh r i) | Cdr (Times Car r i)
= (Br i r f) | NatCase f (bruh r) (Sub Sz-r i)
= (Ix i r) | Br i r 0
= (opp v r i) | Ifz i (Car r v) (opp v Car-r Dec-i Cdr-r)
= (Up i v r) | NatCase r (opp v r) (Sub Sz-r i)
= (Hd x) | Ifz IsApp-x x Hd-(Car x)
= (Last xs) | And IsApp-xs Cdr-xs
= (Null x) | Not IsApp-x
;;; Ordering ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(OrdTag x)=(PlanCase _&1 (_ _ _)&2 (_ _)&3 0 x)
= (Cmp x y)
@ ox OrdTag-x
| OrdWeld (natCmp ox OrdTag-y)
@ natCase | natCmp x y
@ pinCase | Cmp PinItem-x PinItem-y
@ appCase | OrdWeld (Cmp Car-x Car-y) (Cmp Cdr-x Cdr-y)
@ lawCase | OrdWeld (natCmp LawName-x LawName-y)
| OrdWeld (natCmp LawArgs-x LawArgs-y)
(Cmp LawBody-x LawBody-y)
| Ix ox (0 natCase pinCase lawCase appCase)
= (Lth x y) | Eqz (Cmp x y)
= (Eql x y) | Eq1 (Cmp x y)
= (Gth x y) | Eq2 (Cmp x y)
= (Neq x y) | Not (Eql x y)
= (Lte x y) | Not (Gth x y)
= (Gte x y) | Not (Lth x y)
= (Min x y) | If (Lth x y) x y
= (Max x y) | If (Gth x y) x y
;;; Linear Search, Switch ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
= (Find f x)
^ _ 0 Sz-x
? (go i rem)
| Ifz rem i
| If f-(Ix i x) i
| go Inc-i Dec-rem
= (FindEq e x) | Find Eql-e x
= (Any f x) | Neq Sz-x (Find f x)
= (Has e x) | Neq Sz-x (FindEq e x)
= (Switch ks k bs fb) | Br (FindEq k ks) bs fb
;;; Binary Search ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
= (Search key row stride low end) ; Any > Row Any > Nat > Nat > Nat > Nat
@ mid | Div (Add low end) 2
@ ix | Mul stride mid
| If (Gte low end) (Mul ix 2)
| Ix | Cmp key (Ix ix row)
| 0
(Search key row stride low mid) ; LT
(Inc | Mul ix 2) ; EQ
(Search key row stride Inc-mid end) ; GT
= (searchSet key row) | Search key row 1 0 Sz-row
= (searchTab key row) | Search key row 2 0 (Div Sz-row 2)
;;; Folds ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
= (foldr f z row)
^ _ 0 Sz-row
? (go i rem)
| Ifz rem z
| f (Ix i row)
| go (Inc i) (Dec rem)
= (foldri f z row)
^ _ 0 Sz-row
? (go i rem)
| Ifz rem z
| f i (Ix i row)
| go (Inc i) (Dec rem)
= (foldl f z row)
^ _ z 0 Sz-row
? (go acc i rem)
| Seq i
| Seq acc
| Ifz rem acc
| go (f acc | Ix i row) (Inc i) (Dec rem)
;;; Constructing and Combining Rows ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
= (Gen n f) ;; TODO: Should row functions take an explicit head?
^ _ Nat-n ;; Right now they just always use 0 for the head.
? (go i)
| And i (go Dec-i | f Dec-i)
= (Weld x y)
@ xw | Sz x
@ yw | Sz y
| Gen (Add xw yw)
& i
| If (Lth i xw) (Ix i x)
| Ix (Sub i xw) y
= (Insert ix val row)
| Gen (Inc Sz-row)
& i
| Ix (Cmp i ix)
| 0 (Ix i row) val (Ix Dec-i row)
= (Splice at new old)
| Gen (Add Sz-new Sz-old)
& i
@ j | Sub i at
@ newSz | Sz new
| If (Lth i at) (Ix i old)
| If (Lth j newSz) (Ix j new)
| Ix (Sub i newSz) old
= (Map f v) | Gen Sz-v x&(f | Ix x v)
= (Rev row) | (wid @ Sz row)(Gen wid i&(Ix (Sub wid Inc-i) row))
= fst | Ix 0
= snd | Ix 1
= thr | Ix 2
= (Cons x xs) | Weld (0 x) xs
= (Snoc xs x) | Weld xs (0 x)
= (**put r i v) | Up i v r
= (**get r i) | Ix i r
= (**foreach x f) | Map f x
= (Rep i n) | Gen n _&i
= (rowAnd v) | foldr And TRUE v
= (sum v) | foldl Add 0 v
= (all f v) | rowAnd (Map f v)
= (Cat vs) | foldl Weld 0 vs
= (CatMap f r) | Cat (Map f r)
= (ZipWith f a b) | Gen (Min Sz-a Sz-b) i&(f Ix-i-a Ix-i-b)
= (Zip a b) | ZipWith 0 a b
= (Slash v s e) | Gen (Sub e s) i&(get v | Add s i)
= (Slice v s e) | Slash v s (Min e | Sz v)
= (Drop n v) | Slice v n (Sz v)
= (Take n v) | Slice v 0 n
;;; Filling Arrays from Lists ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(FillR f l)=(Ifz l f | FillR f snd-l fst-l)
= (Fill f l)
^ _ l f
? (go l acc)
| Ifz l acc
| go snd-l (acc fst-l)
;;; Function Arities ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(OpArity i)=(Br i (0 1 3 1 6) 1)
= (Arity x)
@ p | i&(If IsNat-i OpArity-i Arity-i)
@ l | (_ a _)&a
@ a | (f _)&(Dec (Arity f))
@ n 0
| PlanCase p l a n x
;;; Rex Trees ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(c tag)=(tag ##rex)
(WORD = c-{WORD})(TEXT = c-{TEXT})(LINE = c-{LINE})(OPEN = c-{OPEN})
(NEST = c-{NEST})(INFX = c-{INFX})(PREF = c-{PREF})(SHUT = c-{SHUT})
(EMBD = c-{EMBD})
= (rexRune x) | If (Neq 4 | Sz x) 0 | Ix 1 x
= (rexSetRune r x) | If (Neq 4 | Sz x) x | Up 1 r x
= (rexHeir x) @ i (Dec Sz-x) | Ifz Dec-i 0 | Ix i x
= (rexSetHeir h x) @ i (Dec Sz-x) | Ifz Dec-i x | Up i h x
= (rexText x) | If (Neq 3 | Sz x) 0 | Ix 1 x
= (rexSetText t x) | If (Neq 3 | Sz x) x | Up 1 t x
= (rexSons x) | If (Neq 4 | Sz x) 0 | Ix 2 x
= (rexSetSons s x) | If (Neq 4 | Sz x) x | Up 2 s x
= (rexEmbd x) | If (Neq 2 | Sz x) 0 | Ix 1 x
= (rexIsEmbd rex) | Eql 2 Sz-rex
= (rexIsLeaf rex) | Eql 3 Sz-rex
= (rexIsNode rex) | Eql 4 Sz-rex
= (rexType rex) | Ix (Sub Sz-rex 2) (0 {EMBD} {LEAF} {NODE})
= rexStyle | Hd
= (**rexOpen rex cb)
@ type | rexType rex
@ style | rexStyle rex
@ rune | rexRune rex
@ text | rexText rex
@ embd | rexEmbd rex
@ sons | rexSons rex
@ nSon | Sz sons
@ heir | rexHeir rex
@ kids | Ifz heir sons (Snoc sons heir)
@ nKid | Sz kids
| **cb type style rune text embd sons nSon heir kids nKid
= (rexKids rex)
@ sons (rexSons rex)
@ heir (rexHeir rex)
| Ifz heir sons (Snoc sons heir)
;;; Convenience Macros ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
= (varE v) | WORD v 0
= (txtE t) | TEXT t 0
= (ctxE nm) | Ifz nm (**txtE nm) (**varE nm)
= (sireErrFmt renderLineNum ss rex msg)
@ ctx (Ix 1 ss)
@ ln (Ix 4 ss)
@ blk (Ix 5 ss)
| OPEN "#" | 0 varE-{block} | blk
| OPEN "#" | 0 varE-{what} | rex
| OPEN "#" | 0 varE-{where} | SHUT {:} (0 ctxE-ctx renderLineNum-ln) 0
| OPEN "#" | 0 varE-{why} | txtE-msg
| 0
= (appE exps) | If (Eq1 Sz-exps) (fst exps) | NEST {#|} exps 0
= (rowE xs) | If Null-xs (EMBD 0) | appE (Cons EMBD-0 xs)
= (sireErr ss rex msg) | ##SireError (sireErrFmt EMBD ss rex msg)
= ({'} ss rex)
@ args (rexKids rex)
| If (Neq 1 Sz-args) | sireErr ss rex {Expected 1 Parameter}
| 0 ss EMBD-(fst args)
({,} st rex)=(0 st rowE-(rexKids rex))
= ({++} ss rex)
^ (ss, rowE (_ 0 rex))
? (go acc rex)
@ sons | rexSons rex
@ rune | rexRune rex
@ itemRex | If (Eql 1 Sz-sons) (fst sons) (OPEN {|} sons 0)
| Ifz rex | acc
| If (Neq {++} rune) | acc rex
| If (Null sons) | sireErr ss rex {usage: (++ x), (++ f x y), etc}
| else | go (acc itemRex) rexHeir-rex
= (binop val ss rex)
@ kids (rexKids rex)
| If (Neq 2 | Sz kids) | sireErr ss rex {this is a binary operator}
| (ss, appE (EMBD val, fst kids, snd kids))
({&&} = binop And)({||} = binop Or)({::} = binop 0)
({==} = binop Eql)({/=} = binop Neq)
; TODO: Use != instead of /=. /= is ugly when used tight-infix
= ({:} ss rex)
@ sons | rexSons rex
@ args | Take Dec-(Sz sons) sons
@ apps | Last sons
@ body | rexHeir rex
| If | Or Eqz-body | Or (Lth Sz-sons 2) | (Neq "<" rexRune-apps)
| sireErr ss rex {Invalid use of :}
^ (ss, _)
| OPEN "|" (rexKids apps)
| OPEN "&" ,(NEST "|" args 0)
| body
;;; Row Traversal ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
= (**openPair x k) | **k (Ix 0 x) (Ix 1 x)
= (**openTriple x k) | **k (Ix 0 x) (Ix 1 x) (Ix 2 x)
= (mapState f row st)
^ foldl _ [st 0] row
& (st_acc x)
: st acc < openPair st_acc
: st x < openPair (f x st)
| (st, acc x)
;;; Maybe, List, Either ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(NONE = 0)(SOME = 0)(NIL = 0)(CONS = 0)(LEFT = 0)(RIGHT = 1)
= (**maybeCase mb non som) | Ifz mb non (**som Cdr-mb)
= (**listCase xs nil cons) | Ifz xs nil (**cons Ix-0-xs Ix-1-xs)
= (**eitherCase x l r) | If Hd-x (**r fst-x) (**l fst-x)
= (listFoldl f z l)
: x xs < listCase l z
@ fzx (f z x)
| Seq fzx
| listFoldl f fzx xs
= (listZipWith f al bl)
: a as < listCase al NIL
: b bs < listCase bl NIL
| CONS (f a b) (listZipWith f as bs)
= (fmapMaybe x f) | And x | SOME | f | fst x
= (listFoldr f z l) | listCase l z (x xs)&(f x | listFoldr f z xs)
= (listSing x) | CONS x 0
= (listMap f l) | listFoldr (x xs)&(CONS f-x xs) NIL l
= (**listForEach l f) | listMap f l
= (listIdx i l) | Ifz i fst-l (listIdx Dec-i snd-l)
= (listUnsafeLast l) | (xs @ snd l)(Ifz xs fst-l | listUnsafeLast xs)
= (listLen l) | listFoldr (x acc & Inc acc) 0 l
= (listFromRow v) | foldr 0 NIL v
= (listOr v) | listFoldr Or 0 v
= (listAny f v) | listOr listMap-f-v
= (listHas e xs) | listAny Eql-e xs
= (listEnumFrom n) | CONS n (listEnumFrom Inc-n)
= (listWeld a b) | listCase a b (x xs)&(CONS x | listWeld xs b)
= (listCat ls) | listFoldr listWeld NIL ls
= (listCatMap f r) | listCat (listMap f r)
= (listZip a b) | listZipWith 0 a b
= (listFilter f lis) | listFoldr (x xs)&(Ifz f-x xs | CONS x xs) 0 lis
= (listGenFrom i n f) | And (Lth i n) | CONS f-i | listGenFrom-(Inc i) n f
= (listGen n f) | listGenFrom 0 n f
= (listRep i n) | listGen n _&i
= (listIndexed l) | listZip (listEnumFrom 0) l
= (listRev xs) | listFoldl (x y & CONS y x) NIL xs
= (listSnoc xs e) | listCase xs (CONS e NIL) (x xs)&(CONS x | listSnoc xs e)
= (listFindIndex pred xs notFound found)
^ listFoldr _ notFound (listIndexed xs)
& (idxVal rest)
| Ifz (pred | snd idxVal) rest
| found (fst idxVal)
;;; Binary Search Trees, Dictionaries ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
= (dictSearchCase key table notFound found)
@ res | searchTab key table
@ ix | Div res 2
| Ifz (Mod res 2) notFound
| Seq ix
| found ix (Ix Inc-ix table)
= bstEmpty | 0
= (**bstSing k v) | [k v 0 0]
= (**bstCase x empty node)
| Ifz x empty
| **node (Ix 0 x) (Ix 1 x) (Ix 2 x) (Ix 3 x)
= (bstWalk x)
: _ _ l r < bstCase x NIL
| listWeld bstWalk-l x::(bstWalk r)
= (bstSearch k x)
: xk xv l r < bstCase x NONE
| Ifz x NONE
@ LT | bstSearch k l
@ EQ | SOME xv
@ GT | bstSearch k r
| Br (Cmp k xk) [LT EQ] GT
(**bstSearchCase k t nf f)=(maybeCase (bstSearch k t) nf f)
= (merge x y)
: xk xv xl xr < bstCase x y
: yk yv yl yr < bstCase y x
| [yk yv (merge x yl) yr]
= (bstAlter k f x)
: xk xv l r < bstCase x (maybeCase (f NONE) 0 (bstSing k))
@ LT | [xk xv (bstAlter k f l) r]
@ EQ | maybeCase (f SOME-xv) (merge l r) nv&[k nv l r]
@ GT | [xk xv l (bstAlter k f r)]
| Br (Cmp k xk) [LT EQ] GT
= (bstLoad table)
^ _ 0 (Div (Sz table) 2)
? (go off end)
@ wid | Sub end off
@ zeroCase | bstEmpty
@ oneCase @ i (Mul 2 off) | bstSing (Ix i table) (Ix Inc-i table)
| Br wid [zeroCase oneCase]
@ mid | Add off (Div wid 2)
@ i | Mul 2 mid
@ k | Ix i table
@ v | Ix Inc-i table
@ l | go off mid
@ r | go Inc-mid end
| [k v l r]
(bstSave x)=(Fill 0 | listCatMap kv&(fst kv :: (snd kv :: NIL)) bstWalk-x)
= (bstIns k v t) | bstAlter k (_ & SOME v) t
= (bstPut t k v) | bstAlter k (_ & SOME v) t
= (bstHas k t) | IsApp (bstSearch k t)
= (bstIdx k t) | bstSearchCase k t 0 a&a
= bstIsEmpty | Eqz
= (bstFromPairsList xs) | listFoldl (t kv & bstIns fst-kv snd-kv t) 0 xs
= (bstUnion x y) | listFoldl (t kv & bstIns fst-kv snd-kv t) y bstWalk-x
;;; Jets for Filling Nats from Nat-Slices ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
= (PadLen n) | Dec (BitSz n)
= (unpackSlice s) | If IsNat-s (0, PadLen s, s) s
= (BitFill ss)
^ fst (foldl _ [0 0] ss)
& (data_sz slice)
: data sz < openPair data_sz
: so ss sd < openTriple (unpackSlice slice)
@ newSz | Add sz ss
@ newData | Add data (Lsh (BitSlice so ss sd) sz)
| Seq newSz
| Seq newData
| [newData newSz]
= (ByteFill ss)
^ BitFill (Map _ ss)
& bs
: off sz data < openTriple bs
| If IsNat-bs bs (Mul 8 off, Mul 8 sz, data)
= (BitFillList ss) | BitFill (Fill 0 ss)
= (ByteFillList ss) | ByteFill (Fill 0 ss)
;;; Strings ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
= newlineChar | 10
= (isDigit c) | And (Gte c {0}) (Lte c {9})
= (isUpper c) | And (Gte c {A}) (Lte c {Z})
= (isLower c) | And (Gte c {a}) (Lte c {z})
= (isAlpha c) | Or isUpper-c isLower-c
= (StrFoldl f z s)
| Ifz s z
| Seq z
@ z (f z (Mod s 256))
| StrFoldl f z (Div s 256)
= (StrFoldr f z s)
| Ifz s z
| f (Mod s 256)
| StrFoldr f z (Div s 256)
= (ByteSz s) | DivCeil BitSz-s 8
= (StrPad s) | BitSet (Mul 8 ByteSz-s) s
= (strCat vs) | ByteFill (Map StrPad vs) ; s/StrPad/n&[0 ByteSz-n n]/
= (strWeld x y) | strCat [x y]
= (StrAny f s) | StrFoldr (c k & Or (f c) k) 0 s
= (StrAll f s) | StrFoldr (c k & And (f c) k) 1 s
= (StrHas c s) | StrAny (Eql c) s
= (ByteIx i n)
| Ifz i (Mod n 256)
| And n
| ByteIx (Dec i) (Div n 256)
= (strFindIndexOff f off str)
@ wid (ByteSz str)
^ _ off
? (loop ix)
| If (Gte ix wid) wid
| If (f (ByteIx ix str)) ix
| loop (Inc ix)
= (strElemIndexOff byte off bar) | strFindIndexOff (Eql byte) off bar
= (ByteSlice off wid n) | BitSlice (Mul 8 off) (Mul 8 wid) n
= (ByteTake wid n) | Trunc (Mul 8 wid) n
= (ByteDrop wid n) | Rsh n (Mul 8 wid)
;;; Keywords, Nicer Error Messages ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
= (listDigits num)
| Ifz num {0}::NIL
^ _ num NIL
? (loop mor acc)
| Ifz mor acc
: mor digit < openPair (DivMod mor 10)
| loop mor (Add digit {0})::acc
= (digits num) | Fill 0 listDigits-num
= (showNat n) | strCat digits-n
= (natE n) | WORD (showNat n) 0
= (renderLnNum ln) | **varE (showNat ln)
= (sireErr ss r msg) | ##SireError (sireErrFmt renderLnNum ss r msg)
= (**gensym st k)
@ nex | Ix 0 st
@ aft | Inc nex
@ st | Up 0 aft st
@ nm | varE (strWeld {_g} showNat-nex)
| Strict 3 aft st nm
| **k st nm
(bloodline lis)=(listFoldr (i r & rexSetHeir r i) 0 lis) ; List Rex > Rex
= ({#} ss rex)
: _ _ _ text _ _ _ _ kids nKid < rexOpen rex
@ k1 | fst kids
@ text | rexText k1
| Ifz nKid | sireErr ss rex } Needs kids>=1
| Ifz text | sireErr ss k1 } needs to be text
@ name | strWeld {#} text
@ bindPin | bstIdx name (Ix 2 ss)
@ macro | snd (PinItem bindPin)
| Ifz bindPin | sireErr ss rex (strWeld {undefined symbol: #} text)
| macro ss rex
;;; Sire Symbols ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
= (isSymbolChar c)
| Or Eql-{_}-c
| Or isAlpha-c isDigit-c
= (strIsSymbol str)
| And str
| And (| Not | isDigit | ByteIx 0 str)
| StrAll isSymbolChar str
= (readSymbol rex err ok)
@ rune (rexRune rex)
@ kids (rexKids rex)
@ nKid (Sz kids)
| If (Eql {.} rune)
| If (Neq 1 nKid)
| err rex {Should be .x, .5, .{x}, etc}
@ kid (Ix 0 kids)
| Ifz (rexIsLeaf kid)
| err rex {Should be .x, .5, .{x}, etc}
| If (Eql {WORD} | rexStyle kid)
| err rex {TODO: readSymbol should support .foo and .234}
| ok (rexText kid)
: _ style _ text _ sons nSon heir _ _ < rexOpen rex
| If (Neq {WORD} style) | err rex {expected a bare word}
| If heir | err rex {unexpected heir}
| Ifz strIsSymbol-text | err rex {bad symobl character}
| ok text
(readSymbolEx ss rex)=(readSymbol rex (sireErr ss) (x & x))
;;; Destructuring Bind ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
= (expandPat ss pat val body)
: _ style rune _ _ sons nSon heir _ _ < rexOpen pat
: ss tmp < gensym ss
@ fail | (sireErr ss pat {invalid pattern})
| If heir fail
^ Switch [{!} {,} {@}] rune _ fail
++ ; {!}
| If (Neq 1 nSon) fail
^ (ss, _)
| OPEN {#@} (tmp, val)
| OPEN {#|} (EMBD Seq, tmp)
| OPEN {@} (fst sons, tmp)
| body
++ ; {,}
^ (ss, OPEN {#@} (tmp, val) (foldri _ body sons))
& (i son heir)
| OPEN "@" (son, appE (EMBD Ix-i, tmp)) heir
++ ; {@}
| If (Neq 2 nSon) fail
: alias pat < openPair sons
| Seq (readSymbolEx ss alias)
^ (ss, _)
| OPEN {#@} (alias, val)
| OPEN {@} (pat, alias)
| body
= ({@} ss rex)
: _ style rune _ _ _ _ heir kids nKid < rexOpen rex
: pat val body < openTriple kids
: _ patStyle patRune _ _ _ _ patHeir _ _ < rexOpen pat
| If (nKid /= 3) | sireErr ss rex {expected three kids}
| If (Eql {WORD} patStyle)
| If patHeir | expandPat ss patHeir val body
| (ss, rexSetRune {#@} rex)
| expandPat ss pat val body
= (isPattern rex)
@ isStructBind | And (Eql {WORD} rexStyle-rex) Not-(Eqz rexHeir-rex)
| (isStructBind || Has rexRune-rex [{!} {,} {@}])
= (rebindPatternArgs ss args)
^ mapState _ args (ss, NIL)
& (son acc)
@ (ss, rebinds) acc
| Ifz isPattern-son (acc, son)
: ss newArgRex < gensym ss
| ((ss, CONS [son newArgRex] rebinds), newArgRex)
= (wutMacro wutRune ss rex)
@ kids@[sigRex bodyRex] | rexKids rex
@ sigRune | rexRune sigRex
@ sigSons | rexSons sigRex
| If (Neq 2 Sz-kids) | sireErr ss rex "bad lambda"
| If (Neq {|} sigRune) | sireErr ss rex "bad lambda"
| If rexHeir-sigRex | sireErr ss sigRex "unexpected heir"
| If (Lth sigSons 2) | sireErr ss rex "bad lambda"
@ fallback | (ss, rexSetRune wutRune rex)
@ ([ss rebinds], args) | rebindPatternArgs ss (Drop 1 sigSons)
| If (Null rebinds) | fallback
^ (ss, _)
| OPEN wutRune [(NEST {|} (Cons fst-sigSons args) 0)]
^ listFoldr _ bodyRex rebinds
& (rebind heir)
| OPEN "@" rebind heir
({?} = wutMacro {#?})({??} = wutMacro {#??})
= ({&} ss rex)
@ kids@[sigRex bodyRex] | rexKids rex
@ sigRune | rexRune sigRex
| If (Neq 2 Sz-kids) | sireErr ss rex "bad lambda"
| If rexHeir-sigRex | sireErr ss sigRex "unexpected heir"
@ fallback | (ss, rexSetRune "#&" rex)
| If (Eql {|} sigRune)
@ [[ss rebinds] args] (rebindPatternArgs ss rexSons-sigRex)
| If (Null rebinds) fallback
^ (ss, _)
| OPEN "#&" [(NEST {|} args 0)]
^ listFoldr _ bodyRex rebinds
& (rebind heir)
| OPEN "@" rebind heir
| If (isPattern sigRex)
: ss newArgRex < gensym ss
| 0 ss | OPEN "#&" [newArgRex]
| OPEN "@" [sigRex newArgRex]
| bodyRex
| fallback
= (unrollTis rex)
@ heir (rexHeir rex)
| Ifz heir | listSing rex
| If (Neq (rexRune heir) {=}) | listSing rex
| CONS (rexSetHeir 0 rex)
| unrollTis heir
= (parseDefine ss rex)
: _ _ _ _ _ _ _ heir kids@[sig val] nKid < rexOpen rex
: _ _ sigRune _ _ _ _ _ sigKids sigNKid < rexOpen sig
^ And (Eql "|" sigRune) | And (Gte sigNKid 2) | And (Eql nKid 2) | _
@ nmRex | fst sigKids
@ nm | If (Eql {**} rexRune-nmRex) (fst | rexSons nmRex) nmRex
| (nm, sig, val)
= ({=} ss rex)
^ 0 ss | bloodline | listMap _ | unrollTis rex
& rex
@ res@(nm, sig, body) (parseDefine ss rex)
| Ifz res (OPEN "#=" (rexKids rex) 0)
| OPEN {#=} (nm, OPEN {??} [sig body] 0) 0
;;; {#simpleswitch} ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
= (isSireDecimal str) | And str | StrAll isDigit str
= (loadSireDecimal s) | StrFoldl (acc c & Add (Mul 10 acc) (Sub c {0})) 0 s
= (readSimpleKey ss rex)
| If (Or rexHeir-rex Not-(rexIsLeaf rex))
| sireErr "not a key"
@ txt (rexText rex)
| If (Eql {WORD} rexStyle-rex && isSireDecimal txt)
| loadSireDecimal txt
| txt
= (readItems ss rex)
| Ifz rex NIL
| If ("-" /= rexRune rex) | sireErr ss rex {Expected a - rune}
| CONS (rexSetHeir 0 rex)
| readItems ss (rexHeir rex)
= (readBranches ss rex) ; Rex > Read (List (Any, Rex))
: item < listForEach (readItems ss rex)
@ sons@[keyRex expRex] | rexSons item
| If (Sz sons /= 2) | sireErr ss rex {expected a key and a value}
| (readSimpleKey ss keyRex, expRex)
= ({#simpleswitch} ss rex)
: _ _ _ _ _ _ _ _ kids nKid < rexOpen rex
@ [_x expr wild armsRex] | kids
| If (Neq 4 nKid) | sireErr ss {expected four kids}
@ arms | Fill 0 (readBranches ss armsRex)
@ keys | Map fst arms
@ vals | Map snd arms
^ (ss, _)
| appE (EMBD Switch, EMBD keys, expr, rowE vals, wild)
;;; {#struct} ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
= ({#struct} ss rex)
@ kids | rexKids rex
@ sign | Ix 2 kids
@ sKid | rexKids sign
@ cnstr | fst sKid
@ fields | Drop 1 sKid
| Ifz && (Sz kids == 3)
&& (rexRune sign == {|})
| (Sz sKid)
| sireErr ss rex {usage: struct#(CONSTRUCTOR field...)}
@ fields
: field < foreach fields
@ fieldKids | rexKids field
@ nm | fst fieldKids
| If && (rexStyle field == {WORD})
| (Eqz rexHeir-field)
field
| If && (rexRune field == {/})
&& (Sz fieldKids == 2)
&& (rexStyle nm == {WORD})
| (Eqz rexHeir-nm)
nm
| Die ["bad input" [rex field]]
; atm, this is just laziness, but eventually this will be the *right*
; way to do things.
@ gettersAndSetters
^ bloodline (listCat _)
: [i getterSym] < listForEach (listIndexed | listFromRow fields)
@ struct | WORD {_x} 0
@ newVal | WORD {_y} 0
@ setterSym | WORD (strWeld (rexText getterSym) "Set") 0
| CONS | OPEN "#=" (getterSym, EMBD (Ix i)) 0
| CONS | OPEN "#=" (setterSym, EMBD (Up i)) 0
| NIL
^ (ss, _)
@ sig | NEST "|" (Cons (PREF "**" [cnstr] 0) fields) 0
@ bod | rowE fields
| OPEN "#=" [sig bod]
| gettersAndSetters
;;; {#simpledata} ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
= ({#simpledata} ss rex)
@ bad | sireErr ss rex {bad #simpledata}
| If (| Neq 2 | Sz | rexSons rex) bad
@ fields
^ _ (rexHeir rex)
? (loop rex)
@ sons | rexSons rex
| Ifz rex NIL
| If Null-sons bad
| If ("-" /= rexRune rex) bad
@ !cnstr | readSymbolEx ss fst-sons
| cnstr::(loop rexHeir-rex)
^ (ss, bloodline _)
: cnstrNm < listForEach fields
| OPEN "#=" (varE cnstrNm, EMBD cnstrNm) 0
;;; {#simplecase} ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
= (readSimpleCaseBranches ss rex)
@ sons | rexSons rex
@ nSon | Sz sons
@ lastSon | Dec nSon
@ valid | (("-" == rexRune rex) || Gte nSon 2)
| Ifz rex | NIL
| Ifz valid | sireErr ss rex {expected something like: - FOO a b c | bodyExpr}
@ syms | Map (readSymbolEx ss) (Take lastSon sons)
| CONS (fst syms, Drop 1 syms, Ix lastSon sons)
| readSimpleCaseBranches ss (rexHeir rex)
(rowMax xs)=(foldl Max 0 xs)
(maxOf f xs)=(rowMax | Map f xs)
= ({#simplecase} ss rex)
@ kids@[_ expr fallback branchListRex] (rexKids rex)
| If (Sz kids /= 4) | sireErr ss rex {expected four params}
@ branches | Fill 0 (readSimpleCaseBranches ss branchListRex)
@ keys | Map fst branches
: ss tmpE < gensym ss
@ tagE | appE (EMBD Hd, tmpE)
@ fieldVar | i&(varE | strWeld "_f" showNat-i)
@ fieldBinds
@ maxNumFields | maxOf [_ fields _]&(Sz fields) branches
: i < listGen maxNumFields
| OPEN "#@" (fieldVar i, appE (EMBD Ix-i, tmpE)) 0
@ bodyExp
^ OPEN "#|" (EMBD Switch, EMBD keys, tagE, _, fallback) 0
^ rowE (Map _ branches)
& [_cnstr fields body]
| bloodline
| listSnoc
: [i field] < listForEach (listIndexed (listFromRow fields))
| OPEN "#@" (varE field, fieldVar i) 0
| OPEN "#|" [body] 0
^ (ss, _)
| OPEN "#@" (tmpE, expr)
| bloodline (listSnoc fieldBinds bodyExp)
;;; Rex Parser Types ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
* # struct (Span a) | SPAN spanLin/a spanOff/Nat spanEnd/Nat spanVal/a
* # struct Line | LN lineFil/Str lineNum/Nat lineTxt/Str
# simpledata (Lexi a)
- LRUNE
- LWORD
- LWYTE
- LSEMI
- LTEXT
- LFAIL
- LTERM
- LLINE multi/(List (Span ()))
- LNEST isBracket/Bit xs:a
= (**getLexiLine x fb ok)
| If (Hd x /= {LLINE}) fb
| **ok (fst x)
;;; Basic Lexing + Nesting ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
= (lexMany lexOne ln off ctx txt)
@ lexeme@[_ off end tok] (lexOne ln off ctx txt)
^ (lexeme :: _)
| If (tok == LTERM) NIL
| lexMany lexOne ln end ctx txt
= (lexNest lexMany lexOne ln typ ctx off txt)
@ ts | lexMany lexOne ln (Inc off) ctx txt
@ [_ _ end _] | listUnsafeLast ts
| (ln, off, end, **LNEST typ ts)
; ASCII is [0-9]..[A..Z]..[_]..[a-z]
= (wordy c) ; Char > Bit
^ (Gte c {0} && (Lte c {z} && _))
| (Gte c {a} || (Lte c {9} || (Eql {_} c || (Gte c {A} && Lte c {Z}))))
runeChars={!#$%&*+,-./:<=>?@\^`|~'} ;; Row Char
(runic c)=(StrHas c runeChars) ; Char > Bit
= (eatCurly txt o d) ; Str > Nat > Nat > Nat
| Ifz d o
@ next (eatCurly txt Inc-o)
# simpleswitch (ByteIx o txt) (next d)
- 0 | o
- "{" | next Inc-d
- "}" | next Dec-d
= (eatCord txt o) ; Str > Nat > Nat > Nat
^ Min (ByteSz txt) (Inc _) ; return ByteSz if unterminated
| strElemIndexOff {"} Inc-o txt
= (lexUgly ln off txt) ; Line > Nat > Char > Str > Str > Lexeme
@ start | Inc-off
@ delim | ByteIx start txt
@ lineStr | [ln off ByteSz-txt LLINE-NIL]
@ noMatch
^ [ln off _ LTEXT]
^ Min (ByteSz txt) (Inc _) ; return ByteSz if unterminated
| strElemIndexOff delim (Inc start) txt
# simpleswitch delim noMatch
- 0 | lineStr
- { } | lineStr
= (lexOne ln o ctx txt)
@ eat | strFindIndexOff
@ c | ByteIx o txt
^ # simpleswitch c _
- {(} | lexNest lexMany lexOne ln FALSE {)} o txt
- {[} | lexNest lexMany lexOne ln TRUE {]} o txt
- "}" | lexUgly ln o txt
- "{" | (ln, o, eatCurly txt Inc-o 1, LTEXT)
- 0 | (ln, o, ByteSz txt, LTERM)
- {;} | (ln, o, ByteSz txt, LSEMI)
- {"} | (ln, o, eatCord txt o, LTEXT)
- { } | (ln, o, eat (Neq 32) o txt, LWYTE)
| If wordy-c | (ln, o, eat c&(Not wordy-c) o txt, LWORD)
| If runic-c | (ln, o, eat c&(Not runic-c) o txt, LRUNE)
| If ctx==c | (ln, o, Inc o, LTERM)
| else | (ln, o, Inc o, LFAIL)
(lexLine ln)=(lexMany lexOne ln 0 0 (lineTxt ln))
;;; Merge Multi-Line Strings ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
= (multiLine topLs) ; List Lexeme > List Lexeme
: a@SPAN[aLin aOff aEnd aTok] ls < listCase topLs NIL
@ fb (a :: multiLine ls)
: b@SPAN[____ bOff bEnd bTok] ls < listCase ls fb
: aExtra < **getLexiLine aTok fb
| If (bTok /= LTERM) fb
@ onMatch
& (newTok more)
@ aTok | LLINE (spanValSet 0 newTok :: aExtra)
| multiLine (SPAN aLin aOff aEnd aTok)::more
: c@SPAN[_ cOff cEnd cTok] ls < listCase ls fb
# simplecase cTok fb
- LLINE _ | If (aOff/=cOff) fb
| onMatch c ls
- LWYTE : d@SPAN[_ dOff dEnd dTok] ls < listCase ls fb
: _ < **getLexiLine dTok fb
| If (aOff/=dOff) fb
| onMatch d ls
;;; Clump Juxtaposed Tokens ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
= (mkClump xs) ; Row Elem > Clump
@ SPAN[lin off _ _] (fst xs)
@ SPAN[_ _ end _] (Last xs)
| (**SPAN lin off end xs)