如何在不重新编译Haskell的情况下更改数据类型



在观看了Bret Victor的一段演讲视频后,我受到启发,编写了一个与他在演讲中演示的开发环境有点相似的快速破解。

基本上,这个想法是,让应用程序在一个窗口中运行,每当在源文件中保存更改时,程序就会更改。

除了我不能在不关闭应用程序并重新编译的情况下更改代码中的状态类型之外,这对小的更改非常有效。

如何解决表达式问题并使我的状态的数据类型能够更改而不会导致重新编译?

p.S。这是代码。我最初不想发帖,因为它真的很乱,很快就被黑了,但人们想要它,这样他们就可以得到它。

首先是显示器和空闲模块(这是一个快速破解,所以我不知道如何将它们作为真正的模块)。

空闲.hs

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

显示.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) (-w)
        vertex $ Vertex3 (-w) w (-w)
points :: Integer -> [(GLfloat,GLfloat,GLfloat)]
points n' = let n = fromIntegral n' in map (k -> let t = 2*pi*k/n in (sin(t),cos(t),0.0))  [1..n]
in do
    clear [ ColorBuffer ]
    counter <- readIORef state
    mapM_ ((x,y,z) -> preservingMatrix $ do
           color $ Color3 ((x+1.0)/2.0) ((y+1.0)/2.0) ((z+1.0)/2.0)
           translate $ Vector3 x y z
           cube (0.3::GLfloat)
           ) $ points (9 + counter)
    flush

主模块

module Main where
import Control.Monad
import Data.Typeable as Typeable
import System.IO
import Data.IORef
import Graphics.Rendering.OpenGL
import Graphics.UI.GLUT
import Language.Haskell.Interpreter
main :: IO ()
main = do
    (_, _) <- getArgsAndInitialize
    createWindow "Hello World"
    action <- newIORef $ do
    clear [ ColorBuffer ]
    flush
    let imports = ["Prelude", "Data.IORef", "Graphics.Rendering.OpenGL", "Graphics.UI.GLUT"]
    let modules = ["State"]
    runFile (undefined :: IORef Integer -> IO ()) "Display.hs" imports $ displayCode ->
    runFile (undefined :: IORef Integer -> IO ()) "Idle.hs" imports $ idleCode -> do
    state <- newIORef 12
    displayCallback $= display displayCode state
    idleCallback $= Just (idle displayCode idleCode state)
    mainLoop
display displayCode state = do
    f <- execute displayCode
    f state
idle displayCode idleCode state = do
    update displayCode
    update idleCode
    f <- execute idleCode
    f state
instance Eq GhcError where
    GhcError s == GhcError t = s == t
instance Eq InterpreterError where
    UnknownError s == UnknownError t = s == t
    WontCompile s == WontCompile t = s == t
    NotAllowed s == NotAllowed t = s == t
    GhcException s == GhcException t = s == t
data V a = V {
    update :: IO (),
    execute :: IO a
 }
runFile :: Typeable a => a -> String -> [String] -> (V a -> IO ()) -> IO ()
runFile theType file imports f = do
    currentError <- newIORef Nothing
    currentAction <- newIORef Nothing
    let v = V {
        update = do
            fileContents <- readFile file
            result <- runInterpreter $ do
                setImports imports
                interpret fileContents theType
                oldError <- readIORef currentError
                case result of
                Right newAction -> do
                    when (oldError /= Nothing) $ do
                        writeIORef currentError Nothing
                        putStrLn (file ++ " Ok!")
                        writeIORef currentAction (Just newAction)
                        Left newError -> do
                            when ((Just newError) /= oldError) $ do
                                writeIORef currentError (Just newError)
                                print newError
                                , execute = do
                                    action <- readIORef currentAction
                                    case action of
                                    Nothing -> do
                                        err <- readIORef currentError
                                        return (error (show err))
                                        Just act -> return act
                                        }
    update v 
    f v

我确信这在GHC中是不可能的。当编译Haskell时,更高级别的语言被降级到Core中,Core也是类型化的。GHC不会启动到核心的转换,直到程序已经键入检查。这也是有原因的:当程序类型检查时,它同时证明了自己。正如jberryman所指出的,唯一的解决方案是为State提供一个灵活的类型,允许多态性,因此类型更改可能不会注册为多态性。

相关内容

最新更新