Wiz*_*zek 3 haskell gadt reflex
任何人都知道如何/可以Foo在此代码中扩展GADT :
{-# language GADTs #-}
{-# language DeriveGeneric #-}
{-# language DeriveAnyClass #-}
{-# language TemplateHaskell #-}
{-# language StandaloneDeriving #-}
import Prelude (Int, String, print, ($))
import Data.GADT.Show ()
import Data.GADT.Compare ()
import Data.Dependent.Map (DMap, fromList, (!))
import Data.Dependent.Sum ((==>))
import Data.GADT.Compare.TH (deriveGEq, deriveGCompare)
import Data.Functor.Identity (Identity)
data Foo a where
AnInt :: Foo Int
AString :: Foo String
deriveGEq ''Foo
deriveGCompare ''Foo
dmap1 :: DMap Foo Identity
dmap1 = fromList [AnInt ==> 1, AString ==> "bar"]
main = do
print $ dmap1 ! AnInt
print $ dmap1 ! AString
-- Prints:
-- Identity 1
-- Identity "bar"
Run Code Online (Sandbox Code Playgroud)
同
ANum :: Num n => Foo n
Run Code Online (Sandbox Code Playgroud)
(或类似的东西)允许多态值dependent-map?
当我尝试时,我得到这样的类型错误:
exp-dep-map.hs:20:1: error:
• Couldn't match type ‘a’ with ‘b’
‘a’ is a rigid type variable bound by
the type signature for:
geq :: forall a b. Foo a -> Foo b -> Maybe (a := b)
at exp-dep-map.hs:20:1-20
‘b’ is a rigid type variable bound by
the type signature for:
geq :: forall a b. Foo a -> Foo b -> Maybe (a := b)
at exp-dep-map.hs:20:1-20
Expected type: Maybe (a := b)
Actual type: Maybe (a :~: a)
• In a stmt of a 'do' block: return Refl
In the expression: do return Refl
In an equation for ‘geq’: geq ANum ANum = do return Refl
• Relevant bindings include
geq :: Foo a -> Foo b -> Maybe (a := b)
(bound at exp-dep-map.hs:20:1)
|
20 | deriveGEq ''Foo
| ^^^^^^^^^^^^^^^^^^^^
Run Code Online (Sandbox Code Playgroud)
编辑:我继续与echatav和isovector(GitHub用户名)一起工作,我们能够进一步解决这个问题,我们还发现手动定义GEq和GCompare实例有帮助.谢谢你,@ rampion,你的答案也证实了我们所发现的.
虽然为大型记录类型手动定义这些并不理想.我想知道是否需要更新TemplateHaskell生成器(deriveGCompare,deriveGEq){才能更新以处理多态性.
另外,我发现对于我目前的用例,我正在寻找的pol'ism实际上更接近于
data Foo n a where
AnInt :: Foo n Int
AString :: Foo n String
ANum :: (Num n, Typeable n, Show n) => Foo n n
Run Code Online (Sandbox Code Playgroud)
手工定义实例也适用于此,而且不太理想.
instance GEq (Foo n) where
geq AnInt AnInt = return Refl
geq AString AString = return Refl
geq ANum ANum = return Refl
geq _ _ = Nothing
instance GCompare (Foo n) where
gcompare AnInt AnInt = GEQ
gcompare AnInt _ = GLT
gcompare _ AnInt = GGT
gcompare AString AString = GEQ
gcompare AString _ = GLT
gcompare _ AString = GGT
gcompare (ANum :: Foo n a) (ANum :: Foo n b) = (eqT @a @b) & \case
Just (Refl :: a :~: b) -> GEQ
Nothing -> error "This shouldn't happen"
gcompare ANum _ = GLT
gcompare _ ANum = GGT
Run Code Online (Sandbox Code Playgroud)
试图使用TH,(例如deriveGEq ''Foo或deriveGEq ''(Foo n))我遇到了问题.
exp-dep-map.hs:39:1: error:
• Expecting one more argument to ‘Foo’
Expected kind ‘* -> *’, but ‘Foo’ has kind ‘* -> * -> *’
• In the first argument of ‘GEq’, namely ‘Foo’
In the instance declaration for ‘GEq Foo’
|
39 | deriveGEq ''Foo
| ^^^^^^^^^^^^^^^^^^^^
Run Code Online (Sandbox Code Playgroud)
exp-dep-map.hs:40:19: error: parse error on input ‘Foo’
|
40 | deriveGEq ''(Foo n)
| ^^^
Run Code Online (Sandbox Code Playgroud)
也许相关:https://github.com/mokus0/dependent-sum-template/pull/6
模板haskell使得很难看到发生了什么,所以我建议你滚动自己的实例GEq来更好地理解错误.
看看定义GEq:
class GEq f where
geq :: f a -> f b -> Maybe (a := b)
Run Code Online (Sandbox Code Playgroud)
我们不会对aor 有任何进一步的限制b,因此我们需要单独证明(或反驳)GADT构造函数上的类型相等.
以上哪些ANum不给我们.
但是,这是可以修复的,如果我们添加另一个约束ANum-Typeable
ANum :: (Num n, Typeable n) => n -> Foo n
Run Code Online (Sandbox Code Playgroud)
现在我们可以eqT用来见证类型相等
geq (ANum _) (ANum _) = eqT
Run Code Online (Sandbox Code Playgroud)