【发布时间】:2014-06-06 03:20:46
【问题描述】:
我正在尝试找到一些comonad 的实际应用,我想我会尝试看看我是否可以将经典的Wumpus 世界表示为comonad。
我想使用此代码让 Wumpus 在世界中左右移动并清理脏瓷砖并避开坑。似乎唯一有用的comonad函数是extract(获取当前图块),而移动和清理图块将无法使用扩展或复制。
我不确定comonads 是否合适,但我看过一个演讲(Dominic Orchard: A Notation for Comonads),其中comonads 用于对二维矩阵中的光标进行建模。
如果comonad 是代表Wumpus 世界的好方法,您能否说明我的代码哪里出错了?如果错了,您能否建议一个简单的共单子应用程序?
module Wumpus where
-- Incomplete model of a world inhabited by a Wumpus who likes a nice
-- tidy world but does not like falling in pits.
import Control.Comonad
-- The Wumpus world is made up of tiles that can be in one of three
-- states.
data Tile = Clean | Dirty | Pit
deriving (Show, Eq)
-- The Wumpus world is a one dimensional array partitioned into three
-- values: the tiles to the left of the Wumpus, the tile occupied by
-- the Wumpus, and the tiles to the right of the Wumpus.
data World a = World [a] a [a]
deriving (Show, Eq)
-- Applies a function to every tile in the world
instance Functor World where
fmap f (World as b cs) = World (fmap f as) (f b) (fmap f cs)
-- The Wumpus world is a Comonad
instance Comonad World where
-- get the part of the world the Wumpus currently occupies
extract (World _ b _) = b
-- not sure what this means in the Wumpus world. This type checks
-- but does not make sense to me.
extend f w@(World as b cs) = World (map world as) (f w) (map world cs)
where world v = f (World [] v [])
-- returns a world in which the Wumpus has either 1) moved one tile to
-- the left or 2) stayed in the same place if the Wumpus could not move
-- to the left.
moveLeft :: World a -> World a
moveLeft w@(World [] _ _) = w
moveLeft (World as b cs) = World (init as) (last as) (b:cs)
-- returns a world in which the Wumpus has either 1) moved one tile to
-- the right or 2) stayed in the same place if the Wumpus could not move
-- to the right.
moveRight :: World a -> World a
moveRight w@(World _ _ []) = w
moveRight (World as b cs) = World (as ++ [b]) (head cs) (tail cs)
initWorld = World [Dirty, Clean, Dirty] Dirty [Clean, Dirty, Pit]
-- cleans the current tile
cleanTile :: Tile -> Tile
cleanTile Dirty = Clean
cleanTile t = t
谢谢!
【问题讨论】:
-
我对 Wumbus 一无所知,所以也许我疯了,但这看起来像一个拉链,所以你说得对,它是一个共生体。
duplicate生成了无数个 wumpus 世界,我们已经移动到了每一个可能的方格 -
谢谢 jozefg。 Wumpus 是 Peter Norvig(等人)的著作《人工智能:一种现代方法》中提到的一种生物。 Wumpus 探索了一个寻求奖励(例如宝藏)和避免危险(例如坑)的世界。鉴于您对重复的解释,我不确定我目前是否需要重复。我想如果我开始尝试找到世界的最佳遍历,枚举所有遍历可能会非常方便。如果 extend 对我的 Wumpus 不是很有帮助,我可能会按照你的建议使用拉链,或者继续尝试找到一个简单的 comonads 应用程序。再次感谢!
-
啊,我明白了 :) 而且我认为你的comonad 实例是错误的..
duplicate在这里更容易定义duplicate w = World (repeat moveLeft w) w (repeate moveRight w)。您的实例不应该丢弃左右正方形上的所有东西以进行扩展 -
我对重复的使用有点困惑。迭代是否有机会更好地工作?下面是两个函数的类型:repeat :: a -> [a], iterate :: (a -> a) -> a -> [a]
-
哦,我的错,我的意思是迭代:)
标签: haskell comonad wumpus-world