-
Notifications
You must be signed in to change notification settings - Fork 19
/
TreeTest.hs
1734 lines (1156 loc) · 55.1 KB
/
TreeTest.hs
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
{-# LANGUAGE CPP #-}
{-# LANGUAGE ExplicitForAll, ConstraintKinds, FlexibleContexts #-} -- For :< experiment
{-# LANGUAGE ScopedTypeVariables, TypeOperators #-}
{-# LANGUAGE ViewPatterns, PatternGuards #-}
{-# LANGUAGE DataKinds, GADTs #-} -- for TU
{-# LANGUAGE LambdaCase, TupleSections #-}
{-# OPTIONS_GHC -Wall #-}
{-# OPTIONS_GHC -fcontext-stack=30 #-}
{-# OPTIONS_GHC -fno-warn-type-defaults #-}
{-# OPTIONS_GHC -fno-warn-unused-imports #-} -- TEMP
{-# OPTIONS_GHC -fno-warn-unused-binds #-} -- TEMP
----------------------------------------------------------------------
-- |
-- Module : TreeTest
-- Copyright : (c) 2014 Tabula, Inc.
--
-- Maintainer : conal@tabula.com
-- Stability : experimental
--
-- Tests with length-typed treetors. To run:
--
-- hermit TreeTest.hs -v0 -opt=LambdaCCC.Monomorphize DoTree.hss resume && ./TreeTest
--
-- Remove the 'resume' to see intermediate Core.
----------------------------------------------------------------------
-- module TreeTest where
-- TODO: explicit exports
import Prelude hiding ({- id,(.), -}foldl,foldr,sum,product,zipWith,reverse,and,or,scanl,minimum,maximum)
import Data.Monoid (Monoid(..),(<>),Sum(..),Product(..))
import Data.Functor ((<$>))
import Control.Applicative -- (Applicative(..),liftA2,liftA3)
import Data.Foldable (Foldable(..),sum,product,and,or,toList,minimum,maximum)
import Data.Traversable (Traversable(..))
-- import Control.Category (id,(.))
import Control.Arrow (Arrow(..))
import qualified Control.Arrow as Arrow
import Data.Tuple (swap)
import Data.Maybe (fromMaybe,maybe)
import Text.Printf (printf)
import Test.QuickCheck (arbitrary,Gen,generate,vectorOf)
-- transformers
import Data.Functor.Identity
import TypeUnary.TyNat
import TypeUnary.Nat (IsNat,natToZ)
import TypeUnary.Vec hiding (transpose,iota)
import qualified TypeUnary.Vec as V
import Control.Compose ((:.)(..),unO)
import LambdaCCC.Misc
(xor,boolToInt,dup,Unop,Binop,Ternop,transpose,(:*),loop,delay,Reversible(..))
import LambdaCCC.Adder
import LambdaCCC.CRC -- hiding (crcS,sizeA)
import LambdaCCC.Bitonic
import LambdaCCC.Counters
import qualified LambdaCCC.RadixSort as RS
-- import Circat.Misc (Reversible(..))
import Circat.Rep (bottom)
import Circat.Pair (Pair(..),sortP)
import qualified Circat.Pair as P
import qualified Circat.RTree as RT
import qualified Circat.LTree as LT
import qualified Circat.RaggedTree as Ra
import Circat.RaggedTree (TU(..), R1, R2, R3, R4, R5, R8, R11, R13)
import Circat.Shift
import Circat.Scan
import Circat.FFT
import Circat.Mealy hiding (ArrowCircuit(..))
import qualified Circat.Mealy as Mealy
import Circat.Circuit (GenBuses(..), GS, Attr, systemSuccess)
import Circat.Complex
-- Strange -- why needed? EP won't resolve otherwise. Bug?
import qualified LambdaCCC.Lambda
import Circat.Classes (IfT)
import LambdaCCC.Lambda (EP,reifyEP)
import LambdaCCC.Run
-- Experiment for Typeable resolution in reification
import qualified Data.Typeable
-- -- To support Dave's FFT stuff, below.
-- -- import Data.Complex (cis)
-- import Data.Newtypes.PrettyDouble
{--------------------------------------------------------------------
Misc
--------------------------------------------------------------------}
liftA4 :: Applicative f =>
(a -> b -> c -> a -> d) -> f a -> f b -> f c -> f a -> f d
liftA4 f as bs cs ds = liftA3 f as bs cs <*> ds
{--------------------------------------------------------------------
Examples
--------------------------------------------------------------------}
type RTree = RT.Tree
type LTree = LT.Tree
type Ragged = Ra.Tree
t0 :: RTree N0 Bool
t0 = pure True
p1 :: Unop (Pair Bool)
p1 (a :# b) = b :# a
psum :: Num a => Pair a -> a
psum (a :# b) = a + b
-- tsum :: Num a => RTree n a -> a
-- tsum = foldT id (+)
-- dot :: (IsNat n, Num a) => RTree n a -> RTree n a -> a
-- dot as bs = tsum (prod as bs)
prod :: (Functor f, Num a) => f (a,a) -> f a
prod = fmap (uncurry (*))
prodA :: (Applicative f, Num a) => Binop (f a)
prodA = liftA2 (*)
-- dot :: Num a => RTree n (a,a) -> a
-- dot = tsum . prod
dot :: (Functor f, Foldable f, Num a) => f (a,a) -> a
dot = sum . prod
square :: Num a => a -> a
square a = a * a
sumSquare :: (Functor f, Foldable f, Num a) => f a -> a
sumSquare = sum . fmap square
squares :: (Functor f, Num a) => f a -> f a
squares = fmap square
squares' :: (Functor f, Num a) => f a -> f a
squares' = fmap (^ (2 :: Int))
{--------------------------------------------------------------------
Dot products
--------------------------------------------------------------------}
dot' :: (Applicative f, Foldable f, Num a) => f a -> f a -> a
dot' as bs = sum (prodA as bs)
dot'' :: (Foldable g, Functor g, Foldable f, Num a) => g (f a) -> a
dot'' = sum . fmap product
dot''' :: (Traversable g, Foldable f, Applicative f, Num a) => g (f a) -> a
dot''' = dot'' . transpose
dotap :: (Foldable t, Num (t a), Num a) => t a -> t a -> a
as `dotap` bs = sum (as * bs)
dotsp :: (Foldable g, Foldable f, Num (f a), Num a) => g (f a) -> a
dotsp = sum . product
-- Infix binary dot product
infixl 7 <.>
(<.>) :: (Foldable f, Applicative f, Num a) => f a -> f a -> a
u <.> v = sum (liftA2 (*) u v)
-- | Monoid under lifted multiplication.
newtype ProductA f a = ProductA { getProductA :: f a }
instance (Applicative f, Num a) => Monoid (ProductA f a) where
mempty = ProductA (pure 1)
ProductA u `mappend` ProductA v = ProductA (liftA2 (*) u v)
productA :: (Foldable g, Applicative f, Num a) => g (f a) -> f a
productA = getProductA . foldMap ProductA
dota :: (Foldable g, Foldable f, Applicative f, Num a) => g (f a) -> a
dota = sum . productA
{--------------------------------------------------------------------
Generalized matrices
--------------------------------------------------------------------}
type Matrix m n a = Vec n (Vec m a)
type MatrixT m n a = RTree n (RTree m a)
type MatrixG p q a = Ragged q (Ragged p a)
infixr 1 $@
-- infixl 9 .@
-- | Apply a linear transformation represented as a matrix
-- ($@) :: (IsNat m, Num a) => Matrix m n a -> Vec m a -> Vec n a
($@) :: (Foldable m, Applicative m, Functor n, Num a) =>
n (m a) -> m a -> n a
mat $@ vec = (`dot'` vec) <$> mat
-- -- | Compose linear transformations represented as matrices
-- (.@) :: (IsNat m, IsNat n, IsNat o, Num a) =>
-- Matrix n o a -> Matrix m n a -> Matrix m o a
(.@) :: ( Applicative o, Traversable n, Applicative n
, Traversable m, Applicative m, Num a ) =>
o (n a) -> n (m a) -> o (m a)
-- no .@ mn = (\ n -> (n <.>) <$> transpose mn) <$> no
no .@ mn = transpose ((no $@) <$> transpose mn)
{--------------------------------------------------------------------
Permutations
--------------------------------------------------------------------}
invertR :: IsNat n => RTree n a -> LTree n a
invertR = invertR' nat
invertR' :: Nat n -> RTree n a -> LTree n a
invertR' Zero = \ (RT.L a ) -> LT.L a
invertR' (Succ m) = \ (RT.B ts) -> LT.B (invertR' m (transpose ts))
-- invertR' (Succ m) = \ (RT.B ts) -> LT.B (transpose (invertR' m <$> ts))
#if 0
RT.unB :: RTree (S n) a -> Pair (RTree n a)
transpose :: Pair (RTree n a) -> RTree n (Pair a)
invertR :: RTree n (Pair a) -> LTree n (Pair a)
LT.B :: LTree n (Pair a) -> LTree (S n) a
RT.unB :: RTree (S n) a -> Pair (RTree n a)
fmap invertR :: Pair (RTree n a) -> Pair (LTree n a)
transpose :: Pair (LTree n a) -> LTree n (Pair a)
LT.B :: LTree n (Pair a) -> LTree (S n) a
#endif
-- We needed the IsNat n for Applicative on RTree n.
-- The reverse transformation is easier, since we know Pair is Applicative.
invertL :: LTree n a -> RTree n a
invertL (LT.L a ) = RT.L a
invertL (LT.B ts) = RT.B (transpose (invertL ts))
-- invertL (LT.B ts) = RT.B (invertL <$> transpose ts)
-- invertR' (Succ m) = \ (RT.B ts) -> LT.B (transpose (invertR' m <$> ts))
#if 0
LT.unB :: LTree (S n) a -> LTree n (Pair a)
invertL :: LTree n (Pair a) -> RTree n (Pair a)
transpose :: RTree n (Pair a) -> Pair (RTree n a)
RT.B :: Pair (RTree n a) -> RTree (S n) a
LT.unB :: LTree (S n) a -> LTree n (Pair a)
transpose :: LTree n (Pair a) -> Pair (LTree n a)
fmap invertL :: Pair (LTree n a) -> Pair (RTree n a)
RT.B :: Pair (RTree n a) -> RTree (S n) a
#endif
{--------------------------------------------------------------------
Run it
--------------------------------------------------------------------}
inTest :: String -> IO ()
inTest cmd = systemSuccess ("cd ../test; " ++ cmd) -- (I run ghci in ../src)
mk :: String -> IO ()
mk s = inTest ("make " ++ s)
doit :: IO ()
doit = mk "doit"
reify :: IO ()
reify = mk "reify"
reifyDone :: IO ()
reifyDone = mk "reify-done"
noReify :: IO ()
noReify = mk "no-reify"
noReifyDone :: IO ()
noReifyDone = mk "no-reify-done"
compile :: IO ()
compile = mk "compile"
dateFigureSvg :: String -> String -> IO ()
dateFigureSvg date fig = systemSuccess (printf "cd ../test; ./date-figure-svg %s \"%s\"" date fig)
figureSvg :: String -> IO ()
figureSvg str = systemSuccess ("cd ../test; ./figure-svg " ++ str)
dateLatestSvg :: String -> IO ()
dateLatestSvg date = systemSuccess (printf "cd ../test; ./date-latest-svg \"%s\"" date)
latestSvg :: IO ()
latestSvg = systemSuccess "cd ../test; ./latest-svg"
do1 :: IO ()
do1 = inTest "hermit TreeTest.hs -v0 -opt=LambdaCCC.Monomorphize DoTreeNoReify.hss"
do2 :: IO ()
do2 = inTest "hermit TreeTest.hs -v0 -opt=LambdaCCC.Monomorphize DoTree.hss"
-- Only works when compiled with HERMIT
main :: IO ()
---- FFT
type C = Complex Double
-- main = go "foo" ()
-- main = go "fft-p" (fft :: Unop (Pair C))
-- main = go "fft-lt1" (fft :: LTree N1 C -> RTree N1 C)
-- main = go "fft-rt1" (fft :: RTree N1 C -> LTree N1 C)
-- twiddles :: forall g f a. (AFS g, AFS f, RealFloat a) => g (f (Complex a))
-- main = go "twiddles-lt1p" (twiddles :: LTree N1 (Pair C))
-- main = go "foo" (omega (size (undefined :: (LTree N1 :. Pair) ())))
-- twiddles :: forall g f a. (AFS g, AFS f, RealFloat a) => g (f (Complex a))
-- twiddles = powers <$> powers (omega (tySize(g :. f)))
main = go "foo" (powers :: Int -> LTree N1 Int)
-- zoop :: Int
-- zoop = 3
-- main = go "foo" zoop
-- main = go "foo" (size (undefined :: RTree N3 ()))
-- main = go "foo" (size (undefined :: (LTree N3 :. Pair) ()))
-- main = go "foo" (size (undefined :: Pair ()))
-- main = go "foo" (negate :: Unop Double)
-- omega n = cis (- 2 * pi / fromIntegral n)
-- main = go "omega-1" (omega (1 :: Int))
-- main = go "omega-2" (omega (2 :: Int))
-- -- Okay
-- main = go "foo" (\ x -> cis (-2 * pi / x) :: C)
-- -- Trips over fromInteger . toInteger
-- main = go "foo" (\ (n :: Int) -> cis (-2 * pi / fromIntegral n) :: C)
-- -- Trips over fromInteger . toInteger (the definition of fromIntegral)
-- main = go "foo" (fromIntegral :: Int -> Double)
-- main = go "foo" (pure (3 :+ 4) :: RTree N2 C)
-- main = go "foo" (size (undefined :: RTree N5 ()))
-- main = go "foo" (exp :: C -> C)
-- main = go "foo" (exp :: Double -> Double)
---- End FFT
-- main = go "map-not-v5" (fmap not :: Vec N5 Bool -> Vec N5 Bool)
-- main = go "map-square-v5" (fmap square :: Vec N5 Int -> Vec N5 Int)
-- main = go "map-rt3" (fmap not :: Unop (RTree N3 Bool))
-- main = go "tdott-2" (dot''' :: Pair (RTree N2 Int) -> Int)
-- main = go "dotsp-v3t2" (dotsp :: Vec N3 (RTree N2 Int) -> Int)
-- main = go "dotsp-t2t2" (dotsp :: RTree N2 (RTree N2 Int) -> Int)
-- main = go "dotsp-pt3" (dotsp :: Pair (RTree N3 Int) -> Int)
-- main = go "dotsp-pv5" (dotsp :: Pair (Vec N5 Int) -> Int)
-- main = go "dotsp-plt3" (dotsp :: Pair (LTree N3 Int) -> Int)
-- main = go "dotap-2" (dotap :: RTree N2 Int -> RTree N2 Int -> Int)
-- main = go "tdot-2" (dot'' :: RTree N2 (Pair Int) -> Int)
-- main = go "test" (dot'' :: RTree N4 (Pair Int) -> Int)
-- main = go "plusInt" ((+) :: Int -> Int -> Int)
-- main = go "or" ((||) :: Bool -> Bool -> Bool)
-- main = goSep "pure-rt3" 1 (\ () -> (pure False :: RTree N3 Bool))
-- main = go "foo" (\ (_ :: RTree N3 Bool) -> False)
-- main = go "sum-p" (sum :: Pair Int -> Int)
-- main = go "sumSquare-p" (sumSquare :: Pair Int -> Int)
-- main = goSep "sumSquare-rt2" 0.75 (sumSquare :: RTree N2 Int -> Int)
-- main = go "sum-v8" (sum :: Vec N8 Int -> Int)
-- main = go "and-v5" (and :: Vec N5 Bool -> Bool)
-- main = go "sum-t3" (sum :: RTree N3 Int -> Int)
-- main = go "sum-lt3" (sum :: LTree N3 Int -> Int)
-- main = go "sum-foldl-v5" (foldl (+) 0 :: Vec N5 Int -> Int)
-- main = go "sum-foldr-v5" (foldr (+) 0 :: Vec N5 Int -> Int)
-- main = go "sum-foldl-t3" (foldl (+) 0 :: RTree N3 Int -> Int)
-- main = go "sum-foldr-t3" (foldr (+) 0 :: RTree N3 Int -> Int)
-- main = do go "squares3" (squares :: RTree N3 Int -> RTree N3 Int)
-- go "sum4" (sum :: RTree N4 Int -> Int)
-- go "dot4" (dot :: RTree N4 (Int,Int) -> Int)
-- main = go "test" (dot :: RTree N4 (Int,Int) -> Int)
-- -- Ranksep: rt1=0.5, rt2=1, rt3=2, rt4=4,rt5=8
-- main = goSep "transpose-prt4" 4 (transpose :: Pair (RTree N4 Bool) -> RTree N4 (Pair Bool))
-- -- Ranksep: rt1=0.5, rt2=1, rt3=2, rt4=4,rt5=8
-- main = goSep "transpose-rt2p" 1 (transpose :: RTree N2 (Pair Bool) -> Pair (RTree N2 Bool))
-- -- Ranksep: rt1=1, rt2=2, rt3=4, rt4=8, rt5=16
-- main = goSep "transpose-v3t5" 16 (transpose :: Vec N3 (RTree N5 Bool) -> RTree N5 (Vec N3 Bool))
-- -- Ranksep: rt1=2, rt2=4, rt3=8, rt4=16, rt5=32
-- main = goSep "transpose-v5t3" 8 (transpose :: Vec N5 (RTree N3 Bool) -> RTree N3 (Vec N5 Bool))
-- -- Ranksep: rt1=0.5, rt2=1, rt3=2, rt4=4, rt5=8
-- main = goSep "invertR-5" 8 (invertR :: RTree N5 Bool -> LTree N5 Bool)
-- main = go "vtranspose-34" (transpose :: Matrix N3 N4 Int -> Matrix N4 N3 Int)
-- main = go "vtranspose-34" (transpose :: Vec N3 (Vec N4 Int) -> Vec N4 (Vec N3 Int))
-- main = go "ttranspose-23" (transpose :: MatrixT N2 N3 Int -> MatrixT N3 N2 Int)
-- main = go "swap" (swap :: Int :* Bool -> Bool :* Int)
-- main = go "add" (\ (a,b) -> a+b :: Int)
-- main = go "rot31" (\ (a,b,c) -> (b,c,a) :: (Bool,Bool,Bool))
-- main = go "rot41" (\ (a,b,c,d) -> (b,c,d,a) :: (Bool,Bool,Bool,Bool))
-- main = go "rev4" (\ (a,b,c,d) -> (d,c,b,a) :: (Bool,Bool,Bool,Bool))
-- main = go "sum-2" (\ (a,b) -> a+b :: Int)
-- main = go "sum-3" (\ (a,b,c) -> a+b+c :: Int)
-- main = go "sum-4a" ((\ (a,b,c,d) -> a+b+c+d) :: (Int,Int,Int,Int) -> Int)
-- main = go "sum-4b" ((\ (a,b,c,d) -> (a+b)+(c+d)) :: (Int,Int,Int,Int) -> Int)
-- main = go "dot-22" ((\ ((a,b),(c,d)) -> a*c + b*d) :: ((Int,Int),(Int,Int)) -> Int)
-- main = go "tdot-4" (dot :: RTree N4 (Int,Int) -> Int)
-- main = go "tpdot-4" (dot'' :: RTree N4 (Pair Int) -> Int)
-- -- Doesn't wedge.
-- main = go "dotp" ((psum . prod) :: Pair (Int,Int) -> Int)
-- main = go "prod1" (prod :: RTree N1 (Int,Int) -> RTree N1 Int)
-- main = go "dot5" (dot :: RTree N5 (Int,Int) -> Int)
-- main = go "squares2" (squares :: Unop (RTree N2 Int))
-- main = go "psum" (psum :: Pair Int -> Int)
-- main = go "tsum1" (tsum :: RTree N1 Int -> Int)
-- -- Not working yet: the (^) is problematic.
-- main = go "squaresp-rt0" (squares' :: Unop (RTree N0 Int))
-- main = goSep "applyLin-v23" 1 (($@) :: Matrix N2 N3 Int -> Vec N2 Int -> Vec N3 Int)
-- main = goSep "applyLin-v42" 1 (($@) :: Matrix N4 N2 Int -> Vec N4 Int -> Vec N2 Int)
-- main = goSep "applyLin-v45" 1 (($@) :: Matrix N4 N5 Int -> Vec N4 Int -> Vec N5 Int)
-- main = goSep [ranksep 2] -t21 (($@) :: MatrixT N2 N1 Int -> RTree N2 Int -> RTree N1 Int)
-- main = go "applyLin-t22" (($@) :: MatrixT N2 N2 Int -> RTree N2 Int -> RTree N2 Int)
-- main = go "applyLin-t23" (($@) :: MatrixT N2 N3 Int -> RTree N2 Int -> RTree N3 Int)
-- main = go "applyLin-t32" (($@) :: MatrixT N3 N2 Int -> RTree N3 Int -> RTree N2 Int)
-- main = go "applyLin-t34" (($@) :: MatrixT N3 N4 Int -> RTree N3 Int -> RTree N4 Int)
-- main = go "applyLin-t45" (($@) :: MatrixT N4 N5 Int -> RTree N4 Int -> RTree N5 Int)
-- main = go "applyLin-v3t2" (($@) :: RTree N2 (Vec N3 Int) -> Vec N3 Int -> RTree N2 Int)
-- main = go "applyLin-t2v3" (($@) :: Vec N3 (RTree N2 Int) -> RTree N2 Int -> Vec N3 Int)
-- main = go "composeLin-v234" ((.@) :: Matrix N3 N4 Int -> Matrix N2 N3 Int -> Matrix N2 N4 Int)
-- main = go "composeLin-t234" ((.@) :: MatrixT N3 N4 Int -> MatrixT N2 N3 Int -> MatrixT N2 N4 Int)
-- -- Takes a very long time. I haven't seen it through yet.
-- main = go "composeLin-t324" ((.@) :: MatrixT N2 N4 Int -> MatrixT N3 N2 Int -> MatrixT N3 N4 Int)
-- main = go "composeLin-t222" ((.@) :: MatrixT N2 N2 Int -> MatrixT N2 N2 Int -> MatrixT N2 N2 Int)
-- main = go "composeLin-t232" ((.@) :: MatrixT N3 N2 Int -> MatrixT N2 N3 Int -> MatrixT N2 N2 Int)
-- -- Shift examples are identities on bit representations
-- main = go "shiftR-v3" (shiftR :: Vec N3 Bool :* Bool -> Bool :* Vec N3 Bool)
-- -- Shift examples are identities on bit representations
-- main = go "shiftR-swap-v3" (shiftR . swap :: Unop (Bool :* Vec N3 Bool))
-- main = go "shiftR-rt2" (shiftR :: RTree N2 Bool :* Bool -> Bool :* RTree N2 Bool)
-- main = go "shiftL-rt1" (shiftL :: Bool :* RTree N1 Bool -> RTree N1 Bool :* Bool)
-- main = go "shiftRF-v3v2" (shiftRF :: Vec N3 Bool :* Vec N2 Bool -> Vec N2 Bool :* Vec N3 Bool)
-- main = go "shiftRF-v2v3" (shiftRF :: Vec N2 Bool :* Vec N3 Bool -> Vec N3 Bool :* Vec N2 Bool)
-- -- Shift in two zeros from the right
-- main = go "shiftRF-v3v2F" (flip (curry shift) (pure False))
-- where
-- shift :: Vec N3 Bool :* Vec N2 Bool -> Vec N2 Bool :* Vec N3 Bool
-- shift = shiftRF
-- -- Shift in two zeros from the left
-- main = go "shiftLF-v2v3F" (curry shift (pure False))
-- where
-- shift :: Vec N2 Bool :* Vec N3 Bool -> Vec N3 Bool :* Vec N2 Bool
-- shift = shiftRF
-- -- Shift five zeros into a tree from the left
-- main = go "shiftLF-v5rt4F" (curry shift (pure False))
-- where
-- shift :: Vec N5 Bool :* RTree N4 Bool -> RTree N4 Bool :* Vec N5 Bool
-- shift = shiftRF
-- main = go "lsumsp-rt2" (lsums' :: Unop (RTree N2 Int))
-- main = go "lsumsp-rt2" (lsums' :: Unop (RTree N2 Int))
-- main = go "lsumsp-lt3" (lsums' :: Unop (LTree N3 Int))
-- main = go "lsums-v5" (lsums :: Vec N5 Int -> (Vec N5 Int, Int))
-- main = go "lsums-rt2" (lsums :: RTree N2 Int -> (RTree N2 Int, Int))
-- main = go "lsums-lt3" (lsums :: LTree N3 Int -> (LTree N3 Int, Int))
-- -- 2:1; 3:1.5; 4:2; 5:2.5
-- main = goSep "lParities-rt5" (5/2) (lParities :: RTree N5 Bool -> (RTree N5 Bool, Bool))
-- -- 2:1; 3:1.5; 4:2; 5:2.5
-- main = goSep "lParities-lt4" (4/2) (lParities :: LTree N4 Bool -> (LTree N4 Bool, Bool))
-- -- 2:1; 3:1.5; 4:2; 5:2.5
-- main = goSep "lParities-ex-rt3" (3/2) (fst . lParities :: Unop (RTree N3 Bool))
-- -- 2:1; 3:1.5; 4:2; 5:2.5
-- main = goSep "lParities-ex-lt3" (3/2) (fst . lParities :: Unop (LTree N3 Bool))
-- main = go "foo" (\ a -> not a)
-- main = go "not" not
-- main = go "not-pair" (\ a -> (not a, not a))
-- main = go "and-curried" ((&&) :: Bool -> Bool -> Bool)
-- main = go "test-add-with-constant-fold" foo
-- where
-- foo :: Int -> Int
-- foo x = f x + g x
-- f _ = 3
-- g _ = 4
-- -- True
-- main = go "foo" (\ a -> not a || True)
-- -- not a
-- main = go "foo" (\ a -> a `xor` True)
-- -- a
-- main = go "foo" (\ a -> a `xor` False)
-- main = go "fmap-gt5" (fmap not :: Unop (Ragged R5 Bool))
-- main = go "sum-gt5" (sum :: Ragged R5 Int -> Int)
-- main = go "sum-gt13p" (sum :: Ragged R13' Int -> Int)
-- main = go "dotsp-gt8" (dotsp :: Pair (Ragged R8 Int) -> Int)
-- main = go "applyLin-gt45" (($@) :: MatrixG R4 R5 Int -> Ragged R4 Int -> Ragged R5 Int)
-- main = go "composeLin-gt234" ((.@) :: MatrixG R3 R4 Int -> MatrixG R2 R3 Int -> MatrixG R2 R4 Int)
-- -- Linear map composition mixing ragged trees, top-down perfect trees, and vectors.
-- main = go "composeLin-gt3rt2v2"
-- ((.@) :: Vec N2 (RTree N2 Int) -> RTree N2 (Ragged R3 Int) -> Vec N2 (Ragged R3 Int))
-- main = go "composeLin-gt1rt0v1"
-- ((.@) :: Vec N1 (RTree N0 Int) -> RTree N0 (Ragged R1 Int) -> Vec N1 (Ragged R1 Int))
-- Note: some of the scan examples redundantly compute some additions.
-- I suspect that they're only the same *after* the zero simplifications.
-- These zero additions are now removed in the circuit generation back-end.
-- ranksep: 8=1.5, 11=2.5
-- main = goSep "lsumsp-gt3" =1.5 (lsums' :: Unop (Ragged Ra.R3 Int))
-- main = go "add3" (\ (x :: Int) -> x + 3)
-- main = go "foo" (not . not)
-- main = go "foo" (\ (a,b :: Int) -> if a then b else b)
-- -- Equivalently: a `xor` not b
-- main = go "foo" (\ (a,b) -> if a then b else not b)
-- main = go "foo" (\ a (b :: Int :* Int) -> (if a then id else swap) b)
-- main = goSep "foo" 2.5 (\ (a, b::Int, c::Int, d::Int) -> if a then (b,c,d) else (c,d,b))
-- main = go "foo" (\ a b -> ( if a then b else False -- a && b
-- , if a then True else b -- a || b
-- , if a then False else b -- not a && b
-- , if a then b else True -- not a || b
-- ))
-- -- Equivalently, (&& not a) <$> b
-- main = goSep "foo" 2 (\ a (b :: Vec N4 Bool) -> if a then pure False else b)
-- -- Equivalently, (|| a) <$> b
-- main = goSep "foo" 2 (\ a (b :: Vec N4 Bool) -> if a then pure True else b)
-- -- Equivalently, (&& not a) <$> b
-- main = goSep "foo" 2 (\ a (b :: RTree N3 Bool) -> if a then pure False else b)
-- -- Equivalently, (a `xor`) <$> b
-- main = go "foo" (\ a (b :: Vec N3 Bool) -> (if a then not else id) <$> b)
-- main = goSep "foo" 2 (\ a (b :: RTree N2 Bool) -> (if a then reverse else id) b)
-- -- Equivalent to \ a -> (a,not a)
-- main = go "foo" (\ a -> if a then (True,False) else (False,True))
-- crcStep :: (Traversable poly, Applicative poly) =>
-- poly Bool -> poly Bool :* Bool -> poly Bool
-- main = goSep "crcStep-v1" 1
-- (crcStep :: Vec N1 Bool -> Vec N1 Bool :* Bool -> Vec N1 Bool)
-- -- ranksep: rt2=1, rt3=2, rt4=4.5
-- main = goSep "crcStep-rt3" 2 (crcStep :: RTree N3 Bool -> RTree N3 Bool :* Bool -> RTree N3 Bool)
-- main = go "crcStepK-rt2" (crcStep (polyD :: RTree N2 Bool))
-- main = goSep "crcStepK-g5" 1
-- (crcStep (ra5 True False False True False))
-- crc :: (Traversable poly, Applicative poly, Traversable msg) =>
-- poly Bool -> msg Bool :* poly Bool -> poly Bool
-- main = go "crc-v3v5" (crc :: Vec N3 Bool -> Vec N5 Bool :* Vec N3 Bool -> Vec N3 Bool)
-- main = go "crcK-v3v5" (crc polyD :: Vec N5 Bool :* Vec N3 Bool -> Vec N3 Bool)
-- main = go "crc-v4rt3" (crc :: Vec N4 Bool -> RTree N3 Bool :* Vec N4 Bool -> Vec N4 Bool)
-- main = go "crc-rt3rt5" (crc :: RTree N3 Bool -> RTree N5 Bool :* RTree N3 Bool -> RTree N3 Bool)
-- main = go "crcK-rt2rt4" (crc polyD :: RTree N4 Bool :* RTree N2 Bool -> RTree N2 Bool)
-- main = go "crcK-v5rt4" (crc polyD :: RTree N4 Bool :* Vec N5 Bool -> Vec N5 Bool)
-- main = go "crc-encode-v3v5" (crcEncode :: Vec N3 Bool -> Vec N5 Bool -> Vec N3 Bool)
-- main = go "crc-encode-v3rt2" (crcEncode :: Vec N3 Bool -> RTree N2 Bool -> Vec N3 Bool)
-- main = go "crc-encode-rt2rt4" (crcEncode :: RTree N2 Bool -> RTree N4 Bool -> RTree N2 Bool)
-- Simple carry-propagate adder
-- main = go "halfAdd" halfAdd
-- main = go "add1" add1
-- main = go "add1-0" (carryIn False add1)
-- main = go "add1p" add1'
-- main = go "adder-state-v3" (adderState :: Adder' (Vec N3))
-- main = go "adder-state-rt2" (adderState :: Adder' (RTree N2))
-- main = go "adder-state-0-v1" (carryIn False adderState :: Adder (Vec N1))
-- main = go "adder-state-0-rt0" (carryIn False adderState :: Adder (RTree N0))
-- -- GHC panic: "tcTyVarDetails b{tv ah8Z} [tv]"
-- main = go "adder-state-trie-v2" (adderStateTrie :: Adder' (Vec N2))
-- main = go "adder-accuml-v8" (adderAccumL :: Adder' (Vec N8))
-- main = go "adder-accuml-rt5" (adderAccumL :: Adder' (RTree N5))
-- Monoidal scan adders
-- main = go "gpCarry" gpCarry
-- main = go "mappend-gpr" (mappend :: Binop GenProp)
ifF :: Bool -> Binop a
ifF c a b = if c then a else b
-- main = go "if-gpr" (ifF :: Bool -> Binop GenProp)
-- main = go "gprs-pair" (fmap genProp :: Pair (Pair Bool) -> Pair GenProp)
-- main = go "scan-gpr-pair" (scanGPs :: Pair (Pair Bool) -> Pair GenProp :* GenProp)
-- main = go "adder-scan-pair" (scanAdd :: Adder Pair)
-- main = go "adder-scanp-pair" (scanAdd' :: Adder' Pair)
-- main = go "adder-scanpp-pair" (scanAdd'' :: Adder Pair)
-- -- Ranksep: rt1=0.5, rt2=0.5, rt3=0.75, rt4=1.5,rt5=2
-- main = goSep "adder-scan-noinline-rt2" 0.5 (scanAdd :: Adder (RTree N2))
-- -- Ranksep: rt1=0.5, rt2=0.5, rt3=0.75, rt4=1.5,rt5=2
-- main = goSep "adder-scan-rt5" 2 (scanAdd :: Adder (RTree N5))
-- -- Ranksep: rt2=0.5, rt3=0.75, rt4=1.5,rt5=2
-- main = goSep "adder-scan-unopt-rt0" 0.5 (scanAdd :: Adder (RTree N0))
-- -- Ranksep: rt2=0.5, rt3=1, rt4=2, rt5=3
-- main = goSep "adder-scanp-rt3" 1 (scanAdd' :: Adder' (RTree N3))
-- -- Ranksep: rt2=0.5, rt3=0.75, rt4=1.5,rt5=2
-- main = goSep "adder-scanpp-rt1" 0.5 (scanAdd'' :: Adder (RTree N1))
-- main = go "foo" (\ ((gx,px),(gy,py)) -> (gx || gy && px, px && py))
-- main = go "case-just" (\ case Just b -> not b
-- Nothing -> True)
-- -- Demos automatic commutation
-- main = go "foo" (\ (b,a) -> a || b)
-- main = go "or-with-swap" (\ (a,b) -> (a || b, b || a))
-- -- not (a && b)
-- main = go "foo" (\ b a -> not a || not b)
-- main = go "foo" (\ (a::Int,x::Bool) -> if x then (square a,True) else (bottom,False))
-- main = go "foo" (\ x -> if x then True else bottom)
-- main = go "foo" (bottom :: Bool)
-- main = go "foo" (bottom::Int,False)
-- main = go "foo" (bottom::Bool, bottom::Int, bottom::Bool)
-- main = go "foo" (\ x -> if x then bottom else bottom :: Bool)
-- main = goSep "if-maybe" 0.75 (\ a (b :: Maybe Bool) c -> if a then b else c)
-- main = go "fmap-maybe-square" (fmap square :: Unop (Maybe Int))
-- main = go "fmap-maybe-not" (fmap not :: Unop (Maybe Bool))
-- main = go "foo" (\ a b -> if b then (not a,True) else (bottom,False))
-- main = go "fromMaybe-bool" (fromMaybe :: Bool -> Maybe Bool -> Bool)
-- main = goSep "fromMaybe-v3" 1.5 (fromMaybe :: Vec N3 Bool -> Maybe (Vec N3 Bool) -> Vec N3 Bool)
-- main = goSep "liftA2-maybe" 0.8 (liftA2 (*) :: Binop (Maybe Int))
-- main = goSep "liftA3-maybe" 0.8 (liftA3 f :: Ternop (Maybe Int))
-- where
-- f x y z = x * (y + z)
-- main = goSep "liftA4-maybe" 0.8 (\ (a,b,c,d) -> liftA4 f a b c d :: Maybe Int)
-- where
-- f w x y z = (w + x) * (y + z)
-- main = go "lift-maybe-1-1a" h
-- where
-- h a = pure square <*> a :: Maybe Int
-- main = go "lift-maybe-1-1b" h
-- where
-- h a = liftA2 (*) a a :: Maybe Int
-- main = go "lift-maybe-1-1c-no-idem" h
-- where
-- h a = fmap square b :: Maybe Int
-- where
-- b = liftA2 (+) a a
-- main = go "lift-maybe-1-2" h
-- where
-- h a = liftA2 (*) a b :: Maybe Int
-- where
-- b = liftA2 (+) a a
-- main = goSep "lift-maybe-1-3" 1 h
-- where
-- h a = liftA3 f a b c :: Maybe Int
-- where
-- b = liftA2 (*) a a
-- c = liftA2 (+) b a
-- f w x y = (w + x) * y
-- main = goSep "lift-maybe-1-4" 0.8 h
-- where
-- h a = liftA4 f a b c d :: Maybe Int
-- where
-- b = liftA2 (*) a a
-- c = liftA2 (+) b a
-- d = liftA2 (*) c b
-- f w x y z = (w + x) * (y + z)
-- main = go "liftA2-justs" (\ (a,b) -> liftA2 (*) (Just a) (Just b) :: Maybe Int)
-- Sums
-- main = go "fmap-either-square" (fmap square :: Unop (Either Bool Int))
-- main = go "case-of-either"
-- (\ case Left x -> if x then 3 else 5 :: Int
-- Right n -> n + 3)
-- -- ranksep 1.5 when unoptimized.
-- main = goSep "if-to-either" 1.5
-- (\ a -> if a then Left 2 else Right (3,5) :: Either Int (Int,Int))
-- main = goSep "case-if-either" 1
-- (\ a -> let e :: Either Int (Int,Int)
-- e = if a then Left 2 else Right (3,5)
-- in
-- case e of
-- Left n -> n + 5
-- Right (p,q) -> p * q )
-- main = goSep "case-if-either-2" 1
-- (\ (a,b,c,d) -> let e :: Either Int (Int,Int)
-- e = if a then Left b else Right (c,d)
-- in
-- case e of
-- Left n -> n + 5
-- Right (p,q) -> p * q )
-- main = goSep "case-if-either-3" 1
-- (\ (a,b,c) -> let e :: Either Int (Int -> Int)
-- e = if a then Left b else Right (b *)
-- in
-- case e of
-- Left n -> n + c
-- Right f -> f c )
-- -- The conditionals vanish
-- main = goSep "case-if-either-3b" 0.7
-- (\ (a,b,c) -> let e :: Either Int (Int -> Int)
-- e = if a then Left b else Right (b +)
-- in
-- case e of
-- Left n -> n + c
-- Right f -> f c )
-- main = go "foo" (\ (a::Int,old) -> dup (old+a))
-- main = goM "foo" (Mealy (\ (a::Int,old) -> dup (old+a)) 0)
-- main = goM "mealy-sum-0" (Mealy (\ (a::Int,old) -> dup (old+a)) 0)
-- We can't yet handle examples built from the Arrow interface.
-- main = goM "mealy-sum-1" (m :: Mealy Int Int)
-- where
-- m :: Mealy Int Int
-- m = loop (arr (\ (a,tot) -> dup (tot+a)) . second (delay 0))
-- serialSum0 :: Mealy Int Int
-- serialSum0 = Mealy (\ (old,a) -> dup (old+a)) 0
-- serialSum1 :: Mealy Int Int
-- serialSum1 = loop (arr (\ (a,tot) -> dup (tot+a)) . second (delay 0))
-- main = goM "mealy-counter-exclusive" (Mealy (\ ((),n::Int) -> (n,n+1)) 0)
-- main = goM "mealy-counter-inclusive" (Mealy (\ ((),n::Int) -> dup (n+1)) 0)
-- main = goM "mealy-sum-exclusive" (Mealy (\ (a::Int,n) -> (n,n+a)) 0)
-- main = goM "mealy-sum-inclusive" (Mealy (\ (a::Int,n) -> dup (n+a)) 0)
-- -- Square of consecutive numbers, inclusive
-- main = goM "mealy-square-counter-inclusive"
-- (Mealy (\ ((),n::Int) -> let n' = n+1 in (square n',n')) 0)
-- -- Prefix sum of square of inputs
-- main = goM "mealy-square-sum-inclusive"
-- (Mealy (\ (a::Int,tot) -> dup (tot + square a)) 0)
-- Serial Fibonacci variants:
-- main = goM "serial-fibonacci-a" $
-- Mealy (\ ((),(a,b)) -> (a,(b,a+b))) (0::Int,1)
-- main = goM "serial-fibonacci-b" $
-- Mealy (\ ((),(a,b)) -> (b,(b,a+b))) (0::Int,1)
-- main = goM "serial-fibonacci-c" $
-- Mealy (\ ((),(a,b)) -> let c = a+b in (c,(b,c))) (0::Int,1)
-- main = goM "serial-fibonacci-a-11" $
-- Mealy (\ ((),(a,b)) -> (a,(b,a+b))) (1::Int,1)
-- main = go "foo" (sumSquare :: RTree N2 Int -> Int)
-- main = goM "sumSP-rt3" (Mealy (\ (as :: RTree N3 (Sum Int),tot) -> dup (fold as <> tot)) mempty)
-- main = goM "sumSP-rt4" (sumSP :: Mealy (RTree N4 Int) Int)
-- main = goM "foldSP-rt1" (foldSP :: Mealy (RTree N1 (Sum Int)) (Sum Int))
-- main = goM "sumSP-rt2" (sumSP :: Mealy (RTree N2 Int) Int)
-- -- Not yet.
-- main = goM "dotSP-rt2p" (dotSP :: Mealy (RTree N2 (Pair Int)) Int)
-- main = goM "dotSP-rt4p" (Mealy (\ (pas :: RTree N4 (Pair Int),tot) -> dup (dot'' pas + tot)) 0)
-- type GS a = (GenBuses a, Show a)
fullAdd :: Pair Bool :* Bool -> Bool :* Bool
fullAdd = add1' . swap
{-# INLINE fullAdd #-}
-- main = go "fullAdd" (add1' . swap) -- fullAdd -- fullAdd doesn't inline
adderS :: Bool -> Mealy (Pair Bool) Bool
adderS = Mealy (add1 . swap)
-- main = goM "adderS" (adderS False)
-- main = goMSep "sumS" 0.5 (sumS :: Mealy Int Int)
-- main = goMSep "sumS-rt3" 1.5 (sumS :: Mealy (RTree N3 Int) (RTree N3 Int))
-- main = goMSep "sumPS-rt1" 0.75 (sumPS :: Mealy (RTree N1 Int) Int)
-- main = goM "dotPS-rt3p" (m :: Mealy (RTree N3 (Pair Int)) Int)
-- where
-- m = Mealy (\ (ts,tot) -> let tot' = tot + fmap product ts in (sum tot',tot')) 0
-- main = goMSep "mac-p" 1 (mac :: Mealy (Pair Int) Int)
-- main = goMSep "mac-prt2" 1 (mac :: Mealy (Pair (RTree N2 Int)) (RTree N2 Int))
-- main = goM "sum-mac-prt1" (sumMac :: Mealy (Pair (RTree N1 Int)) Int)
matVecMultSA :: (Foldable f, Applicative f, Num a, GS (f a)) =>
Mealy (f a) a
matVecMultSA =
Mealy (\ (row,s@(started,vec)) ->
if started then (row <.> vec, s) else (0, (True,row))) (False,pure 0)
matVecMultS :: (Foldable f, Applicative f, Num a, GS (f a)) =>
Mealy (f a) a
matVecMultS =
Mealy (\ (row,s@(started,vec)) ->
(row <.> vec, if started then s else (True,row))) (False,pure 0)
-- main = goM "mat-vec-mult-rt1" (matVecMultS :: Mealy (RTree N1 Int) Int)