在Haskell中实例化类型变量

dan*_*tin 5 haskell prolog unification typeclass successor-arithmetics

编辑:解决了.我不知道在源文件中启用语言扩展没有在GHCi中启用语言扩展.解决方案是:set FlexibleContexts在GHCi中.


我最近发现Haskell中的类和实例中的类型声明是Horn子句.因此,我将The Art of Prolog,第3章中的算术运算编码为Haskell.例如:

fac(0,s(0)).
fac(s(N),F) :- fac(N,X), mult(s(N),X,F).

class Fac x y | x -> y
instance Fac Z (S Z)
instance (Fac n x, Mult (S n) x f) => Fac (S n) f

pow(s(X),0,0) :- nat(X).
pow(0,s(X),s(0)) :- nat(X).
pow(s(N),X,Y) :- pow(N,X,Z), mult(Z,X,Y).

class Pow x y z | x y -> z
instance (N n) => Pow (S n) Z Z
instance (N n) => Pow Z (S n) (S Z)
instance (Pow n x z, Mult z x y) => Pow (S n) x y
Run Code Online (Sandbox Code Playgroud)

在Prolog中,值在证明中为(逻辑)变量实例化.但是,我不明白如何在Haskell中实例化类型变量.也就是说,我不明白Haskell相当于Prolog查询的内容

?-f(X1,X2,...,Xn)
Run Code Online (Sandbox Code Playgroud)

是.我认为

:t undefined :: (f x1 x2 ... xn) => xi
Run Code Online (Sandbox Code Playgroud)

会导致Haskell实例化xi,但这会产生Non type-variable argument in the constraint错误,即使FlexibleContexts启用也是如此.

Ed'*_*'ka 3

不确定 Prolog 示例,但我会在 Haskell 中按以下方式定义它:

{-# LANGUAGE MultiParamTypeClasses, EmptyDataDecls, FlexibleInstances,
FlexibleContexts, UndecidableInstances, TypeFamilies, ScopedTypeVariables #-}

data Z
data S a
type One = S Z
type Two = S One
type Three = S Two
type Four = S Three 


class Plus x y r
instance (r ~ a) => Plus Z a r
instance (Plus a b p, r ~ S p) => Plus (S a) b r

p1 = undefined :: (Plus Two Three r) => r


class Mult x y r
instance (r ~ Z) => Mult Z a r
instance (Mult a b m, Plus m b r) => Mult (S a) b r

m1 = undefined :: (Mult Two Four r) => r


class Fac x r
instance (r ~ One) => Fac Z r
instance (Fac n r1, Mult (S n) r1 r) => Fac (S n) r

f1 = undefined :: (Fac Three r) => r


class Pow x y r
instance (r ~ One) => Pow x Z r
instance (r ~ Z) => Pow Z y r
instance (Pow x y z, Mult z x r) => Pow x (S y) r

pw1 = undefined :: (Pow Two Four r) => r

-- Handy output
class (Num n) => ToNum a n where
    toNum :: a -> n
instance (Num n) => ToNum Z n where
    toNum _ = 0
instance (ToNum a n) => ToNum (S a) n where
    toNum _ = 1 + toNum (undefined :: a) 

main = print $ (toNum p1, toNum m1, toNum f1, toNum pw1)
Run Code Online (Sandbox Code Playgroud)

更新:

正如 danportin 在 TypeFamilies 下面的评论中指出的那样,这里不需要“惰性模式”(在实例上下文中)(他的初始代码更短、更清晰)。

不过,这种模式的一个应用,我在这个问题的上下文中可以想到的是:假设我们想将布尔逻辑添加到我们的类型级算术中:

data HTrue
data HFalse

-- Will not compile
class And x y r | x y -> r
instance And HTrue HTrue HTrue
instance And a b HFalse -- we do not what to enumerate all the combination here - they all HFalse
Run Code Online (Sandbox Code Playgroud)

但由于“功能依赖冲突”,这将无法编译。在我看来,我们仍然可以在没有fundeps的情况下表达这种重叠的情况:

class And x y r
instance (r ~ HTrue) => And HTrue HTrue r
instance (r ~ HFalse) => And a b r

b1 = undefined :: And HTrue HTrue r => r   -- HTrue
b2 = undefined :: And HTrue HFalse r => r  -- HFalse
Run Code Online (Sandbox Code Playgroud)

这绝对不是最好的方法(它需要 IncoherentInstances)。所以也许有人可以建议另一种“创伤较小”的方法。

  • @danportin,是的,我同意 - 这里不需要这种“惰性模式”。我将编辑我的帖子以反映这一点。我认为当我们面临重叠实例的情况时,这种模式会很有用(否则我们会遇到功能依赖冲突)。请参阅我的类型级别 And 示例 (2认同)