Skip to content

Instantly share code, notes, and snippets.

@gelisam
gelisam / SpeedyTraverse.hs
Last active Apr 15, 2019
benchmarking various implementations of parallel traverse
View SpeedyTraverse.hs
-- in response to https://twitter.com/snoyberg/status/1116710317554315265
--
-- Green threads are very cheap, so I want to investigate under which
-- circumstances it is worth making the logic more complex in order to minimize
-- their number. So I wrote multiple implementations, ran them with a different
-- number of inputs, and with different task sizes.
--
-- On my four-capabilities machine, my observations are:
--
-- * With very small (10 μs) tasks, the single-threaded 'traverse' wins,
@gelisam
gelisam / Dag.hs
Created Mar 12, 2019
using indexed Monads to make illegal DAGs underrepresentable
View Dag.hs
{-# LANGUAGE FlexibleInstances, GADTs, MultiParamTypeClasses, RebindableSyntax #-}
import Data.Maybe
import Prelude (IO, putStrLn, ($), id, (.), fst, snd)
data Void
data Dag where
Dag :: Dag' a -> Dag
@gelisam
gelisam / Dyna.hs
Created Mar 10, 2019
dynamic programming using recursion schemes
View Dyna.hs
-- Solving a dynamic programming in many ways, including using existing
-- recursion schemes and by defining new ones. The problem of solving this
-- particular problem using recursion schemes was posed by Sandy Maguire.
{-# LANGUAGE FlexibleContexts, RankNTypes, TypeApplications, TypeFamilies, ScopedTypeVariables #-}
{-# OPTIONS -Wno-orphans #-}
module Dyna where
import Test.DocTest
import Data.Functor.Foldable (Base, Fix, Recursive(project), Corecursive(embed, ana), hylo, cataA)
@gelisam
gelisam / Main.hs
Created Dec 12, 2018
Averaged across persons, excluding legal fees, how much money had each person spent by time 6?
View Main.hs
-- in response to https://www.reddit.com/r/haskell/comments/a50xpr/datahaskell_solve_this_small_problem_to_fill_some/
{-# LANGUAGE BangPatterns, OverloadedStrings, RecordWildCards, ScopedTypeVariables #-}
module Main where
import Control.Category ((>>>))
import Data.Function ((&))
import Data.Map.Strict (Map, (!))
import Data.Set (Set)
import Test.DocTest (doctest)
@gelisam
gelisam / MkHList.hs
Last active Dec 7, 2018
a polyvariadic function returning a GADT
View MkHList.hs
-- in response to https://twitter.com/Iceland_jack/status/1070073876829429760
{-# LANGUAGE AllowAmbiguousTypes, DataKinds, FlexibleInstances, GADTs, InstanceSigs, ScopedTypeVariables, TypeApplications, TypeFamilies, TypeOperators #-}
module Main where
import Test.DocTest
-- The challenge is to define a polymorphic 'mkHList' which can be specialized
-- to all of those types:
--
-- mkList :: HList '[]
-- mkList :: d -> HList (d ': '[])
@gelisam
gelisam / FreeArrow.hs
Created Nov 24, 2018
An explanation of free Arrows
View FreeArrow.hs
{-# LANGUAGE Arrows, GADTs, LambdaCase, RankNTypes, ScopedTypeVariables #-}
module FreeArrow where
import Test.DocTest
import Prelude hiding (id, (.))
import Control.Arrow
import Control.Category
import Control.Monad.Trans.Class
import Control.Monad.Trans.Writer
import System.IO
@gelisam
gelisam / WordCount.hs
Created Nov 15, 2018
an exploration of monoidal word-counting
View WordCount.hs
-- an exploration of monoidal word-counting
{-# LANGUAGE BangPatterns #-}
module Main where
import Data.Char
import Data.List
import Data.Semigroup
-- I will implement wordcount several times. One of the goal
@gelisam
gelisam / EIO.hs
Last active Feb 8, 2019
Keeping track of which exceptions have and haven't been handled
View EIO.hs
-- a continuation of https://gist.github.com/gelisam/137effb33d2777328d366dcb563d8d13
{-# LANGUAGE FlexibleContexts, FlexibleInstances, GeneralizedNewtypeDeriving, LambdaCase, MultiParamTypeClasses #-}
module EIO where
import Control.Monad
import Data.Void
import Test.DocTest
import qualified Control.Exception as E
@gelisam
gelisam / CouldBe.hs
Last active Mar 7, 2019
Keeping track of which errors have and haven't been handled
View CouldBe.hs
-- in response to https://www.parsonsmatt.org/2018/11/03/trouble_with_typed_errors.html
{-# LANGUAGE FlexibleContexts, FlexibleInstances, MultiParamTypeClasses #-}
module CouldBe where
import Control.Monad
import Data.Void
-- Here is an alternate, much simpler solution to the problem of keeping track of which errors have
-- and haven't been handled. It doesn't use prisms nor generics, it simply uses the monad
@gelisam
gelisam / DashDashArguments.hs
Created Nov 6, 2018
Named arguments in Haskell
View DashDashArguments.hs
-- in response to https://twitter.com/int_index/status/1059430673193275392
{-# LANGUAGE DataKinds, FlexibleContexts, FlexibleInstances, GADTs, MultiParamTypeClasses, TypeOperators #-}
module DashDashArguments where
import GHC.TypeLits
import Test.DocTest
-- The challenge is to implement named arguments in this style:
--
You can’t perform that action at this time.