Skip to content

Instantly share code, notes, and snippets.

View kuribas's full-sized avatar

Kristof Bastiaensen kuribas

View GitHub Profile
@kuribas
kuribas / Subtype.hs
Last active July 21, 2018 09:31
haskell inheritance
{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances, FlexibleContexts #-}
module Subtype where
data Derived super sub = Derived super sub
class IsInstance b a where
getData :: (a -> v) -> b -> v
setData :: (a -> s -> s) -> b -> s -> s
instance IsInstance (Derived a b) a where
@kuribas
kuribas / chessvariant.hs
Created August 4, 2018 07:52
chess variant
{-# LANGUAGE TypeFamilies, FlexibleContexts #-}
module ChessVariant where
import Data.Proxy
data Square = Square Int Int Int
newtype Side = Side Int
data Piece variant = Piece (PieceType variant) Side
data MoveStep variant =
@kuribas
kuribas / lookup.hs
Created August 11, 2018 20:57
lookup
{-# LANGUAGE DataKinds, KindSignatures, TypeOperators, TypeFamilies, GADTs,
MultiParamTypeClasses, FunctionalDependencies, ExplicitForAll,
FlexibleInstances, FlexibleContexts, UndecidableInstances,
ScopedTypeVariables, TypeApplications
#-}
module Fossa where
import GHC.TypeLits
import Data.Proxy
-- Determine if s matches the symbol in the first tuple of the type
@kuribas
kuribas / SimpleParser.hs
Last active September 20, 2018 06:59
simple parser
module SimpleParser where
import Text.Parsec
import Data.Text as T
import Data.Char
type SimpleParser a b = Parsec [a] () b
parseMaybe :: Show a => (a -> Maybe b) -> SimpleParser a b
parseMaybe f = tokenPrim show (\s _ _ -> incSourceColumn s (sourceColumn s)) f
@kuribas
kuribas / forall.hs
Last active November 14, 2018 10:46
forall in type
{-# LANGUAGE RankNTypes #-}
module Test where
type MyType a = forall m. Monad m => m a
test :: MyType Int
test = pure 3
x :: IO Int
x = test
{-# LANGUAGE DeriveFunctor #-}
module Test where
import Data.Functor.Foldable
import Control.Monad.Free
import qualified Control.Monad.Trans.Free as T
data Tree t = Tree t t
deriving Functor
-- this is the same as Functor fmap, but that instance cannot be
-- written, since the type variable is one level too deep.
fixmap :: (Functor (f a), Bifunctor f) => (a -> b) -> Fix (f a) -> Fix (f b)
fixmap f = cata (Rec . first f)
@kuribas
kuribas / linkGraph.hs
Created March 14, 2019 08:52
Graph tie the knot
module LinkGraph where
import Data.IntMap
import Data.Function
import Data.Maybe
import Prelude hiding (lookup)
data Fix f = Rec { unRec :: f (Fix f)}
unsafeLinkGraph :: Functor f => IntMap (f Int) -> IntMap (Fix f)
unsafeLinkGraph g =
@kuribas
kuribas / simplifysum.hs
Created July 21, 2019 11:45
simplify sum
data Sum = Plus Sum Sum
| Lit Integer
| Var String
deriving Show
simplify :: Sum -> Sum
simplify (Var a) = Var a
simplify (Lit a) = Lit a
simplify (Plus (Lit 0) a) = simplify a
simplify (Plus a (Lit 0)) = simplify a
@kuribas
kuribas / sums-extract-groups.hs
Last active September 25, 2019 11:06
find subsets with sum
{-# LANGUAGE ScopedTypeVariables #-}
module Sums where
import Data.IntMap.Lazy (IntMap)
import Data.Maybe
import Data.Monoid ((<>))
import qualified Data.IntMap as IntMap
-- find all subsets which add upto the given size, given a function to
-- extract the size. Only pick at most one element from each input subset.
sums :: forall a.(a -> Int) -> Int -> [[a]] -> [[a]]