# Generate a Function from a Type

This post is about elm, but I expect most or all of what I say here could also apply to Haskell, PureScript or other ML family languages.

One cool thing about Langauges in the ML family is that they can take a function and generate a type for that function.

But why can’t we go the other way? (Idris does do this) Lets take a type and generate part or all of a function. Here are a few ideas.

Lets say we want a function called `this2that`

. What if we could write
the type signature of the function like this `this2that: This -> That`

and have a tool take a look at this and work out the body. Maybe we
have other functions with signatures like `This -> A`

, `A -> B`

and ```
B
-> That
```

. Well then the body of `this2that`

could be formed by just
putting those functions together.

If we think of types and functions as a graph database, with the types being the vertices and the functions being the edges we could easily work out what combination of functions would be needed. In fact it would be a rather short prolog program.

Or take another example,

```
type alias Location = {x : Int, y: Int}
type Direction = North | South |East| West
move : Direction -> Location -> Location
```

The body of the function should look something like this

```
move dir loc =
case dir of
North -> {loc | ?rhs}
South -> {loc | ?rhs}
East -> {loc | ?rhs}
West -> {loc | ?rhs}
```

What if we could just type `move:`

and hit say Escape-tab and have the
editor fill that out for us. Then all we would have to do is fill in
the `?rhs`

(Right hand side) of the equations

This should exist and I would imagine that there are a lot more patterns that we could find.

Email me with other examples and I will add a second blog post.

Want to test your elm code? Buy the book