I’m trying to build a list at the type level, but I’m having some trouble figuring out how to enforce constraints.
My base code is:
data Foo z q = Foo1 (z q)
| Foo2 (z q)
class Qux q -- where ...
class Baz z -- where ...
class Bar a where -- a has kind *->*
type BCtx a q :: Constraint -- using ConstraintKinds to allow constraints on the concrete type
f :: (BCtx a q) => a q -> a q -> a q
g :: (BCtx a q, BCtx a q') => a q -> a q'
instance (Baz z) => Bar (Foo z) where
type BCtx (Foo z) q = (Num (z q), Qux q) -- for example
f (Foo1 x) (Foo1 y) = Foo1 $ x+y -- these functions need access to the type q to do arithmetic mod q
f (Foo1 x) (Foo2 y) = Foo2 $ x-y
-- ...
You can think of the qs above representing prime powers. I would also like to represent composite numbers using a type list of qis. I’m imagining something like:
data QList qi qs = QCons qi qs
| QNil
with the data
data FList c q = FNil
| FCons (c (Head q)) (FList c (Tail q))
where (Head q) should correspond to qi and (Tail q) should correspond to qs. Note that the q parameter for FList is NOT (necessarily) a (Qux q), it is a list of (Qux qi). (I don’t want to flesh out anything more about this list, since it’s one of the design problems I’m posing). I would like to work “modulus-wise” on the FList:
instance (Bar c) => Bar (FList c) where
type BCtx (FList c) q = () -- Anything I put here is not enough
f (FCons x xs) (FCons y ys) = FCons (f x y) (f xs ys)
-- the left call to `f` calls a concrete instance, the right call to `f` is a recursive call on the rest of the list
-- ...
Compiling these codes snippets together in GHC result in (modulo transcription, abstraction, and typing errors):
Could not deduce (BCtx c (Head q), BCtx c (Tail q))
and then
Could not deduce (BCtx c (Head (Tail q)), BCtx c (Tail (Tail q)))
etc.
I see why I’m getting this error, but not how to fix it.
Concretely, I’m expecting an FList c q type where c~Foo z and q~QCons q1 (QCons q2 QNil), and of course my list will satisfy all of the BCtx constraints at every level.
I’m not sure that fixing those particular errors will result in compiling code, but it is a start. The entire Bar class is basically fixed (the Constraint kind is required, and the instances of Bar must have kind * -> *). I don’t believe I can use existential types to create a list of generic objects because I need access to the qi parameter. I am willing to change the type of FList and QList to allow me to work modulus-wise on a collection of Bars.
Thanks for your time!
To handle type lists, it’s necessary to discriminate empty from nonempty lists and handle them separately. The ‘Could not deduce’ errors in your code occur because your instance assumes a nonempty list, when in fact the list may or may not be empty. Here is a solution using the extensions
TypeFamilies,TypeOperators,DataKinds, andGADTs.With
DataKinds, type lists are predefined. They have kind[*], but they’ll be used in a context where kind*is expected, so an operator is needed to cast them:Using type lists,
FListis defined asIt’s defined as a GADT to express how it’s only possible to construct
FLists over the typeInjList q'for some type-listq'. For instance, the termFCons [True] FNilhas typeFList [] (InjList (Bool ': '[])). On the other hand, sinceBoolisn’t of the formInjList q', there are no terms (except ⊥) of typeFList [] Bool. By pattern matching on anFList, a function can verify that it has been given a non-⊥ argument, and further determine whether it’s been passed an empty type list.An instance of
BarforFLists has to handle nil lists and cons lists separately. A nil list has an empty context.A cons list has components for the head and tail of the list. This is expressed by pattern matching on the type-list in the associated type instance of
BCtx. The functionfexamines its argument to verify that it’s not ⊥ and to decide whether it’s an empty list.We can load the code into GHCi to verify that it works: