circuithub / rel8

Hey! Hey! Can u rel8?
https://rel8.readthedocs.io
Other
150 stars 38 forks source link

Couldn't match type `ListTable Result (SomeData Result)` with `[SomeData Result]` #321

Closed abigailalice closed 2 months ago

abigailalice commented 2 months ago

I encountered this type error recently while trying to store a ListTable within one of my table schemas, defined sort of like

data SomeTable f = SomeTable
    { someId :: Column f SomeId
    , nested :: ListTable f (SomeData f)
    }

This was kind of confusing, as I'd assumed these were the same types. I was still able to get my code to work, simply by splitting up the SomeTable data type into its individual fields after selecting it with each, and returning them in a tuple, rather than in the SomeTable constructor.

My main question is is this working by design, or something preventing type inference, or something else? I've used many plenty to returned nested tables, and never encountered any issues, which is why I thought the types were equivalent. Though this is the first time I've ever stored a ListTable within a schema, which seems relevant, since it's only when I tried to work with a SomeTable Result that the ListTable Result stopped being a list.

ocharles commented 2 months ago

If you want to store a List table in a Rel8able, try using HList. That has special support for using ListTable with Expr and [] with Result. Does that help?

abigailalice commented 2 months ago

Yeah, that seemed to work, though I'm still left wondering why tupling the ListTable didn't experience the same problem. But I suppose it's not really important as long as I know how to avoid it. Thanks.

ocharles commented 2 months ago

It's unfortunately very difficult to make what you want work. If we have a Query (ListTable expr a), we really want the result here to be a [a'] - having to work through some other list type would (imo) kinda suck. So this means we need ListTable to select into []. But in your record, that ListTable appears without any wrapping, which means we can't make the same mapping. This is why it tries to produce a ListTable Expr, but that's contradictory (because a Foo Expr Rel8able has to select to Foo Result). So unfortunately this is all rather "due to technical reasons", which I never like as an answer... But this is the best solution we know about for now!

abigailalice commented 2 months ago

I guess I'll close this then, I did find the stuff in the documentation in HList mentioning storing it in tables. I might work on adding examples of things like this to the cookbook.