7

我正在尝试在 Haskell 中创建一个类型化的表达式解析器,到目前为止效果很好,但我目前正在努力实现高阶函数。我把问题归结为一个简单的例子:

{-# LANGUAGE TypeFamilies,GADTs,FlexibleContexts,RankNTypes #-}

-- A function has an argument type and a result type
class Fun f where
  type FunArg f
  type FunRes f

-- Expressions are either constants of function applications
data Expr a where
  Const :: a -> Expr a
  App :: Fun f => f -> FunArg f -> Expr (FunRes f)

-- A very simple function
data Plus = Plus

-- Which takes two integer expressions and returns an integer expression
instance Fun Plus where
  type FunArg Plus = (Expr Int,Expr Int)
  type FunRes Plus = Int

-- A more complicated function which lifts a function to lists (like in haskell)
data Map f r = Map f

-- For this we need the concept of lifting function arguments:
class Liftable a where
  type LiftRes a

-- A singleton argument is lifted by changing the expression type from a to [a]
instance Liftable (Expr a) where
  type LiftRes (Expr a) = Expr [a]

-- Two function arguments are lifted by lifting each argument
instance (Liftable a,Liftable b) => Liftable (a,b)  where
  type LiftRes (a,b) = (LiftRes a,LiftRes b)

-- Now we can declare a function instance for Map
instance (Fun f,Liftable (FunArg f),r ~ LiftRes (FunArg f)) => Fun (Map f r) where
  type FunArg (Map f r) = r
  type FunRes (Map f r) = [FunRes f]

-- Now a parser for functions:
parseFun :: [String] -> (forall f. Fun f => f -> a) -> a
-- The parser for the plus function is easy:
parseFun ["plus"] f = f Plus
-- But the parser for map is not possible:
parseFun ("map":sym) f 
  = parseFun sym (\fun -> f (Map fun))

问题似乎是没有办法让类型检查器相信每个 LiftRes 本身都是 Liftable,因为递归类声明是被禁止的。

我的问题是:我该如何进行这项工作?是否有其他类型的表达式解析器示例可以从中获取提示?

编辑:似乎这个关于类型族约束的讨论似乎非常相关。但是,我无法使他们的解决方案适用于我的情况,也许有人可以提供帮助?

4

1 回答 1

4

使您的示例工作的最简单方法是Liftable (FunArg f)从实例声明中删除约束。但我认为你的例子太简洁了,它并没有说明你为什么真正需要它。

所以下一个最好的事情是给类添加一个Liftable (FunArg f)超类约束Fun

class Liftable (FunArg f) => Fun f where
  ...

如果这不可行(即,如果不是所有函数都具有可提升的参数类型),那么您不能期望编写parseFun给定类型的 a。

更笼统的评论:我认为您在这里尝试做的事情很奇怪,而且可能一次太多了。从非结构化字符串解析为上下文无关数据类型已经够难的了。为什么不先这样做,然后编写一个单独的函数,将您的语言的“无类型”但结构化的表示转换为有类型的表示。

编辑(作为对评论的回应,已修订):正如您在问题中链接的关于类型族约束的讨论中指出的那样,您可以通过使用绕过超类循环限制ConstraintKinds。这是一种使您的简化示例工作的方法。也许这将扩展到完整的解决方案?

{-# LANGUAGE RankNTypes, ScopedTypeVariables, TypeFamilies, FlexibleContexts, GADTs #-}

import Data.Constraint  -- from the constraints package
import Data.Proxy       -- from the tagged package

-- A function has an argument type and a result type
class Liftable (FunArg f) => Fun f where
  type FunArg f
  type FunRes f

-- Expr, Plus, and instance Fun Plus as before

class Liftable a where
  type LiftRes a
  get :: p a -> Dict (Liftable (LiftRes a))
    -- acquire "superclass" dictionary by calling this method and
    -- then pattern matching on the result

instance Liftable (Expr a) where
  type LiftRes (Expr a) = Expr [a]
  get _ = Dict

instance (Liftable a, Liftable b) => Liftable (a, b) where
  type LiftRes (a, b) = (LiftRes a, LiftRes b)
  get (_ :: p (a, b)) =
    case get (Proxy :: Proxy a) of -- extra code required
      Dict -> case get (Proxy :: Proxy b) of -- extra code required
        Dict -> Dict

data Map f r = Map f

instance (Fun f, Liftable r, r ~ LiftRes (FunArg f)) => Fun (Map f r) where
  type FunArg (Map f r) = r
  type FunRes (Map f r) = [FunRes f]

parseFun :: forall a. [String] -> (forall f. Fun f => f -> a) -> a
parseFun ["plus"]      f = f Plus
parseFun ("map" : sym) f = parseFun sym
  (\ (fun :: g) -> case get (Proxy :: Proxy (FunArg g)) of -- extra code required
                     Dict -> f (Map fun))
于 2013-02-26T12:44:22.920 回答