git.iko.soy/purescript-literals
on main help sign in
Add base `Literal` type. Fix exports and type names. Add `IntLit`.
commit05b308f295722d4599d495ddb7d8accff01ce30e authorTomasz Rybarczyk <paluho@gmail.com> dateWed 27 Nov 2019, 00:27 (7 years ago) parent1b1c852
8 files changed+117−51
spago.dhall+1 src/Literals.purs+14 src/Literals/Boolean.purs+9−5 src/Literals/Int.purs+53 src/Literals/Literal.purs+8 src/Literals/Number.purs+18−22 src/Literals/String.purs+6−8 test/Main.purs+8−16
spago.dhall+1
@@ -8,6 +8,7 @@ You can edit this file as you like.
88 [ "assert"
99 , "effect"
1010 , "console"
11+ , "integers"
1112 , "numbers"
1213 , "partial"
1314 , "psci-support"
src/Literals.pursnew file+14
@@ -0,0 +1,14 @@
1+module Literals
2+ ( module Boolean
3+ , module Int
4+ , module Literal
5+ , module Number
6+ , module String
7+ ) where
8+
9+import Literals.Boolean (BooleanLit, false_, true_) as Boolean
10+import Literals.Int (class Digit, class Int, class Int', IntLit, intLit) as Int
11+import Literals.Literal (Literal, toValue) as Literal
12+import Literals.Number (class Number, class Number', NumberLit, numberLit) as Number
13+import Literals.String (StringLit, stringLit) as String
14+
src/Literals/Boolean.purs+9−5
@@ -1,13 +1,17 @@
1−module Literals.Boolean where
1+module Literals.Boolean
2+ ( BooleanLit
3+ , false_
4+ , true_
5+ ) where
26
7+import Literals.Literal (Literal)
38import Unsafe.Coerce (unsafeCoerce)
49
5−data True
6−data False
10+type BooleanLit sym = Literal Boolean sym
711
8−true_ :: True
12+true_ :: BooleanLit "true"
913true_ = unsafeCoerce true
1014
11−false_ :: False
15+false_ :: BooleanLit "false"
1216false_ = unsafeCoerce false
1317
src/Literals/Int.pursnew file+53
@@ -0,0 +1,53 @@
1+module Literals.Int
2+ ( IntLit
3+ , IntegerPart
4+ , Sign
5+ , class Digit
6+ , class Int
7+ , class Int'
8+ , intLit
9+ , kind IntPart
10+ ) where
11+
12+import Prelude
13+
14+import Data.Int (fromString)
15+import Data.Maybe (fromJust)
16+import Data.Symbol (class IsSymbol)
17+import Literals.Literal (Literal)
18+import Partial.Unsafe (unsafePartial)
19+import Prim.Symbol (class Cons)
20+import Type.Prelude (SProxy(..), reflectSymbol)
21+import Unsafe.Coerce (unsafeCoerce)
22+
23+class Digit (s :: Symbol)
24+
25+instance digit0 :: Digit "0"
26+else instance digit1 :: Digit "1"
27+else instance digit2 :: Digit "2"
28+else instance digit3 :: Digit "3"
29+else instance digit4 :: Digit "4"
30+else instance digit5 :: Digit "5"
31+else instance digit6 :: Digit "6"
32+else instance digit7 :: Digit "7"
33+else instance digit8 :: Digit "8"
34+else instance digit9 :: Digit "9"
35+
36+foreign import kind IntPart
37+foreign import data Sign :: IntPart
38+foreign import data IntegerPart :: IntPart
39+
40+class Int (sym :: Symbol)
41+instance intInstance :: (Int' h t Sign, Cons h t s) => Int s
42+
43+class Int' (head :: Symbol) (tail :: Symbol) (part :: IntPart)
44+
45+instance signPart :: (Cons h' t' t, Int' h' t' IntegerPart) => Int' "-" t Sign
46+else instance signPartEmpty :: (Int' h t IntegerPart) => Int' h t Sign
47+else instance lastIntegerPart :: (Digit h) => Int' h "" IntegerPart
48+else instance digitIntegerPart :: (Digit h, Cons h' t' t, Int' h' t' IntegerPart) => Int' h t IntegerPart
49+
50+type IntLit sym = Literal Int sym
51+
52+intLit :: forall sym. IsSymbol sym => Int sym => IntLit sym
53+intLit = unsafeCoerce $ unsafePartial $ fromJust $ fromString $ reflectSymbol (SProxy :: SProxy sym)
src/Literals/Literal.pursnew file+8
@@ -0,0 +1,8 @@
1+module Literals.Literal where
2+
3+import Unsafe.Coerce (unsafeCoerce)
4+
5+foreign import data Literal :: Type -> Symbol -> Type
6+
7+toValue :: ∀ s a. Literal a s -> a
8+toValue = unsafeCoerce
src/Literals/Number.purs+18−22
@@ -1,26 +1,26 @@
1−module Literals.Number where
1+module Literals.Number
2+ ( FractionalPart
3+ , IntegerPart
4+ , NumberLit
5+ , Sign
6+ , class Number
7+ , class Number'
8+ , kind NumberPart
9+ , numberLit
10+ ) where
11+
12+import Prelude
213
314import Data.Maybe (fromJust)
415import Data.Number (fromString)
516import Data.Symbol (class IsSymbol, SProxy(..))
17+import Literals.Int (class Digit)
18+import Literals.Literal (Literal)
619import Partial.Unsafe (unsafePartial)
720import Prim.Symbol (class Cons)
821import Type.Prelude (reflectSymbol)
922import Unsafe.Coerce (unsafeCoerce)
1023
11−class Digit (s :: Symbol)
12−
13−instance digit0 :: Digit "0"
14−else instance digit1 :: Digit "1"
15−else instance digit2 :: Digit "2"
16−else instance digit3 :: Digit "3"
17−else instance digit4 :: Digit "4"
18−else instance digit5 :: Digit "5"
19−else instance digit6 :: Digit "6"
20−else instance digit7 :: Digit "7"
21−else instance digit8 :: Digit "8"
22−else instance digit9 :: Digit "9"
23−
2424class Number (sym :: Symbol)
2525
2626instance numberInstance :: (Number' h t Sign, Cons h t s) => Number s
@@ -35,16 +35,12 @@ class Number' (head :: Symbol) (tail :: Symbol) (part :: NumberPart)
3535instance signPart :: (Cons h' t' t, Number' h' t' IntegerPart) => Number' "-" t Sign
3636else instance signPartEmpty :: (Number' h t IntegerPart) => Number' h t Sign
3737else instance pointIntegerPart :: (Cons h' t' t, Number' h' t' FractionalPart) => Number' "." t IntegerPart
38−else instance lastIntegerPart :: (Digit h) => Number' h "" IntegerPart
39−else instance digitIntegerPart :: (Digit h, Cons h' t' t, Number' h' t' IntegerPart) => Number' h t IntegerPart
4038else instance lastFractionalPart :: (Digit h) => Number' h "" FractionalPart
39+else instance digitIntegerPart :: (Digit h, Cons h' t' t, Number' h' t' IntegerPart) => Number' h t IntegerPart
4140else instance digitFractionalPart :: (Digit h, Cons h' t' t, Number' h' t' FractionalPart) => Number' h t FractionalPart
4241
43−foreign import data Literal :: Symbol -> Type
44−
45−literal :: forall s. IsSymbol s => Number s => Literal s
46−literal = (unsafeCoerce (unsafePartial (fromJust (fromString (reflectSymbol (SProxy :: SProxy s))))))
42+type NumberLit sym = Literal Number sym
4743
48−toNumber :: forall s. Literal s -> Number
49−toNumber = unsafeCoerce
44+numberLit :: forall sym. IsSymbol sym => Number sym => NumberLit sym
45+numberLit = unsafeCoerce $ unsafePartial $ fromJust $ fromString $ reflectSymbol (SProxy :: SProxy sym)
5046
src/Literals/String.purs+6−8
@@ -1,17 +1,15 @@
11module Literals.String
2− ( Literal
3− , literal
4− , toString
2+ ( stringLit
3+ , StringLit
54 ) where
65
76import Data.Symbol (class IsSymbol, SProxy(..), reflectSymbol)
7+import Literals.Literal (Literal)
88import Unsafe.Coerce (unsafeCoerce)
99
10−foreign import data Literal :: Symbol -> Type
10+type StringLit sym = Literal String sym
1111
12−literal :: forall sym. IsSymbol sym => Literal sym
13−literal = unsafeCoerce (reflectSymbol (SProxy :: SProxy sym))
12+stringLit :: forall sym. IsSymbol sym => StringLit sym
13+stringLit = unsafeCoerce (reflectSymbol (SProxy :: SProxy sym))
1414
15−toString :: forall sym. Literal sym -> String
16−toString = unsafeCoerce
1715
test/Main.purs+8−16
@@ -3,8 +3,7 @@ module Test.Main where
33import Prelude
44
55import Effect (Effect)
6−import Literals.Number (Literal, literal) as Number
7−import Literals.String (Literal, literal) as String
6+import Literals (NumberLit, StringLit, IntLit, intLit, numberLit, stringLit)
87import Test.Assert (assertEqual)
98import Unsafe.Coerce (unsafeCoerce)
109
@@ -12,28 +11,21 @@ import Unsafe.Coerce (unsafeCoerce)
1211main :: Effect Unit
1312main = do
1413 assertEqual
15− { actual: unsafeCoerce (String.literal :: String.Literal "foo")
14+ { actual: unsafeCoerce (stringLit :: StringLit "foo")
1615 , expected: "foo"
1716 }
1817 assertEqual
19− { actual: unsafeCoerce (Number.literal :: Number.Literal "8")
18+ { actual: unsafeCoerce (numberLit :: NumberLit "8.0")
2019 , expected: 8.0
2120 }
22− assertEqual
23− { actual: unsafeCoerce (Number.literal :: Number.Literal "-8")
21+ assertEqual { actual: unsafeCoerce (numberLit :: NumberLit "-8.0")
2422 , expected: -8.0
2523 }
2624 assertEqual
27− { actual: unsafeCoerce (Number.literal :: Number.Literal "100.0")
28− , expected: 100.0
25+ { actual: unsafeCoerce (intLit :: IntLit "8")
26+ , expected: 8
2927 }
30− assertEqual
31− { actual: unsafeCoerce (Number.literal :: Number.Literal "-100.0")
32− , expected: -100.0
28+ assertEqual { actual: unsafeCoerce (intLit :: IntLit "-8")
29+ , expected: -8
3330 }
34− assertEqual
35− { actual: unsafeCoerce (Number.literal :: Number.Literal "0.0")
36− , expected: 0.0
37− }
38−
3931