Haskell Level Level Indicator Function for Type Class

For my vile and mostly obscure reasons, I decided to want a level type that indicates the presence of an instance of a type class for the type. It will work as follows:

 > :kind! HasClass Show Int
 > 'True
 > :kind! HasClass Monoid Int
 > 'False

Given that types of restrictions, etc. added to the GHC recently, I get the feeling that this is possible, but it doesn’t come to my mind not every neat implementation. It can be done?

+4
source share
1 answer

Why not just that?

class HasClass (c :: * -> Constraint) a where
  type Has c a :: Bool

instance HasClass c a where
  type Has c a = 'False

instance (c a) => HasClass c a where
  type Has c a = 'True

If you do not mind several extensions:

{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
+1
source

Source: https://habr.com/ru/post/1529938/


All Articles