First, let me start by quoting from Wikipedia on mutation testing: "Mutation testing involves modifying a program’s source code or byte code in small ways." The mutations should not break the execution and therefore the mutated programs should pass the type checking. So, I grabbed a version of HTE available online and installed it with Cabal.

```
> module Test.QuickMutate where
>
> import Language.Haskell.THIH.Typecheck.Load
> import Language.Haskell.THIH.Typecheck.Types hiding (Module)
> import Language.Haskell.Exts.FromTHIH
> import Language.Haskell.Exts hiding (Name)
> import Data.List
```

Since it is needed to mutate and change different parts of the program, I decided to use Uniplate to do the generics.

```
> import Data.Generics.Uniplate.Data
```

Also, it is required to typecheck all of the mutated programs without interruptions, therefore, I used the following hack:

```
> import Control.DeepSeq
> import System.IO.Unsafe
> import Control.Exception as CE
>
> tcHSE :: Module -> Maybe String
> tcHSE ast = unsafePerformIO $
> CE.catch
> (let x = show $ tcHSEModuleWithPrelude ast
> in deepseq x (return $ Just x))
> ((\_ -> return Nothing)
> :: (SomeException -> IO (Maybe a)))
```

For automating mutation testing, we need a database of mutation operators. It can be modeled as a list of pairs of names (quantified).

```
> type MutationOps = [(QName,QName)]
```

Therefore, the mutating function takes the database of mutation operators and the program and generates the mutated programs.

```
> mutate :: MutationOps -> Module -> [Module]
```

As the first step, I defined the transformations using Uniplate:

```
> mutateE :: (QName , QName) -> Exp -> Exp
> mutateE p e = transformBi (mutateQ p) e
>
> mutateQ :: (QName , QName) -> QName -> QName
> mutateQ (op,op') x
> | x == op = op'
> | otherwise = x
>
> mutate dic m = let
> ms' = [transformBi (mutateE pair) m | pair <- dic]
> -- ...
```

Then, I used HTE to infer the type of the original program and all the mutated programs. Finally, I filtered out mutated programs with bad types, types different from the original type and duplications.

```
> -- ...
> Just ty = tcHSE m
> ms'' = filter ( \ m'-> Just ty == tcHSE m') ms'
> in (nub (m : ms'') )
```

To make it easier to play with the code, I made a front-end that takes string instead of AST.

```
> mutateStr :: [(String,String)] -> String -> [String]
> mutateStr dic inp = let
> ParseOk m = parseModule inp
> in map prettyPrint $ concat $ map getDecl
> $ mutate [(toQn x ,toQn y ) | (x,y) <- dic] m
> where
> toQn :: String -> QName
> toQn inp = let ParseOk (Var m ) = parseExp inp in m
>
> getDecl :: Module -> [Decl]
> getDecl(Module _ _ _ _ _ _ dcs) = dcs
```

Then I started to test.

Testing if it actually does the mutation where it should:

```
> test_new = mutateStr [("(+)","(-)")] "f x = x + x"
```

```
ghci> test_new
["f x = x + x","f x = x - x"]
```

Testing if it accepts a mutation that results in valid, yet different type:

```
> test_wrong = mutateStr [("(+)","(++)")] "f x = x + x"
```

```
ghci> test_wrong
["f x = x + x"]
```

Testing if it accepts a mutation that does not typecheck:

```
> test_bad = mutateStr [("(+)","f")] "f x = x + x"
```

```
ghci> test_bad
["f x = x + x"]
```

Testing if it removes duplications:

```
> test_dup = mutateStr [("(-)","(+)")] "f x = x + x"
```

```
ghci> test_dup
["f x = x + x"]
```

So far it behaves as expected. Of course, there is much more left to do; this is just the beginning!

]]>

Here are some updates and good news:

- Haskell-Name is alive now and is being actively developed! Thanks to Roman
- Haskell-Types vLocalAssumptions had some progress but still much work is left.
- Haskell-Desugar is online if you have not noticed yet.
- Haskell-Compile & Haskell-Interpret had some progress; I am currently implementing a backend using Epic, the backend of Agda, Epigram and Idris.
- I am currently writing a paper on how to map back the inferred types from the desugared code to the original code. If everything goes as expected, this bidirectionality will be added to HTE.
- There are also two other tiny projects:

