터플 체인에서 빈 튜플을 제거하는 코드를 작성하려고합니다.튜플을 평평하게하는 중복 인스턴스
이코드 :
{-# 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
혼합 된 형식을 반환 할 수있는 함수를 적용하는 유사 쿼러입니다. '()'을 제거하고 싶습니다. –
그것이 작동하지 않는 방법과 예상 한대로 명확히 할 수 있습니까? 이와 같이 겹치는 것은 다형성 타입에 사용될 때 항상 약해집니다. –