Showing posts with label functional programming. Show all posts
Showing posts with label functional programming. Show all posts

Wednesday, March 30, 2016

Compilation as a Typed EDSL-to-EDSL Transformation

[arXiv:1603.08865]

Abstract

This article is about an implementation and compilation technique that is used in RAW-Feldspar which is a complete rewrite of the Feldspar embedded domain-specific language (EDSL) (Axelsson et al. 2010). Feldspar is high-level functional language that generates efficient C code to run on embedded targets.

The gist of the technique presented in this post is the following: rather writing a back end that converts pure Feldspar expressions directly to C, we translate them to a low-level monadic EDSL. From the low-level EDSL, C code is then generated.

This approach has several advantages:

  1. The translation is simpler to write than a complete C back end.
  2. The translation is between two typed EDSLs, which rules out many potential errors.
  3. The low-level EDSL is reusable and can be shared between several high-level EDSLs.

Although the article contains a lot of code, most of it is in fact reusable. As mentioned in Discussion, we can write the same implementation in less than 50 lines of code using generic libraries that we have developed to support Feldspar.

Introduction

There has been a recent trend to implement low-level imperative EDSLs based on monads (Persson, Axelsson, and Svenningsson 2012; J. D. Svenningsson and Svensson 2013; Hickey et al. 2014; Bracker and Gill 2014). By using a deep embedding of monadic programs, imperative EDSL code can be translated directly to corresponding code in the back end (e.g. C or JavaScript code).

A deep embedding of a monad with primitive instruction gives a representation of the statements of an imperative language. But in order for a language to be useful, there also needs to be a notion of pure expressions. Take the following program as example:

prog = do
    i <- readInput
    writeOutput (i+i)

In order to generate code from this program we need a representation of the whole syntax, including the expression i+i. And it is usually not enough to support such simple expressions. For example, in RAW-Feldspar, we can write the following,

prog = do
    i <- fget stdin
    printf "sum: %d\n" $ sum $ map square (0 ... i)
  where
    square x = x*x

and have it generate this C code:

 #include <stdint.h>
 #include <stdio.h>
int main()
{
    uint32_t v0;
    uint32_t let1;
    uint32_t state2;
    uint32_t v3;

    fscanf(stdin, "%u", &v0);
    let1 = v0 + 1;
    state2 = 0;
    for (v3 = 0; v3 < (uint32_t) (0 < let1) * let1; v3++) {
        state2 = v3 * v3 + state2;
    }
    fprintf(stdout, "sum: %d\n", state2);
    return 0;
}

Note that the pure expression sum $ map square (0 ... i) was turned into several statements in the C code (everything from let1 = to the end of the loop). What happened here was that sum $ map square (0 ... i) was first transformed to a more low-level (but still pure) expression that involves a let binding and a looping construct. This expression was then translated to C code.

However, the story is actually more interesting than that: Instead of translating the pure expression directly to C, which can be a tedious and error-prone task, it is first translated to a typed low-level EDSL that only has simple expressions and a straightforward translation to C. Going through a typed EDSL makes the translation simple and robust, and this low-level EDSL has the advantage of being generic and reusable between different high-level EDSLs.

This post will show a simplified version of the technique used in RAW-Feldspar. For the full story, see the RAW-Feldspar implementation (tag article-2016-03). The low-level EDSL that is used for compiling Feldspar to C is provided by the package imperative-edsl.

Outline of the technique

Our technique is based on a deep monadic embedding of imperative programs:

data Prog exp a where ...

instance Monad (Prog exp)

By parameterizing Prog on the expression type exp, we can have imperative programs with different representations of expressions.

Next, we define two expression languages:

data LowExp a  where ...
data HighExp a where ...

LowExp is a simple language containing only variables, literals, operators and function calls. HighExp is a richer language that may contain things like let binding, tuples and pure looping constructs.

Since LowExp is so simple, we can easily – once and for all – write a back end that translates Prog LowExp to C code:

lowToCode :: Prog LowExp a -> String

Once this is done, we can implement the compiler for HighExp simply as a typed translation function between two EDSLs:

translateHigh :: Prog HighExp a -> Prog LowExp a

compile :: Prog HighExp a -> String
compile = lowToCode . translateHigh

If we want to make changes to HighExp, we only need to update the translateHigh function. And if we want to implement an EDSL with a different expression language, we simply write a different translation function for that language.

A deep embedding of imperative programs

The code is available as a literate Haskell file.

We make use of the following GHC extensions:

