Alex Rivera | Logout

How can I change my data types without causing a recompile in Haskell?

Asked 2012-02-25T19:09:30.510
9

After watching a video of a talk by Bret Victor, I was inspired to write a quick hack that was somewhat similar to a development environment he demonstrated in the talk.

Basically the idea is, one has the app running in one window and whenever one saves a change in a source file the program changes.

This works great for small changes except that I can't change the type of the state in my code without shutting down the app and recompiling.

How can I solve the expression problem and have the data type of my state be able to change without causing a recompile?

P.S. Here's the code. I originally didn't want to post because it was really messy and quickly hacked together, but people wanted it so they can get it.

First the display and the idle module, (this was a quick hack so I didn't figure out how to do them as real modules).

Idle.hs

\state -> do
    counter <- readIORef state
    writeIORef state ((counter + 1)`mod`3)
    postRedisplay Nothing

Display.hs

\state -> let
cube w = do 
    renderPrimitive Quads $ do
        vertex $ Vertex3 w w w
        vertex $ Vertex3 w w (-w)
        vertex $ Vertex3 w (-w) (-w)
        vertex $ Vertex3 w (-w) w
        vertex $ Vertex3 w w w
        vertex $ Vertex3 w w (-w)
        vertex $ Vertex3 (-w) w (-w)
        vertex $ Vertex3 (-w) w w
        vertex $ Vertex3 w w w
        vertex $ Vertex3 w (-w) w
        vertex $ Vertex3 (-w) (-w) w
        vertex $ Vertex3 (-w) w w
        vertex $ Vertex3 (-w) w w
        vertex $ Vertex3 (-w) w (-w)
        vertex $ Vertex3 (-w) (-w) (-w)
        vertex $ Vertex3 (-w) (-w) w
        vertex $ Vertex3 w (-w) w
        vertex $ Vertex3 w (-w) (-w)
        vertex $ Vertex3 (-w) (-w) (-w)
        vertex $ Vertex3 (-w) (-w) w
        vertex $ Vertex3 w w (-w)
        vertex $ Vertex3 w (-w) (-w)
        vertex $ Vertex3 (-w) (-w) 
Edit
Report

1 Answer

2

I'm pretty sure it is impossible in GHC. When Haskell is compiled, the higher level language is desugared into Core, which is also typed. GHC will not initiate the transformation into Core until the program has been typed checked. There's a reason for this, too: as the program type checks it simultaneously proves itself. As jberryman noted, the only work around would be to have a flexible type for State which would allow polymorphism, so a type change might not register as one.

answered 2012-04-18T18:53:15.587

Your Answer