为什么这个实例声明在Haskell中不起作用?

时间:2014-03-08 17:48:25

标签: haskell

我正在尝试创建一个数据类型来表示Haskell中的抽象语法树。我一直在阅读有关免费monad,GADT,混合Typeable / Dynamic和其他可能的解决方案的各种事情,但我很好奇我能想出的解决方案是否可行(即使需要扩展),以及如果没有,为什么不呢?

基本上我有类似的类型类:

data AST a b = AST a b
data Atom a = Atom a

class Eval e where
    eval :: e a -> a
instance Eval Atom where
    eval (Atom a) = a

然后对于AST的Eval实例,我想要类似的东西:

instance Eval (e1 (a -> b)), Eval (e2 a) => Eval (AST b) where
    eval (AST f x) = eval f $ eval x

在英语中,作为Eval b的实例表示某些东西可以被评估为b,并且我希望AST只有Eval b的实例才有它的第一个参数可以被评估为a -> b,其第二个参数可以被评估为a。所以这个想法是AST不一定是类型安全的,但是如果它不是类型安全的那么它不是Eval的实例,所以如果你有一个在非类型安全的AST上调用eval的代码它就不会编译。或者甚至只是制作一个像

这样的功能
typecheck :: Eval e => e a -> e a
typecheck = id

作为静态测试。我假设这是不可能的,考虑到我在代表AST时看到的所有其他事情,但为什么不呢?任何扩展都可以使这个基本想法可行吗?我正在做的一个主要要求是我需要能够在运行时生成AST,将AST转换为文本并在之后检查它(所以我显然需要一些NamedFunction数据类型),我需要能够很容易用它来表示任意无点的Haskell表达式(所以任何可以由一些任意但有限的原始函数/值组构成的东西,但没有let / where / case / lambdas / etc)。

编辑:我觉得问题的一部分是推断AST在上述情况下应该是Eval b的实例。在Atom的情况下,我只是说instance Eval Atom where,但对于AST,如果我有instance ... => Eval AST where,那么我并没有真正说明它是Eval b,只是Eval {1}},如果没有AST的更多参数,它将无法编译,所以也许问题就在那里,但我仍然不确定是否有办法告诉编译器我是什么我真的以后。

1 个答案:

答案 0 :(得分:2)

您可以使用TypeFamiliesFlexibleContextsFunctionalDependenciesFlexibleInstancesMultiParamTypeClasses来完成与您在发布的问题中尝试的内容类似的内容。< / p>

TypeFamiliesFunctionalDependencies都是类型检查器可以完全基于另一种类型确定一种类型的机制。这些将为我们解决两个问题。您遇到的第一个问题是,我们无法在类的实例声明中从类型a中获取类型ba->b。第二个问题是我们需要能够从表达式的类型中判断出它所评估的类型。 TypeFamilies允许我们创建可以解构类型的类型级函数。 FunctionalDependencies允许我们声明可以从其他类型恢复类型。

编辑: TypeFamilies提供了比FunctionalDependencies更好的解决方案。

<强> TypeFamilies

使用TypeFamilies,我们可以从类型

