Is operational really isomorphic to a free monad?

前端 未结 3 1798
北荒
北荒 2021-01-30 09:46

Proofs

In this blog post, Tekmo makes the point that we can prove that ExitSuccess exits because (I presume) it\'s like the Const functor for

3条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-30 10:05

    The answer is no, you cannot prove that the operational one ignores the rest of the program on exitSuccess. Contrast TeletypeI with TeletypeF to see why. I'll rewrite TeletypeF in GADT notation for easier comparison

    data TeletypeF x where                     | data TeletypeI x where
      PutStrLn :: String -> x  -> TeletypeF x  |   PutStrLn :: String -> TeletypeI ()
      GetLine :: (String -> x) -> TeletypeF x  |   GetLine :: TeletypeI String
      ExitSuccess ::              TeletypeF x  |   ExitSuccess :: TeletypeI ()
    

    Using TeletypeF, we can build actual programs right away:

    GetLine (\str -> PutStrLn (map toUpper str) ExitSuccess)
    

    TeletypeI does not come with a way to refer to "the rest of the program" in the same way that TeletypeF does.

    -- TeletypeF:
    GetLine (\str -> "rest of program" goes here)
    -- or
    PutStrLn someString ("rest of program" goes here)
    -- or
    ExitSuccess -- there is no "rest of program" slot provided
    

    Since TeletypeI lacks this "rest of the program" information, you can no longer gain any knowledge when you come across ExitSuccess.

    -- TeletypeI
    PutStrLn someString -- no information about "rest of program"
    -- or
    GetLine -- no information about "rest of program"
    -- or
    ExitSuccess -- no information about "rest of program"
    

    What is permissible to come as the "rest of the program" is entirely up to the Program type, which doesn't know anything about the instruction set it is being applied to. It simply allows you to bind instructions together, and terminate via Return.

提交回复
热议问题