Created
December 29, 2014 14:32
-
-
Save maoe/65715752873f34dede4a to your computer and use it in GitHub Desktop.
Universally quantified constraints
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE GADTs #-} | |
{-# LANGUAGE RankNTypes #-} | |
{-# LANGUAGE ScopedTypeVariables #-} | |
{-# LANGUAGE TypeOperators #-} | |
module ForallConstraints where | |
import Data.Monoid | |
import Data.Constraint | |
import Data.Constraint.Forall | |
data Box t where | |
Box :: t a -> Box t | |
-- This doesn't typecheck | |
-- example :: forall t. (forall a. Monoid (t a)) => Box t -> Box t | |
-- example (Box x) = Box (x <> x) | |
-- Bake the constraint into the data type | |
data MonBox t where | |
MonBox :: Monoid (t a) => t a -> MonBox t | |
example1 :: MonBox t -> MonBox t | |
example1 (MonBox x) = MonBox (x <> x) | |
-- Use the constraints package | |
example2 :: ForallF Monoid t => Box t -> Box t | |
example2 (Box (x :: t a)) = Box (x <> x) | |
\\ (instF :: ForallF Monoid t :- Monoid (t a)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment