Skip to content

Instantly share code, notes, and snippets.

View dmwit's full-sized avatar

Daniel Wagner dmwit

View GitHub Profile
@dmwit
dmwit / Custom.hs
Created September 13, 2019 23:45
a better tutorialspoint
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
> 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 _
@dmwit
dmwit / terminal.txt
Created August 17, 2019 19:23
what am I doing wrong?
% tree
.
├── a
│   └── a.cabal
└── b
├── b.cabal
└── cabal.project.local
2 directories, 3 files
% cat a/a.cabal
-- Test.hs
{-# LANGUAGE PatternSynonyms #-}
module Test (Foo(Bar,Baz)) where
data Foo = Bar
pattern Baz = Bar
-- whatever-else.hs
import Test (Foo(Baz))
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]))
{-# 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)
@dmwit
dmwit / Dd.txt
Last active July 8, 2019 21:25
automatic differentiation comment
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,
@dmwit
dmwit / reader-alt
Created July 2, 2019 15:28
With MonadReader (and other mtl classes), one thing you can't do is polymorphically reach "under" a given part of the transformer stack. For example, there's no polymorphic way to say "I want to run `ask`, but not the outer-most `ask`, the next one down". Here's an idea for how to have that possibility.
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE KindSignatures #-}
import Control.Monad.Reader (ReaderT)
import Control.Monad.State (StateT)
import qualified Control.Monad.Reader as R
import qualified Control.Monad.State as S
class Monad m => DmwitMonadReader m where
type Env m
type Wrapped m :: * -> *
import Data.Map (Map)
import qualified Data.Map as M
partitionM :: Applicative f => (a -> f Bool) -> [a] -> f ([a], [a])
partitionM f xs = mconcat <$> traverse inject xs where
inject x = (\b -> ([x | b], [x | not b])) <$> f x
partitions :: Ord a => [a] -> [Map a Integer]
partitions = go 0 where
go _ [] = [M.empty]

Keybase proof

I hereby claim:

  • I am dmwit on github.
  • I am dmwit (https://keybase.io/dmwit) on keybase.
  • I have a public key whose fingerprint is FBCF E1CE 584E 46E2 CEC5 692A 620F C421 171F 63FF

To claim this, I am signing this object: