经过一些工作,事实证明这是相当简单的,它甚至适用于相互递归的类型。我敢肯定有一些边缘情况会失败,但我还没有发现。
{-# LANGUAGE
MultiParamTypeClasses
, FunctionalDependencies
, DataKinds
, TypeOperators
, TypeFamilies
, FlexibleContexts
, FlexibleInstances
, UndecidableInstances
, PolyKinds
, ConstraintKinds
, DeriveGeneric
, OverlappingInstances
#-}
module IsRecursive where
import GHC.Generics
import Data.Proxy
type family (:||) (a :: Bool) (b :: Bool) :: Bool where
True :|| x = True
x :|| True = True
a :|| b = False
data T2 a b
type family Elem (x :: k) (xs :: [k]) :: Bool where
Elem x '[] = False
Elem x (x ': xs) = True
Elem x (y ': xs) = Elem x xs
class IsRecursive' (tys :: [* -> *]) (rep :: * -> *) (r :: *) | tys rep -> r where
isRecursive' :: Proxy tys -> Proxy rep -> Proxy r
isRecursive' _ _ = Proxy
-- These types have recursive `Rep`s but aren't recursive because there is no `Rep` for primitive types
instance IsRecursive' tys (K1 R Int) (T2 False tys)
instance IsRecursive' tys (K1 R Double) (T2 False tys)
instance IsRecursive' tys (K1 R Char) (T2 False tys)
instance IsRecursive' tys (K1 R Float) (T2 False tys)
-- Recursive instances - unwrap one layer of `Rep` and look inside
instance IsRecursive' tys U1 (T2 False tys)
instance IsRecursive' tys (Rep c) r => IsRecursive' tys (K1 i c) r
instance (IsRecursive' tys f (T2 r0 tys0), IsRecursive' tys g (T2 r1 tys1), r2 ~ (r0 :|| r1)) => IsRecursive' tys (f :+: g) (T2 r2 tys1)
instance (IsRecursive' tys f (T2 r0 tys0), IsRecursive' tys g (T2 r1 tys1), r2 ~ (r0 :|| r1)) => IsRecursive' tys (f :*: g) (T2 r2 tys1)
instance (IsRecursive' tys f r) => IsRecursive' tys (M1 i c f) r
-- This is where the magic happens
-- Datatype declaration reps are represented as `M1 D`
-- When one is encountered, save it in the list so far and continue recursion
instance (IsRecDataDec (Elem tyrep tys) tyrep tys f r, tyrep ~ (M1 D c f)) => IsRecursive' tys (M1 D c f) r
-- Context reduction is strict, so this class makes sure we
-- only recurse if `Elem tyrep tys == False`; otherwise every recursive type
-- would cause a stack overflow
class IsRecDataDec (b :: Bool) (c :: * -> *) (tys :: [* -> *]) (f :: * -> *) (r :: *) | b c tys f -> r
instance IsRecDataDec True c tys f (T2 True (c ': tys))
instance IsRecursive' (c ': tys) f r => IsRecDataDec False c tys f r
class IsRecursive t
instance IsRecursive' '[] (Rep t) (T2 True tys) => IsRecursive t
data TBool (b :: Bool) = TBool
instance Show (TBool True) where show _ = "True"
instance Show (TBool False) where show _ = "False"
isRecursive :: IsRecursive' '[] (Rep t) (T2 r tys) => t -> TBool r
isRecursive _ = TBool
-- test cases
data K = K K deriving Generic
data A = A B deriving Generic
data B = B Q deriving Generic
data Q = Q A deriving Generic