Skip to content

Instantly share code, notes, and snippets.

View chpatrick's full-sized avatar

Patrick Chilton chpatrick

View GitHub Profile
@chpatrick
chpatrick / gist:7358782
Last active October 26, 2016 19:15
Haskell checked exceptions
{-# LANGUAGE TypeFamilies, KindSignatures, DataKinds, TypeOperators, GADTs, MultiParamTypeClasses, FlexibleInstances, GeneralizedNewtypeDeriving, OverlappingInstances, ScopedTypeVariables, FlexibleContexts #-}
import Control.Applicative
import Control.DeepSeq
import qualified Control.Exception as E
-- Closed type family, needs GHC HEAD.
type family Minus (e :: *) (es :: [*]) :: [*] where
Minus e '[] = '[]
Minus e (e ': es) = Minus e es
@chpatrick
chpatrick / Methods.hs
Created January 1, 2014 18:28
Haskell "Methods" - computations that can either take the "object" they operate on as a parameter, or from the ReaderT monad. Useful when the user wants to perform multiple monadic operations on an immutable value. The downside is that it complicates the types of the functions themselves.
{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances, FlexibleContexts, FunctionalDependencies #-}
import Control.Monad.Reader
class Method o m a f | f -> o m a where
on :: (o -> m a) -> f
instance Method o m a (o -> m a) where
on = id
import numpy as np
import re
import sys
'''
Load a PFM file into a Numpy array. Note that it will have
a shape of H x W, not W x H. Returns a tuple containing the
loaded image and the scale factor from the file.
'''
def load_pfm(file):
{-# LANGUAGE MultiParamTypeClasses, TypeFamilies, GeneralizedNewtypeDeriving, FlexibleInstances, LambdaCase, DeriveFunctor, ConstraintKinds #-}
import Control.Applicative
import Control.Monad.State
import Control.Monad.Trans
import qualified Data.ByteString as BS
import Data.Conduit
import Data.Monoid
import qualified Data.Sequence as S
import Data.Word
@chpatrick
chpatrick / marshal.hs
Last active August 29, 2015 13:57
Automatic type-safe binding generation sketch (GHC HEAD)
{-# LANGUAGE TypeFamilies, GADTs, DataKinds, TypeOperators, FlexibleInstances, OverlappingInstances, ConstraintKinds, MultiParamTypeClasses, ForeignFunctionInterface #-}
import Control.Applicative
import Foreign
import Foreign.C
-- heterogenous list
data HList (ts :: [ * ]) where
E :: HList '[]
(:.) :: t -> HList ts -> HList (t ': ts)
@chpatrick
chpatrick / typeable-reify
Last active September 2, 2017 07:53
Black magic and GADT serialization
# Black magic and GADT serialization
I thought it would be a cool exercise to write a simple but flexible RPC system in Haskell. The idea was that the server would have some functions and values available for you to use, and you can assemble them into a function call as you like, or provide your own parameters. In the end, writing the serialization and deserialization code ended up leading to some far more interesting challenges.
Some types, such as functions, cannot be serialized so they must be made available on the server. I represented this with the following GADT:
```haskell
type ValueID = Word32
type TypeID = Word32
{-# LANGUAGE OverloadedStrings, NamedFieldPuns, LambdaCase #-}
import Control.Applicative
import Data.List
import Data.Map (Map)
import Data.Maybe
import qualified Data.Map as M
import Data.Ord
import Data.Text (Text)
import qualified Data.Text as T
@chpatrick
chpatrick / hexbench.hs
Created November 5, 2014 21:21
BS -> Hex
{-# LANGUAGE OverloadedStrings #-}
module Main(main, toHexNice) where
import Control.Monad
import Criterion.Main
import Data.Bits
import Data.Monoid
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as LBS
@chpatrick
chpatrick / baked.hs
Last active August 10, 2021 18:11
Baked-in Storable vectors
{-# LANGUAGE QuasiQuotes, TemplateHaskell, ScopedTypeVariables #-}
module BakedVector where
import qualified Data.ByteString.Lazy as BSL
import Data.ByteString.Lazy (unpack)
import Data.ByteString.Builder (toLazyByteString)
import Data.ByteString.Builder.Prim
import Foreign.Storable
import qualified Data.Vector.Storable as VS
@chpatrick
chpatrick / rechunk.hs
Created February 12, 2015 13:11
Generalized Conduit re-chunking
{-# LANGUAGE DefaultSignatures, LambdaCase #-}
import Conduit
import qualified Data.ByteString as BS
import qualified Data.DList as DL
import Data.Monoid
import Data.Sequences as S
import qualified Data.Vector as V
import qualified Data.Vector.Storable as SV
import qualified Data.Vector.Unboxed as UV