Skip to content

Instantly share code, notes, and snippets.

@andrewthad
Created April 16, 2018 20:04
Show Gist options
  • Save andrewthad/cf1ac10ea14df7972ca55fb0d2011ae7 to your computer and use it in GitHub Desktop.
Save andrewthad/cf1ac10ea14df7972ca55fb0d2011ae7 to your computer and use it in GitHub Desktop.
Constraint Backpack
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE TypeInType #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE TypeFamilies #-}
unit example where
signature MyStuff where
import GHC.Types
data MyConstraint :: Type -> Constraint
buildNumber :: MyConstraint a => a -> a
combineNumbers :: MyConstraint a => a -> a -> a
module NumberStuff where
import MyStuff
buildTwoNumbers :: MyConstraint a => a -> a -> a
buildTwoNumbers a b = combineNumbers (buildNumber a) (buildNumber b)
unit number-example-a where
module NumberUnknown where
import GHC.Types
type MyConstraint = Num
buildNumber :: Num a => a -> a
buildNumber = negate
combineNumbers :: Num a => a -> a -> a
combineNumbers = (+)
unit main where
dependency example[MyStuff=number-example-a:NumberUnknown]
module Main where
import NumberStuff
import GHC.Types
main = print (buildTwoNumbers 5 7 :: Int)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment