How I Ruined Today Reading Haskell Posts

Lot of gold here (comments included). Graphs are important and I haven’t yet grokked the FGL thing or other methods of handling them

Functional programming with graphs from haskell

A new methodology for Arduino Haskell bindings

Sharing in Haskell using Data.Reify.

Resources on String Diagrams, and Adjunctions, and Kan Extensions

I’ve been trying to figure out Kan Extensions

Ralf Hinze on Kan Extensions


But while doing that I went down a rabbit hole on String Diagrams

This post is the first one on String Diagrams that made sense to me.

I had seen this stuff before, but I hadn’t appreciated it until I saw what Haskell expressions it showed equivalence between. They are not obvious equivalences

This seems like a very useful video on this topic.

In Summary, it is an excellent notation for talking about transformations of a long sequence of composed Functors  F G H … into some other long sequence of Functors. The conversion of functors runs from up to down. The composition of functors procedes left to right.  F eta is the fmap of eta, and eta F is eta with the forall’ed type unified to be of the form F a.

Adjunctions L -| R are asymmetric between cups and caps. L is on the left in cups and on the right in caps. That’s what makes squiggles pull straightable

I think I have an interesting idea for a linear algebra library based on this stuff


John Baez and Mike Stay’s Rosetta Stone (A touch stone I keep returning to)

Dan Piponi gave a talk which is another touch stone of mine that I come back to again and again. There is a set of corresponding blog posts.

Other resources:

NCatLab article

John Baez hosted seminars



Dan Marsden’s Article

Marsden and Hinze have been collaborating

Stephen Diehl on Adjunctions


A Section From an old Oregon Programming Language Summer School (a rich set of resources)


Marsden and Hinze have been collaborating


Mike Stay doing a very interesting series of Category Theory in Javascript. He uses contracts in place of types. Defeats one of the big points of types (static analysis), but still pretty cool



I think that about covers everything I know about.

Oh yeah, there is the whole Coecke and Abramsky categorical quantum mechanics stuff too.

Using the Purescript Servant Bridge

Alright, here is some garbage code. Hope it helps you, sorry if it confuses you.

Checkout the Counter example

that is where I pulled everything from. I ripped out just about everything fancy just so you and I can see the truly bare bones example.

It’s mostly boiler plate on the generator side.

He does a couple fancier things that you may want, like rearranging names and changing folders. Here is a more basic example

This goes in app/ if you’re using a stack template

