As always I’m terribly sorry for my bad english, but writing the posts and having a feedback is the only reasonable way to improve it.
This post describes an easy exercise that gives a feeling of use typelevel numbers. This post is a part of another work that hopefully will be covered in following posts.
If you want to know more about typelevel literals and friends you can take a look at the following post it describes the theory much better than I can.
Intead of using type level Strings we will address numbers. In this post I want to create a datatype that will contain a number that is known statically (at compilation time) and which can be extracted from type at runtime.
The first part of the problem (having a number) in a type may be interesting if you want to distiguish types assosiated with different numbers.
For simplicity we will take Data.Tagged.Tagged type from
tagged package. However it’s possible to create our own type as I did at the beggining (just I did a first time).
We add helper methods that allow us to extract infomation about the type-parameter from the value:
toProxy :: Tagged n a -> Proxy ntoProxy _ = Proxy
And add a constrictor helper that allow to create a type with information about number that is provided by us.
mkT :: proxy n -> a -> Tagged n amkT _ a = Tagged a
Just check that we can create a fancy values:
*Main> :t mkT (Proxy :: Proxy Nothing) 8mkT (Proxy :: Proxy Nothing) 8 :: Num a => Tagged 'Nothing a
From this point we can start real fun. At first lest create a value with natural type parameter:
*Main> :t mkT (Proxy :: Proxy 3) 8mkT (Proxy :: Proxy 3) 8 :: Num a => Tagged 3 a
Now we want to be able to use this information at runtime. Let’s introduce a new method that will use
natVal :: forall n proxy. KnownNat n => proxy n -> Integer`
take a type that acts as a type proxy and returns the integer assosiated with a type.
useT :: (KnownNat n, Num a) => Tagged n a -> auseT t@(Tagged a) = fromIntegral (natVal (toProxy t)) + a
Here we are:
extracting type information by calling
extracting natural number from a proxy by calling
returning a calculated results
*Main> useT $ mkT (Proxy :: Proxy 3) 811
On the next step we will introduce a code that works for natural numbers, by definition rational number is
a % b, where
a \in Z and
b \in N. Lets lift this information to a type level by introducing a typelevel rational:
data (:%%) a b
And introduce type extraction mechanisms
-- | extract numerator typenumerator :: proxy (n :%% m) -> Proxy nnumerator _ = Proxy-- | Extract denomenator typedenomenator :: proxy (n :%% m) -> Proxy mdenomenator _ = Proxy
Today we don’t conver math operations over the type-parameters, this may be a topic for another post. So now we may be quite happy as we can create values tagged by something that looks like a rational number.
*Main> :t mkT (Proxy :: Proxy (3 :%% 8)) 8mkT (Proxy :: Proxy (3 :%% 8)) 8 :: Num a => Tagged (3 :%% 8) a
Now we can write a function that will use information about the type at runtime:
useTF :: (KnownNat n, KnownNat m, Fractional a) => Tagged (n :%% m) a -> auseTF t@(Tagged a) = v + awhere v = fromRational $natVal (numerator $ toProxy t) :% natVal (denomenator $ toProxy t)
*Main> useTF $ mkT (Proxy :: Proxy (3 :%% 8)) 88.375
Now we want to solve the following problem: it’s not easy to write types for ratio, the reason that for a complex cases we need somehow to find the ratio that fits. Also we want to support real numbers. Having in mind the fact that real values are not supported on the typelevel, we may be quite happy with Rational approximation.
In order to solve those 2 problems we need:
real values support
ability to write code easily
We will introduce a TH function that will find a good approximation and create required type:
mkFloatProxy :: RealFrac a => a -> Q ExpmkFloatProxy x = [| Proxy :: Proxy ($(nk a) :%% $(nk b)) |]where (a :% b) = toRational xnk x = sigT (litT (numTyLit x)) (ConT $ mkName "Nat")
the magic that we have in
nk x we need to create a type parameter of kind
Nat. Now we can write a code like:
test = mkT $(mkFloatProxy pi) 7.0
This is quite nice, next steps may be support of negative values, using SomeNat and operations on values.
comments powered by Disqus