我如何撰写更自由的'哈斯克尔的影响?

时间:2016-08-17 09:38:49

标签: haskell effect-systems

我试图将基于变形金刚的monad堆栈中的简单解释器重写为基于更自由的效果,但我遇到了将我的意图传达给GHC类型系统的困难。

我目前只使用StateFresh效果。我使用了两种状态,我的效果运动员看起来像这样:

runErlish g ls = run . runGlobal g . runGensym 0 . runLexicals ls
  where runGlobal    = flip runState
        runGensym    = flip runFresh'
        runLexicals  = flip runState

除此之外,我已经使用这种类型定义了一个FindMacro函数:

findMacro :: Members [State (Global v w), State [Scope v w]] r
             => Arr r Text (Maybe (Macro (Term v w) v w))

到目前为止所有这些都完美无缺。问题来自于我尝试编写macroexpand2(嗯,macroexpand1,但我简化它以便更容易理解这个问题):

macroexpand2 s =
  do m <- findMacro s
     return $ case m of
       Just j -> True
       Nothing -> False

这会产生以下错误:

Could not deduce (Data.Open.Union.Member'
                    (State [Scope v0 w0])
                    r
                    (Data.Open.Union.FindElem (State [Scope v0 w0]) r))
from the context (Data.Open.Union.Member'
                    (State [Scope v w])
                    r
                    (Data.Open.Union.FindElem (State [Scope v w]) r),
                  Data.Open.Union.Member'
                    (State (Global v w))
                    r
                    (Data.Open.Union.FindElem (State (Global v w)) r))
  bound by the inferred type for `macroexpand2':
             (Data.Open.Union.Member'
                (State [Scope v w])
                r
                (Data.Open.Union.FindElem (State [Scope v w]) r),
              Data.Open.Union.Member'
                (State (Global v w))
                r
                (Data.Open.Union.FindElem (State (Global v w)) r)) =>
             Text -> Eff r Bool
  at /tmp/flycheck408QZt/Erlish.hs:(79,1)-(83,23)
The type variables `v0', `w0' are ambiguous
When checking that `macroexpand2' has the inferred type
  macroexpand2 :: forall (r :: [* -> *]) v (w :: [* -> *]).
                  (Data.Open.Union.Member'
                     (State [Scope v w])
                     r
                     (Data.Open.Union.FindElem (State [Scope v w]) r),
                   Data.Open.Union.Member'
                     (State (Global v w))
                     r
                     (Data.Open.Union.FindElem (State (Global v w)) r)) =>
                  Text -> Eff r Bool
Probable cause: the inferred type is ambiguous

好的,我可以在类型中添加Members注释:

macroexpand2 :: Members [State (Global v w), State [Scope  v w]] r
                => Text -> Eff r Bool

现在我明白了:

Overlapping instances for Member (State [Scope v0 w0]) r
  arising from a use of `findMacro'
Matching instances:
  instance Data.Open.Union.Member'
             t r (Data.Open.Union.FindElem t r) =>
           Member t r
    -- Defined in `Data.Open.Union'
There exists a (perhaps superclass) match:
  from the context (Members
                      '[State (Global v w), State [Scope v w]] r)
    bound by the type signature for
               macroexpand2 :: Members
                                 '[State (Global v w), State [Scope v w]] r =>
                               Text -> Eff r Bool
    at /tmp/flycheck408QnV/Erlish.hs:(79,17)-(80,37)
(The choice depends on the instantiation of `r, v0, w0'
 To pick the first instance above, use IncoherentInstances
 when compiling the other instance declarations)
In a stmt of a 'do' block: m <- findMacro s
In the expression:
  do { m <- findMacro s;
       return
       $ case m of {
           Just j -> True
           Nothing -> False } }
