Skip to content

Instantly share code, notes, and snippets.

@Icelandjack
Icelandjack / MultipleInstances.markdown
Created May 27, 2017 16:48
Define multiple instances simultaneously
@Icelandjack
Icelandjack / MonoidToSemigroup.markdown
Last active January 22, 2018 19:16
Monoid to Semigroup
demoteMonoid :: (forall m. Monoid m => m) -> Semigroup m => Maybe m
demoteMonoid k = eval k where
  eval :: MONOID m -> Semigroup m => Maybe m
  eval = getOption . foldMap (Option . Just)

data MONOID a = NIL | SING a | MONOID a :<> MONOID a
  deriving Foldable

instance Monoid (MONOID a) where
@Icelandjack
Icelandjack / Methods.markdown
Created May 27, 2017 13:37
Type Family for Type Class Methods

This could be synthesised

type family   Methods (cls :: Type -> Constraint) (a :: Type) = (res :: [Pair Symbol Type]) | res -> a
type instance Methods A a = '["a" :- a]
type instance Methods B b = '["b" :- (b -> b)]
type instance Methods C c = '["c" :- (c -> Int), "b" :- (c -> c), "a" :- c]

data Pair a b = a :- b
 

1

Comparing IO a for equality, cannot be captured by Bool.

class EQ a where
  type Logic a
  (=~=) :: a -> a -> Logic a 
  
instance EQ Int where
@Icelandjack
Icelandjack / 12001.markdown
Last active July 27, 2017 09:13
GHC Trac #12001: Add pattern synonyms to base

Ticket #12001.

Data.List.NonEmpty

pattern Singleton :: a -> NonEmpty a
pattern Singleton a <- (uncons -> (a, Nothing))
  where Singleton a = a N.:| []

infixr 5 :|
@Icelandjack
Icelandjack / 11439.markdown
Created May 17, 2017 13:46
GHC Trac #11439 (case signatures?)

Good example from Eisenberg:

type Effect = Type -> Type -> Type -> Type

class Handler (e :: Effect) (m :: Type -> Type) where
  handle :: e res res' t -> res -> (res' -> t -> m a) -> m a  

For Random

@Icelandjack
Icelandjack / RecordPatternDictionary.markdown
Last active August 7, 2017 09:09
Open Monad dictionary with record pattern synonym (and as patterns)
{-# Language PatternSynonyms #-}
{-# Language ViewPatterns    #-}

data DMonad m = DMonad
  (forall a.   a -> m a)
  (forall a b. m a -> (a -> m b) -> m b)
  (forall a.   String -> m a)

pattern Open :: (forall a. a -&gt; [a]) -&gt; (forall a a'. [a] -&gt; (a -&gt; [a']) -&gt; [a']) -&gt; (forall a. String -&gt; [a]) -&gt; xxx
@Icelandjack
Icelandjack / NewtypeDeriving.markdown
Last active April 4, 2023 04:49
Newtype Deriving

One of the best parts of Haskell is getting functionality for free

newtype T a = T_ (Compose [] Maybe a) 
  deriving (Functor, Foldable, Traversable, Applicative, Alternative)

{-# Complete T #-}
pattern T :: [Maybe a] -> T a
pattern T a = T_ (Compose a)

HList : Sublist -> Hask

type Cat k = k -> k -> Type

data Sublist :: Cat [a] where
  Stop :: Sublist '[] '[]
  Drop :: Sublist xs ys -> Sublist (x:xs) ys
  Keep :: Sublist xs ys -> Sublist (x:xs) (x:ys)