Haskell-Dependency which does the dependency analysis and the related transformations.

Haskell-Dictionary which removes the constraints by using dictionaries. - Added to the wish-list / future plan:

Developing translation to System FC (aka GHC Core)

Metaprogramming / Template Haskell - Few nice ongoing research projects, yet still too early to judge

Well, I am excited about these, lots of fun! I am currently, struggling with my schedule, pushing it to get a few more hours to work on these. Things may go slow, but in general there would be steady progress.

Indeed, I would welcome any sort of support!

]]>

**Update**: the sound is fixed now!

]]>

- Generating Functions
For each datatype

*D*,

*> data X = X1 | X2 Int | X3 { l1 :: String} | X4 { l2 :: Int, l1::String}*- Default data constructors:

for each constructor*Ci*, generate a function named*newCi*of type*newCi :: D*that constructs a new value of type*D*using*Ci*and*undefined*in each field.

*> newX1,newX2,newX3,newX4 :: X*

*> newX1 = X1*

*> newX2 = X2 undefined*

*> newX3 = X3 undefined*

*> newX4 = X4 undefined undefined* - Match functions:

for each constructor*Ci*, generate a function named*isCi*of type*isCi :: D -> Bool*that checks if a value of type*D*is tagged with*Ci*.

*> isX1, isX2, isX3, isX4 :: X -> Bool*

> isX1 = \x -> case x of { X1 -> True ; _ -> False}

> isX2 = \x -> case x of { X2 _ -> True ; _ -> False}

> isX3 = \x -> case x of { X3 _ -> True ; _ -> False}

> isX4 = \x -> case x of { X4 _ _ -> True ; _ -> False} - Getter functions:

for each label*li :: Ti*, generate a function named*li*of type*li :: D -> Ti*that extracts value of the field labeled*li*.

*> l1 :: X -> String*

*> l1 = \x -> case x of { X3 x0 -> x0 ; X4 _ x0 -> x0 ; _ -> error “error!”}*

*> l2 :: X -> Int*

*> l2 = \x -> case x of {X4 x0 _ -> x0 ; _ -> error “error!”}* - Setter functions:

for each label*li :: Ti*, generate a function named*setli*of type*setli :: Ti -> D -> D*that sets value of the field labeled*li*.

*> setl1 :: String -> X -> X*

*> setl1 = \v -> \x -> case x of { X3 _ -> X3 v ; X4 x0 _ -> X4 x0 v ; _ -> error**“error!”*}

*> setl2 :: Int -> X -> X*

*> setl2 = \v -> \x -> case x of {X4 _ x0 -> X4 v x0 ; _ -> error “error!”}*

- Default data constructors:
- Desugar record update expression as a chain of setter function applications.

*> x { l1 = “test”, l2 = 1} —> ((setl2 1) . (setl1 “test”)) x* - Desugar record constructor expression as the record update of corresponding default data constructor.

*> X4 { l1 = “test”, l2 = 1} —> newX4 { l1 = “test”, l2 = 1}* - Desugar the empty record pattern
*Ci {}*as a guard using the match function*isCi.*

> f (X2{}) = “test” —> f x0 | isX2 x0 = “test” - Desugar the non-empty record pattern using patter guards, getter functions and empty record pattern.

*> f (X4 {l2= 2, l1=x}) = “test” —> f (x0@(X4 {})) | 2 <- l2 x0, x <- l1 x0 = “test”* - Desugar data declaration with record syntax to the normal ADT syntax.

>*data X = X1 | X2 Int | X3 { l1 :: String} | X4 { l2 :: Int, l1::String}*

> —>

> data X = X1 | X2 Int | X3 String | X4 Int String

For more details:

https://github.com/shayan-najd/Haskell-Desugar/blob/master/Language/Haskell/Exts/Desugar/Record.hs

]]>

You can find the abstract at:

http://www.haskell.org/haskellwiki/HaskellImplementorsWorkshop/2012/Broberg

]]>

https://github.com/shayan-najd/HTE

You may read the previous posts about the architecture and design of HTE.

It has the following known issues:

