Skip to content

Instantly share code, notes, and snippets.

View kseo's full-sized avatar

Kwang Yul Seo kseo

  • CodeChain
  • Seoul
View GitHub Profile
@kseo
kseo / Graph.hs
Created February 13, 2016 03:45
An example of type family
{-# LANGUAGE TypeFamilies #-}
class Graph g where
type Vertex g
data Edge g
src, tgt :: Edge g -> Vertex g
outEdges :: g -> Vertex g -> [Edge g]
newtype G1 = G1 [Edge G1]
instance Graph G1 where
@kseo
kseo / Arith.hs
Created February 13, 2016 03:23
An example of type family
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeFamilies #-}
class Add a b where
type SumTy a b
add :: a -> b -> SumTy a b
instance Add Integer Double where
type SumTy Integer Double = Double
@kseo
kseo / Arith.hs
Last active December 15, 2016 12:23
GADT
{-# LANGUAGE GADTs #-}
data Expr a where
I :: Int -> Expr Int
B :: Bool -> Expr Bool
Add :: Expr Int -> Expr Int -> Expr Int
Mul :: Expr Int -> Expr Int -> Expr Int
Eq :: Expr Int -> Expr Int -> Expr Bool
eval :: Expr a -> a
@kseo
kseo / Tree.hs
Created February 12, 2016 05:12
Monads are Trees with Grafting
-- http://blog.sigfpe.com/2010/01/monads-are-trees-with-grafting.html
import Control.Applicative
import Control.Monad
data Tree a = Fork (Tree a) (Tree a) | Leaf a | Nil deriving Show
tree1 = Fork (Fork (Leaf 2) Nil) (Fork (Leaf 2) (Leaf 3))
instance Functor Tree where
fmap = liftM
@kseo
kseo / MonadComprehension.hs
Created February 12, 2016 04:07
Monad comprehension
-- https://ghc.haskell.org/trac/ghc/wiki/MonadComprehensions
{-# LANGUAGE MonadComprehensions #-}
import Control.Monad
mapl :: Monad m => (a -> m b) -> ([a] -> m [b])
mapl f [] = return []
mapl f (x:xs) = [y:ys | y <- f x, ys <- mapl f xs]
@kseo
kseo / Seq.hs
Created February 11, 2016 13:53
Sequence implementation using Monoid
-- http://apfelmus.nfshost.com/articles/monoid-fingertree.html
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances #-}
import Prelude hiding ((!!), head)
import Data.Monoid
data Tree v a = Leaf v a
| Branch v (Tree v a) (Tree v a)
@kseo
kseo / Polymorphic.hs
Created February 11, 2016 11:23
Polymorphic recursion
import Prelude hiding (length)
data Nested a = a :<: (Nested [a]) | Epsilon
infixr 5 :<:
nested = 1 :<: [2,3,4] :<: [[4,5],[7],[8,9]] :<: Epsilon
length :: Nested a -> Int
length Epsilon = 0
length (_ :<: xs) = 1 + length xs
@kseo
kseo / Nested.hs
Created February 11, 2016 11:01
Nested types
{-# LANGUAGE FlexibleContexts #-}
data BinaryTree a = Leaf
| InternalNode (BinaryTree a) a (BinaryTree a) deriving Show
data CompleteBinaryTree a = Leaves
| NonLeaves a (CompleteBinaryTree (a,a)) deriving Show
data AList a b = Nil
| Cons a (AList b a) deriving Show
@kseo
kseo / Length.hs
Created February 2, 2016 15:13
An implementation of length function in terms of Const functor
import Prelude hiding (length)
newtype Const a b = Const a
unConst :: Const c a -> c
unConst (Const x) = x
length' :: [a] -> Const Int a
length' [] = Const 0
length' (x:xs) = Const (1 + unConst (length' xs))
@kseo
kseo / SCFold.hs
Created February 2, 2016 09:09
Short-Circuiting Fold
import Data.Maybe
ssfold :: (a -> Bool) -> (a -> b -> a) -> a -> [b] -> a
ssfold p f a0 xs = foldr (\x xs a -> if p a then a else xs (f a x)) id xs a0