-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
28 changed files
with
374 additions
and
84 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
{-- | ||
The following module exemplifies the _flexibly typed numeric literal_ (FTNL) typing rules. | ||
A numeric literal with a suffix (one of 'l', 'n', 'f', 'd', 'L', 'N', 'F', 'D' ) is | ||
never a FTNL. The type is fixed to 'Long', 'Integer', 'Double' or 'Float'. | ||
An integer FTNL is either @0@ or a non-null digit followed by arbitrary many digits, | ||
without suffix letter. Octal literals (which are starting with @0@ and have at least one | ||
further digit) and hexadecimal literals (starting with @0x@) are not FTNLs, rather, | ||
their type is invariably 'Int'. | ||
A floating point FTNL is a number in decimal or scientific notation, without suffix letter. | ||
-} | ||
|
||
module examples.NumericLiterals where | ||
|
||
import Prelude.Floating | ||
|
||
{-- | ||
1. If there is one and only one numeric type (that is, instance of type class 'Num') | ||
that is valid for the integer FTNL, | ||
then the literal denotes a value of that type. | ||
Since @n@ is 'Integer', both literals must be 'Integer' | ||
-} | ||
rule1 :: Integer -> Bool | ||
{-- Alternativly, the following types would be possible: | ||
> rule1 :: Int -> Bool | ||
> rule1 :: Long -> Bool | ||
> rule1 :: Integral i => i -> Bool | ||
-} | ||
rule1 n = n `rem` 2 != 0 | ||
|
||
--- Since @x@ is 'Float', both literals must be 'Float' literals. | ||
--- Alternativly, the following types would be possible: | ||
--- > rule1d :: Double -> Double | ||
--- > rule1d :: Real r => r -> r | ||
rule1d :: Float -> Float | ||
rule1d x = 0.5 * x + 1 | ||
|
||
|
||
|
||
{-- | ||
2. If only a type that is an instance of 'Real', 'PrimitiveFloating' | ||
or some class that has 'Real' as superclass would be valid, | ||
it denotes a value of type 'Double'. | ||
Since | ||
> sqrt :: Floating α => α -> α | ||
> (**) :: Floating α => α -> α -> α | ||
and 'Floating' is a subclass of 'Real', both literals will be 'Double'. | ||
-} | ||
-- rule2 :: Float -- also possible | ||
-- rule2 :: Floating r => r -- also possible (see below) | ||
-- rule2 :: Double -- default | ||
rule2 = sqrt 2 ** 7 | ||
|
||
{-- | ||
3. If a type signature is present which forces the type to be polymorphic, | ||
the literal will be replaced by an application of 'fromInt' or 'fromDouble' | ||
to the same literal | ||
(which then itself will get typed as 'Int' or 'Double' by the previous rules). | ||
Because of the type signature, the literals must be polymorphic, which is achieved | ||
through implicit application of 'fromInt' to @7@ and 'fromDouble' to @2.0@. | ||
However, this requires that type variables | ||
that stand for the type of the literal must have a constraint that | ||
ensures they are numbers, i.e. instances of (a subclass of) 'Num'. | ||
-} | ||
rule3a :: Floating r => r | ||
rule3a = sqrt 2.0 ** 7 | ||
|
||
rule3b :: Integral n => n -> Bool | ||
rule3b n = n `rem` 2 != 0 | ||
|
||
-- inferred type is more constrained than expected type | ||
-- inferred: Num a => a -> a | ||
-- expected: a -> a | ||
-- rule3c :: a -> a | ||
-- rule3c n = n + 2 | ||
|
||
{-- | ||
4. If the type of an integer literal cannot be determined by the rules above, | ||
it defaults to 'Int'. | ||
The functions 'rem' and '!=' are polymorphic over 'Integral'/'Eq' types, | ||
so no concrete type is enforced by the code, and rules 2 and 3 above | ||
are not applicable. Therefore, it defaults to 'Int'. | ||
-} | ||
rule4 n = n `rem` 2 != 0 | ||
|
||
--- @2.0@ gets type 'Double' since the type ox @x@ is unknown. | ||
rule4d x = x / 2.0 | ||
|
||
|
||
|
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.