Skip to content

Instantly share code, notes, and snippets.

every dot and stroke I paint will be alive

Rob Rix robrix

every dot and stroke I paint will be alive
Block or report user

Report or block robrix

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
robrix / diffused-effects.txt
Created Sep 22, 2019
Results of compiling a benchmark against fused-effects & diffused-effects with -dshow-passes
View diffused-effects.txt
compile: input file benchmark/Send/Send10.hs
*** Checking old interface for Send.Send10 (use -ddump-hi-diffs for more details):
*** Parser [Send.Send10]:
!!! Parser [Send.Send10]: finished in 0.55 milliseconds, allocated 0.957 megabytes
*** Renamer/typechecker [Send.Send10]:
!!! Renamer/typechecker [Send.Send10]: finished in 186.13 milliseconds, allocated 77.516 megabytes
*** Desugar [Send.Send10]:
Result size of Desugar (before optimization)
= {terms: 195, types: 4,554, coercions: 6,170, joins: 0/7}
Result size of Desugar (after optimization)
robrix / Rollable.hs
Last active Mar 11, 2018
Deriving a nonrecursive base functor from a recursive datatype using GHC.Generics
View Rollable.hs
{-# LANGUAGE AllowAmbiguousTypes, DataKinds, DefaultSignatures, DeriveAnyClass, DeriveFunctor, DeriveGeneric, FlexibleContexts, FlexibleInstances, MultiParamTypeClasses, ScopedTypeVariables, TypeApplications, TypeFamilies, TypeOperators #-}
module Rollable where
import Data.Functor.Foldable
import GHC.Generics
data Tree = Empty | Node Tree Int Tree
deriving (Eq, Generic, Ord, Rollable, Show)
depth :: Tree -> Int
robrix / ParameterizedRecursion.hs
Created Jun 29, 2017
Functions defined with fix are more composable than directly recursive functions
View ParameterizedRecursion.hs
module ParameterizedRecursion where
import Data.Function
-- A recursive function…
showTable :: (Show a, Show b) => [(a, b)] -> String
showTable ((a, b) : rest) = show a ++ " | " ++ show b ++ "\n" ++ showTable rest
showTable [] = ""
-- …can be defined instead as a fixpoint…
robrix / SES.hs
Last active Mar 2, 2017
SES (shortest edit script) implemented as a dynamorphism
View SES.hs
dyna :: Functor f => (f (Cofree f a) -> a) -> (c -> f c) -> c -> a
dyna a c = extract . h
where h = cofree . uncurry (:<) . (a &&& identity) . fmap h . c
ses :: Eq a => [a] -> [a] -> [These a a]
ses as bs = dyna (selectBest . edges (length as)) (editGraph as) (as, bs)
-- | A vertex in the edit graph.
data Vertex a x = Vertex { xs :: [a], ys :: [a], next :: Maybe x }
deriving (Eq, Functor, Show)
robrix / Constructor.hs
Last active Jan 21, 2017
Generically-derivable mechanism for producing predicates from datatype constructors
View Constructor.hs
{-# LANGUAGE DefaultSignatures, DeriveAnyClass, StandaloneDeriving, TypeFamilies, TypeOperators #-}
module Constructor where
import Data.Function (on)
import GHC.Generics
import Prologue
-- | The class of types for which we can determine whether an inhabitant was constructed with some specific constructor.
-- Note that the provided instance for functions returning 'HasConstructor' types, @HasConstructor b => HasConstructor (a -> b)@, applies its first argument to 'undefined'. Thus, data types with strict fields cannot safely implement 'HasConstructor' instances, since they would diverge. If you really want to play with fire, then you’ll have to apply the constructors to any strict fields yourself on the left-hand side.
robrix / Bidi.hs
Last active Apr 24, 2018
Bidirectional type elaboration for the simply-typed lambda calculus with unit values & types.
View Bidi.hs
{-# LANGUAGE DeriveFunctor #-}
module Bidi where
-- For 'guard'.
import Control.Monad
-- We use Cofree to represent type-annotated terms.
import Control.Comonad.Cofree
import Data.Functor.Classes
-- We use Fix to represent unannotated terms.
import Data.Functor.Foldable
robrix / JoinMonoid.hs
Created Sep 2, 2016
Joining a foldable collection of Monoids by some separator element.
View JoinMonoid.hs
import Data.Maybe
import Data.Monoid
-- | Join a Foldable collection of Monoids by a separator element.
join :: (Monoid a, Foldable t) => a -> t a -> a
join sep = fromMaybe mempty . fst . foldr combine (Nothing, True)
where combine each (into, isFirst) = if isFirst
then (Just each, False)
else (Just each <> Just sep <> into, False)
robrix / Parametricity.txt
Last active Jun 12, 2018
Higgledy Piggledy — Parametricity
View Parametricity.txt
Higgledy piggledy
quite a nice property
functions can use;
Enforcing the absence of
state or identity
up to the type level
promotes reuse.
robrix / Main.hs
Created Jul 16, 2016
Recursive/Corecursive over F
View Main.hs
{-# LANGUAGE DeriveFoldable, DeriveFunctor, FlexibleContexts, KindSignatures, RankNTypes, TypeFamilies #-}
module Main where
import Data.Bifunctor
newtype F f a = F { runF :: forall r. (a -> r) -> (f r -> r) -> r }
wrap :: Functor f => f (F f a) -> F f a
wrap f = F (\ p i -> i (fmap (\ (F r) -> r p i) f))
robrix / FieldSet.hs
Created Jun 2, 2016
Extensible field sets, inspired by @aaronlevin’s extensible effects in the Van Laarhoven free monad:
View FieldSet.hs
{-# LANGUAGE DataKinds, FlexibleContexts, FlexibleInstances, GADTs, MultiParamTypeClasses, PolyKinds, TypeOperators #-}
module FieldSet where
infix 9 :=>
-- | We can probably replace this with a wrapper around `Tagged`.
newtype a :=> b = (:=>) b
deriving (Eq, Show)
-- | “Smart” (actually quite dumb) constructor for the tagged type above for convenience
field :: b -> a :=> b
You can’t perform that action at this time.