Skip to content

Instantly share code, notes, and snippets.

@Icelandjack
Icelandjack / blog_deriving.markdown
Last active October 7, 2019 22:43
Blog Post: Derive instances of representationally equal types

Reddit discusson thread.

I made a way to get more free stuff and free stuff is good.

The current implementation of deriveVia is here, it works with all the examples here. Needs GHC 8.2 and th-desugar.

It doesn't take long

for new Haskellers to get pampered by their compiler. For the price of a line or two the compiler offers to do your job, to write uninteresting code for you (in the form of type classes) such as equality, comparison, serialization, ... in the case of 3-D vectors

@Icelandjack
Icelandjack / repvia.hs
Created September 26, 2019 19:18
Deriving Comonad through many different ways
newtype (f `RepVia` via) a = RepVia (f a)
instance (Representable f, Coercible via (Rep f)) => Functor (f `RepVia` via) where
fmap :: forall a b. (a -> b) -> ((f `RepVia` via) a -> (f `RepVia` via) b)
fmap = coerce $ fmapRep @f @a @b
instance (Representable f, Coercible via (Rep f)) => Distributive (f `RepVia` via) where
collect :: forall g a b. Functor g => (a -> (f `RepVia` via) b) -> (g a -> (f `RepVia` via) (g b))
collect = coerce (collect @f @g @a @b)
@Icelandjack
Icelandjack / Notation.markdown
Last active July 6, 2019 18:37
Notes on Notation

Non-standard operators I use :) linked to from my twitter profile

I use kind synonyms

type T   = Type
type TT  = T -> T
type TTT = T -> TT
type C   = Constraint
type TC  = T -> C

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)
@Icelandjack
Icelandjack / Row_types.markdown
Last active March 13, 2019 03:51
Row Types for Type Classes

Type classes in terms of row types

type Semigroup s = { Monoid s with mappend :: s -> s -> s | _ }

type Pointed f = { Applicative f with pure  :: forall a. a -> f a | _ }
type Apply   f = { Applicative f with (<*>) :: forall a b. f (a -> b) -> f a -> f b | _ }
type Bind    m = { Monad       m with (>>=) :: forall a b. m a -> (a -> m b) -> m b | _ }
@Icelandjack
Icelandjack / ComonadVia.hs
Created March 8, 2019 12:19
DerivingVia (Co Any Pair) and (Co All Pair)
{-# Language DeriveFunctor #-}
{-# Language DerivingVia #-}
{-# Language FlexibleContexts #-}
{-# Language FlexibleInstances #-}
{-# Language GADTs #-}
{-# Language GeneralizedNewtypeDeriving #-}
{-# Language InstanceSigs #-}
{-# Language ScopedTypeVariables #-}
{-# Language TypeApplications #-}
{-# Language TypeFamilies #-}
@Icelandjack
Icelandjack / Category_Theory.markdown
Last active January 6, 2019 14:45
Category Theory: "Think bigger thoughts"

http://www.cs.ox.ac.uk/people/bob.coecke/AbrNikos.pdf

Why study categories—what are they good for? We can offer a range of answers for readers coming from different backgrounds:

  • For mathematicians: category theory organises your previous mathematical experience in a new and powerful way, revealing new connections and structure, and allows you to “think bigger thoughts”.
  • For computer scientists: category theory gives a precise handle on important notions such as compositionality, abstraction, representationindependence, genericity and more. Otherwise put, it provides the fundamental mathematical structures underpinning many key programming concepts.
  • For logicians: category theory gives a syntax-independent view of the fundamental structures of logic, and opens up new kinds of models and interpretations.
  • For philosophers: category theory opens up a fresh approach to structuralist foundations of mathematics and science; and an alternative to the traditional focus on set theory
  • For **p
@Icelandjack
Icelandjack / Kleisli_Category.hs
Last active January 6, 2019 11:20
Kind-indexed Category instance for Kleisli
-- https://www.reddit.com/r/haskell/comments/abxem5/experimenting_with_kleisli_instance_of/
{-# Language TypeApplications #-}
{-# Language RankNTypes #-}
{-# Language DataKinds #-}
{-# Language KindSignatures #-}
{-# Language PolyKinds #-}
{-# Language TypeOperators #-}
{-# Language GADTs #-}
{-# Language TypeFamilies #-}
@Icelandjack
Icelandjack / ZipWith.markdown
Last active December 29, 2018 09:37
Variable-arity zipWith in terms of Applicative ZipList

I was implementing "variable-arity zipWith" from Richard Eisenberg's thesis (recommended) when I noticed it used

apply :: [a -> b] -> [a] -> [b]
apply (f:fs) (a:as) = f a : apply fs as
apply _      _      = []

and

repeat :: a -> [a]
@Icelandjack
Icelandjack / SystemF.hs
Last active November 27, 2018 02:00
System F
-- SYSTEM F
-- http://homepages.inf.ed.ac.uk/slindley/papers/embedding-f.pdf
--
-- Type-level lambdas
-- https://gist.github.com/AndrasKovacs/ac71371d0ca6e0995541e42cd3a3b0cf
{-# language TemplateHaskell, ScopedTypeVariables, RankNTypes,
TypeFamilies, UndecidableInstances, DeriveFunctor, GADTs,
TypeOperators, TypeApplications, AllowAmbiguousTypes,