Skip to content

Instantly share code, notes, and snippets.

@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 -> [a]) -> (forall a a'. [a] -> (a -> [a']) -> [a']) -> (forall a. String -> [a]) -> xxx
@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 / 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 :|

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 / 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
 
@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 / MultipleInstances.markdown
Created May 27, 2017 16:48
Define multiple instances simultaneously
@Icelandjack
Icelandjack / id.markdown
Last active June 25, 2017 06:59
Identity function, id :: a -> a

Principle of identity expansion

Whenever some construction has a certain relationship to all constructions of the same kind within a category, it must, in particular, have this relationship to itself. Socrates’ dictum to “know thyself” is as important in category theory as it is in life. So whenever we encounter a universal construction we will see what we can learn about it by “probing it with itself”. In the case of a terminal object, this means choosing X ≔ T in the definition.

Lemma 3.1.1.2 (identity expansion for terminals) If T is a terminal object then !(T) = id(T).

Lemma 3.1.4.2 (identity expansion for initials) If S is an initial object then ¡(S) = id(S).

@Icelandjack
Icelandjack / Deriving.hs
Last active July 2, 2017 13:42
Template Haskell deriving, first implementation
{-# Language InstanceSigs, ViewPatterns, TupleSections, GeneralizedNewtypeDeriving, TemplateHaskell, LambdaCase #-}
module D where
import Language.Haskell.TH
import Data.Coerce
deriveVia :: Name -> Name -> Name -> Q [Dec]
deriveVia className ty viaNewTy = do
a <- reify className