{-# LANGUAGE AutoDeriveTypeable    #-}
{-# LANGUAGE DataKinds             #-}
{-# LANGUAGE FlexibleInstances     #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings     #-}
{-# LANGUAGE ScopedTypeVariables   #-}
{-# LANGUAGE TypeOperators         #-}

module Main where

import           Control.Applicative
import           Control.Lens
import           Data.Aeson
import           Data.Monoid
import           Data.Proxy
import qualified Data.Set                           as Set
--import           Data.Text                          (Text)
import qualified Data.Text                          as T
import qualified Data.Text.Encoding                 as T
import qualified Data.Text.IO                       as T
import           Language.PureScript.Bridge
import           Language.PureScript.Bridge.PSTypes
import           Servant.API
import           Servant.PureScript
import           Servant.Subscriber.Subscribable

import qualified Lib

myBridge :: BridgePart
myBridge = defaultBridge

data MyBridge

myBridgeProxy :: Proxy MyBridge
myBridgeProxy = Proxy

instance HasBridge MyBridge where
  languageBridge _ = buildBridge myBridge

-- List all the types you want to end up in ServerTypes.purs Auto builds lenses too?
myTypes :: [SumType 'Haskell]
myTypes =  [
           mkSumType (Proxy :: Proxy Lib.User)
--          , mkSumType (Proxy :: Proxy Lib.Hello)

mySettings :: Settings
mySettings = defaultSettings

main :: IO ()
main = do
  let frontEndRoot = "frontend/src"
  writeAPIModuleWithSettings mySettings frontEndRoot myBridgeProxy Lib.userAPI'
  writePSTypes frontEndRoot (buildBridge myBridge) myTypes

Mostly things can just be the defaults. Look at the Counter Example for some more config stuff you can do.

You do need to separate out the user json api by itself. If you hand writeAPIModuleWithSettings an api that has the RAW serving the index.html, it freaked out. Maybe there is a way to handle that, but it’s not like that is probably what you want anyhow.

The myTypes Sum Type you want to add to for every type that you want to export over to purescript. frontEndRoot is where the generated files will go.

The Proxy business is a bunch of typelevel programming boilerplate. So is the empty MyBridge type.

There is basically no content to this code.

You also need to add this app/PSGenerator.hs file to your cabal file.

executable psGenerator
  hs-source-dirs:      app
  main-is:             PSGenerator.hs
  other-modules:       Lib
  -- Other library packages from which modules are imported.
  build-depends:       base >=4.8 && < 6.0
                     , aeson
                     , containers
                     , filepath
                     , ghc-mod
                     , http-api-data
                     , http-types
                     , lens
                     , mainland-pretty
                     , purescript-bridge
                     , servant
                     , servant-foreign
                     , servant-purescript
                     , servant-subscriber
                     , text
                     , servant-server
                     , wai
                     , warp
                     , bytestring

  -- Directories containing source files.
  hs-source-dirs:      src

  -- Base language which the package is written in.
  default-language:    Haskell2010


Every time you want to run the generator, you need to run

stack exec psGenerator


This then will put an API file and a Data type file into your purescript source in frontend/src

Using the API is a touch annoying but correct. If you look at the generated signature

getUsers :: forall eff m.
            MonadAsk (SPSettings_ SPParams_) m => MonadError AjaxError m => MonadAff ( ajax :: AJAX | eff) m
            => m (Array User)

There are a lot of constraints you need to satisfy in the monad m in order to call this thing. You need a monad that is Reader-like for getting the SPSettings_, needs to handle a Possible AjaxError, and needs to be an Aff-like monad. Woof.

It makes sense that you’d want to do all of this, but it is a burdensome mental overhead to get started.

Here’s some very basic source that shows how to at least get to the stage where you can log it the resulting request. I’m just dumping the error like a bad boy.

module Main where
import Prelude

import Control.Monad.Aff (Aff)
import Control.Monad.Aff.Console
import Control.Monad.Trans.Class
import Data.Maybe
import Control.Monad.Eff.Console (CONSOLE)

import ServerAPI
import Lib (User(..), _User)

import Control.Monad.Aff
import Control.Monad.Eff.Class
import Data.Either
import Control.Monad.Reader.Trans
import Control.Monad.Except.Trans
import Control.Monad.Eff.Exception (EXCEPTION)
import Network.HTTP.Affjax (AJAX, get)

import Servant.PureScript.Affjax
import Servant.PureScript.Settings

import Data.Array ((!!))
import Unsafe.Coerce 

import Data.Lens
import Data.Symbol (SProxy(..))
import Data.Lens.Record (prop)
import Data.Traversable

settings = defaultSettings $ SPParams_ { baseURL : "http://localhost:9000/" }

type MySettings = SPSettings_ SPParams_
type APIEffect eff = ReaderT MySettings (ExceptT AjaxError (Aff ( ajax :: AJAX, err :: EXCEPTION  | eff)))

runEffect :: forall eff. MySettings -> APIEffect eff (Array User) -> Aff ( ajax :: AJAX, err :: EXCEPTION | eff) (Array User)
runEffect settings m = do
    er <- runExceptT $ runReaderT m settings
    case er of
      Left err -> pure $ []
      Right v -> pure v

getName :: User -> String
getName (User {name : n }) = n

name :: forall a b r. Lens { name :: a | r } { name :: b | r } a b
name = prop (SProxy :: SProxy "name")

main = launchAff do
          users <- runEffect settings getUsers
          log $ maybe "No name" (view $ _User <<< name) (users !! 1 )
          log $ (view $ traversed <<< _User <<< name) users

Note that you have to install purescript-servant-support as it tells you when you run psGenerator. I’ve been using psc-package. It is often helpful to go in to the psc-package.json file and update to the latest package-set. Just a little tip.

You see that the ExceptT handles the AjaxError and the ReaderT supplies the settings, which uses the defaults + a baseURL to point the request to

The whole thing is run inside an Aff monad.


Here’s the basic servant code

{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeOperators #-}
module Lib
    ( startApp
    , app
    , userAPI
    , userAPI'
    , Hello
    , User
    ) where

--import Prelude ()
--import Prelude.Compat

--import Control.Monad.Except
--import Control.Monad.Reader
--import Data.Aeson.Compat
import Data.Aeson.Types
--import Data.Attoparsec.ByteString
import Data.ByteString (ByteString)
import Data.List
import Data.Maybe
--import Data.String.Conversions
--import Data.Time.Calendar
import GHC.Generics
--import Lucid
--import Network.HTTP.Media ((//), (/:))
import Network.Wai
import Network.Wai.Handler.Warp
import Servant
--import System.Directory
--import Text.Blaze
--import Text.Blaze.Html.Renderer.Utf8
import qualified Data.Aeson.Parser
--import qualified Text.Blaze.Html
import Data.Text  (Text)

data User = User
  { name :: String
  , age :: Int
  , email :: String
  } deriving (Eq, Show, Generic)

instance FromJSON User
instance ToJSON User

instance FromJSON Hello
instance ToJSON Hello

users1 :: [User]
users1 =
  [ User "Isaac Newton"    372 "" 
  , User "Albert Einstein" 136 ""         

type UserAPI = "users" :> Get '[JSON] [User] 
type UserAPI1 = UserAPI :<|> Raw

server1 :: Server UserAPI1
server1 = return users1 :<|> serveDirectoryFileServer "./frontend"

userAPI :: Proxy UserAPI1
userAPI = Proxy

userAPI' :: Proxy UserAPI
userAPI' = Proxy

-- 'serve' comes from servant and hands you a WAI Application,
-- which you can think of as an "abstract" web application,
-- not yet a webserver.
app1 :: Application
app1 = serve userAPI server1

main :: IO ()
main = do putStrLn "on port 9000"
          run 9000 app1

startApp = main
app = app1


Again, I started using the stack servant template, whose directory structure I’m complying with.


Edit: Some more comments: Purescript bridge is a seperate project from servant-purescript. Purescript bridge will translate your Haskell types. Servant purescript writes your api calls. The two lines in the main of PSGenerator.hs do these sepearte tasks. the writeAPI writes the API calls and  writePSTypes writes the types.

If you want to transport a parametrized data type like (Maybe a) in the myTypes things, hand the type a special type from here

works like a charm



Downloading and Collecting Coursera videos

I like to watch and listen to my coursera videos on my commute. The app has download functionality but the quizzes and crap require your intervention. I need just a block of stuff so I can be hands free.

coursera-dl is a command line tool to download coursera content

basic usage is like so

coursera-dl -h is a help menu

you can pass in your username and password with -u and -p or setup a ~/.netrc file as described in the README

coursera-dl –list-courses -n

I think it should list courses by default honestly.

This downloads all mp4 videos

coursera-dl cloud-computing -n -f “mp4”


I then made a dirty script that will go through each week and concatenate the videos of that week hopefully in order into a single mp4 file. It is not a clean script. It will throw some errors and build some weird extra files, but it gets the job done. Run it in the course directory


#maybe if you want to pre speedup. It's slow though
#ffmpeg -i input-video.mp4 -vf "setpts=0.68*PTS" -filter:a "atempo=1.467" output-video.mp4

#makes a directory and builds one video per week

#mkdir videos

for D in `find . -type d` -maxdepth 1
 #if [["$D" != "videos/"]]; then
 #//Do whatever you need with D
 find $D -name \*.mp4 -print | sort > videolist.txt
 sed -i -e 's/^/file /' videolist.txt 
 ffmpeg -f concat -safe 0 -i videolist.txt -codec copy ./$D.mp4