Skip to content

Instantly share code, notes, and snippets.

View chpatrick's full-sized avatar

Patrick Chilton chpatrick

View GitHub Profile
@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
@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