Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Haskell GHCi中的典型家族骗局_Haskell_Ghc - Fatal编程技术网

Haskell GHCi中的典型家族骗局

Haskell GHCi中的典型家族骗局,haskell,ghc,Haskell,Ghc,这是我的密码: {-# LANGUAGE TypeFamilies, TypeOperators, DataKinds, PolyKinds, FlexibleContexts, UndecidableInstances #-} module Foo where import Data.Singletons.Prelude import Data.Type.Equality data TP a b -- foldl (\(bool, r) x -> (bool &&

这是我的密码:

{-# LANGUAGE TypeFamilies, TypeOperators, DataKinds, PolyKinds, FlexibleContexts, UndecidableInstances #-}

module Foo where

import Data.Singletons.Prelude
import Data.Type.Equality

data TP a b

-- foldl (\(bool, r) x -> (bool && (r == x), r)) (True, head xs) xs
type family Same (b :: Bool) (r :: k) (rq :: [k]) :: k where
  Same bool r (x ': xs) = Same (bool :&& (r == x)) r xs
  Same bool r '[] = TP bool r

data NotEqualFailure
-- converts a True result to a type
type family EqResult tp where
  EqResult (TP 'True r) = r
  EqResult (TP 'False r) = NotEqualFailure



data Zq q i
type family Foo rq
type instance Foo (Zq q i) = i
type R = 
  EqResult 
    (Same 'True (Foo (Zq Double Int)) 
      (Map (TyCon1 Foo) 
        '[Zq Double Int, Zq Float Int]))

f :: R
f = 3
这在GHC 7.8.3中没有编译:

No instance for (Num NotEqualFailure)
      arising from a use of ‘f’
    In the expression: f
    In an equation for ‘it’: it = f
但如果我注释掉
f
并启动GHCi:

> :kind! R
R :: *
= Int
所以GHC似乎无法决定我列表中的元素是否相等。如果它们相等,
EqResult
应返回公共类型(
Int
此处),否则返回
NotEqualFailure
。有人能解释一下这是怎么回事吗?如果您也认为这看起来像一个bug,请告诉我,我将在GHC trac上存档

如果您能够找到一种以不同的方式编写“EqResult(Same…)”的方法,它可能会绕过这个问题


编辑 我重写了类型族,但不幸的是,我基本上遇到了相同的问题。现在代码更小更简单了

{-# LANGUAGE TypeFamilies, TypeOperators, DataKinds, FlexibleContexts, UndecidableInstances #-}

module Foo where

import Data.Singletons.Prelude
import Data.Singletons.Prelude.List
import Data.Type.Equality

-- foldl (\(bool, r) x -> (bool && (r == x), r)) (True, head xs) xs
type family Same rq where
  Same (x ': xs) = 
    EqResult (And (Map ((TyCon2 (==)) $ x) xs)) x

data NotEqualFailure
-- converts a True result to a type
type family EqResult b v where
  EqResult 'True r = r
  EqResult 'False r = NotEqualFailure

type R = Same '[Int, Int]

f :: R
f = 3
GHCi仍然可以将
R
解析为
Int
,但GHC现在根本无法解析
EqResult
的类型族(之前它错误地将其解析为
NotEqualFailure
)。请注意,如果我将列表的大小更改为1,即
'[Int]
,则此示例有效


编辑2 我删除了所有的外部库,让所有的东西都能正常工作。这个解决方案可能是最小的,但我仍然想知道为什么较大的示例会破坏GHC

{-# LANGUAGE TypeFamilies, DataKinds,
             UndecidableInstances #-}

module Foo where

type family Same (rq :: [*]) :: * where
  Same (x ': xs) = EqTo x xs

--tests if x==y for all x\in xs
type family EqTo y xs where
  EqTo y '[] = y
  EqTo y (y ': xs) = EqTo y xs
  EqTo y (x ': xs) = NotEqualFailure

data NotEqualFailure

type R = Same '[Int, Int]
f :: R
f = 3

事实上有一个bug,它出现在GHC的下一个版本中。

我也尝试了各种解决方案,当我们在图片中引入
Map
时,事情似乎就破裂了。我还尝试了一个更简单的映射(没有像
singletons
版本那样失效),但它也不起作用。