In an equation for `macroexpand2':
    macroexpand2 s
      = do { m <- findMacro s;
             return
             $ case m of {
                 Just j -> True
             Nothing -> False } }

我被告知irc尝试forall r v w.没有任何区别。出于好奇,我在编译这个代码时尝试使用IncoherentInstances(我不喜欢检查更自由和播放的分支),看看它是否会给我一个关于什么是线索的线索继续它没有:

Could not deduce (Data.Open.Union.Member'
                    (State [Scope v0 w0])
                    r
                    (Data.Open.Union.FindElem (State [Scope v0 w0]) r))
  arising from a use of `findMacro'
from the context (Members
                    '[State (Global v w), State [Scope v w]] r)
  bound by the type signature for
             macroexpand2 :: Members
                               '[State (Global v w), State [Scope v w]] r =>
                             Text -> Eff r Bool
  at /tmp/flycheck408eru/Erlish.hs:(79,17)-(80,37)
The type variables `v0', `w0' are ambiguous
Relevant bindings include
  macroexpand2 :: Text -> Eff r Bool
    (bound at /tmp/flycheck408eru/Erlish.hs:81:1)
Note: there are several potential instances:
  instance (r ~ (t' : r'), Data.Open.Union.Member' t r' n) =>
           Data.Open.Union.Member' t r ('Data.Open.Union.S n)
    -- Defined in `Data.Open.Union'
  instance (r ~ (t : r')) =>
           Data.Open.Union.Member' t r 'Data.Open.Union.Z
    -- Defined in `Data.Open.Union'
In a stmt of a 'do' block: m <- findMacro s
In the expression:
  do { m <- findMacro s;
       return
       $ case m of {
           Just j -> True
           Nothing -> False } }
