haskell - Typeclass instance turned Int into Num a? -
i tried create variadic function returns repa shape:
class indexable r idx :: [int] -> r instance indexable int idx [x] = x instance (indexable b) => indexable (int :. b) idx (x:xx) = x :. (idx xx) instance (indexable a) => indexable (z :. a) idx xs = z :. (idx xs) instance (indexable r) => indexable (int -> r) idx xs = \x -> idx (x:xs)
it works:
> fromlistunboxed (idx [] (3 :: int)) [1..3] :: array u dim1 int aunboxed (z :. 3) [1,2,3]
but have explicitly use 3 :: int
instead of 3
, despite instances explicitly using int
. following confused me:
> :t idx [] 3 idx [] 3 :: (num a, indexable (a -> t)) => t
i thought type signatures in instances force number int
, turned num a
. why? , how compiler recognize numbers going int
s?
type classes open: later on, possibly in module, define instance. compiler can't rely on not doing that, can not commit instances have defined far.
you try function instance, instead:
instance (indexable r, ~ int) => indexable (a -> r) idx xs = \x -> idx (x:xs)
unlike instance int -> r
, instance covers whole function space a -> r
, requires a ~ int
. tells compiler there can not other instance of form char -> r
or similar later on, compiler allowed commit function instance.
Comments
Post a Comment