Last active
January 10, 2016 23:16
-
-
Save max630/b4727fbb1b09c83ba672 to your computer and use it in GitHub Desktop.
attempt to use DataKinds
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 MultiParamTypeClasses #-} | |
{-# LANGUAGE FunctionalDependencies #-} | |
{-# LANGUAGE TypeOperators #-} | |
{-# LANGUAGE TypeFamilies #-} | |
{-# LANGUAGE ExplicitNamespaces #-} | |
{-# LANGUAGE DataKinds #-} | |
{-# LANGUAGE FlexibleInstances #-} | |
{-# LANGUAGE FlexibleContexts #-} | |
{-# LANGUAGE OverlappingInstances #-} | |
{-# LANGUAGE UndecidableInstances #-} | |
module Foo where | |
import Prelude hiding ((-)) | |
import GHC.TypeLits (Nat, type (-), type (<=)) | |
import Data.Proxy (Proxy(Proxy)) | |
class Foo (n :: Nat) x y | n x -> y, x y -> n where | |
foo :: Proxy n -> x -> y | |
instance Foo 0 (x,a) x where | |
foo _ (x,_) = x | |
reduce :: Proxy n -> Proxy (n - 1) | |
reduce _ = Proxy | |
instance (Foo (n - 1) x y, 1 <= n) => Foo n (a,x) y where | |
foo n (_,x) = foo (reduce n) x | |
-- > :l Foo | |
-- *Foo> :set -XDataKinds | |
-- *Foo> foo (Proxy :: Proxy 0) ("",(2::Int,True)) | |
-- "" | |
-- *Foo> foo (Proxy :: Proxy 1) ("",(2::Int,True)) | |
-- 2 | |
-- *Foo> foo Proxy ("",(2::Int,True)) :: String | |
-- "" | |
-- *Foo> foo Proxy ("",(2::Int,True)) :: Int | |
-- 2 | |
-- *Foo> foo Proxy ("",(2::Int,("a",()))) :: String | |
-- "" | |
-- *Foo> foo (Proxy :: Proxy 2) ("",(2::Int,("a",()))) :: String | |
-- "a" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
But:
PS: version 2 suddenly works