In an equation for `macroexpand2':
    macroexpand2 s
      = do { m <- findMacro s;
             return
             $ case m of {
                 Just j -> True
                 Nothing -> False } }

所以,这是我对更自由的内部结构的理解用完了,我有疑问:

  1. 为什么会有重叠的实例?我不明白它来自哪里。
  2. IncoherentInstances 实际做什么?自动选择听起来很可能导致难以调试的错误。
  3. 如何在另一个函数中实际使用findMacro?
  4. 干杯!

1 个答案:

答案 0 :(得分:17)

可扩展效果的类型推断在历史上一直很糟糕。让我们看一些例子:

{-# language TypeApplications #-}

-- mtl
import qualified Control.Monad.State as M

-- freer
import qualified Control.Monad.Freer as F
import qualified Control.Monad.Freer.State as F

-- mtl works as usual
test1 = M.runState M.get 0

-- this doesn't check
test2 = F.run $ F.runState F.get 0  

-- this doesn't check either, although we have a known
-- monomorphic state type
test3 = F.run $ F.runState F.get True

-- this finally checks
test4 = F.run $ F.runState (F.get @Bool) True

-- (the same without TypeApplication)
test5 = F.run $ F.runState (F.get :: F.Eff '[F.State Bool] Bool) True

我将尝试解释一般问题并提供最少的代码说明。代码的自包含版本可以是found here

在最基本的级别(忽略优化的表示),Eff的定义如下:

{-# language
  GADTs, DataKinds, TypeOperators, RankNTypes, ScopedTypeVariables,
  TypeFamilies, DeriveFunctor, EmptyCase, TypeApplications,
  UndecidableInstances, StandaloneDeriving, ConstraintKinds,
  MultiParamTypeClasses, FlexibleInstances, FlexibleContexts,
  AllowAmbiguousTypes, PolyKinds
  #-}

import Control.Monad

data Union (fs :: [* -> *]) (a :: *) where
  Here  :: f a -> Union (f ': fs) a
  There :: Union fs a -> Union (f ': fs) a

data Eff (fs :: [* -> *]) (a :: *) =
  Pure a | Free (Union fs (Eff fs a))

deriving instance Functor (Union fs) => Functor (Eff fs)  

换句话说,Eff是来自仿函数列表的联合的免费monad。 Union fs a表现得像n-ary Coproduct。对于两个仿函数,二进制Coproduct类似于Either

data Coproduct f g a = InL (f a) | InR (g a)

相比之下,Union fs a让我们从仿函数列表中选择一个仿函数:

type MyUnion = Union [[], Maybe, (,) Bool] Int

-- choose the first functor, which is []
myUnion1 :: MyUnion
myUnion1 = Here [0..10]

-- choose the second one, which is Maybe
myUnion2 :: MyUnion
myUnion2 = There (Here (Just 0))

-- choose the third one
myUnion3 :: MyUnion
myUnion3 = There (There (Here (False, 0)))

让我们以State效果为例。首先,我们需要为Functor设置Union fs个实例,因为Eff只有Monad实例Functor (Union fs)

Functor (Union '[])是微不足道的,因为空联盟没有值:

instance Functor (Union '[]) where
  fmap f fs = case fs of {} -- using EmptyCase

否则我们会在联盟中添加一个仿函数:

instance (Functor f, Functor (Union fs)) => Functor (Union (f ': fs)) where
  fmap f (Here fa) = Here (fmap f fa)
  fmap f (There u) = There (fmap f u)

现在是State定义和跑步者:

run :: Eff '[] a -> a
run (Pure a) = a

data State s k = Get (s -> k) | Put s k deriving Functor

runState :: forall s fs a. Functor (Union fs) => Eff (State s ': fs) a -> s -> Eff fs (a, s)
runState (Pure a)                 s = Pure (a, s)
runState (Free (Here (Get k)))    s = runState (k s) s
runState (Free (Here (Put s' k))) s = runState k s'
runState (Free (There u))         s = Free (fmap (`runState` s) u)

我们已经可以开始编写和运行我们的Eff程序了,尽管我们缺乏所有的糖和便利:

action1 :: Eff '[State Int] Int
action1 =
  Free $ Here $ Get $ \s ->
  Free $ Here $ Put (s + 10) $
  Pure s

-- multiple state
action2 :: Eff '[State Int, State Bool] ()
action2 =
  Free $ Here $ Get $ \n ->            -- pick the first effect
  Free $ There $ Here $ Get $ \b ->    -- pick the second effect
  Free $ There $ Here $ Put (n < 10) $ -- the second again
  Pure ()

现在:

> run $ runState action1 0
(0,10)
> run $ (`runState` False) $ (`runState` 0) action2
(((),0),True)

这里只缺少两件必不可少的东西。

第一个是Eff的monad实例,它允许我们使用do - 符号代替FreePure,还让我们使用许多多态monadic功能。我们将在此处跳过它,因为它写得很直接。

第二个是推理/重载,用于从效果列表中选择效果。以前我们需要编写Here x以选择第一个效果,There (Here x)选择第二个效果,依此类推。相反,我们想在效果列表中编写多态的代码,因此我们必须指定的是,某些效果是列表的元素,而某些隐藏的类型类魔法将插入适当的{ {1}} - :S

ThereMember f fs的元素时,我们需要一个f a类,可以将Union fs a - s注入f - s。从历史上看,人们已经以两种方式实施了它。

首先,直接使用fs

OverlappingInstances

其次,通过首先使用类型系列计算class Member (f :: * -> *) (fs :: [* -> *]) where inj :: f a -> Union fs a instance Member f (f ': fs) where inj = Here instance {-# overlaps #-} Member f fs => Member f (g ': fs) where inj = There . inj -- it works injTest1 :: Union [[], Maybe, (,) Bool] Int injTest1 = inj [0] injTest2 :: Union [[], Maybe, (,) Bool] Int injTest2 = inj (Just 0) f的索引,然后使用不重叠的类实现fs,由inj引导-s计算索引。这通常被认为更好,因为人们往往不喜欢重叠的实例。

f

data Nat = Z | S Nat type family Lookup f fs where Lookup f (f ': fs) = Z Lookup f (g ': fs) = S (Lookup f fs) class Member' (n :: Nat) (f :: * -> *) (fs :: [* -> *]) where inj' :: f a -> Union fs a instance fs ~ (f ': gs) => Member' Z f fs where inj' = Here instance (Member' n f gs, fs ~ (g ': gs)) => Member' (S n) f fs where inj' = There . inj' @n type Member f fs = Member' (Lookup f fs) f fs inj :: forall fs f a. Member f fs => f a -> Union fs a inj = inj' @(Lookup f fs) -- yay injTest1 :: Union [[], Maybe, (,) Bool] Int injTest1 = inj [0] 库使用第二种解决方案,而freer使用第一种方法用于早于7.8的GHC版本,第二种用于较新的GHC-s。

无论如何,两个解决方案都有相同的推理限制,即我们几乎总是extensible-effects只有具体的单态类型,而不是包含类型变量的类型。 ghci中的示例:

Lookup

这是有效的,因为> :kind! Lookup Maybe [Maybe, []] Lookup Maybe [Maybe, []] :: Nat = 'Z Maybe中没有类型变量。

[Maybe, []]

这个因为> :kind! forall a. Lookup (Either a) [Either Int, Maybe] forall a. Lookup (Either a) [Either Int, Maybe] :: Nat = Lookup (Either a) '[Either Int, Maybe] 类型变量阻止减少而被卡住了。

a

这是有效的,因为我们对任意类型变量的唯一了解是它们等于它们自己,而> :kind! forall a. Lookup (Maybe a) '[Maybe a] forall a. Lookup (Maybe a) '[Maybe a] :: Nat = Z 等于a

一般来说,类型族减少会受到变量的影响,因为约束求解可能会在以后将它们精炼到不同类型,因此GHC不能对它们做任何假设(除了等于它们自己)。基本上同样的问题出现在a实现中(尽管没有任何类型的系列)。

让我们根据这一点重新审视OverlappingInstances

freer

GHC知道我们有一个具有单一效果的堆栈,因为import Control.Monad.Freer import Control.Monad.Freer.State test1 = run $ runState get 0 -- error 适用于run。它也知道该效果必须是Eff '[] a。但是当我们写State s时,GHC只知道它对某些新的get变量有State t效果,并且t必须保持,所以当它尝试计算相当于Num t的{​​{1}},它会卡在类型变量上,并且任何进一步的实例解析都会在卡住类型族表达式上跳起来。另一个例子:

freer

这也失败了,因为GHC需要计算Lookup (State t) '[State s],虽然我们知道状态必须是foo = run $ runState get False -- error ,但由于Lookup (State s) '[State Bool]变量,这仍然会卡住。

Bool

这是有效的,因为s的状态类型可以解析为foo = run $ runState (modify not) False -- this works modify not会减少。

现在,经过这次大迂回之后,我将在你的帖子结尾处提出你的问题。

  1. Bool并不表示任何可能的解决方案,只是一种类型错误工件。我需要更多的代码上下文来确定它究竟是如何产生的,但我确定它不相关,因为只要Lookup (State Bool) '[State Bool]被卡住,案件就变得无望了。

  2. Overlapping instances也无关紧要,没有帮助。我们需要一个具体的效果位置索引,以便能够为程序生成代码,如果Lookup卡住,我们就无法通过索引来提取索引。

  3. IncoherentInstances的问题在于它对状态内的类型变量有Lookup个效果。只要您想使用findMacro,就必须确保StatefindMacro的{​​{1}}和v参数是已知的具体类型。您可以通过输入注释来执行此操作,或者更方便地使用TypeApplications,并编写w以指定ScopeGlobal。如果您在多态函数中有findMacro @Int @Int,则需要为该函数使用v = Int注释启用w = Int,绑定findMacroScopedTypeVariables,并写{ {1}}当你使用它时。您还需要为多态vw启用forall v w.(如评论中所述)。我认为虽然在GHC 8中它是一个合理的扩展,与findMacro @v @w一起启用。

  4. 附录:

    然而,幸运的是,新的GHC 8功能允许我们修改可扩展效果的类型推断,我们可以推断{-# language AllowAmbiguousTypes #-}可以做的所有事情,以及v无法处理的一些事情。关于效果的排序,新类型推断也是不变的。

    我有一个minimal implementation here以及一些例子。但是,它还没有用在我所知道的任何效果库中。我可能会对其进行一次注释,并执行拉取请求以将其添加到w