-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathDefs.hs
1632 lines (1247 loc) · 45.4 KB
/
Defs.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 UndecidableInstances, FlexibleInstances, FlexibleContexts, BangPatterns #-}
module Defs where
-- Built-in functions
import Data.Function (fix)
import qualified Data.Char as C
import Data.Char (chr,ord)
import Data.List
import qualified Data.Set as S (member, insert, singleton)
import Data.Ord (comparing)
import Data.Bits ((.&.), (.|.))
import Data.Ratio ((%), numerator, denominator)
import Numeric (showFFloat)
-- Type of numeric values: integer fractions and Doubles
-- 1:%0 is infinity
-- -1:%0 is negative infinity
-- 0:%0 is Any
data TNum = !Integer :% !Integer
| TDbl !Double
-- Convert to Double
doublify :: TNum -> Double
doublify (p :% q) = fromInteger p / fromInteger q
doublify (TDbl a) = a
instance Eq TNum where
p :% 0 == q :% 0 = p == q
p :% q == r :% s = p*s == q*r
x == y = doublify x == doublify y
instance Ord TNum where
compare (p :% 0) (r :% 0) = compare p r
compare (p :% q) (r :% s) = compare (p*s) (q*r)
compare x y = compare (doublify x) (doublify y)
boolToNum :: Bool -> TNum
boolToNum True = 1
boolToNum False = 0
-- Instances for TNum
instance Show TNum where
show (p :% 1) = show p
show (1 :% 0) = "Inf"
show (0 :% 0) = "Any"
show ((-1) :% 0) = "-Inf"
show (p :% q) = show p ++ "/" ++ show q
show (TDbl d) = showFFloat Nothing d ""
instance Read TNum where
readsPrec n str
| p@(_:_) <- tryInt,
x@(_:_) <- [(k, q) | (k, '/':rest) <- p, q <- readsPrec n rest]
= [(cancel $ k :% m, rest2) | (k, (m, rest2)) <- x]
| p@(_:_) <- tryInt = [(k :% 1, rest) | (k, rest) <- p]
| p@(_:_) <- tryDbl = [(TDbl k, rest) | (k, rest) <- p]
| 'I':'n':'f':rest <- str = [(1 :% 0, rest)]
| 'A':'n':'y':rest <- str = [(0 :% 0, rest)]
| '-':'I':'n':'f':rest <- str = [((-1) :% 0, rest)]
| otherwise = []
where tryInt = readsPrec n str :: [(Integer, String)]
tryDbl = readsPrec n str :: [(Double, String)]
-- Simplify a fraction
cancel :: TNum -> TNum
cancel (p :% 0) = signum p :% 0
cancel (p :% q)
| k <- signum q * p,
n <- abs q,
r <- gcd k n
= div k r :% div n r
cancel a = a
-- Create a binary numeric operator
-- operate f (!) applies f to fractions and (!) to Doubles,
-- converting fractions to Doubles when necessary
operate :: (Integer -> Integer -> Integer -> Integer -> (Integer, Integer)) -> (Double -> Double -> TNum) -> TNum -> TNum -> TNum
operate f _ (p :% q) (r :% s) | (x, y) <- f p q r s = cancel $ x :% y
operate _ (!) a b = doublify a ! doublify b
instance Num TNum where
(+) = operate (\p q r s -> (p*s + r*q, q*s)) ((TDbl .) . (+))
(-) = operate (\p q r s -> (p*s - r*q, q*s)) ((TDbl .) . (-))
(*) = operate (\p q r s -> (p*r, q*s)) ((TDbl .) . (*))
abs (p :% q) = abs p :% q
abs (TDbl a) = TDbl $ abs a
signum (p :% _) = signum p :% 1
signum (TDbl a) = round (signum a) :% 1
negate (p :% q) = negate p :% q
negate (TDbl a) = TDbl $ negate a
fromInteger = (:% 1)
instance Real TNum where
toRational (p :% q) = p % q
toRational (TDbl a) = toRational a
instance Enum TNum where
toEnum n = toEnum n :% 1
fromEnum (p :% q) = fromEnum $ p % q
fromEnum (TDbl n) = fromEnum n
succ = (+1)
pred = (-1+)
enumFrom = iterate succ
enumFromThen a b = iterate (+(b-a)) a
enumFromTo a c = case compare a c of
GT -> []
EQ -> [a]
LT -> takeWhile (<= c) $ iterate succ a
enumFromThenTo a b c = case (compare a c, compare a b) of
(GT, GT) -> takeWhile (>= c) $ iterate (+(b-a)) a
(GT, _) -> []
(EQ, GT) -> [a]
(EQ, EQ) -> repeat a
(EQ, LT) -> [a]
(LT, GT) -> []
(LT, EQ) -> repeat a
(LT, LT) -> takeWhile (<= c) $ iterate (+(b-a)) a
instance Integral TNum where
toInteger (p :% q) = div p q
toInteger (TDbl d) = truncate d
quotRem a@(_ :% _) b@(_ :% _)
| d@(p :% q) <- a / b,
k <- div p q :% 1
= if q == 0
then (d, a * signum d)
else (k, a - b*k)
quotRem a b
| x <- doublify a,
y <- doublify b,
r <- truncate $ x / y
= (r :% 1, TDbl $ x - y * fromInteger r)
instance Fractional TNum where
fromRational r = numerator r :% denominator r
(/) = operate
(\p q r s -> (p*s, q*r))
(\x y -> if y == 0
then round (signum x) :% 0
else TDbl $ x/y)
-- Lift a numeric function to TNums
-- The extra arguments are results for Inf and -Inf
numeric :: (Double -> Double) -> TNum -> TNum -> (TNum -> TNum)
numeric f pinf ninf = g
where g (1 :% 0) = pinf
g (0 :% 0) = 0 :% 0
g ((-1) :% 0) = ninf
g x = TDbl $ f $ doublify x
instance Floating TNum where
pi = TDbl pi
exp = numeric exp (1 :% 0) 0
log = numeric log (1 :% 0) 0
sqrt = numeric sqrt (1 :% 0) ((-1) :% 0)
sin = numeric sin 0 0
cos = numeric cos 0 0
tan = numeric tan 0 0
asin = numeric asin 0 0
acos = numeric acos 0 0
atan = numeric atan (pi/2) (-pi/2)
sinh = numeric sinh (1 :% 0) ((-1) :% 0)
cosh = numeric cosh (1 :% 0) (1 :% 0)
tanh = numeric tanh 1 (-1)
asinh = numeric asinh (1 :% 0) ((-1) :% 0)
acosh = numeric acosh (1 :% 0) 0
atanh = numeric atanh 0 0
instance RealFrac TNum where
properFraction a@(_ :% 0) = (0, a)
properFraction (p :% q) | r <- quot p q = (fromInteger r, (p - r) :% q)
properFraction (TDbl a) | (n, r) <- properFraction a = (n, TDbl r)
-- Class of all Husk types (used for defaulting)
class Husky a where
defVal :: a
instance Husky TNum where
defVal = 0
instance Husky Char where
defVal = ' '
instance (Husky a) => Husky [a] where
defVal = []
instance (Husky a, Husky b) => Husky (a,b) where
defVal = (defVal, defVal)
instance (Husky b) => Husky (a -> b) where
defVal = const defVal
-- String conversion
class ToString a where
toString :: a -> String
instance {-# OVERLAPPING #-} ToString String where
toString = id
instance {-# OVERLAPPING #-} ToString [String] where
toString = unlines
instance {-# OVERLAPPING #-} ToString [[String]] where
toString = unlines.map unwords
instance Concrete a => ToString a where
toString = show
-- Class of concrete values
class (Husky a, Show a, Read a, Eq a, Ord a, ToString a) => Concrete a where
isTruthy :: a -> Bool
toTruthy :: a -> TNum
func_false :: a
func_false = defVal
func_true :: a
func_lt :: a -> a -> TNum
func_gt :: a -> a -> TNum
func_le :: a -> a -> TNum
func_ge :: a -> a -> TNum
func_neq :: a -> a -> TNum
func_congr :: a -> a -> TNum
func_simil :: a -> a -> TNum
func_maxval :: a
func_minval :: a
func_heads :: a -> [a]
func_tails :: a -> [a]
func_eq :: a -> a -> TNum
func_eq x y = boolToNum $ x == y
func_or :: a -> a -> a
func_or y x = if isTruthy x then x else y
func_and :: a -> a -> a
func_and y x = if isTruthy x then y else x
func_read :: [Char] -> a
func_read x | ((val, _):_) <- reads x = val
| otherwise = defVal
func_or' :: (Concrete a, Concrete b) => a -> b -> TNum
func_or' x y = func_or (toTruthy x) (toTruthy y)
func_and' :: (Concrete a, Concrete b) => a -> b -> TNum
func_and' x y = func_and (toTruthy x) (toTruthy y)
instance Concrete TNum where
isTruthy = (/= 0)
toTruthy (TDbl d) = roundAway d :% 1
toTruthy n = n
func_true = 1
func_lt y x = max 0 $ toTruthy (y-x)
func_gt y x = max 0 $ toTruthy (x-y)
func_le y x | x > y = 0
| otherwise = toTruthy $ y-x+1
func_ge y x | x < y = 0
| otherwise = toTruthy $ x-y+1
func_neq y x = abs $ toTruthy (x-y)
func_maxval = 1 :% 0
func_minval = (-1) :% 0
func_congr 0 0 = 1
func_congr 0 _ = 0
func_congr _ 0 = 0
func_congr _ _ = 1
func_simil x y | abs (x-y) <= 1 = 1
| otherwise = 0
func_heads x | x >= 0 = [1 .. x]
| otherwise = [x .. -1]
func_tails x | x >= 0 = [x, x-1 .. 1]
| otherwise = [-1, -2 .. x]
instance Concrete Char where
isTruthy = not . C.isSpace
toTruthy = boolToNum.isTruthy
func_true = '!'
func_lt y x = fromIntegral $ max 0 (ord y - ord x)
func_gt y x = fromIntegral $ max 0 (ord x - ord y)
func_le y x = fromIntegral $ max 0 (ord y - ord x + 1)
func_ge y x = fromIntegral $ max 0 (ord x - ord y + 1)
func_neq y x = abs.fromIntegral $ (ord x)-(ord y)
func_maxval = maxBound
func_minval = minBound
func_congr x y | isTruthy x == isTruthy y = 1
| otherwise = 0
func_simil x y | x==y || x == succ y || y == succ x = 1
| otherwise = 0
func_heads x = ['\0'..x]
func_tails x = [x, pred x..'\0']
instance Concrete a => Concrete [a] where
isTruthy = (/= [])
toTruthy = genericLength
func_true = [func_true]
func_lt = go 1
where go n (_:_) [] = n
go n (y:ys) (x:xs) | x < y = n
| x > y = 0
| otherwise = go (n+1) ys xs
go _ _ _ = 0
func_gt x y = func_lt y x
func_le = go 1
where go n _ [] = n
go n (y:ys) (x:xs) | x < y = n
| x > y = 0
| otherwise = go (n+1) ys xs
go _ _ _ = 0
func_ge x y = func_le y x
func_neq = go 1
where go n [] [] = 0
go n (x:xs) (y:ys) | x /= y = n
| otherwise = go (n+1) xs ys
go n _ _ = n
func_maxval = repeat func_maxval
func_minval = []
func_congr [] [] = 1
func_congr [] _ = 0
func_congr _ [] = 0
func_congr (x:xs) (y:ys) = if func_congr x y == 0 then 0 else func_congr xs ys
func_simil (x:xs) (y:ys) = func_simil xs ys
func_simil [] [] = 1
func_simil _ _ = 0
func_heads=tail.inits
func_tails=init.tails
instance (Concrete a, Concrete b) => Concrete (a, b) where
isTruthy (x, y) = isTruthy x && isTruthy y
toTruthy (x, y) = toTruthy x * toTruthy y
func_true = (func_true, func_true)
func_lt (x, y) (x', y') = if x == x' then func_lt y y' else func_lt x x'
func_gt (x, y) (x', y') = if x == x' then func_gt y y' else func_gt x x'
func_le (x, y) (x', y') = if x > x' then func_lt y y' else func_le x x'
func_ge (x, y) (x', y') = if x < x' then func_gt y y' else func_ge x x'
func_neq (x, y) (x', y') = if x == x' then func_neq y y' else func_neq x x'
func_maxval = (func_maxval, func_maxval)
func_minval = (func_minval, func_minval)
func_congr (a,b) (c,d) = if func_congr a c + func_congr b d == 2 then 1 else 0
func_simil (a,b) (c,d) = if func_simil a c + func_simil b d == 2 then 1 else 0
func_heads (a,b) = [(c,d)|c<-func_heads a,d<-func_heads b]
func_tails (a,b) = [(c,d)|c<-func_tails a,d<-func_tails b]
roundAway :: Double -> Integer
roundAway d = if d<0 then floor d else ceiling d
--Primes (quite efficient implementation, but not the most efficient)
primes_list = 2 : oddprimes
where
oddprimes = sieve [3,5..] 9 oddprimes
sieve (x:xs) q ps@ ~(p:t)
| x < q = x : sieve xs q ps
| otherwise = sieve (xs `minus` [q, q+2*p..]) (head t^2) t
minus (x:xs) (y:ys) = case (compare x y) of
LT -> x : minus xs (y:ys)
EQ -> minus xs ys
GT -> minus (x:xs) ys
minus xs _ = xs
-- Built-in functions
func_fix :: (a -> a) -> a
func_fix = fix
func_fixp :: Concrete a => (a -> a) -> a -> a
func_fixp f a = go (S.singleton a) $ f a
where go xs x | x `S.member` xs = x
| otherwise = go (S.insert x xs) $ f x
func_fixpL :: Concrete a => (a -> [a]) -> a -> [a]
func_fixpL f a = cs
where f' = concatMap f
b = func_fixp (take 1 . f') [a]
n = succ $ length $ takeWhile (/= b) $ tail $ iterate (take 1 . f') b
f'' = foldr1 (.) $ replicate n f'
cs = b ++ tail (f'' cs)
func_app :: (a -> b) -> a -> b
func_app = id
func_com :: (b -> c) -> (a -> b) -> a -> c
func_com = (.)
func_com2 :: (c -> d) -> (a -> b -> c) -> a -> b -> d
func_com2 f g x = f . g x
func_com3 :: (d -> e) -> (a -> b -> c -> d) -> a -> b -> c -> e
func_com3 f g x y = f . g x y
func_com4 :: (e -> f) -> (a -> b -> c -> d -> e) -> a -> b -> c -> d -> f
func_com4 f g x y z = f . g x y z
func_add :: TNum -> TNum -> TNum
func_add = (+)
func_sub :: TNum -> TNum -> TNum
func_sub b a = a - b
func_mul :: TNum -> TNum -> TNum
func_mul = (*)
func_div :: TNum -> TNum -> TNum
func_div b a = a / b
func_idiv :: TNum -> TNum -> TNum
func_idiv b a = a `div` b
func_mod :: TNum -> TNum -> TNum
func_mod b a = a `mod` b
func_divds :: TNum -> TNum -> TNum
func_divds b a =
if func_mod b a == 0
then if a == 0
then 1
else func_idiv b a
else 0
func_neg :: TNum -> TNum
func_neg x = -x
func_inv :: TNum -> TNum
func_inv = recip
-- Triangular numbers: sum of all numbers in [1..n]
func_trian :: TNum -> TNum
func_trian (p :% 1) = div (p*(p+1)) 2 :% 1
func_trian r = r*(r+1)/2
func_fact :: TNum -> TNum
func_fact n = product [1..n]
func_pure :: a -> [a]
func_pure = (: [])
func_cons :: a -> [a] -> [a]
func_cons = (:)
func_snoc :: [a] -> a -> [a]
func_snoc x y = x ++ [y]
func_cat :: [a] -> [a] -> [a]
func_cat = (++)
func_head :: (Husky a) => [a] -> a
func_head [] = defVal
func_head (x:_) = x
func_last :: (Husky a) => [a] -> a
func_last [] = defVal
func_last xs = last xs
func_tail :: [a] -> [a]
func_tail [] = []
func_tail (_:xs) = xs
func_init :: [a] -> [a]
func_init [] = []
func_init xs = init xs
func_pair :: a -> b -> (a, b)
func_pair = (,)
func_swap :: (a,b) -> (b,a)
func_swap (x,y) = (y,x)
func_fst :: (a,b) -> a
func_fst = fst
func_snd :: (a,b) -> b
func_snd = snd
func_map :: (a -> b) -> [a] -> [b]
func_map = map
func_mapr :: [(a -> b)] -> a -> [b]
func_mapr fs a = [f a | f<-fs]
func_zip :: (a -> b -> c) -> [a] -> [b] -> [c]
func_zip = zipWith
func_filter :: Concrete b => (a -> b) -> [a] -> [a]
func_filter f = filter $ isTruthy . f
func_select :: Concrete a => [a] -> [b] -> [b]
func_select x y = [b | (a, b) <- zip x y, isTruthy a]
func_scanl :: (b -> a -> b) -> b -> [a] -> [b]
func_scanl = scanl
func_scanl1 :: (a -> a -> a) -> [a] -> [a]
func_scanl1 = scanl1
func_scanr :: (a -> b -> b) -> b -> [a] -> [b]
func_scanr = scanr
func_scanr1 :: (a -> a -> a) -> [a] -> [a]
func_scanr1 = scanr1
func_len :: [a] -> TNum
func_len = genericLength
func_nlen :: TNum -> TNum
func_nlen = genericLength.show
func_countf :: Concrete b => (a -> b) -> [a] -> TNum
func_countf c = genericLength . filter (isTruthy . c)
func_count :: Concrete a => a -> [a] -> TNum
func_count x = genericLength . filter (== x)
func_count' :: Concrete a => [a] -> a -> TNum
func_count' = flip func_count
func_index :: (Husky a) => TNum -> [a] -> a
func_index _ [] = defVal
func_index i xs
| (1 :% 0) <- i = last xs
| (0 :% 0) <- i = defVal
| ((-1) :% 0) <- i = head xs
| toInteger i>0 = genericIndex (cycle xs) $ toInteger i-1
| otherwise = genericIndex (cycle $ reverse xs) $ -toInteger i
func_index2 :: (Husky a) => [a] -> TNum -> a
func_index2 = flip func_index
func_rev :: [a] -> [a]
func_rev = reverse
func_nats :: [TNum]
func_nats = [1, 2 ..]
func_sum :: [TNum] -> TNum
func_sum = sum
func_prod :: [TNum] -> TNum
func_prod = product
func_concat :: [[a]] -> [a]
func_concat = concat
func_cartes :: [[a]] -> [[a]]
func_cartes [] = [[]]
func_cartes (x:xs) = func_mix (:) x $ func_cartes xs
func_mix :: (a -> b -> c) -> [a] -> [b] -> [c]
func_mix f xs ys = concat $ func_adiags $ func_table f xs ys
-- Lazy merges
merge3 :: [a] -> [a] -> [a] -> [a]
merge3 (x:xs) ys zs = x : next ys
where next (b:bs) = b : next' zs
where next' (c:cs) = c : merge3 xs bs cs
next' [] = merge2 xs ys
next [] = merge2 xs zs
merge3 [] ys zs = merge2 ys zs
merge2 :: [a] -> [a] -> [a]
merge2 (x:xs) ys = x : next ys
where next (y:ys) = y : merge2 xs ys
next [] = xs
merge2 [] ys = ys
func_if :: Concrete a => b -> b -> a -> b
func_if b c a = if isTruthy a then b else c
func_if2 :: Concrete a => (a->b) -> b -> a -> b
func_if2 g c a = if isTruthy a then g a else c
func_fif :: Concrete a => (x->b) -> (x->b) -> (x->a) -> x -> b
func_fif g h f x = if isTruthy (f x) then g x else h x
func_not :: Concrete a => a -> TNum
func_not a = if isTruthy a then 0 else 1
func_fnot :: Concrete b => (a -> b) -> a -> TNum
func_fnot f = func_not . f
func_hook :: (a -> b -> c) -> (a -> b) -> a -> c
func_hook x y z = x z (y z)
func_hookf :: (a -> b -> c) -> (b -> a) -> b -> c
func_hookf x y z = x (y z) z
func_const :: a -> b -> a
func_const x _ = x
func_id :: a -> a
func_id x = x
func_flip :: (a -> b -> c) -> (b -> a -> c)
func_flip = flip
func_foldl :: (b -> a -> b) -> b -> [a] -> b
func_foldl = foldl
func_foldl1 :: (Husky a) => (a -> a -> a) -> [a] -> a
func_foldl1 _ [] = defVal
func_foldl1 f xs = foldl1 f xs
func_foldr :: (a -> b -> b) -> b -> [a] -> b
func_foldr = foldr
func_foldr1 :: (Husky a) => (a -> a -> a) -> [a] -> a
func_foldr1 _ [] = defVal
func_foldr1 f xs = foldr1 f xs
func_take :: TNum -> [a] -> [a]
func_take n
| n >= 0 = genericTake n
| otherwise = reverse . genericTake (-n) . reverse
func_take2 :: [a] -> TNum -> [a]
func_take2 = flip func_take
func_takew :: Concrete b => (a -> b) -> [a] -> [a]
func_takew _ [] = []
func_takew f (x:xs)
| isTruthy(f x) = x : func_takew f xs
| otherwise = []
func_drop :: TNum -> [a] -> [a]
func_drop n
| n >= 0 = genericDrop n
| otherwise = reverse . genericDrop (-n) . reverse
func_drop2 :: [a] -> TNum -> [a]
func_drop2 = flip func_drop
func_dropw :: Concrete b => (a -> b) -> [a] -> [a]
func_dropw _ [] = []
func_dropw f (x:xs)
| isTruthy(f x) = func_dropw f xs
| otherwise = x:xs
func_span :: Concrete b => (a -> b) -> [a] -> ([a],[a])
func_span f xs = go f ([],xs)
where go f result@(hs,(t:ts)) | isTruthy(f t) = go f (hs++[t],ts)
| otherwise = result
go f (hs,[]) = (hs,[])
func_list :: b -> (a -> [a] -> b) -> [a] -> b
func_list c _ [] = c
func_list _ f (x:xs) = f x xs
func_listN :: (Husky b) => (a -> [a] -> b) -> [a] -> b
func_listN _ [] = defVal
func_listN f (x:xs) = f x xs
func_listF :: b -> (([a] -> b) -> (a -> [a] -> b)) -> [a] -> b
func_listF c f = go
where go [] = c
go (x:xs) = f go x xs
func_listNF :: (Husky b) => (([a] -> b) -> (a -> [a] -> b)) -> [a] -> b
func_listNF f = go
where go [] = defVal
go (x:xs) = f go x xs
func_fork :: (a -> b -> c) -> (x -> a) -> (x -> b) -> x -> c
func_fork f g h x = f (g x) (h x)
func_fork2 :: (a -> b -> c) -> (x -> y -> a) -> (x -> y -> b) -> x -> y -> c
func_fork2 f g h x y = f (g x y) (h x y)
func_argdup :: (a -> a -> b) -> a -> b
func_argdup f x = f x x
func_iter :: (a -> a) -> a -> [a]
func_iter = iterate
func_iterL :: (a -> [a]) -> [a] -> [a]
func_iterL f = go
where go [] = []
go xs = xs ++ go (concatMap f xs)
func_iterP :: ([a] -> a) -> [a] -> [a]
func_iterP f = \as -> as ++ go as
where go xs | x <- f xs = x : go (xs ++ [x])
func_rep :: a -> [a]
func_rep = repeat
func_ord :: Char -> TNum
func_ord = fromIntegral.ord
func_chr :: TNum -> Char
func_chr = chr.fromInteger.toInteger
func_show :: Concrete a => a -> String
func_show = show
func_empty :: [a]
func_empty = []
func_predN :: TNum -> TNum
func_predN n = n-1
func_succN :: TNum -> TNum
func_succN n = n+1
func_predC :: Char -> Char
func_predC = pred
func_succC :: Char -> Char
func_succC = succ
func_elem :: Concrete a => [a] -> a -> TNum
func_elem xs x | Just i <- elemIndex x xs = fromIntegral i + 1
| otherwise = 0
func_elem' :: Concrete a => a -> [a] -> TNum
func_elem' = flip func_elem
func_sort :: Concrete a => [a] -> [a]
func_sort = sort
func_sorton :: Concrete b => (a -> b) -> [a] -> [a]
func_sorton = sortOn
-- f x y means x is greater then y
func_sortby :: Concrete b => (a -> a -> b) -> [a] -> [a]
func_sortby f xs = sortOn (\x -> length [y | y <- xs, isTruthy $ f x y]) xs
func_max :: Concrete a => a -> a -> a
func_max = max
func_min :: Concrete a => a -> a -> a
func_min = min
func_maxl :: Concrete a => [a] -> a
func_maxl = foldr max func_minval
func_minl :: Concrete a => [a] -> a
func_minl = foldr min func_maxval
func_del :: Concrete a => a -> [a] -> [a]
func_del = delete
func_diffl :: Concrete a => [a] -> [a] -> [a]
func_diffl [] xs = xs
--first option: multiset difference
func_diffl (y:ys) xs = func_diffl ys $ func_del y xs
--second option: filter elements (disregards multiciplities)
--func_diffl (y:ys) xs = func_diffl ys $ filter (/=y) xs
func_nub :: Concrete a => [a] -> [a]
func_nub = nub
func_nubon :: Concrete b => (a -> b) -> [a] -> [a]
func_nubon f = nubBy (\x y -> f x == f y)
func_nubby :: Concrete b => (a -> a -> b) -> [a] -> [a]
func_nubby f = nubBy (\x y -> isTruthy $ f x y)
func_words :: [Char] -> [[Char]]
func_words = words
func_unwords :: [[Char]] -> [Char]
func_unwords = unwords
func_lines :: [Char] -> [[Char]]
func_lines = lines
func_unlines :: [[Char]] -> [Char]
func_unlines = unlines
func_pfac :: TNum -> [TNum]
func_pfac = factorize 2
where factorize _ 1 = []
factorize d n
| d * d > n = [n]
| n `mod` d == 0 = d : factorize d (n `div` d)
| otherwise = factorize (d + 1) n
func_subs :: Concrete a => a -> a -> [a] -> [a]
func_subs x y = map (\z -> if z == x then y else z)
func_subs2 :: Concrete a => [a] -> [a] -> [a] -> [a]
func_subs2 _ _ [] = []
func_subs2 x y s@(h:t) | Just s2 <- stripPrefix x s = y++func_subs2 x y s2
| otherwise = h : func_subs2 x y t
func_group :: Concrete a => [a] -> [[a]]
func_group = group
func_groupOn :: Concrete b => (a -> b) -> [a] -> [[a]]
func_groupOn f = groupBy (\x y -> f x == f y)
func_groupBy :: Concrete b => (a -> a -> b) -> [a] -> [[a]]
func_groupBy _ [] = []
func_groupBy p (a:as) = consHead a $ go a as
where go _ [] = [[]]
go a (x:xs) | isTruthy $ p a x = consHead x $ go x xs
| otherwise = [] : consHead x (go x xs)
consHead x ~(xs:xss) = (x:xs):xss
func_perms :: [a] -> [[a]]
func_perms = permutations
func_subl :: Concrete a => [a] -> [a] -> TNum
func_subl super sub = subindex 1 super sub
where subindex i _ [] = i
subindex i super@(_:xs) sub = if sub`isPrefixOf`super then i else subindex (i+1) xs sub
subindex _ [] _ = 0
-- Index of first truthy, or 0
func_any :: Concrete b => (a->b) -> [a] -> TNum
func_any f = go 1
where go _ [] = 0
go n (x:xs)
| isTruthy $ f x = n
| otherwise = go (n+1) xs
-- Length + 1 or 0
func_all :: Concrete b => (a->b) -> [a] -> TNum
func_all f = go 1
where go n [] = n
go n (x:xs)
| isTruthy $ f x = go (n+1) xs
| otherwise = 0
func_trsp :: [[a]] -> [[a]]
func_trsp = transpose
--Transpose with -> turns into a square matrix before transposing, padding with the given element
func_trspw :: a -> [[a]] -> [[a]]
func_trspw padding rows | all null rows = []
| otherwise = map (headwith padding) rows : func_trspw padding (map (drop 1) rows)
where headwith _ (x:_) = x
headwith padding _ = padding
--Zip, but keep extra elements from the longer list unaltered
func_zip' :: (a -> a -> a) -> [a] -> [a] -> [a]
func_zip' _ [] ys = ys
func_zip' _ xs [] = xs
func_zip' f (x:xs) (y:ys) = f x y : func_zip' f xs ys
func_cmap :: (a -> [b]) -> [a] -> [b]
func_cmap = concatMap
func_smap :: (a -> TNum) -> [a] -> TNum
func_smap f = sum . map f
func_cmapr :: [(a -> [b])] -> a -> [b]
func_cmapr fs = concat . func_mapr fs
func_smapr :: [(a -> TNum)] -> a -> TNum
func_smapr fs = sum . func_mapr fs
func_combin :: (b -> b -> c) -> (a -> b) -> a -> a -> c
func_combin f g x y = f (g x) (g y)
func_n2i :: TNum -> TNum
func_n2i x = func_floor $ x+1/2 --round halves towards positive infinity
func_c2i :: Char -> TNum
func_c2i c | Just i <- elemIndex c "0123456789" = fromIntegral i
| otherwise = 0
-- Read the first number found in the string, or 0 if nothing found
func_s2i :: String -> TNum
func_s2i s = case takeWhile C.isDigit $ dropWhile (not . C.isDigit) s of
"" -> 0
x -> read x
func_list2 :: a -> a -> [a]
func_list2 x y = [x,y]
func_list3 :: a -> a -> a -> [a]
func_list3 x y z = [x,y,z]
func_list4 :: a -> a -> a -> a -> [a]
func_list4 x y z t = [x,y,z,t]
func_nubw :: Concrete a => [a] -> [a]
func_nubw xs = go [] xs
where go ys (x:xs) | elem x ys = []
| otherwise = x:go (x:ys) xs
go _ [] = []
func_table :: (a -> b -> c) -> [a] -> [b] -> [[c]]
func_table f as bs = [[f a b | b <- bs] | a <- as]
func_lmap :: (a -> b -> c) -> [a] -> b -> [c]
func_lmap f as b = map (flip f b) as
func_rmap :: (a -> b -> c) -> a -> [b] -> [c]
func_rmap f a = map (f a)
func_mapacL :: (a -> b -> a) -> (a -> b -> c) -> a -> [b] -> [c]
func_mapacL _ _ _ [] = []
func_mapacL f g x (y:ys) = g x y : func_mapacL f g (f x y) ys
func_mapacR :: (b -> a -> a) -> (b -> a -> c) -> a -> [b] -> [c]
func_mapacR _ _ _ [] = []
func_mapacR f g x (y:ys) = g y (foldr f x ys) : func_mapacR f g x ys
func_replic :: TNum -> a -> [a]
func_replic n = replicate $ fromInteger $ toInteger n
func_replif :: a -> TNum -> [a]
func_replif = flip func_replic
func_abs :: TNum -> TNum
func_abs = abs
func_sign :: TNum -> TNum
func_sign = signum
-- x y -> base-x digits of y
func_base :: TNum -> TNum -> [TNum]
func_base 0 n = [n]
func_base 1 n
| (d, m) <- divMod n 1, m /= 0 = func_base 1 d ++ [m]
| otherwise = replicate (fromInteger $ toInteger $ abs n) $ signum n
func_base (-1) n
| (d, m) <- divMod n 1, m /= 0 = go (func_base (-1) d) m
| n > 0 = func_take (2*abs n - 1) $ cycle [1, 0]
| otherwise = func_take (2*abs n) $ cycle [1, 0]
where go [] m = [m]
go [0] m = [m]
go [k] m = [k,0,m]
go (k:ks) m = k : go ks m
func_base b n = reverse $ go n
where go m | m >= 0 || b > 0, abs m < abs b = [m]
| (d, r) <- divMod m b =
if r >= 0 || b > 0
then r : go d
else r-b : go (d+1)
func_base2 :: TNum -> [TNum]
func_base2 = func_base 2
func_base10 :: TNum -> [TNum]
func_base10 = func_base 10
-- x y -> y interpreted in base x
func_abase :: TNum -> [TNum] -> TNum
func_abase b ds = sum [b^n * d | (n, d) <- zip [0..] $ reverse ds]
func_abase2 :: [TNum] -> TNum
func_abase2 = func_abase 2
func_abas10 :: [TNum] -> TNum
func_abas10 = func_abase 10
func_double :: TNum -> TNum
func_double = (* 2)
func_halve :: TNum -> TNum
func_halve n = n / 2
-- a b -> b^a
func_power :: TNum -> TNum -> TNum
func_power (m :% 1) n
| m >= 0 = n^m
| otherwise = n^^m
func_power m n = n**m
func_square :: TNum -> TNum
func_square n = n * n
-- Should return a rational if input is a perfect square