Wednesday, December 13, 2017

Defining backticks using "pipe" operations

(Thanks to Simon Fowler and others on the Links team for the discussion leading to this...)

In Haskell, you can use a 2-argument function as if it were an infix operator by putting "backticks" around it like this:

ghci> let plus x y = x + y
ghci> 1 `plus` 2
3

In a number of functional languages such as F# and OCaml, the following "pipe" operations have become popular (I'll continue using Haskell syntax, though):

ghci> let (|>) x f = f x
ghci> let (<|) f x = f x
ghci> [1,2,3] |> map (plus 1)
[2,3,4]


Read more »

Labels: ,

Tuesday, September 30, 2014

Dijkstra monads

Dijkstra monads in monadic computation
Bart Jacobs
CMCS 2014

This paper discusses a monad for predicate transformers (dubbed the Dijkstra monad in an earlier paper by Swamy et al. that is now also on my reading list). This post is an attempt to express a very, very small part of the ideas of the paper in Haskell, to try to make some sense of them.

Read more »

Labels: ,

Wednesday, July 10, 2013

Exploring the compiler forest in Haskell

The combinators from the Compiler Forest paper can be implemented in Haskell as follows.  (Note: This requires recent GHC with options -XKindSignatures -XGADTs -XTypeFamilies.)
First, we define a type for partial compilers:

data PC :: * -> * -> * where
  PC :: (s -> (s', t' -> t)) -> PC (s,t) (s', t')


In Haskell terms, this is a "generalized algebraic data type", but this is solely so that we can think of PC as a two-argument type constructor instead of four.
Read more »

Labels: ,