[Haskell-cafe] Selda: confused about type signtures

Marc Busqué marc at lamarciana.com
Mon Apr 23 07:23:58 UTC 2018


On Sun, 22 Apr 2018, Tom Ellis wrote:

> Unless I am much mistaken `Cols s a` is just `a`[1,2].  That explains why
> the type is ambiguous.  There's nothing that fixes `s`.

I also thought that, but if I substitute `Cols s a` by `a`, then the
compilation error is:

```
• Couldn't match type ‘Res (Cols s0 a)’ with ‘Res a’
   Expected type: IO [Res a]
     Actual type: IO [Res (Cols s0 a)]
   NB: ‘Res’ is a type function, and may not be injective
   The type variable ‘s0’ is ambiguous
• In the expression: withDB $ query (select table)
   In an equation for ‘list’:
       list table = withDB $ query (select table)
• Relevant bindings include
     table :: Table a (bound at src/Hedger/Category.hs:44:6)
     list :: Table a -> IO [Res a]
       (bound at src/Hedger/Category.hs:44:1)
    |
44 | list table = withDB $ query (select table)
```

Also, notice that the inferred type does differentiate between `Cols s a` and `a`.

> Can you show us a minimal example of `list` working?  I'm quite confused
> about how it could work.

```
type CategoriesSchema = RowID:*:Text
type ExpensesSchema = RowID:*:Text:*:Double:*:RowID

categories:: Table (CategoriesSchema)
(categories, categoryID :*: rest) = tableWithSelectors "categories"
     $ autoPrimary "id"
     :*: required "name"

expenses:: Table (ExpensesSchema)
expenses = table "expenses"
     $ autoPrimary "id"
     :*: required "name"
     :*: required "amount"
     :*: required "category_id" `fk` (categories, categoryID)

withDB (tryCreateTable categories)
withDB (tryCreateTable expenses)

withDB $ insert_ categories [ def :*: "foo" ]

list categories
--- [1 :*: "foo"]

list expenses
--- []

Marc Busqué
http://waiting-for-dev.github.io/about/


More information about the Haskell-Cafe mailing list