Skip to content

Instantly share code, notes, and snippets.

@carymrobbins
Last active November 16, 2018 23:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carymrobbins/28e5ccf435d77170cd8a900a36d4b8c4 to your computer and use it in GitHub Desktop.
Save carymrobbins/28e5ccf435d77170cd8a900a36d4b8c4 to your computer and use it in GitHub Desktop.
Huge hack to support explicit numeric literals in Haskell
{- TODO: The Num and Fractional instances don't implement all methods.
- In practice this is fine but for completeness they should be done.
-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeFamilies #-}
module NumericLits where
data I = I
i :: I
i = I
instance (a ~ Int) => Num (I -> a) where
fromInteger = const . fromInteger
data F = F
f :: F
f = F
instance (a ~ Float) => Num (F -> a) where
fromInteger = const . fromInteger
instance (a ~ Float) => Fractional (F -> a) where
fromRational = const . fromRational
data D = D
d :: D
d = D
instance (a ~ Double) => Num (D -> a) where
fromInteger = const . fromInteger
instance (a ~ Double) => Fractional (D -> a) where
fromRational = const . fromRational
λ 0i
0
λ :t 0i
0i :: Int
λ :t 0f
0f :: Float
λ :t 0.2f
0.2f :: Float
λ :t 0d
0d :: Double
λ :t 0.4d
0.4d :: Double
λ 0i + 5
5
λ :t 0i + 5
0i + 5 :: Int
λ 7.2f * 8.3
59.76
λ :t 7.2f * 8.3
7.2f * 8.3 :: Float
λ 8.4f * 6.3d
<interactive>:15:8: error:
• Couldn't match type ‘Float’ with ‘Double’
arising from the literal ‘6.3’
• In the expression: 6.3
In the second argument of ‘(*)’, namely ‘6.3 d’
In the expression: 8.4 f * 6.3 d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment