我試圖編寫代碼從元組鏈中刪除空元組。編譯器拒絕程序:重疊的實例扁平化元組
代碼:
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverlappingInstances #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE TypeOperators #-}
infixr 9 :*
data a :* b = a :* !b
deriving (Show, Eq, Ord)
class Flatten a b | a -> b where
flatten :: a -> b
instance Flatten a a where
flatten = id
instance Flatten a b => Flatten (() :* a) b where
flatten (() :* y) = flatten y
instance Flatten b c => Flatten (a :* b) (a :* c) where
flatten (x :* y) = x :* flatten y
test :: Int :*()
test = flatten $ 0 :*()
[1 of 1] Compiling Main (Test\Test.hs, interpreted)
Test\Test.hs:26:8:
Overlapping instances for Flatten (Int :*()) (Int :*())
arising from a use of `flatten'
Matching instances:
instance [overlap ok] Flatten a a
-- Defined at Test\Test.hs:15:10-20
instance [overlap ok] Flatten b c => Flatten (a :* b) (a :* c)
-- Defined at Test\Test.hs:21:10-49
In the expression: flatten
In the expression: flatten $ 0 :*()
In an equation for `test': test = flatten $ 0 :*()
Failed, modules loaded: none.
目標:
flatten (0:*():*1:*2:3:*():*():*4:*()) == (0:*1:*2:*3:*4:*())
你爲什麼使用元組而不是列表?這似乎是一個非常困難的方式來做一些非常簡單的事情。 – amccausl 2012-01-07 02:57:43
適用於允許返回混合類型的函數的準標準函數。我想刪除'()'。 – 2012-01-07 03:04:09
你能否澄清它不工作的方式,以及你期望它做什麼?像這樣重疊在使用多態類型時總會有些脆弱。 – 2012-01-07 03:04:11