{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE UndecidableInstances #-}

And we also need a few helper modules:

import Control.Monad.State
import Control.Monad.Writer
import Data.Int
import Data.IORef

We are now ready to define our monadic deep embedding. Our EDSL will be centered around the monad Prog (inspired by the Program monad in Operational and the work of J. D. Svenningsson and Svensson (2013)):

data Prog exp a where
  Return :: a -> Prog exp a
  (:>>=) :: Prog exp a -> (a -> Prog exp b) -> Prog exp b
  CMD    :: CMD exp a -> Prog exp a

instance Functor (Prog exp) where
  fmap f m = m >>= return . f

instance Applicative (Prog exp) where
  pure  = return
  (<*>) = ap

instance Monad (Prog exp) where
  return = Return
  (>>=)  = (:>>=)

As seen in the Monad instance, Return and (:>>=) directly represent uses of return and (>>=), respectively. CMD injects a primitive instruction from the type CMD a which will be defined below.

The attentive reader may react that Prog actually does not satisfy the monad laws, since it allows us to observe the nesting of (>>=) in a program. This is, however, not a problem in practice: we will only interpret Prog in ways that are ignorant of the nesting of (>>=).

(The Operational package honors the monad laws by only providing an abstract interface to Program, preventing interpretations that would otherwise be able to observe the nesting of (>>=).)

Primitive instructions

The CMD type contains the primitive instructions of the EDSL. It has instructions for mutable references, input/output and loops:

data CMD exp a where
  -- References:
  InitRef :: Type a => exp a -> CMD exp (Ref a)
  GetRef  :: Type a => Ref a -> CMD exp (Val a)
  SetRef  :: Type a => Ref a -> exp a -> CMD exp ()

  -- Input/output:
  Read     :: CMD exp (Val Int32)
  Write    :: exp Int32 -> CMD exp ()
  PrintStr :: String -> CMD exp ()

  -- Loops:
  For :: exp Int32 -> (Val Int32 -> Prog exp ()) -> CMD exp ()

The instructions InitRef, GetRef and SetRef correspond to newIORef, readIORef and writeIORef from Data.IORef.

Read and Write are used to read integers from stdin and write integers to stdout. PrintStr prints a static string to stdout.

For n body represents a for-loop of n iterations, where body gives the program to run in each iteration. The argument to body ranges from 0 to n-1.

As explained in Outline of the technique, exp represents pure expressions, and we have it as a parameter in order to be able to use the same instructions with different expression types.

Now we have three things left to explain: Type, Val and Ref.

The Type class is just used to restrict the set of types that can be used in our EDSL. For simplicity, we will go with the following class

class    (Eq a, Ord a, Show a) => Type a
instance (Eq a, Ord a, Show a) => Type a

Val is used whenever an instruction produces a pure value (e.g. GetRef). It would make sense to use exp a instead of Val a. However, that would lead to problems later when we want to translate between different expression types. We can think of Val a as representing a value in any expression type.

In order to interpret a program, we must have concrete representations of the types returned by the primitive instructions. For example, interpreting the program CMD Read :>>= \a -> ..., requires creating an actual value of type Val Int32 to pass to the continuation. However, depending on the interpretation, we may need different representations for this value.

In a standard interpretation (e.g. running the program in Haskell’s IO monad), we want Val a to be represented by an actual value of type a. But in a non-standard interpretation such as compilation to C code, we want Val a to be a variable identifier. To reconcile these different needs, we define Val as a sum type:

data Val a
  = ValRun a       -- Concrete value
  | ValComp VarId  -- Symbolic value

-- Variable identifier
type VarId = String

For the same reason, Ref is defined to be either an actual IORef or a variable identifier:

data Ref a
  = RefRun (IORef a)  -- Concrete reference
  | RefComp VarId     -- Symbolic reference

Unfortunately, having sum types means that our interpretations will sometimes have to do incomplete pattern matching. For example, the standard interpretation of GetRef r will assume that r is a RefRun (a variable identifier has no meaning in the standard interpretation), while the interpretation that compiles to C will assume that it is a RefComp. However, the EDSL user is shielded from this unsafety. By making Val and Ref abstract types, we ensure that users’ programs are unaware of the interpretation which is applied to them. As long as our interpreters are correct, no-one will suffer from the unsafety due to Val and Ref being sum types.

Interpretation

The following function lifts an interpretation of instructions in a monad m to an interpretation of programs in the same monad (corresponding to interpretWithMonad in Operational):

interpret :: Monad m
          => (forall a . CMD exp a -> m a)
          -> Prog exp a -> m a
interpret int (Return a) = return a
interpret int (p :>>= k) = interpret int p >>= interpret int . k
interpret int (CMD cmd)  = int cmd

Using interpret, we can define the standard interpretation that runs a program in the IO monad:

runIO :: EvalExp exp => Prog exp a -> IO a
runIO = interpret runCMD

runCMD :: EvalExp exp => CMD exp a -> IO a
runCMD (InitRef a)           = RefRun <$> newIORef (evalExp a)
runCMD (GetRef (RefRun r))   = ValRun <$> readIORef r
runCMD (SetRef (RefRun r) a) = writeIORef r (evalExp a)
runCMD Read                  = ValRun . read <$> getLine
runCMD (Write a)             = putStr $ show $ evalExp a
runCMD (PrintStr s)          = putStr s
runCMD (For n body)          =
    mapM_ (runIO . body . ValRun) [0 .. evalExp n - 1]

Most of the work is done in runCMD which gives the interpretation of each instruction. Note the use of ValRun and RefRun on both sides of the equations. As said earlier, ValComp and RefComp are not used in the standard interpretation.

Since running a program also involves evaluating expressions, we need the constraint EvalExp exp that gives access to the function evalExp (for example in the interpretation of InitRef). EvalExp is defined as follows:

class EvalExp exp where
  -- Evaluate a closed expression
  evalExp :: exp a -> a

Front end

We are now ready to define the smart constructors that we will give as the interface to the EDSL user. For many instructions, the smart constructor just takes care of lifting it to a program using CMD, for example:

initRef :: Type a => exp a -> Prog exp (Ref a)
initRef = CMD . InitRef

setRef :: Type a => Ref a -> exp a -> Prog exp ()
setRef r a = CMD (SetRef r a)

But instructions that involve Val will need more care. We do not actually want to expose Val to the user, but rather use exp to hold values. To achieve this, we introduce a class for expressions supporting injection of constants and variables:

class FreeExp exp where
  -- Inject a constant value
  constExp :: Type a => a -> exp a

  -- Inject a variable
  varExp   :: Type a => VarId -> exp a

Using FreeExp, we can make a general function that converts Val to an expression – independent of interpretation:

valToExp :: (Type a, FreeExp exp) => Val a -> exp a
valToExp (ValRun a)  = constExp a
valToExp (ValComp v) = varExp v

Now we can define the rest of the front end, using valToExp where needed:

getRef :: (Type a, FreeExp exp) => Ref a -> Prog exp (exp a)
getRef = fmap valToExp . CMD . GetRef

readInput :: FreeExp exp => Prog exp (exp Int32)
readInput = valToExp <$> CMD Read

writeOutput :: exp Int32 -> Prog exp ()
writeOutput = CMD . Write

printStr :: String -> Prog exp ()
printStr = CMD . PrintStr

for :: FreeExp exp
    => exp Int32 -> (exp Int32 -> Prog exp ()) -> Prog exp ()
for n body = CMD $ For n (body . valToExp)

As a convenience, we also provide a function for modifying a reference using a pure function:

modifyRef :: (Type a, FreeExp exp)
          => Ref a -> (exp a -> exp a) -> Prog exp ()
modifyRef r f = setRef r . f =<< getRef r

Pure expressions

Now that we have a front end for the imperative EDSL, we also need to define an expression type before we can use it for anything.

We begin with a very simple expression type that only has variables, literals and some primitive functions:

data LowExp a where
  LVar :: Type a => VarId -> LowExp a
  LLit :: Type a => a -> LowExp a
  LAdd :: (Num a, Type a) => LowExp a -> LowExp a -> LowExp a
  LMul :: (Num a, Type a) => LowExp a -> LowExp a -> LowExp a
  LNot :: LowExp Bool -> LowExp Bool
  LEq  :: Type a => LowExp a -> LowExp a -> LowExp Bool

As common in Haskell EDSLs, we provide a Num instance to make it easier to construct expressions:

instance (Num a, Type a) => Num (LowExp a) where
  fromInteger = LLit . fromInteger
  (+) = LAdd
  (*) = LMul

This instance allows us to write things like 5+6 :: LowExp Int32.

The implementation of LowExp is completed by instantiating the EvalExp and FreeExp classes:

instance EvalExp LowExp where
  evalExp (LLit a)   = a
  evalExp (LAdd a b) = evalExp a + evalExp b
  evalExp (LMul a b) = evalExp a * evalExp b
  evalExp (LNot a)   = not $ evalExp a
  evalExp (LEq a b)  = evalExp a == evalExp b

instance FreeExp LowExp where
  constExp = LLit
  varExp   = LVar

First example

We can now write our first imperative example program:

sumInput :: Prog LowExp ()
sumInput = do
  r <- initRef 0
  printStr "Please enter 4 numbers\n"
  for 4 $ \i -> do
    printStr " > "
    n <- readInput
    modifyRef r (+n)
  printStr "The sum of your numbers is "
  s <- getRef r
  writeOutput s
  printStr ".\n"

This program uses a for-loop to read four number from the user. It keeps the running sum in a reference, and prints the final sum before quitting. An example run can look as follows:

*EDSL_Compilation> runIO sumInput
Please enter 4 numbers
 > 1
 > 2
 > 3
 > 4
The sum of your numbers is 10.

Code generation

The code generator is defined using interpret with the target being a code generation monad Code:

code :: ShowExp exp => Prog exp a -> Code a
code = interpret codeCMD

lowToCode :: Prog LowExp a -> String
lowToCode = runCode . code

The definition of codeCMD is deferred to Appendix: Code generation.

In this article, the code generator only produces simple imperative pseudo-code. Here is the code generated from the sumInput example:

*EDSL_Compilation> putStrLn $ lowToCode sumInput
    r0 <- initRef 0
    readInput "Please enter 4 numbers\n"
    for v1 < 4
        readInput " > "
        v2 <- stdin
        v3 <- getRef r0
        setRef r0 (v3 + v2)
    end for
    readInput "The sum of your numbers is "
    v4 <- getRef r0
    writeOutput v4
    readInput ".\n"

High-level expressions

The expression language LowExp is really too simple to be very useful. For example, it does not allow any kind of iteration, so any iterative program has to be written using the monadic for-loop.

To fix this, we introduce a new expression language, HighExp:

data HighExp a where
  -- Simple constructs (same as in LowExp):
  HVar :: Type a => VarId -> HighExp a
  HLit :: Type a => a -> HighExp a
  HAdd :: (Num a, Type a) => HighExp a -> HighExp a -> HighExp a
  HMul :: (Num a, Type a) => HighExp a -> HighExp a -> HighExp a
  HNot :: HighExp Bool -> HighExp Bool
  HEq  :: Type a => HighExp a -> HighExp a -> HighExp Bool

  -- Let binding:
  Let  :: Type a
       => HighExp a                 -- value to share
       -> (HighExp a -> HighExp b)  -- body
       -> HighExp b

  -- Pure iteration:
  Iter :: Type s
       => HighExp Int32            -- number of iterations
       -> HighExp s                -- initial state
       -> (HighExp s -> HighExp s) -- step function
       -> HighExp s                -- final state

instance (Num a, Type a) => Num (HighExp a) where
  fromInteger = HLit . fromInteger
  (+) = HAdd
  (*) = HMul

instance FreeExp HighExp where
  constExp = HLit
  varExp   = HVar

HighExp contains the same simple constructs as LowExp, but has been extended with two new constructs: let binding and iteration. Both of these constructs use higher-order abstract syntax (HOAS) (Pfenning and Elliot 1988) as a convenient way to introduce local variables.

The following program uses HighExp to represent expressions:

powerInput :: Prog HighExp ()
powerInput = do
  printStr "Please enter two numbers\n"
  printStr " > "; m <- readInput
  printStr " > "; n <- readInput
  printStr "Here's a fact: "
  writeOutput m; printStr "^"; writeOutput n; printStr " = "
  writeOutput $ Iter n 1 (*m)
  printStr ".\n"

It asks the user to input two numbers m and n, and and prints the result of m^n. Note the use of Iter to compute m^n as a pure expression.

Compilation as a typed EDSL translation

Now we are getting close to the main point of the article. This is what we have so far:

  • A way to generate code from low-level programs (of type Prog LowExp a).
  • The ability to write high-level programs (of type Prog HighExp a).

What is missing is a function that translates high-level programs to low-level programs. Generalizing the problem a bit, we need a way to rewrite a program over one expression type to a program over a different expression type – a process which we call “re-expressing”.

Since Prog is a monad like any other, we can actually express this translation function using interpret:

reexpress :: (forall a . exp1 a -> Prog exp2 (exp2 a))
          -> Prog exp1 b
          -> Prog exp2 b
reexpress reexp = interpret (reexpressCMD reexp)

reexpressCMD :: (forall a . exp1 a -> Prog exp2 (exp2 a))
             -> CMD exp1 b
             -> Prog exp2 b
reexpressCMD reexp (InitRef a)  = CMD . InitRef =<< reexp a
reexpressCMD reexp (GetRef r)   = CMD (GetRef r)
reexpressCMD reexp (SetRef r a) = CMD . SetRef r =<< reexp a
reexpressCMD reexp Read         = CMD Read
reexpressCMD reexp (Write a)    = CMD . Write =<< reexp a
reexpressCMD reexp (PrintStr s) = CMD (PrintStr s)
reexpressCMD reexp (For n body) = do
    n' <- reexp n
    CMD $ For n' (reexpress reexp . body)

The first argument to reexpress translates an expression in the source language to an expression in the target language. Note the type of this function: exp1 a -> Prog exp2 (exp2 a). Having a monadic result type means that source expressions do not have to be mapped directly to corresponding target expressions – they can also generate imperative code!

The ability to generate imperative code from expressions is crucial. Consider the Iter construct. Since there is no corresponding construct in LowExp, the only way we can translate this construct is by generating an imperative for-loop.

Translation of HighExp

Now that we have a general way to “re-rexpress” programs, we need to define the function that translates HighExp to LowExp. The first cases just map HighExp constructs to their corresponding LowExp constructs:

transHighExp :: HighExp a -> Prog LowExp (LowExp a)
transHighExp (HVar v)   = return (LVar v)
transHighExp (HLit a)   = return (LLit a)
transHighExp (HAdd a b) = LAdd <$> transHighExp a <*> transHighExp b
transHighExp (HMul a b) = LMul <$> transHighExp a <*> transHighExp b
transHighExp (HNot a)   = LNot <$> transHighExp a
transHighExp (HEq a b)  = LEq  <$> transHighExp a <*> transHighExp b

It gets more interesting when we get to Let. There is no corresponding construct in LowExp, so we have to realize it using imperative constructs:

transHighExp (Let a body) = do
  r  <- initRef =<< transHighExp a
  a' <- CMD $ GetRef r
  transHighExp $ body $ valToExp a'

The shared value a must be passed to body by-value. This is achieved by initializing a reference with the value of a and immediately reading out that value. Note that we cannot use the smart constructor getRef here, because it would return an expression of type LowExp while body expects a HighExp argument. By using CMD $ GetRef r, we obtain a result of type Val, which can be translated to any expression type using valToExp.

Exercise: Change the translation of Let to use call-by-name semantics instead.

Translation of Iter is similar to that of Let in that it uses a reference for the local state. The iteration is realized by an imperative for loop. In each iteration, the state variable r is read, then the next state is computed and written back to the r. After the loop, the content of r is returned as the final state.

transHighExp (Iter n s body) = do
  n' <- transHighExp n
  r  <- initRef =<< transHighExp s
  for n' $ \_ -> do
    sPrev <- CMD $ GetRef r
    sNext <- transHighExp $ body $ valToExp sPrev
    setRef r sNext
  getRef r

Exercise: Change the translation of Iter by unrolling the body twice when the number of iterations is known to be a multiple of 2:

transHighExp (Iter (HMul n (HLit 2)) s body) = do
  ...

All that is left now is to put the pieces together and define the compile function:

translateHigh :: Prog HighExp a -> Prog LowExp a
translateHigh = reexpress transHighExp

compile :: Prog HighExp a -> String
compile = lowToCode . translateHigh

To see that it works as expected, we compile the powerInput example:

*EDSL_Compilation> putStrLn $ compile powerInput
    printStr "Please enter two numbers\n"
    printStr " > "
    v0 <- readInput
    printStr " > "
    v1 <- readInput
    printStr "Here's a fact: "
    writeOutput v0
    printStr "^"
    writeOutput v1
    printStr " = "
    r2 <- initRef 1
    for v3 < v1
        v4 <- getRef r2
        setRef r2 (v4 * v0)
    end for
    v5 <- getRef r2
    writeOutput v5
    printStr ".\n"

Note especially the for-loop resulting from the pure expression Iter n 1 (*m).

Discussion

The simplicity of the presented technique can be seen by the fact that most of the code is reusable.

  • Prog, interpret and reexpress are independent of the expression language.
  • LowExp and its associated functions can be used as target for many different high-level languages.

The only (non-trivial) parts that are not reusable are HighExp and transHighExp. This shows that in fact very little work is needed to define an EDSL complete with code generation.

Generalized implementation

In fact, most of the code presented in this article is available in generic libraries:

  • Prog, interpret and reexpress are available in operational-alacarte (under the names Program, interpretWithMonad and reexpress).
  • Variants of the instructions in CMD are available as separate composable types in the package imperative-edsl (which, in turn, depends on operational-alacarte).

As a proof of concept, we provide a supplementary file that implements the HighExp compiler using the generic libraries.

The whole compiler, not counting imports and the definition of HighExp, is just 30 lines of code! What is more, it emits real runnable C code instead of the pseudo-code used in this article.

Richer high-level languages

Part of the reason why transHighExp was so easy to define is that HighExp and LowExp support the same set of types. Things get more complicated if we want to extend HighExp with, for example, tuples.

Readers interested in how to implement more complex expressions using the presented technique are referred to the RAW-Feldspar source code (tag article-2016-03).

Appendix: Code generation

Here we define he missing parts of the code generator introduced in Code generation.

Code generation of instructions

-- Code generation monad
type Code = WriterT [Stmt] (State Unique)

type Stmt   = String
type Unique = Integer

runCode :: Code a -> String
runCode = unlines . flip evalState 0 . execWriterT . indent

-- Emit a statement in the generated code
stmt :: Stmt -> Code ()
stmt s = tell [s]

-- Generate a unique identifier
unique :: String -> Code VarId
unique base = do
  u <- get; put (u+1)
  return (base ++ show u)

-- Generate a unique symbolic value
freshVar :: Type a => Code (Val a)
freshVar = ValComp <$> unique "v"

-- Generate a unique reference
freshRef :: Type a => Code (Ref a)
freshRef = RefComp <$> unique "r"

-- Modify a code generator by indenting the generated code
indent :: Code a -> Code a
indent = censor $ map ("    " ++)

-- Code generation of instructions
codeCMD :: ShowExp exp => CMD exp a -> Code a
codeCMD (InitRef a) = do
  r <- freshRef
  stmt $ unwords [show r, "<- initRef", showExp a]
  return r
codeCMD (GetRef r) = do
  v <- freshVar
  stmt $ unwords [show v, "<- getRef", show r]
  return v
codeCMD (SetRef r a) = stmt $ unwords ["setRef", show r, showExp a]
codeCMD Read = do
  v <- freshVar
  stmt $ unwords [show v, "<- readInput"]
  return v
codeCMD (Write a)    = stmt $ unwords ["writeOutput", showExp a]
codeCMD (PrintStr s) = stmt $ unwords ["printStr", show s]
codeCMD (For n body) = do
  i <- freshVar
  stmt $ unwords ["for", show i, "<", showExp n]
  indent $ code (body i)
  stmt "end for"

Showing expressions

instance Show (Val a) where show (ValComp a) = a
instance Show (Ref a) where show (RefComp r) = r

class ShowExp exp where
  showExp :: exp a -> String

bracket :: String -> String
bracket s = "(" ++ s ++ ")"

instance ShowExp LowExp where
  showExp (LVar v)   = v
  showExp (LLit a)   = show a
  showExp (LAdd a b) = bracket (showExp a ++ " + " ++ showExp b)
  showExp (LMul a b) = bracket (showExp a ++ " * " ++ showExp b)
  showExp (LNot a)   = bracket ("not " ++ showExp a)
  showExp (LEq a b)  = bracket (showExp a ++ " == " ++ showExp b)

Acknowledgement

This work is funded by the Swedish Foundation for Strategic Research, under grant RAWFP.

References

Axelsson, Emil, Koen Claessen, Gergely Dévai, Zoltán Horváth, Karin Keijzer, Bo LyckegÃ¥rd, Anders Persson, Mary Sheeran, Josef Svenningsson, and András Vajda. 2010. “Feldspar: A Domain Specific Language for Digital Signal Processing Algorithms.” In 8th IEEE/ACM International Conference on Formal Methods and Models for Codesign (MEMOCODE 2010), 169–78. IEEE. doi:10.1109/MEMCOD.2010.5558637.

Bracker, Jan, and Andy Gill. 2014. “Sunroof: A Monadic DSL for Generating JavaScript.” In Practical Aspects of Declarative Languages, edited by Matthew Flatt and Hai-Feng Guo, 8324:65–80. Lecture Notes in Computer Science. Springer International Publishing. doi:10.1007/978-3-319-04132-2_5.

Hickey, Patrick C., Lee Pike, Trevor Elliott, James Bielman, and John Launchbury. 2014. “Building Embedded Systems with Embedded DSLs.” In Proceedings of the 19th ACM SIGPLAN International Conference on Functional Programming, 3–9. ICFP ’14. New York, NY, USA: ACM. doi:10.1145/2628136.2628146.

Persson, Anders, Emil Axelsson, and Josef Svenningsson. 2012. “Generic Monadic Constructs for Embedded Languages.” In Implementation and Application of Functional Languages, edited by Andy Gill and Jurriaan Hage, 7257:85–99. Lecture Notes in Computer Science. Springer Berlin Heidelberg. doi:10.1007/978-3-642-34407-7_6.

Pfenning, Frank, and Conal Elliot. 1988. “Higher-Order Abstract Syntax.” In Proceedings of the ACM SIGPLAN 1988 Conference on Programming Language Design and Implementation, 199–208. PLDI ’88. New York, NY, USA: ACM. doi:10.1145/53990.54010.

Svenningsson, Josef David, and Bo Joel Svensson. 2013. “Simple and Compositional Reification of Monadic Embedded Languages.” In Proceedings of the 18th ACM SIGPLAN International Conference on Functional Programming, 299–304. ICFP ’13. New York, NY, USA: ACM. doi:10.1145/2500365.2500611.

Thursday, November 19, 2015

Simple construction of fixed-point data types

(This post is literate Haskell, available here.)

This post will show a trick for making it easier to work with terms representated as fixed-points of functors in Haskell. It is assumed that the reader is familiar with this representation.

First some extensions:

{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeFamilies #-}

Representing terms as fixed-points

A common generic term representation is as a fixed-point of a functor:

newtype Term f = In { out :: f (Term f) }

A simple example functor is the following, representing arithmetic expressions:

data Arith a
    = Num Int
    | Add a a
  deriving (Functor)

One problem with the Term representation is that it’s quite cumbersome to construct with it. For example, the following is the representation of the expression (1+2)+3(1+2)+3:

term1 :: Term Arith
term1 =
    In (Add
      (In (Add
        (In (Num 1))
        (In (Num 2))
      ))
      (In (Num 3))
    )

This post will show a simple trick for inserting the In constructors automatically, allowing us to write simply:

term2 :: Term Arith
term2 = deepIn10 $ Add (Add (Num 1) (Num 2)) (Num 3)

Deep conversion of terms

First, let’s have a look at the type of the above expression:

_ = Add (Add (Num 1) (Num 2)) (Num 3) :: Arith (Arith (Arith a))

We see that the type has three levels of Arith because the tree has depth three. So in general, we need a function with the following type scheme:

f (f (f ... (f (Term f)))) -> Term f

The purpose of this function is to insert an In at each level, from bottom to top. The reason for having Term f in the argument type (rather than just a) is that In expects an argument of type Term f.

This function has to be overloaded on the argument type, so we put it in a type class:

class Smart f a
  where
    deepIn  :: f a -> Term f
    deepOut :: Term f -> f a

Here f is the functor and a can be Term f, f (Term f), f (f (Term f)), etc. The class also contains the inverse function deepOut for reasons that will be explained later.

First we make the base instance for when we only have one level:

instance Smart f (Term f)
  where
    deepIn  = In
    deepOut = out

Next, we make a recusive instance for when there is more than one level:

instance (Smart f a, Functor f) => Smart f (f a)
  where
    deepIn  = In . fmap deepIn
    deepOut = fmap deepOut . out

And that’s it!

Ambiguous types

Now there’s only one problem. If we try to use deepIn $ Add (Add (Num 1) (Num 2)) (Num 3), GHC will complain:

No instance for (Smart Arith a0) arising from a use of ‘deepIn’

As said earlier, the type of the given expression is Arith (Arith (Arith a)), which means that we get an ambiguous type a which means that it’s not possible to know which of the above instances to pick for this a.

So we need to constrain the type before calling deepIn. One idea might be to use a type function for constraining the a. Unfortunately, as far as I know, it’s not possible to make a type function that maps e.g.

Arith (Arith ... (Arith a))

to

Arith (Arith ... (Arith (Term Arith)))

This is because a is a polymorphic variable, and type functions cannot case split on whether or not a type is polymorphic.

But there is one thing we can do. The type Arith (Arith (Arith a)) shows that the expression has (at most) three levels. But there is nothing stopping us from adding more levels in the type.

Why not make a version of deepIn10 that forces 10 levels in the type?

type Iter10 f a = f (f (f (f (f (f (f (f (f (f a)))))))))

deepIn10 :: (Functor f, a ~ Iter10 f (Term f)) => f a -> Term f
deepIn10 = deepIn

This function can be used to insert In constructors in terms of at most depth 10.

Constructing terms from existing terms

The above example, term2, was constructed from scratch. We might also want to construct terms that contain other, already constructed, terms. This is where deepOut comes into play:

term3 = deepIn10 $ Add (deepOut term2) (Num 4)

Monday, October 26, 2015

Sudoku solver using SAT+

(This post is literate Haskell, available here.)

Inspired by an old(-ish) Galois post about using its SAT back end to solve sudokus, here is a similar sudoku solver written in Haskell using Koen Claessen’s SAT+ library.

Representing sudokus

We represent sudokus as a simple column-of-rows:

type Sudoku = [[Maybe Int]]

A valid sudoku will have 9 rows and 9 columns and each cell is either Nothing or a value between 1 and 9.

The rules of sudoku mandates that each of the 27 “blocks” (rows, columns and 3×33\times 3 squares) only contain distincts numbers in the interval 1-9. So the first thing we need is a function for extracting the blocks of a sudoku:

blocks :: [[a]] -> [[a]]

The argument is a sudoku (9×99\times 9 elements) and the result is a list of blocks (27×927\times 9 elements). We use a polymorphic function in order to make it work also for the SAT representation of sudokus below.

(We omit the definition of blocks from this post, because it’s part of a lab here at Chalmers.)

Solving constraints using SAT+

SAT+ is a library that adds abstractions and convenience functions on top of the SAT solver MiniSAT. Here we will only make use of quite a small part of the library.

The inequality constraints of sudokus can be expressed using the following functions:

newVal   :: Ord a => Solver -> [a] -> IO (Val a)
val      :: a -> Val a
notEqual :: Equal a => Solver -> a -> a -> IO ()

Function newVal creates a logical value that can take on any of the values in the provided list. A specific known value is created using val, and finally, inequality between two values is expressed using notEqual.

The Solver object required by newVal and other functions is created by newSolver:

newSolver :: IO Solver

After a solver has been created and constraints have been added using the functions above, we can use solve to try to find a solution:

solve :: Solver -> [Lit] -> IO Bool

If solve returns True we can then use modelValue to find out what values were assigned to unknowns:

modelValue :: Solver -> Val a -> IO a

These are all the SAT+ functions we need to solve a sudoku.

The solver

In order to solve a sudoku, we first convert it to a “matrix” of logical values:

type Sudoku' = [[Val Int]]

fromSudoku :: Solver -> Sudoku -> IO Sudoku'
fromSudoku sol = mapM (mapM fromCell)
  where
    fromCell Nothing  = newVal sol [1..9]
    fromCell (Just c) = return (val c)

This simply converts the matrix of Maybe Int to a matrix of Val Int, such that Just c is translated to the logical value val c and Nothing is translated to a logical value in the range 1-9.

Next, we need to add inequality constraints according to the rules of sudoku. After obtaining the blocks using blocks, we just add constraints that all values in a block should be different:

isOkayBlock :: Solver -> [Val Int] -> IO ()
isOkayBlock sol cs = sequence_ [notEqual sol c1 c2  | c1 <- cs, c2 <- cs, c1/=c2]

isOkay :: Solver -> Sudoku' -> IO ()
isOkay sol = mapM_ (isOkayBlock sol) . blocks

After solving a sudoku, we need to be able to convert it back to the original representation. This is done using modelValue to query for the value of each cell:

toSudoku :: Solver -> Sudoku' -> IO Sudoku
toSudoku sol rs = mapM (mapM (fmap Just . modelValue sol)) rs

Now we have all the pieces needed to define the solver:

solveSud :: Sudoku -> IO (Maybe Sudoku)
solveSud sud = do
    sol  <- newSolver
    sud' <- fromSudoku sol sud
    isOkay sol sud'
    ok <- solve sol []
    if ok
      then Just <$> toSudoku sol sud'
      else return Nothing

Tuesday, September 29, 2015

Strictness can fix non-termination!

(This post is literate Haskell, available here.)

I’ve always thought that strictness annotations can only turn terminating programs into non-terminating ones. Turns out that this isn’t always the case. As always, unsafePerformIO changes things.

import Data.IORef
import System.IO.Unsafe

Consider the following function for turning an IORef into a pure value:

unsafeFreezeRef :: IORef a -> a
unsafeFreezeRef = unsafePerformIO . readIORef

(I explain why I’m interested in this function below.)

This function is of course unsafe, but we may naively expect it to work in the following setting where it’s used inside a function that returns in IO:

modRef :: (a -> a) -> IORef a -> IO ()
modRef f r = writeIORef r $ f $ unsafeFreezeRef r

However, this doesn’t work, because the readIORef inside unsafeFreezeRef happens to be performed after writeIORef, leading to a loop.

So, strangely, the solution is to use a strictness annotation to force the read to happen before the write:

modRef' :: (a -> a) -> IORef a -> IO ()
modRef' f r = writeIORef r $! f $ unsafeFreezeRef r

This is the first time I’ve seen a strictness annotation turn a non-terminating program into a terminating one.

Here is a small test program that terminates for modRef' but not for modRef:

test = do
    r <- newIORef "hello"
    modRef' tail r
    putStrLn =<< readIORef r

I’m not sure this tells us anything useful. We should probably stay away from such ugly programming anyway… But at least I found this example quite interesting and counterintuitive.

Why on earth would anyone write unsafeFreezeRef?

In case you wonder where this strange example comes from, I’m working on a generic library for EDSLs that generate C code. Among other things, this EDSL has references that work like Haskell’s IORefs.

The purpose of unsafeFreezeRef in the library is to avoid temporary variables in the generated code when we know they are not needed. The idea is that the user of unsafeFreezeRef must guarantee that result is not accessed after any subsequent writes to the reference. Reasoning about when a value is used is quite easy in the EDSL due to its strict semantics. For example, we don’t need any strictness annotation in the definition of modRef due to the fact that setRef (the equivalent of writeIORef) is already strict.

The reason I noticed the problem of laziness and unsafeFreezeRef was that the EDSL evaluator turned out to be too lazy when writing to references. So I had a program which produced perfectly fine C code, but which ran into a loop when evaluating it in Haskell. The solution was to make the evaluator more strict; see this commit and this.