- Records are not supported yet! Soon, I will explain in more details. I am currently working on adding support for extensible records and variants.
- It relies on a name resolution process developed as a separate package (Haskell-Name-Exts). But, the code should still be entirely usable.
- The dependency analysis is too simplistic! A separate package will take care of it (Haskell-Dependency-Exts).
- Just a basic interface
- Bugs!

You may start from :

https://github.com/shayan-najd/HTE/blob/master/Language/Haskell/THIH/Typecheck/Load.hs

For example:

ghci> tcStringWithPrelude3 “data X = X; f y = X”

[("f","forall (t0 :: *) . t0 -> X")]

Feedback is more than welcome.

]]>

Congratulations, from our data it seems that you have successfully passed the Final Evaluations.

What a wonderful adventure! Invaluable experience that I truly owe to my mentor, Niklas Broberg, without whom I would still be decoding meanings out of the Greek symbols in the research papers. Also, I would like to thank Haskell.Org committee and Google for making this project a possibility.

Soon, I will put a snapshot (08/20) of the code (v. Haskell 2010) for the public access.

]]>

Firstly, the core part of the typechecker is nearly identical to the one (THIH) developed (and extended) by Mark P. Jones. The desugaring system is carefully coded to follow translations defined in Haskell 2010 language report. Kind inference is done by the typechecker itself; it employs the demotion schemes described earlier. Finally, the layered architecture of the project keeps each part separated.

The system is composed of the following components:

- HSE – Parser

:: (User input – Haskell 2010 :: String) -> AST - Desugaring System

:: AST -> Desugared AST - ToTHIH

:: Desugared AST -> Surface THIH Syntax (implicitly kinded) - Kind Inference

:: Surface THIH Syntax -> Original THIH Syntax (explictly kinded)- Demotion

:: Types of Surface THIH Syntax -> THIH Expressions - Typeinference (THIH)

:: THIH Expressions (implicitly typed) -> Inferred Types - Promotion

:: Inferred Types -> Inferred Kinds - Syntax Conversion

:: (Surface THIH Syntax, Kinds) -> Original THIH Syntax

- Demotion
- Loading

:: Module Headers / Top-level declarations -> Environment - THIH

:: Original THIH Syntax -> Environment -> THIH Assumptions - FromTHIH

:: THIH Assumptions -> Pretty Assumptions

]]>

Having a sound typechecker that demands type terms to be explicitly kinded, I was thinking of using the typechecker itself to do the kind inference.

Up to date, I am not aware of any similar “bootstrapping” method for kind inference and I am not sure if the resulting system is sound.

For more information about kind inference in Haskell, you may refer to:

Haskell 2010 Report

“A Static Semantics for Haskell” by Karl-Filip Faxen

“Giving Haskell a Promotion” — about the new extensions to GHC

In the following, I am going to sketch my idea with concrete examples. I used GHCi 7.4.1 with -XNoMonomorphismRestriction, -XKindSignature and -XExplicitForAll flags.

`{-# LANGUAGE NoMonomorphismRestriction, KindSignatures, ExplicitForAll #-}`

First I define the ***** kind:

`data Star :: *`

star :: Star

star = undefined

Then, for each primitive type, I define a function with explicit type:

`tInt :: Star`

tInt = undefined

I also define a function to represent the function type constructor (->):

infixr 6 -->

(-->) :: Star -> Star -> Star

a --> b = undefined

With GHC 7.2, if I am not mistaken, we have the “constraint” kind reserved as the kind of class constraints.

I code it as:

`data Constraint :: *`

constraint :: Constraint

constraint = undefined

For example:

`cEq :: Star -> Constraint`

cEq = undefined

A combinator to represent the predicates:

`infixr 5 ==>`

(==>) :: Constraint -> Star -> Star

(==>) = undefined

Now it is time to use these combinators to represent type signatures. For example:

f1 :: a -> Int

f1 = undefined

```
```f2 :: a

f2 = undefined

f3 :: Int

f3 = undefined

`f4 :: Eq a => a -> Int`

f4 = undefined

is modeled as:

`-- inferred type: tf1 :: Star -> Star`

tf1 a = a --> tInt

```
```-- inferred type: tf2 :: forall (t :: *). t -> t

tf2 a = a

-- inferred type: tf3 :: Star

tf3 = tInt

