Skip to content

Instantly share code, notes, and snippets.

View chessai's full-sized avatar

chessai chessai

View GitHub Profile
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE UnboxedTuples #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeInType #-}
-- | Conversion between unlifted and lifted datatypes
module Packed.Levity
( -- * Types
Rep
, Levity(..)
@chessai
chessai / overwrite.hs
Created October 20, 2018 00:44
Overwrite.hs
hPutMutableByteArray#
:: SIO.Handle
-> MutableByteArray# RealWorld -- ^ Invariant MUST BE PINNED
-> Int#
-> State# RealWorld
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE MagicHash #-}
module Strict (idInt, idIntCon) where
import GHC.Types
idIntCon :: Int -> Int
idIntCon (I# x) = (I# x)
@chessai
chessai / haddock.sh
Created October 25, 2018 22:03
Sometimes haddocks fail to build, even when your package builds. This script will build and upload the haddocks for you, without needing to re-upload the package or do some weird revision stuff
#!/bin/sh
set -e
dir=$(mktemp -d dist-new-docs.XXXXXX)
trap 'rm -r "$dir"' EXIT
cabal new-haddock --builddir="$dir" --haddock-for-hackage --haddock-option=--hyperlinked-source
# Starting with cabal 2.0, `--publish` is needed for uploading to non-candidate releases
cabal upload --publish -d $dir/*-docs.tar.gz
data Ordered a = Empty | Decreasing a | Increasing a
inc :: Ordered a -> Bool
inc (Decreasing _) = False
inc _ = True
dec :: Ordered a -> Bool
dec (Increasing _) = False
dec _ = True
[nix-shell:~/development/nixpkgs]$ nix repl default.nix
Welcome to Nix version 2.0.4. Type :? for help.
Loading 'default.nix'...
Added 9659 variables.
nix-repl> :b haskell.compiler.ghcLinear
these derivations will be built:
/nix/store/pifcl83nn43wkvsdwg1k4m089g44jqvl-hscolour-1.24.4.drv
/nix/store/77psczhb9v2j3k2av0pj52vc886v86xm-hscolour-1.24.4.drv
tryAll :: IO a -> IO (Either SomeException a)
tryAll = try
forkIO_ :: IO () -> IO ()
forkIO_ x = forkIO x >> pure ()
foldCommuteIO :: forall t m a. (Foldable t, Monoid m) => (a -> IO m) -> t a -> IO m
foldCommuteIO f xs = do
var <- newEmptyMVar
total <- foldlM (\ !n a -> forkIO_ ( tryAll (f a) >>= putMVar var) >> pure (n + 1)) 0 xs
@chessai
chessai / cool.hs
Last active November 13, 2018 21:13
import Data.Functor.Compose
doubleFmap :: (Functor f, Functor g) => (a -> b) -> f (g a) -> f (g b)
doubleFmap = fmap . fmap
fooMonad :: (Monad f, Functor g) => f (a -> b) -> f (g a) -> f (g b)
fooMonad fab fga = fab >>= \f -> doubleFmap f fga
fooApplicative :: (Applicative f, Applicative g) => f (a -> b) -> f (g a) -> f (g b)
fooApplicative fab fga = getCompose (Compose (fmap pure fab) <*> Compose fga)
mkArg :: Int -> String
mkArg n = "arg" ++ show n
mkDegenerate :: Int -> Int -> String
mkDegenerate start end = "f " ++ (mconcat $ intersperse "@" $ map mkArg [start..end]) ++ " = " ++ mkArg end
newtype Branch (o :: OS) e m a = Branch (ExceptT e m a)
deriving
( Applicative, Eq, Foldable, Functor, Monad, Traversable
)
branchToExceptT :: Branch o e m a -> ExceptT e m a
branchToExceptT = coerce