This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
In the Essence of Automatic Differentiation, Conal defines | |
D+ :: (a -> b) -> a -> (b, a -o b) | |
D+(f,a) = (f(a), D(f,a)) | |
where a and b are vector spaces, -o is the space of linear functions, and D is | |
the derivative operation. | |
But to me this suffers the same problem as considering numbers to be the | |
derivatives of 1D spaces functions, vectors/matrices for higher dimensions, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE TypeApplications #-} | |
{-# LANGUAGE TypeFamilies #-} | |
import Type.Reflection | |
data Foo = Int Int | Bool Bool | |
wrap :: Typeable b => b -> Maybe Foo | |
wrap b | |
| Just HRefl <- eqTypeRep t (typeRep @Int) = Just (Int b) | |
| Just HRefl <- eqTypeRep t (typeRep @Bool) = Just (Bool b) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sums :: Num a => [a] -> (a, [a]) | |
sums [] = (0, []) | |
sums (x:xs) = let ~(v,vs) = sums xs; v' = v+x in v' `seq` (v',v':vs) | |
main :: IO () | |
main = print (fst (sums [0..1000000 :: Int])) | |
-- OR | |
-- main = print (last (scanl1 (+) [0..1000000 :: Int])) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Test.hs | |
{-# LANGUAGE PatternSynonyms #-} | |
module Test (Foo(Bar,Baz)) where | |
data Foo = Bar | |
pattern Baz = Bar | |
-- whatever-else.hs | |
import Test (Foo(Baz)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% tree | |
. | |
├── a | |
│ └── a.cabal | |
└── b | |
├── b.cabal | |
└── cabal.project.local | |
2 directories, 3 files | |
% cat a/a.cabal |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> mapM_ (print . pretty) $ runInfer (SourceApp (SourceApp (SourceVar "plus") (SourceVar "readLn")) (SourceVar "readLn")) | |
let appPMPPM f x = pure (f (pure x)) | |
appMaMaM f x = f >>= ($x) | |
appMMMPM f x = f >>= ($pure x) | |
appMPMMM f x = f >>= (x>>=) | |
appMaPaM f x = fmap ($x) f | |
appMMPPM f x = fmap ($pure x) f | |
in | |
(<*>) (fmap plus readLn) readLn | |
:: m _ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Custom ( | |
showEven, | |
showBoolean | |
) where | |
showEven:: Int-> Bool | |
showEven x = x `rem` 2 == 0 | |
showBoolean :: Bool->Int | |
showBoolean c = if c then 1 else 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
c (1, 1) = 1 | |
c (2, 2) = 1 | |
c (i, 1) | i > 2 = -(a ! (i-2, 1)) | |
c (i, j) | i == j = 2 * (a ! (i-1, j-1)) | |
| i > j && j > 1 = 2 * (a ! (i-1, j-1)) - (a ! (i-2, j-1)) | |
| otherwise = 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
derangements :: Integer -> Integer | |
derangements n = id | |
. sum | |
. zipWith (*) signs | |
. scanl (*) 1 | |
$ [n,n-1..1] | |
where | |
signs = drop (n .&. 1) (cycle [1,-1]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
newtype A = A Int | |
newtype B = B Int | |
type Database = String | |
type JSON = String | |
class ParseDatabase a where parse :: Database -> Maybe a | |
instance ParseDatabase A where parse db = A <$> readMaybe s | |
instance ParseDatabase B where parse db = B <$> readMaybe s |