Skip to content

Instantly share code, notes, and snippets.

View dmwit's full-sized avatar

Daniel Wagner dmwit

View GitHub Profile
@dmwit
dmwit / bf.hs
Created April 11, 2019 15:20
simple symbolic execution for straight-line bf
import Data.Int
import Data.IntMap (IntMap)
import qualified Data.IntMap as IM
data CellUpdate = CellUpdate
{ inputID :: Maybe Int
, offset :: Int8
} deriving (Eq, Ord, Read, Show)
instance Monoid CellUpdate where mempty = CellUpdate Nothing 0
@dmwit
dmwit / xmonad.hs
Created April 15, 2019 22:34
how to xmonad graceful exit
import Control.Concurrent
import Control.Monad.State
import qualified Data.Map as M
import System.Exit
import XMonad.Core
import XMonad.Operations
import qualified XMonad.StackSet as W
windowIDs :: W.StackSet i l a sid sd -> [a]
windowIDs ss
{-# Language
Rank2Types,
GADTs,
DataKinds,
PolyKinds,
TypeFamilies,
DatatypeContexts,
MultiParamTypeClasses,
UndecidableInstances,
UndecidableSuperClasses
-- bad: everything in ... is nested
do
v <- foo
case v of
Matches v' -> ...
_ -> throwError "doesn't match"
-- good: ... is not nested
do
v <- foo
@dmwit
dmwit / 83601.cry
Created May 4, 2019 17:27
cryptol code to solve https://puzzling.stackexchange.com/q/83601/8871; use `cryptol 83601.cry -c ':sat isSolution'` to run it
isSolution w o n e = all isDigit [w,o,n,e] /\ no*won*won == wonewon /\ no != 0 where
no = fromDigits [o,n]
won = fromDigits [n,o,w]
wonewon = fromDigits [n,o,w,e,n,o,w] : [32]
isDigit x = 0 <= x /\ x <= 9
fromDigits digits = sum [digit * 10^^exp | digit <- digits | exp <- [0...]]
@dmwit
dmwit / gmonoid.hs
Created May 8, 2019 17:48
maybe a way to use generic-deriving
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
import Generics.Deriving.Monoid
import Generics.Deriving.Semigroup
import GHC.Generics
data Foo = Foo () deriving (Generic, GSemigroup, GMonoid)
instance Semigroup Foo where (<>) = gsappend
instance Monoid Foo where mempty = gmempty
data RawDebtRec = RawDebtRec
{ company :: Text
, debt :: Int
, phones :: [Int]
}
parseCompany o = o .: "company"
<|> o .: "company" >>= (.: "name")
parseInt o = parseJSON o <|> (read <$> parseJSON o)

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:

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]
@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 :: * -> *