constraints-extras: Utility package for constraints

[ bsd3, constraints, library ] [ Propose Tags ]

Convenience functions and TH for working with constraints. See for example usage.

[Skip to Readme]
Versions [RSS] [faq],,,,,,,,,,,, 0.3,,,
Dependencies aeson, base (==4.11.*), constraints (>=0.9 && <0.11), constraints-extras, markdown-unlit, template-haskell (>=2.11 && <2.14) [details]
License BSD-3-Clause
Copyright Obsidian Systems LLC
Author Cale Gibbard, Ali Abrar
Revised Revision 1 made by phadej at 2019-09-28T19:46:46Z
Category Constraints
Source repo head: git clone git://
Uploaded by abrar at 2019-01-25T22:39:25Z
Distributions Arch:, LTSHaskell:, NixOS:, Stackage:
Executables readme
Downloads 13157 total (756 in the last 30 days)
Rating 2.0 (votes: 1) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Hackage Matrix CI
Docs uploaded by user
Build status unknown [no reports yet]




Note: This package has metadata revisions in the cabal description newer than included in the tarball. To unpack the package including the revisions, use 'cabal get'.

Maintainer's Corner

For package maintainers and hackage trustees


Readme for constraints-extras-

[back to package description]


Example usage:

NB: This example can be built with -pgmL markdown-unlit.

{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeApplications  #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE ExistentialQuantification #-}

import Data.Aeson
import Data.Constraint.Forall
import Data.Constraint.Extras
import Data.Constraint.Extras.TH

data A :: * -> * where
  A_a :: A Int
  A_b :: Int -> A ()

data B :: * -> * where
  B_a :: A a -> A a -> B a
  B_x :: Int -> B Int

data V :: (* -> *) -> * where
  V_a :: A Int -> V A

deriveArgDict ''A
deriveArgDict ''B
deriveArgDictV ''V

data DSum k f = forall a. DSum (k a) (f a)

-- Derive a ToJSON instance for our 'DSum'
instance forall k f.
  ( Has' ToJSON k f -- Given a value of type (k a), we can obtain an instance (ToJSON (f a))
  , ForallF ToJSON k -- For any (a), we have an instance (ToJSON (k a))
  ) => ToJSON (DSum k f) where
  toJSON (DSum (k :: k a) f) = toJSON
    ( whichever @ToJSON @k @a $ toJSON k -- Use the (ForallF ToJSON k) constraint to obtain the (ToJSON (k a)) instance
    , has' @ToJSON @f k $ toJSON f -- Use the (Has' ToJSON k f) constraint to obtain the (ToJSON (f a)) instance

data Some k = forall a. Some (k a)

-- Derive a FromJSON instance for our 'DSum'
instance (FromJSON (Some f), Has' FromJSON f g) => FromJSON (DSum f g) where
  parseJSON x = do
    (jf, jg) <- parseJSON x
    Some (f :: f a) <- parseJSON jf
    g <- has' @FromJSON @g f (parseJSON jg)
    return $ DSum f g

main :: IO ()
main = return ()