我正在努力描述重写术语和文字(一阶逻辑)意味着什么。也就是说,我想要一个可以在术语和文字上都可以调用的函数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
的值(我敢肯定,这是错误的术语)。
任何提示都将受到欢迎。
下面给出一个更具体的示例,但是需要明确说明要调用哪个函数applyTermSub
或applyLitSub
,其次,替换映射的实现会泄漏到更一般的程序。
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
等),其次,我想要一个可以捕获术语和文字的单个替换函数。
答案 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
传递给该类型构造函数,可以避免这种情况
σ