gpt4 book ai didi

haskell - 如何避免丑陋的代码在 Haskell(语言扩展)中解决这个问题?

转载 作者:行者123 更新时间:2023-12-04 21:23:47 24 4
gpt4 key购买 nike

我正在尝试编写一个模拟世界中几种生物的程序。基本上,这个词通过一系列生物发送信息,每个生物都会给出自己的回应,这反过来又会改变世界。

我在下面的框架中简化了我要写的内容:

module Structure0 where
type Message = String
class Creature a where
processInput :: a -> Message -> Message
class World a where
processAction :: a -> b -> Message -> a
getCreatures :: a -> [b]

---- USAGE EXAMPLE ----
data Parrot = Parrot Int deriving Show
instance Creature Parrot where
processInput p s = s
data ParrotWorld = ParrotWorld [Parrot]
instance World ParrotWorld where
processAction w p s = w
getCreatures (ParrotWorld ps) = ps

在这段代码中,我希望 World 类定义中的参数 b 可以假定属于 Creature 类的所有数据值,例如:
processAction :: (Creature b) => a -> b -> Message -> a

当然这个例子不是真正的haskell代码,让我们通过说明我找到的两个解决方案:第一个,涉及ExistentialQuantification:
{-# LANGUAGE ExistentialQuantification #-}
module Structure1 where
type Message = String
class Creature_ a where
processInput :: a -> Message -> Message
data Creature = forall c. Creature_ c => Creature c
instance Creature_ Creature where
processInput (Creature c) = processInput c
class World a where
processAction :: a -> Creature -> Message -> a
getCreatures :: a -> [Creature]

---- USAGE EXAMPLE ----
data Parrot = Parrot Int deriving Show
instance Creature_ Parrot where
processInput u s = s
data ParrotWorld = ParrotWorld [Creature]
instance World ParrotWorld where
processAction w p s = w
getCreatures (ParrotWorld ps) = ps

第二个,由#haskell 上的一个好心人建议,使用 TypeFamilies:
{-# LANGUAGE TypeFamilies, FlexibleContexts #-}
module Structure2 where
type Message = String
class Creature a where
processInput :: a -> Message -> Message
class (Creature (WorldCreature a)) => World a where
type WorldCreature a :: *
processAction :: a -> WorldCreature a -> Message -> a
getCreatures :: a -> [WorldCreature a]
---- USAGE EXAMPLE ----
data Parrot = Parrot Int deriving Show
instance Creature Parrot where
processInput p s = s
data ParrotWorld = ParrotWorld [Parrot]
instance World ParrotWorld where
type WorldCreature ParrotWorld = Parrot
processAction w p s = w
getCreatures (ParrotWorld ps) = ps

本练习的主要目标是编写漂亮、优雅的代码。
所以,问题:

1)我应该将 Creature 表示为类而不是数据吗?
(我这样做是因为 Creature 只是实现 processInput 函数的东西,许多实际的 Creature 实现变化很大;特别是在原型(prototype)设计期间,我不想不断改变 Creature 模式匹配的方式.

2)我提供的第一个解决方案有点难看,因为维护 Creature 和 Creature_ 版本的样板。但是,它的好处是我可以编写 [Creature] 类型的混合列表;问题是我无法对对象进行模式匹配,例如:
\(Creature (Parrot x)) -> x

由于类型系统,将失败。我可以把这一切做好吗?

3)第二种解决方案存在可扩展性问题:假设我要构建一个包含两种生物的世界,例如 Parrot1 和 Parrot2:在这种情况下我该如何编写代码?

4)我是否从错误的角度构建代码?我可以使用普通的 haskell 获得一个优雅的解决方案吗?

谢谢你们 :)

卡罗

最佳答案

1 类与数据

Creature 应该是一个类——它描述了一个接口(interface)。当您考虑实际传达值时,或者当您需要引入新类型,用新行为包装现有对象时,应该使用数据。例如,Identity monad 需要将它的值包装成一个新类型,否则你会看到 instance Monad a为所有 a ,这将导致与将任何其他内容设为 Monad 时发生冲突实例。但是,您可能需要包装它。

2 个列表

有一种方法可以使用 Data.Dynamic ,但是每次我考虑这样做时,我都能够想出一种方法来代替常规类型类。也就是说,我没有写过那么多 Haskell,而且很多库肯定依赖于 Data.Dynamic。 .如果你想真正解开一个类型,那么你可能需要使用它。

3 外延性

和以前一样,如果您可以在类中保留特定于类型的功能,那是最好的。如果您可以发布一个示例,说明为什么不能向 Creature 添加其他功能,那将是最有帮助的。 .我假设你想数 numParrots在下面的示例中,您确实需要将它们拆箱。

4 条一般性评论

一个问题总是有很多解决方案。根据您的描述,我认为“不同的世界应该包含不同类型的消息”,而不是一个世界应该与特定类型的生物相关联(例如 ParrotWorld )。

另一种解决方案

这是我的解决方案,使用 Data.Typeable .如上所述,这是我第一次使用它,所以可能有更清洁的方法。

{-# LANGUAGE DeriveDataTypeable,
ImpredicativeTypes,
NoMonomorphismRestriction,
RankNTypes,
ScopedTypeVariables #-}

module Test where

import Data.Typeable

type Message = String

class Typeable α => Creature α where
processInput :: α -> Message -> Message

-- box a creature
type BoxedC = (Message -> Message, Typeable β => Maybe β)
boxC :: Creature α => α -> BoxedC
boxC x = (processInput x, cast x)

class World α where
-- from your description, I'd not have Creature as part of this.
processAction :: α -> Message -> α
getCreatures :: α -> [BoxedC]

data Parrot = Parrot { parrotMessage :: String } deriving Typeable
data Lizard = Lizard { lizardMessage :: String } deriving Typeable

instance Creature Parrot where processInput p _ = (parrotMessage p)
instance Creature Lizard where processInput l _ = (lizardMessage l)

-- NOTE: Keep it simple and use a single World instance
-- (i.e. no typeclass) unless you need it.
data BaseWorld = BaseWorld { creatureList :: [BoxedC] }
instance World BaseWorld where
processAction w _ = w
getCreatures = creatureList

w = BaseWorld [boxC $ Parrot "parrot1", boxC $ Lizard "Lizard1"]

numParrots :: [BoxedC] -> Int
numParrots lst = foldl (+) 0 (map (go . snd) lst) where
go :: (forall β. Typeable β => Maybe β) -> Int
go (Just x :: Maybe Parrot) = 1
go _ = 0

test = numParrots (getCreatures w)

这个想法与你的相似:我们先将生物装箱,然后再将它们放入列表中。装箱的元素有足够的数据,以便您可以在需要时取消装箱。最后要提到的一件事,虽然它可能不是你想要的,但闭包是强大的。如果您可以将其结果表示为函数组合,则无需保留生物列表。例如,在伪代码中,你可以有一个函数
bind_creature :: Creature -> World -> World

它将一个生物添加到一个世界,并且 World 有一个返回其下一次迭代的类型,
data World = World { nextWorld :: World }

您将其设置为自身作为基础,即 w = World w .为简单起见,我们假设每个生物都有一个功能
transformWorld :: Creature -> World -> World

然后你可以实现 bind_creature 之类的,
bind_creature c w = World { nextWorld = transformWorld c (nextWorld w) }

希望能帮助到你。

关于haskell - 如何避免丑陋的代码在 Haskell(语言扩展)中解决这个问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7382522/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com