Concealing Prefix Lifts

September 24, 2014

Concealing infix lift-like operations is easy. For example,

sin `liftA` [pi,tau]
sin ↥ [π,τ]

The two lines are equivalent (In real code it’ll be <$>, which is also concealed with ↥, because in essence it is a lift operation.)

Now, how to conceal the prefix version?

liftA sin [pi,tau]

It appears that we can exploit the nice properties of Haskell: the space operator has highest precedence, and since liftA f is always a function by itself (a lifted one), why not just write exactly that: a new function? Behold:

↥sin [π,τ]

Notice that there is no space between ↥ and sin, which means that ↥sin identifier is a function. (Well, technically not, since ↥ is not allowed in the first place, and surely is not a valid name identifier, but we can pretend. There is already a precedence of a similar dualism, i.e. the subtraction operator and the minus sign.) By prepending ↥ we just make a notation for a lifted function.

So, liftA sin produces a new function, which is ↥sin (read as “lifted sin function”). And now these are equivalent:

sin ↥ [π,τ] -- Take sin, lift it, and apply to a list
↥sin [π,τ]  -- Apply lifted sin to a list

Surprisingly, this concealing scheme can be easily implemented, I’ll publish it soon.

Tip: GHCi as a better bc

September 19, 2014

I use it for ages myself, and I thought I’d share:

$ cabal install statistics
$ cat .ghc/ghci.conf
import Control.Applicative
import Control.Monad
import Control.Monad.Instances
import Data.Monoid
import Data.Ratio -- approxRational
import Data.Vector
import Statistics.Sample

default (Double)

$ ghci <<< "1/3"

$ ghci <<< "mean $ fromList [1e12,2e1,3e4]"

Be cautious about “default (Double)”, though. It kind of creates a global state, which is not so fun to debug:

$ echo > empty.hs

$ ghci
λ= import Numeric.Matrix
λ= inv $ fromList [[1,1],[1,2]]
Just 2.0 -1.0
-1.0 1.0

λ= :l empty.hs

λ= inv $ fromList [[1,1],[1,2]]

Super Vision

August 30, 2014

GitHub project page.

Subscripts in Haskell Diagrams

March 31, 2014

Not sure what the official way of doing subscripts in Diagrams is, but surprisingly this tiny piece of code does the job:

subs :: String -> String -> Diagram B R2
subs x y = norm ||| strutX (0.05) ||| sub
    norm = stroke $ textSVG x 1
    sub = stroke $ textSVG y 1 # translateY (-0.35) # scale 0.55

For example,

subs "Unicode: X₈ / Haskell: X" "8"

Why not just use the unicode subscripts? Simple: not all subscript characters are defined in the unicode.

…or you can find more in the archives.