[Haskell-cafe] Re: HList error with hFoldr

oleg at okmij.org oleg at okmij.org
Mon Jan 28 00:45:45 EST 2008


> After some fooling around, I came up with something I think makes
> sense.  Let me know if this is the right/wrong thing.  It seems to
> work for the examples I've tried so far.

> instance (Floating f, MetricSpace e f
>          ,MetricSpace e' f, HZip l l (HCons (e', e') l')
>          ,HFoldr ApplyDistSum Float l' f)
>          => MetricSpace (HCons e l) f where
>     c `dist` c' = sqrt $ hFoldr ApplyDistSum (0::Float) (hZip c c')

It seems strange that you need the types e and e' (perhaps this is a
quirk or a bug of GHC 6.8). With GHC 6.6, I have derived the following


instance (Floating f, MetricSpace e f, HFoldr ApplyDistSum Float l1 f,
	  HZip (HCons e l) (HCons e l) (HCons (e,e) l1))
         => MetricSpace (HCons e l) f where
    c `dist` c' = sqrt $ hFoldr ApplyDistSum (0::Float) (hZip c c')

which matches my intuitive understanding, and also sufficient to run
the given examples. 

When I wrote `I derived with GHC' I meant it literally. First I wrote
the instance without any constraints:

instance ()
         => MetricSpace (HCons e l) f where
    c `dist` c' = sqrt $ hFoldr ApplyDistSum (0::Float) (hZip c c')

GHC of course complained about many missing constraints. I started
adding the constraints from the list of complaints, until GHC was
satisfied. This is basically a cut-and-paste job from the Emacs buffer
with GHC error messages to the buffer with the code.


More information about the Haskell-Cafe mailing list