1

I'd like to go from a type to value as follows, but without using the depreciated DatatypeContexts:

{-# LANGUAGE DataKinds #-}          
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE DatatypeContexts #-}   

import Data.Proxy
import GHC.TypeLits

data (KnownNat n) => MagicN n a = MagicN a             
  deriving (Show)                                      

getMagicN :: forall n. (KnownNat n) => MagicN n Integer
getMagicN = MagicN $ natVal (Proxy :: Proxy n)         

MagicN five = getMagicN :: MagicN 5 Integer                     

I'm not too concerned if this or another method (Peano numbers etc) is used; the requirement is to be able to construct the value from the type information alone.

Thanks!

Scott
  • 4,070
  • 3
  • 21
  • 16

1 Answers1

3

You just need a better way to constrain the kind of MagicN's first parameter. {-# LANGUAGE KindSignatures #-} will give you just what you need.

{-# LANGUAGE DataKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE KindSignatures #-}

import Data.Proxy
import GHC.TypeLits

data MagicN (n :: Nat) a = MagicN a
  deriving (Show)

All the rest is the same.

Note: KindSignatures is implied by PolyKinds and by TypeFamilies, so if you're using either of the latter you won't need to add it explicitly.

dfeuer
  • 48,079
  • 5
  • 63
  • 167