Skip to content

Deriving Via complains 'not a unary constraint, as expected by a deriving clause' on reasonable code

Summary

i have the following snippet that fails to be accepted in ghc 8.10.4 but i think should be valid deriving via code

{-# LANGUAGE
  DeriveFunctor,DeriveFoldable,DeriveTraversable, PatternSynonyms, ExplicitForAll,
  ScopedTypeVariables,DerivingVia,DeriveTraversable,MultiParamTypeClasses,FunctionalDependencies

#-}
{-# LANGUAGE TypeSynonymInstances,FlexibleInstances, UndecidableInstances ,FlexibleContexts , TypeFamilies , GADTs#-}
{-# LANGUAGE TypeOperators #-}
module Monad.Overlay where
import qualified Data.ListTrie.Base.Map as LBM
import Control.Monad.Trans.Reader (ReaderT(..))
import Control.Monad.Trans.State  (StateT(..))
 
import Control.Monad.Trans.Class
import Control.Monad.Trans.Compose -- from MMorph 
{-
this is a abstraction over having a stateful map that programs will update transactionally
if/when they succeed (vs abort)

-}
newtype OverlayM map m a =
    OverlayedM { __runOverlay ::  map  {- reader -}
      -> map  {- state-}
             -- ->[ OpLog a]  --- the oplog can be just be cps writer or such if its ever needed
      -> m (a, map {-, [OpLog a ]-}) }
  deriving (Functor,Applicative,Monad) via  (ReaderT (map) (StateT (map) m ))

  deriving( MonadTrans (OverlayM map) ) via (ReaderT map `ComposeT` StateT map )

i get the error

Monad/Overlay.hs: line 30, column 13:
    error:
    • ‘MonadTrans (OverlayM map)’ is not a unary constraint, as expected by a deriving clause
    • In the newtype declaration for ‘OverlayM’
   |

Steps to reproduce

run that in ghc

Expected behavior

i'd expect, hope for deriving via to work :)

failing that, i'd hope that the unary constraint thing were more clearly documented anywhere, i cant seem to find any good links about it! not even the original paper

Environment

  • GHC version used: ghc 8.10.4, HQ build osx intel

Optional:

  • Operating System: OSX 11.5.1
  • System Architecture:

cc @Icelandjack @RyanGlScott @kosmikus

Edited by Carter Schonwald
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information