Chapter 03

トイ・マシンの動作の表現

Nobuo Yamashita
Nobuo Yamashita
2021.12.01に更新
このチャプターの目次

Toy.eval (toy02)

トイ・マシンの動作は、初期状態から終了状態までの状態遷移列 [ToyState] で表現します。そこで、初期状態から遷移列を生成する関数 eval を用意しましょう。ToyState の詳細はあとで考えるとして、とりあえず、()型の型シノニムということにしておきます。

type ToyState = ()

eval :: ToyState -> [ToyState]
eval state = state : rests
    where
        rests | isFinal state = []
              | otherwise     = eval (step state)

isFinal は現在の状態が、終了状態かどうかを判定する述語関数です。

isFinal :: ToyState -> Bool
isFinal = undefined

step は状態を一段だけ遷移する関数です。

step :: ToyState -> ToyState
step = undefined

コード

ここまでの`Toy`モジュール全体
src/Toy.hs
module Toy where

type SourceCode  = String
type Interactive = [Input] -> [Output]
type Input  = String
type Output = String

drive :: Interactive -> (String -> String)
drive f = unlines . f . lines

toy :: SourceCode -> Interactive
toy prog = undefined

type ToyState = ()

eval :: ToyState -> [ToyState]
eval state = state : rests
    where
        rests | isFinal state = []
              | otherwise     = eval (step state)

isFinal :: ToyState -> Bool
isFinal = undefined

step :: ToyState -> ToyState
step = undefined