【发布时间】:2015-04-07 23:59:24
【问题描述】:
我正在尝试在 Haskell 中编写一个数据处理模块,该模块接受与不同模式相关的changesets,并通过一系列规则传递这些数据,这些规则可以根据数据选择性地执行操作。 (这主要是为了更好地理解 Haskell 的学术练习)
为了更好地解释我在做什么,这里有一个 Scala 的工作示例
// We have an open type allowing us to define arbitrary 'Schemas'
// in other packages.
trait Schema[T]
// Represents a changeset in response to user action - i.e. inserting some records into a database.
sealed trait Changeset[T]
case class Insert[T]( schema:Schema[T], records:Seq[T]) extends Changeset[T]
case class Update[T]( schema:Schema[T], records:Seq[T]) extends Changeset[T]
case class Delete[T]( schema:Schema[T], records:Seq[T]) extends Changeset[T]
// Define a 'contacts' module containing a custom schema.
package contacts {
object Contacts extends Schema[Contact]
case class Contact( firstName:String, lastName:String )
}
// And an 'accounts' module
package accounts {
object Accounts extends Schema[Account]
case class Account( name:String )
}
// We now define an arbitrary number of rules that each
// changeset will be checked against
trait Rule {
def process( changeset: Changeset[_] ):Unit
}
// As a contrived example, this rule keeps track of the
// number of contacts on an account
object UpdateContactCount extends Rule {
// To keep it simple let's pretend we're doing IO directly here
def process( changeset: Changeset[_] ):Unit = changeset match {
// Type inference correctly infers the type of `xs` here.
case Insert( Contacts, xs ) => ??? // Increment the count
case Delete( Contacts, xs ) => ??? // Decrement the count
case Insert( Accounts, xs ) => ??? // Initialize to zero
case _ => () // Don't worry about other cases
}
}
val rules = [UpdateContactCount, AnotherRule, SomethingElse]
重要的是“架构”和“规则”都可以扩展,这部分特别是在我尝试在 Haskell 中执行此操作时抛出了一些曲线球。
到目前为止,我在 Haskell 中所拥有的是
{-# LANGUAGE GADTs #-}
-- In this example, Schema is not open for extension.
-- I'd like it to be
data Schema t where
Accounts :: Schema Account
Contacts :: Schema Contact
data Account = Account { name :: String } deriving Show
data Contact = Contact { firstName :: String, lastName :: String } deriving Show
data Changeset t = Insert (Schema t) [t]
| Update (Schema t) [t]
| Delete (Schema t) [t]
-- Whenever a contact is inserted or deleted, update the counter
-- on the account. (Or, for new accounts, set to zero)
-- For simplicity let's pretend we're doing IO directly here.
updateContactCount :: Changeset t -> IO ()
updateContactCount (Insert Contacts contacts) = ???
updateContactCount (Delete Contacts contacts) = ???
updateContactCount (Insert Accounts accounts) = ???
updateContactCount other = return ()
这个例子工作得很好 - 但我想对此进行扩展,以便 Schema 可以是一个开放类型(即我不知道所有的可能性提前),同时也做同样的事情规则。即我不知道updateContactCount 函数的时间负责人,我只是传递了[Rule] 类型的列表。即类似的东西。
type Rule = Changeset -> IO ()
rules = [rule1, rule2, rule3]
我的第一次尝试是创建一个 Schema 类型类,但是 Haskell 仍然坚持将函数锁定为单一类型。数据种类似乎有同样的限制。
由此,我真的有两个具体的问题。
是否可以创建一个可以对开放类型进行模式匹配的函数,就像我们在 Scala 中所做的那样?
在 Haskell 中是否有更优雅的惯用方式来处理上述场景?
【问题讨论】:
-
你的意思是
rules :: [Rule],而不是=? -
不。我的意思是
rules = [rule1, rule2, rule3]。我看到我的错字 - 将修改。