中提取所需的类型
{-#LANGUAGE FlexibleContexts, TypeFamilies, UndecidableInstances #-}
module Main (
    main
) where

data App ef ea = App ef ea
    deriving (Show)

data Atom a = Atom a
    deriving (Show)

class Eval e where
    -- The type of what an expression evaluates to can be determined from the type of the expession
    -- V is a type function that gets this type from the type of the expression
    type V e :: *
    eval :: e -> V e

instance Eval (Atom a) where
    type V (Atom a) = a
    eval (Atom a) = a

-- the class of functions from a to b
-- The only allowed f is (a->b)
-- This creates two type functions, A and B, which can be used to get the type arguments to ->
class (f ~ (A f -> B f)) => F f where
    type A a :: *
    type B b :: *

instance F (a->b) where
    type A (a->b) = a
    type B (a->b) = b

instance (Eval ef, F (V ef), Eval ea, V ea ~ A (V ef)) => Eval (App ef ea) where
    -- B (V ef) is the only thing that requires UndecidableInstances.
    -- It is probably decidable.
    type V (App ef ea) = B (V ef)
    eval (App ef ea) = eval ef $ eval ea

用法要求显式指定多态类型的类型不仅由于单态限制,而且还因为App (Atom (Integer->Integer)) (Atom Int)是合法类型的事实,尽管Integer->Integer不能适用于Int

-- Example code

instance Show (a->b) where
    show _ = "->"

test1 :: (Num n) => App (Atom (n->n)) (Atom n)
test1 = App (Atom (+1)) (Atom 3)

test2 = App (App (Atom ((+) :: Int -> Int -> Int)) (Atom (1 :: Int))) (Atom (3 :: Int))

test3 = App (Atom reverse) (Atom "abc")

main = do
    print test1
    print $ eval test1
    putStrLn ""
    print test2
    print $ eval test2
    putStrLn ""
    print test3
    print $ eval test3

尝试使用不兼容类型的应用程序评估抽象语法树

-- This still type checks
appStringToString = App (Atom "def") (Atom "abc")
-- But this won't
fails = eval appStringToString

在编译时失败

Couldn't match type `A [Char]' with `[Char]'
In the expression: eval appStringToString
In an equation for `fails': fails = eval appStringToString

编辑:定义以下内容,并在示例中使用它代替App,可以放弃所有示例中的所有类型注释。

app :: (Eval ef, F (V ef), Eval ea, V ea ~ A (V ef)) => ef -> ea -> App ef ea
app = App

app在构建Eval时捕获并保留App实例App所需的类型信息。使用app构造的表达式树通过构造是正确的。例如

appStringToString = app (Atom "def") (Atom "abc")

导致编译器错误:

Couldn't match type `A [Char]' with `[Char]'
Expected type: A (V (Atom [Char]))
  Actual type: V (Atom [Char])
In the expression: app (Atom "def") (Atom "abc")
In an equation for `appStringToString':
    appStringToString = app (Atom "def") (Atom "abc")

FunctionalDependencies添加类似的功能无法解决类型推断问题,即使使用NoMonomorphismRestriction也是如此。这使TypeFamilies明显胜过FunctionalDependencies

<强> FunctionalDependencies

编辑: TypeFamilies提供了更好的解决方案。本节仅供比较。

使用FunctionalDependencies,我们声明可以在以后恢复类型。它不像TypeFamilies那样处理多态性。

{-#LANGUAGE FlexibleInstances, MultiParamTypeClasses, FunctionalDependencies, UndecidableInstances #-}
module Main (
    main
) where

data App ef ea = App ef ea
    deriving (Show)

data Atom a = Atom a
    deriving (Show)

-- Expressions that evaluate to a
-- The type of what an expression evaluates to can be determined from the type of the expession
class Eval a e | e -> a where
    eval :: e -> a

instance Eval a (Atom a) where
    eval (Atom a) = a

-- the class of functions from a to b
class F a b f | f -> a, f -> b where
    func :: f -> (a->b)

instance F a b (a->b) where
    func = id

-- Class of expressions that evaluate to a function a->b
class (Eval f e, F a b f) => EvalF a b f e | e -> f, e -> a, e ->b

-- This requires UndecidaableInstances, but should be decidable
instance (Eval f e, F a b f) => EvalF a b f e

-- This requires UndecidaableInstances, but should be decidable
instance (EvalF a b f ef, Eval a ea) => Eval b (App ef ea) where
    eval (App ef ea) =  func (eval ef) $ eval ea

编译器在评估test1时无法推断出数字的类型,因此该示例需要一个额外的,繁琐的类型注释:

-- Example code

instance Show (a->b) where
    show _ = "->"

test1 :: (Num n) => App (Atom (n->n)) (Atom n)
test1 = App (Atom (+1)) (Atom 3)

test2 = App (App (Atom ((+) :: Int -> Int -> Int)) (Atom (1 :: Int))) (Atom (3 :: Int))

test3 = App (Atom reverse) (Atom "abc")

main = do
    print test1
    print $ eval (test1 :: App (Atom (Int->Int)) (Atom Int))
    putStrLn ""
    print test2
    print $ eval test2
    putStrLn ""
    print test3
    print $ eval test3

尝试使用不兼容类型的应用程序评估抽象语法树

-- This still type checks
appStringToString = App (Atom "def") (Atom "abc")
-- But this won't
fails = eval appStringToString

在编译时失败

No instance for (F [Char] a0 [Char]) arising from a use of `eval'
Possible fix: add an instance declaration for (F [Char] a0 [Char])
In the expression: eval appStringToString
In an equation for `fails': fails = eval appStringToString