-
-
Save pasberth/4055260 to your computer and use it in GitHub Desktop.
Macraのコンパイラに遅延評価を実装
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
vm'' vmState@(VM a ThawInst _ (val:r) s mem) = do | |
case a of | |
(Thunk body envRef) -> do | |
S.put vmState { | |
vmInst = body | |
} | |
vm' -- 再帰 | |
newVMState <- S.get | |
S.put newVMState { | |
, vmEnvMem = -- なんかenvmem更新する処理 | |
} | |
vm' | |
_ -> do | |
S.liftIO $ do | |
putStr $ concat ["Invalid application: ", show a] | |
return () |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 今、 | |
"!funcall !lambda x x 1" --#1 | |
-- をコンパルすると | |
Right (FrameInst HaltInst (ConstExpr 1.0 (ArgInst (CloseInst "x" (ReferInst "x" ReturnInst) ApplyInst)))) -- #2 | |
-- これで1を遅延させるには、thunkに包んでやる。 | |
!funcall !lambda x !funcall x nil !lambda nil 1 --#3 | |
-- コンパイルするとこうなる | |
Right (FrameInst HaltInst (CloseInst "nil" (ConstExpr 1.0 ReturnInst) (ArgInst (CloseInst "x" (FrameInst ReturnInst (ReferInst "nil" (ArgInst (ReferInst "x" ApplyInst)))) ApplyInst)))) -- #4 | |
-- Macraを遅延評価にするなら、#1を直接#4にコンパイルしてやればいい。 | |
-- Thunkは必ず0引数なので、ThunkInstを導入して、CloseInstと区別する. | |
Right (FrameInst HaltInst (FreezeInst (ConstExpr 1.0 ReturnInst) (CloseInst "x" (FrameInst ReturnInst (ArgInst (ReferInst "x" ApplyInst))) ThawInst))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment