fumieval / extensible

Extensible records, variants, structs, effects, tangles
BSD 3-Clause "New" or "Revised" License
128 stars 20 forks source link

Can't use hfoldMap with show #7

Closed maxigit closed 7 years ago

maxigit commented 7 years ago

I'm evaluated this package so I've been trying a few thing on the repl. I would like to transform a record to a list of string for that I'm using hfoldmap. I can't get it to compile and get the following error message

(hfoldMap (show .  getField)) :: Forall (KeyValue KnownSymbol Show) xs => Record xs -> String

<interactive>:647:12: error:
    • Could not deduce (Show (AssocValue x))
        arising from a use of ‘show’
      from the context: Forall (KeyValue KnownSymbol Show) xs
        bound by the inferred type of
                 it :: Forall (KeyValue KnownSymbol Show) xs => Record xs -> String
        at <interactive>:647:1-93
      or from: Forall (KeyValue KnownSymbol Show) xs1
        bound by an expression type signature:
                   Forall (KeyValue KnownSymbol Show) xs1 => Record xs1 -> String
        at <interactive>:647:1-93
    • In the first argument of ‘(.)’, namely ‘show’
      In the first argument of ‘hfoldMap’, namely ‘(show . getField)’
      In the expression:
          (hfoldMap (show . getField)) ::
            Forall (KeyValue KnownSymbol Show) xs => Record xs -> String

(I know this is not the exact code I need, but I should be able to get what I want, once I get that to compile).

Is it possible to make it work ?

Thanks/

fumieval commented 7 years ago

Unfortunately, hfoldMap is not enough to propagate Show constraints.

I added hfoldMapFor in a29cf9c. Try the following code:

hfoldMapFor (Proxy :: Proxy (KeyValue KnownSymbol Show)) (show . runIdentity . getField)
maxigit commented 7 years ago

That's great. I'll have a look at it. Could you explain why Forall doesn't work in that particular case ? I thought that was the point of it.

fumieval commented 7 years ago

Thank you for reporting, I hadn't come up with these functions.

In the argument of hfoldMap, the qualification is just forall x, so the function doesn't know anything about x. Due to the limitation of the type system, you need to specify a constraint explicitly.