Skip to content

Instantly share code, notes, and snippets.

View edsko's full-sized avatar

Edsko de Vries edsko

View GitHub Profile
@edsko
edsko / Lenses.hs
Last active August 29, 2015 14:16
Proof of equivalence for van Laarhoven lenses
-- Example from http://arxiv.org/pdf/1103.2841v1.pdf
-- "Functor is to Lens as Applicative is to Biplate"
-- NOTE: Proof of equivalence here _IMPLIES_ proof of equivalence for
-- van Laarhoven lenses (Section 4)
{-# OPTIONS_GHC -Wall #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE RecordWildCards #-}
@edsko
edsko / Overlapping.hs
Created September 24, 2015 12:39
Incoherence using only overlapping instances and existentials, and avoiding it using a type family
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeOperators #-}
import Prelude hiding (compare)
import Data.Type.Equality
class Compare a b where
@edsko
edsko / Checked.hs
Last active October 26, 2016 19:15
Lightweight checked exceptions in Haskell
{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
#if __GLASGOW_HASKELL__ >= 710
{-# LANGUAGE AllowAmbiguousTypes #-}
#endif
-- | Lightweight checked exceptions
{-# LANGUAGE GADTs #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE StaticPointers #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ConstraintKinds #-}
{-# OPTIONS_GHC -Wall -fno-warn-orphans #-}
@edsko
edsko / Defunctionalization.hs
Last active May 19, 2017 08:00
Using defunctionalization to avoid partially applied type synonyms
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE PolyKinds #-}
data Fn a = Id | Const a
type family Apply (fn :: Fn k) (a :: k) :: k where
Apply Id a = a
Apply (Const b) a = b
import Control.Applicative
import Control.Monad
import Control.Monad.IO.Class
import System.IO.Unsafe
newtype LazyIO a = LazyIO { runLazyIO :: IO a }
instance Monad LazyIO where
return a = LazyIO (return a)
LazyIO x >>= f = LazyIO $ unsafeInterleaveIO $ x >>= runLazyIO . f
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-------------------------------------------------------------------------------
Lazy n-ary cartesian product
Most of these functions (except for 'cart' itself) are taken from
"Generics.Deriving.Enum", which in turn are taken from Mark Jones' talk
at AFP 2008.
-------------------------------------------------------------------------------}
-- | n-ary lazy cartesian product
ncart :: HList [] as -> [HList Identity as]
data Strategy = Concat
(+++) :: [a] -> [a] -> Strategy -> [a]
(+++) xs ys Concat = xs ++ ys
(.@) :: (Strategy -> a) -> Strategy -> a
(.@) = ($)
test :: [Int]
test = [1] +++ [2] .@ Concat
{-@ data List [listLen] a = Nil | Cons { hd :: a , tl :: List a } @-}
data List a = Nil | Cons a (List a)
{-@ measure listLen @-}
{-@ listLen :: List a -> Nat @-}
listLen :: List a -> Int
listLen Nil = 0
listLen (Cons _ cs) = 1 + listLen cs