haskell - Type class instance checking can't detect "piecewise instances" -


i have family of data types indexed type-level integers, , define them instances of type class in "piecewise" manner, causes problems when trying derive instances of class. illustrate, have isolated problem follows. consider code:

{-# language scopedtypevariables, typesynonyminstances,              flexibleinstances  , undecidableinstances #-}  data 0 data succ n  type 1   = succ 0 type 2   = succ 1 type 3 = succ 2  class nat n   toint :: n -> int  instance nat 0   toint _ = 0 instance nat 1 ------------------------- start modify   toint _ = 1 instance (nat n) => nat (succ (succ n))   toint _ = 2 + toint (undefined :: n) --------- end modify 

this slight modification of type-level integers defined in "fun type functions" kiselyov, jones , shan. compiles fine , toint seems work expected. right nat contains integers zero, one, two , on.

however ghc complains after add following lines , recompile:

class likeint n   likeint :: n -> int  instance (nat n) => likeint (succ n)   likeint = toint 

error: not deduce (nat (succ n)) arising use of "toint" context (nat n).

my guess when ghc infers toint has argument of type succ n, instances nat zero, succ zero , (nat n0) => succ (succ n0), , succ n matches none of those. guess supported successful compile when replace modify block original

instance (nat n) => nat (succ n)   toint _ = 1 + toint (undefined :: n) 

how can likeint work toint, modified block? important actual project.

can't define instance?

instance nat n => likeint n   likeint = toint  *main> likeint (undefined :: zero) 0 *main> likeint (undefined :: one) 1 *main> likeint (undefined :: two) 2 *main> likeint (undefined :: three) 3 

or avoid nat constraint?


Comments