danidiaz / red-black-record

Extensible records and variants indexed by a type-level Red-Black tree.
BSD 3-Clause "New" or "Revised" License
18 stars 1 forks source link

Use custom type errors #15

Open danidiaz opened 5 years ago

danidiaz commented 5 years ago

Perhaps we should introduce some custom type errors.

For example, instead of this

ghci> getFieldI @"foo" (addFieldI @"bar" (5::Int) unit)

<interactive>:15:1: error:
    * No instance for (KeyHelper 'LT "foo" 'E Int 'E)
        arising from a use of `getFieldI'

We could print someking like "Key foo not found in map".

danidiaz commented 5 years ago

A possible implementation for key lookups (should the error go in the precondition instead?):

instance KeyHelper GT k E v' right where 
    type    Value' GT k E v' right = 
                      TypeError (Text "Key " :<>: ShowType k :<>: Text " not found in map.")
    field'  _ = error "unreachable"
    branch'   = error "unreachable"

instance KeyHelper LT k left v E where
    type Value'    LT k left v E = 
                     TypeError (Text "Key " :<>: ShowType k :<>: Text " not found in map.")
    field'  _ = error "unreachable"
    branch'   = error "unreachable"

In general, it would be desirable for the internals of the red-black tree not to leak into the error messages.

danidiaz commented 5 years ago

Improving errors for Delete and Insert would be more involved because of how they use non-linear patterns

instance InsertableHelper2 EQ k v color left k v right where
    type Insert2           EQ k v color left k v right = N color left k v right

We would need yet another auxiliary typeclass perhaps, one which used an explicit comparison.