Haskell的类和实例的正确类型

时间:2019-04-30 18:20:00

标签: class haskell instance

我正在努力描述重写术语和文字(一阶逻辑)意味着什么。也就是说,我想要一个可以在术语和文字上都可以调用的函数applySubstitution。 我认为替换可以表示为一个函数。但是,通过以下代码,我遇到了刚性类型变量错误。

{-# LANGUAGE UnicodeSyntax #-}

module Miniexample where
import qualified Data.Maybe as M

data Term a = F a [Term a]
            | V a

data Literal a = P a [Term a]
               | E (Term a) (Term a)

class Substitutable b where
  substitute :: b -> (Term a -> Maybe (Term a)) -> b

instance Substitutable (Term a) where
  substitute x@(V _) σ    = M.fromMaybe x (σ x)
  substitute f@(F l xs) σ = M.fromMaybe f' (σ f)
    where f' = F l (map (flip substitute σ) xs)

instance Substitutable (Literal a) where
  substitute (P l xs) σ = P l (map (flip substitute σ) xs)
  substitute (E s t) σ  = E (substitute s σ) (substitute t σ)

class Substitution σ where
  asSub :: σ -> (a -> Maybe a)

applySubstitution σ t = substitute t (asSub σ)

(<|) t σ = applySubstitution σ t

出现以下错误:

• Couldn't match type ‘a1’ with ‘a’
  ‘a1’ is a rigid type variable bound by
    the type signature for:
      substitute :: forall a1.
                    Term a -> (Term a1 -> Maybe (Term a1)) -> Term a
    at /.../Miniexample.hs:16:3-12
  ‘a’ is a rigid type variable bound by
    the instance declaration
    at /.../Miniexample.hs:15:10-31
  Expected type: Term a1
    Actual type: Term a
• In the first argument of ‘σ’, namely ‘x’
  In the second argument of ‘M.fromMaybe’, namely ‘(σ x)’
  In the expression: M.fromMaybe x (σ x)
• Relevant bindings include
    σ :: Term a1 -> Maybe (Term a1)
      (bound at /.../Miniexample.hs:16:22)
    x :: Term a
      (bound at /.../Miniexample.hs:16:14)
    substitute :: Term a -> (Term a1 -> Maybe (Term a1)) -> Term a
      (bound at /.../Miniexample.hs:16:3)

在我看来,b类中的类型变量Substitutable应该能够接受Term a的值(我敢肯定,这是错误的术语)。

任何提示都将受到欢迎。

下面给出一个更具体的示例,但是需要明确说明要调用哪个函数applyTermSubapplyLitSub,其次,替换映射的实现会泄漏到更一般的程序。

module Miniexample where
import qualified Data.Maybe as M
import qualified Data.List as L

data Term a = F a [Term a]
            | V a deriving (Eq)

data Literal a = P a [Term a]
               | E (Term a) (Term a) deriving (Eq)


termSubstitute :: (Term a -> Maybe (Term a)) -> Term a -> Term a
termSubstitute σ x@(V _)    = M.fromMaybe x (σ x)
termSubstitute σ f@(F l xs) = M.fromMaybe f' (σ f)
    where f' = F l (map (termSubstitute σ) xs)

litSubstitute :: (Term a -> Maybe (Term a)) -> Literal a -> Literal a
litSubstitute σ (P l xs) = P l (map (termSubstitute σ) xs)
litSubstitute σ (E s t)  = E (termSubstitute σ s) (termSubstitute σ t)

applyTermSub :: (Eq a) => Term a -> [(Term a, Term a)] -> Term a
applyTermSub t σ = termSubstitute (flip L.lookup σ) t

applyLitSub :: (Eq a) => Literal a -> [(Term a, Term a)] -> Literal a
applyLitSub l σ = litSubstitute (flip L.lookup σ) l

-- variables
x  = V "x"
y  = V "y"
-- constants
a  = F "a" []
b  = F "b" []
-- functions
fa = F "f" [a]
fx = F "f" [x]

σ = [(x,y), (fx, fa)]

test = (applyLitSub (P "p" [x, b, fx]) σ) == (P "p" [y, b, fa])

理想情况下,我希望有一个用于替换的接口(即,一个人可以使用Data.Map等),其次,我想要一个可以捕获术语和文字的单个替换函数。

1 个答案:

答案 0 :(得分:1)

您得到的错误是投诉,Term a中指定的instance Substitutable (Term a)Term a接受的σ类型不同。这是因为Haskell通过a函数对substitute进行了量化,但没有对实例定义的其余部分进行量化。因此,substitute的实现必须接受σ的{​​{1}}处理Term a1 some 值的a1,但不能保证是特定的{ {1}}定义您的实例。 (是的,您的实例是在所有a上定义的...但是从实例定义的范围内,就好像选择了特定的a一样。)

通过使用类型构造函数(而不是仅使用类型构造函数)对a类进行参数化,然后将与Substitutable类型中使用的相同的a传递给该类型构造函数,可以避免这种情况

σ