Skip to content

Commit

Permalink
Fix Rel8able to work with polymorphic types (#113)
Browse files Browse the repository at this point in the history
Currently, a declaration such as:

```haskell
newtype IdRecord a f = IdRecord { recordId :: Column f a } deriving Generic
instance DBType a => Rel8able (IdRecord a)
```

Will fail, producing the error:

```
    • Could not deduce (rel8-1.0.0.1:Rel8.Generic.Rel8able.Serialize
                          (a Data.Type.Equality.== Identity a) (Expr a) a)
        arising from a use of ‘rel8-1.0.0.1:Rel8.Generic.Rel8able.$dmgfromColumns’
      from the context: DBType a
        bound by the instance declaration
        at lib/CircuitHub/Model/Types.hs:231:10-42
```

The stuck `==` type family gets in the way, even though we can clearly see that this is `False`. This commit fixes this error by introducing a variation of `==` that anticipates this case, adding an explicit pattern. With this new definition of `==`, things work as expected.
  • Loading branch information
ocharles committed Jul 8, 2021
1 parent 032242f commit 0532a61
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/Rel8/Generic/Rel8able.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
{-# language FlexibleInstances #-}
{-# language LambdaCase #-}
{-# language MultiParamTypeClasses #-}
{-# language PolyKinds #-}
{-# language QuantifiedConstraints #-}
{-# language ScopedTypeVariables #-}
{-# language StandaloneKindSignatures #-}
Expand All @@ -25,8 +26,9 @@ module Rel8.Generic.Rel8able
where

-- base
import Data.Functor.Identity ( Identity )
import Data.Kind ( Constraint, Type )
import Data.Type.Equality ( type (==) )
import Data.Type.Bool ( type (&&) )
import GHC.Generics ( Generic, Rep, from, to )
import Prelude

Expand Down Expand Up @@ -58,6 +60,20 @@ type KRel8able :: Type
type KRel8able = K.Rel8able


-- This is almost 'Data.Type.Equality.==', but we add an extra case.
type (==) :: k -> k -> Bool
type family a == b where
-- This extra case is needed to solve the equation "a == Identity a",
-- which occurs when we have polymorphic Rel8ables
-- (e.g., newtype T a f = T { x :: Column f a })
a == Identity a = 'False

-- These cases are exactly the same as those in 'Data.Type.Equality.==.
f a == g b = f == g && a == b
a == a = 'True
_ == _ = 'False


type Serialize :: Bool -> Type -> Type -> Constraint
class transposition ~ (a == Transpose Result expr) =>
Serialize transposition expr a
Expand Down
8 changes: 8 additions & 0 deletions tests/Rel8/Generic/Rel8able/Test.hs
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,11 @@ data NestedTableTestB f = NestedTableTestB
}
deriving stock Generic
deriving anyclass Rel8able



newtype IdRecord a f = IdRecord { recordId :: Column f a }
deriving stock Generic


instance DBType a => Rel8able (IdRecord a)

0 comments on commit 0532a61

Please sign in to comment.