Safe casting type over several potential types

I currently have a pattern similar to the following happening in my code (this is simplified from the actual code, which is a large GADT that handles expressions).

data I = A | B | C | D

type family TC1 (a :: I) :: Constraint where
  TC1 'A = ()
  TC1 'C = ()
  TC1 _ = ('True ~ 'False)

type family TC4 (a :: I) :: Constraint where
  TC4 'B = ()
  TC4 'C = ()
  TC4 'D = ()
  TC4 _ = ('True ~ 'False)

data T (t :: I) where
  T1 :: (TC1 t) => T t
  T2 :: T B
  T3 :: T t
  T4 :: (TC4 t) => T t
  etc

castL :: (Typeable t) => [Maybe t] -> Maybe t
castL l = case catMaybes l of
  (x:_) -> Just x
  [] -> Nothing

mkT1 :: Typeable a => Maybe (T a)
mkT1 = castL [cast (T1 :: T 'A), cast (T1 :: T 'C)]

      

A few questions:

  • Is there a better way to specify a constraint that always fails instead ('True ~ 'False)

    ?
  • mkT1

    looks a little hacky, but it looks like it works. Is there a better way to do this?
  • Is there a better way to structure the whole thing (I realize this is a tricky question as I cannot include all the details of what I am trying to achieve in a long enough post, but I am basically generating a parse tree).
+3
haskell


source to share


No one has answered this question yet

Check out similar questions:

41
Can I restrict the type family?
17
How to add constraint to type constructor
fourteen
Unsafe attraction with Haskell constraints
13
Comparison of patterns in observational theory
7
Typed expression parser
7
differences: GADT, data family, data family which is GADT
five
Comparison of templates in length using this GADT:
3
Difference between a family of types and a partially new type? (and partial data?)
2
Restricted Circular Printing
1
How can I implement from JSON to GADT with custom type class constraints?



All Articles
Loading...
X
Show
Funny
Dev
Pics