Using Cont to acquire values from the future and the past

后端 未结 2 1235
自闭症患者
自闭症患者 2021-02-02 13:02

I\'m writing a brainfuck interpreter in Haskell, and I came up with what I believe to be a very interesting description of a program:

data Program m = Instructio         


        
2条回答
  •  遇见更好的自我
    2021-02-02 13:32

    Being terribly lazy with this answer since I'm not comfortable with Cont, but is MonadFix perhaps what you're looking for? State is an instance, though not Cont, and it lets you do things that look like (using "recursive do" notation):

    {-# LANGUAGE DoRec #-}
    parseInst str = do
        rec ctl <- parseInstructionsLinkingTo ctl str
    

    This was the solution I discovered for my actors library: we want a spawn operation that returns the spawned actor's mailbox, but then how can we launch mutually-communicating actors? Or an actor with access to its own mailbox?

    With a suitable MonadFix instance we can do:

    fork3 = do
        rec mb1 <- spawn $ actorSpamming mb2 mb3
            mb2 <- spawn $ actorSpamming mb1 mb2
            mb3 <- spawn $ actorSpamming mb2 mb3
        send "go" mb1
    

    Hope above gives you ideas.

提交回复
热议问题