`-- inferred type: tf4 :: Star -> Star`

tf4 a = cEq a ==> a --> tInt

Here is the trick; we demote the type living in the type-universe to a normal value. Consequently, the inferred type for this value, hopefully,

represents the kind of the original type. Therefore, by this technique we can use the typechecker to also infer kinds.

For example, the inferred type of tf1 represents the kind of f1.

GHCi> :t tf1

tf1 :: Star -> Star

Now let us go one step further and model ADTs by the same technique. We first define the sum operator.

`infixr 4 <+>`

(<+>) :: Star -> Star -> Star

(<+>) = undefined

and then we can represent

`data T a b = T1 b Int`

| T2 (b -> Int)

| T3 (a b)

-- | T4 a -- ill-kinded

-- | T5 c -- ill-kinded

with

`-- inferred type: tT :: (Star -> Star) -> Star -> Star`

tT a b = (b --> tInt --> tT a b)

<+> ((b --> tInt) --> tT a b)

<+> (a b --> tT a b)

-- <+> (a --> tT a b) -- ill-typed

-- <+> (c --> tT a b) -- ill-typed

Brilliant! If one part of the original definition is ill-kinded, the representation becomes ill-typed.

Now to add support for type classes, we model the cons operator in the same fashion:

`infixr 4 <:>`

(<:>) :: forall (a :: *). (a -> Star)-> Constraint -> Constraint

(<:>) = undefined

Now we model the following definition,

`class C a where`

c1 :: a b -> Int

c2 :: b (a c) -> Int

-- f3 :: a c b -> Int -- ill-kinded

```
```class D a where

d0 :: a -> Int

`class E a where`

e1 :: E a => a -> Int

to:

`cC a = (\b -> a b --> tInt)`

<:>(\(b,c)-> b --> (a c) --> tInt)

-- <:> (\(b,c)-> a c b --> tInt) -- ill-typed

<:>constraint

```
```cd a = (\() -> tInt)

<:>constraint

`cD a = (\ () -> cD a ==> a --> tInt)`

<:>constraint

Notice, the definition of class E is recursive through the use of predicate “E a” but typechecker has no difficulty inferring the right type.

Finally, the support for type synonyms is straightforward:

`type S a b c d = T a b`

```
```

`sS a b c d = tT a b`

According to Haskell report, definitions should be grouped by their dependency order. Having this framework we can reuse the dependency analysis of let bindings

by simply putting the definition in the same let binding.

This kind system, in addition to features of Haskell 2010, supports constraint kinds and kind polymorphism.

]]>

Despite my genuine interest in Generic programming, I decided to take a more lightweight and rather simplistic method for Haskell-Desugaring-Exts (HDE). Mainly, to avoid introducing dependencies.

Here, I briefly describe the current implementation. As you will notice, it is not a very intelligent design; therefore it would be nice to know your view. How would you design the system?

Firstly, we need a mechanism to generate unique names. I use State Monad as a counter:

type Unique a = ErrorT String (ReaderT String (State Int)) a

```
```newVar :: Unique String

newVar = do

seed <- ask

r <- lift get

lift $ put $ succ r

return $ seed ++ (show r)

runUnique :: Unique a -> String -> Either String a

runUnique c seed = evalState ((runReaderT $ runErrorT c) seed) 0

initUnique :: Unique ()

initUnique = lift $ put 0

`error :: String -> Unique a`

error = throwError

I also used Reader and Error Monad transfomer to keep a unique seed for generating new names and reporting errors correspondingly.

For desugaring, I introduced an overload function:

class Desugar a where

desugar :: a -> Unique a

and then for simplicity, I defined few combinators:

-- Applicative style of applying desugaring

```
```infixl 4 **,$$

(**) :: Desugar a => Unique (a -> b) -> a -> Unique b

x ** y = x <*> desugar y

`($$) :: Desugar a => (a -> b) -> a -> Unique b`

x $$ y = pure x <*> desugar y

then I started to implement:

instance Desugar Kind where

desugar (KindParen k) =

desugar k

desugar KindBang =

return KindBang

desugar (KindFn k1 k2) =

KindFn $$ k1 ** k2

desugar (KindVar n) =

KindVar $$ n

In this example, I desugar the Kind terms by removing the parentheses.

]]>