Skip to content

Instantly share code, notes, and snippets.

@merijn
merijn / NoRankN.hs
Last active December 22, 2016 08:21
Haskell RankNType example
module RankN where
data Foo = Foo Int | Bar Double
mangle :: Num a => (a -> a) -> Foo -> Foo
mangle f (Foo i) = Foo (f i)
mangle f (Bar d) = Bar (f d)
{-
NoRankN.hs:6:25:
Authors

Merijn Verstraaten

Date

2014/11/22

So You Want to Be a Super Cool GHC Hacker?

So you have a pet peeve/bug/feature request that you'd like to see added to GHC. You made sure there was a Trac ticket for it, but despite your patient waiting no one is solving your problem. Those selfish GHC hackers!

{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
module HList where
import Prelude hiding (head, tail, zip)
@merijn
merijn / gist:39dc86e345e87276c523
Last active January 2, 2019 10:07
Index list in haskell
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
import GHC.Prim (Constraint)
data Nat = Succ Nat | Zero
@merijn
merijn / MyState.hs
Last active January 15, 2021 19:20
MyState homework
data MyState s a = MyState (s -> (a, s))
get :: MyState s s
get = undefined
put :: s -> MyState s ()
put = undefined
modify :: (s -> s) -> MyState s ()
modify = undefined
@merijn
merijn / Foo.hs
Created August 23, 2014 07:01
Minimal C main FFI example
{-# LANGUAGE ForeignFunctionInterface #-}
module Foo where
import Foreign.C.String
import Foreign.C.Types
foreign export ccall start_server :: CInt -> CString -> IO ()
foreign export ccall start_client :: CString -> IO ()
start_server :: CInt -> CString -> IO ()
@merijn
merijn / gist:6130082
Last active January 2, 2019 10:07
Type family that disallows certain types for type variables.
{-# LANGUAGE ConstraintKinds, DataKinds, PolyKinds, TypeFamilies, TypeOperators #-}
import GHC.Exts (Constraint)
type family Restrict (a :: k) (as :: [k]) :: Constraint where
Restrict a (a ': as) = ("Error!" ~ "Tried to apply a restricted type!")
Restrict x (a ': as) = Restrict x as
Restrict x '[] = ()
foo :: Restrict a [(), Int] => a -> a