Skip to content

A peer-to-peer stack for code collaboration

Replace Scientific with Rational (#211)

James Haydon364c6383e272
diff --git a/exe/RadicleExe.hs b/exe/RadicleExe.hs
index 5915259..fd0d3fc 100644
--- a/exe/RadicleExe.hs
+++ b/exe/RadicleExe.hs
@@ -2,7 +2,6 @@ module RadicleExe (main) where
import API
import Control.Monad.Catch (MonadThrow)
-import Data.Scientific (floatingOrInteger)
import qualified Data.Text as T
import GHC.Exts (fromList)
import Network.HTTP.Client (defaultManagerSettings, newManager)
@@ -15,6 +14,7 @@ import Servant.Client
import System.Console.Haskeline (InputT)
import System.Directory (doesFileExist)
+import qualified Radicle.Internal.Number as Num
import qualified Radicle.Internal.PrimFns as PrimFns
main :: IO ()
@@ -99,8 +99,8 @@ clientPrimFns mgr = fromList . PrimFns.allDocs $ [sendPrimop, receivePrimop]
\ for the last `n` inputs that have been evaluated."
, \case
[String url, Number n] -> do
- case floatingOrInteger n of
- Left (_ :: Float) -> throwErrorHere . OtherError
+ case Num.isInt n of
+ Left _ -> throwErrorHere . OtherError
$ "receive!: expecting int argument"
Right r -> do
liftIO (runClientM' url mgr (since r)) >>= \case
diff --git a/exe/ReferenceDoc.hs b/exe/ReferenceDoc.hs
index 5329b12..22f9de8 100644
--- a/exe/ReferenceDoc.hs
+++ b/exe/ReferenceDoc.hs
@@ -78,7 +78,7 @@ main = do
, "apply", "type", "atom?", "boolean?", "string?", "number?", "keyword?", "list?"
, "dict?", "read", "read-many", "throw", "Y", "Y2", "to-json", "uuid?", "make-counter"
, "public-key?" ]
- maths = ["+", "*", "-", "<", ">", "integral?"]
+ maths = ["+", "*", "-", "/", "<", ">", "integral?"]
evalFns = ["base-eval", "eval", "updatable-eval"]
envStuff = ["pure-env", "get-current-env", "set-current-env", "set-env!"]
seqs = ["nth", "foldl", "foldr", "map", "seq", "take", "drop"]
diff --git a/src/Radicle/Internal/Core.hs b/src/Radicle/Internal/Core.hs
index 838d371..e8330cf 100644
--- a/src/Radicle/Internal/Core.hs
+++ b/src/Radicle/Internal/Core.hs
@@ -19,8 +19,7 @@ import qualified Data.IntMap as IntMap
import Data.List.NonEmpty (NonEmpty)
import qualified Data.List.NonEmpty as NonEmpty
import qualified Data.Map as Map
-import Data.Scientific
- (Scientific, floatingOrInteger, toBoundedInteger)
+import Data.Scientific (Scientific)
import Data.Semigroup ((<>))
import Data.Sequence (Seq(..))
import qualified Data.Sequence as Seq
@@ -32,6 +31,7 @@ import Radicle.Internal.Annotation (Annotated)
import qualified Radicle.Internal.Annotation as Ann
import qualified Radicle.Internal.Doc as Doc
import qualified Radicle.Internal.Identifier as Identifier
+import qualified Radicle.Internal.Number as Num
import Radicle.Internal.Orphans ()
@@ -131,7 +131,7 @@ data ValueF r =
-- | Symbolic identifiers that evaluate to themselves.
| KeywordF Ident
| StringF Text
- | NumberF Scientific
+ | NumberF Rational
| BooleanF Bool
| ListF [r]
| VecF (Seq r)
@@ -167,7 +167,7 @@ pattern String i <- (Ann.match -> StringF i)
where
String = Ann.annotate . StringF
-pattern Number :: ValueConC t => Scientific -> Annotated t ValueF
+pattern Number :: ValueConC t => Rational -> Annotated t ValueF
pattern Number i <- (Ann.match -> NumberF i)
where
Number = Ann.annotate . NumberF
@@ -219,14 +219,9 @@ isAtom :: Value -> Maybe Ident
isAtom (Atom i) = pure i
isAtom _ = Nothing
--- should be a prism
-isInt :: Value -> Maybe Integer
-isInt (Number s) = either (const Nothing :: Double -> Maybe Integer) pure (floatingOrInteger s)
-isInt _ = Nothing
-
instance A.FromJSON Value where
parseJSON = \case
- A.Number n -> pure $ Number n
+ A.Number n -> pure $ Number (toRational n)
A.String s -> pure $ String s
A.Array ls -> List . toList <$> traverse parseJSON ls
A.Bool b -> pure $ Boolean b
@@ -255,7 +250,7 @@ instance A.FromJSON Value where
-- "null"
maybeJson :: Value -> Maybe A.Value
maybeJson = \case
- Number n -> pure $ A.Number n
+ Number n -> A.Number <$> hush (Num.isSci n)
String s -> pure $ A.String s
Boolean b -> pure $ A.Bool b
List ls -> toJSON <$> traverse maybeJson ls
@@ -519,22 +514,15 @@ instance (CPA t, FromRad t a) => FromRad t (Maybe a) where
fromRad _ = Left "Expecting :Nothing or [:Just _]"
instance FromRad t (Annotated t ValueF) where
fromRad = pure
+instance CPA t => FromRad t Rational where
+ fromRad (Number n) = pure n
+ fromRad _ = Left "Not a number"
instance CPA t => FromRad t Scientific where
- fromRad x = case x of
- Number n -> pure n
- _ -> Left "Expecting number"
+ fromRad = fromRad >=> Num.isSci
instance CPA t => FromRad t Int where
- fromRad = \case
- Number s -> case toBoundedInteger s of
- Just i -> pure i
- Nothing -> Left "Number was not a bounded int"
- _ -> Left "Expecting number"
+ fromRad = fromRad >=> Num.isInt
instance CPA t => FromRad t Integer where
- fromRad = \case
- Number s -> case floatingOrInteger s of
- Left (_ :: Double) -> Left "Expecting whole number"
- Right i -> pure i
- _ -> Left "Expecting number"
+ fromRad = fromRad >=> Num.isInteger
instance CPA t => FromRad t Text where
fromRad x = case x of
String n -> pure n
@@ -591,7 +579,7 @@ instance CPA t => ToRad t Int where
instance CPA t => ToRad t Integer where
toRad = Number . fromIntegral
instance CPA t => ToRad t Scientific where
- toRad = Number
+ toRad = Number . toRational
instance CPA t => ToRad t Text where
toRad = String
instance ToRad t (Ann.Annotated t ValueF) where
diff --git a/src/Radicle/Internal/Number.hs b/src/Radicle/Internal/Number.hs
new file mode 100644
index 0000000..db3f163
--- /dev/null
+++ b/src/Radicle/Internal/Number.hs
@@ -0,0 +1,24 @@
+module Radicle.Internal.Number where
+
+import Protolude
+
+import Data.Scientific (Scientific, fromRationalRepetendUnlimited)
+
+isInteger :: Rational -> Either Text Integer
+isInteger (a :% b) | b == 1 = pure a
+isInteger _ = Left "Not a whole number"
+
+isInt :: Rational -> Either Text Int
+isInt x = isInteger x >>= int
+ where
+ int i =
+ if inBounds i then pure (fromIntegral i) else Left "Not a bounded whole number (int)"
+
+ inBounds i =
+ fromIntegral (minBound :: Int) <= i
+ && i <= fromIntegral (maxBound :: Int)
+
+isSci :: Rational -> Either Text Scientific
+isSci r = case fromRationalRepetendUnlimited r of
+ (s, Nothing) -> pure s
+ _ -> Left "Does not have a non-repeating decimal expansion"
diff --git a/src/Radicle/Internal/Parse.hs b/src/Radicle/Internal/Parse.hs
index b8507ef..cba5ef6 100644
--- a/src/Radicle/Internal/Parse.hs
+++ b/src/Radicle/Internal/Parse.hs
@@ -71,8 +71,16 @@ boolLiteralP = lexeme $ tag =<< BooleanF <$> (char '#' >>
(char 't' >> pure True) <|> (char 'f' >> pure False))
numLiteralP :: VParser
-numLiteralP = tag =<< NumberF <$> signed L.scientific
+numLiteralP = tag =<< NumberF <$> signed pos
where
+ posrat =
+ do n <- L.decimal
+ _ <- char '/'
+ d <- L.decimal
+ pure (n % d)
+
+ pos = try posrat <|> fromIntegral <$> (L.decimal :: Parser Integer)
+
-- We don't allow spaces between the sign and digits so that we can remain
-- consistent with the general Scheme of things.
signed p = M.option identity ((identity <$ char '+') <|> (negate <$ char '-')) <*> p
diff --git a/src/Radicle/Internal/Pretty.hs b/src/Radicle/Internal/Pretty.hs
index 8d03563..16f72b0 100644
--- a/src/Radicle/Internal/Pretty.hs
+++ b/src/Radicle/Internal/Pretty.hs
@@ -29,7 +29,7 @@ instance (Copointed t, Ann.Annotation t) => Pretty (Ann.Annotated t ValueF) wher
Keyword i -> ":" <> pretty i
Ref i -> pretty i
String t -> "\"" <> pretty (escapeStr t) <> "\""
- Number n -> pretty (show n :: Text)
+ Number (a :% b) -> pretty a <> if b == 1 then "" else "/" <> pretty b
Boolean True -> "#t"
Boolean False -> "#f"
List vs -> case vs of
diff --git a/src/Radicle/Internal/PrimFns.hs b/src/Radicle/Internal/PrimFns.hs
index cabbd59..7c624ee 100644
--- a/src/Radicle/Internal/PrimFns.hs
+++ b/src/Radicle/Internal/PrimFns.hs
@@ -5,7 +5,6 @@ import Protolude hiding (TypeError)
import qualified Data.Aeson as Aeson
import qualified Data.IntMap as IntMap
import qualified Data.Map as Map
-import Data.Scientific (Scientific, floatingOrInteger)
import Data.Sequence (Seq(..))
import qualified Data.Sequence as Seq
import qualified Data.Text as T
@@ -16,6 +15,7 @@ import qualified Radicle.Internal.Annotation as Ann
import Radicle.Internal.Core
import Radicle.Internal.Crypto
import qualified Radicle.Internal.Doc as Doc
+import qualified Radicle.Internal.Number as Num
import Radicle.Internal.Parse
import Radicle.Internal.Pretty
import qualified Radicle.Internal.UUID as UUID
@@ -179,8 +179,8 @@ purePrimFns = fromList $ allDocs $
, "Returns all but the first `n` items of a sequence, unless the sequence is empty,\
\ in which case an exception is thrown."
, twoArg "drop" $ \case
- (Number n, vs) -> case floatingOrInteger n of
- Left (_ :: Double) -> throwErrorHere $ OtherError "drop: first argument must be an integer"
+ (Number n, vs) -> case Num.isInt n of
+ Left _ -> throwErrorHere $ OtherError "drop: first argument must be an int"
Right i -> case vs of
List xs -> pure . List $ drop i xs
Vec xs -> pure . Vec $ Seq.drop i xs
@@ -191,8 +191,8 @@ purePrimFns = fromList $ allDocs $
, "Returns the first `n` items of a sequence, unless the sequence is too short,\
\ in which case an exception is thrown."
, twoArg "take" $ \case
- (Number n, vs) -> case floatingOrInteger n of
- Left (_ :: Double) -> throwErrorHere $ OtherError "take: first argument must be an integer"
+ (Number n, vs) -> case Num.isInt n of
+ Left _ -> throwErrorHere $ OtherError "take: first argument must be an integer"
Right i -> case vs of
List xs -> pure . List $ take i xs
Vec xs -> pure . Vec $ Seq.take i xs
@@ -205,8 +205,8 @@ purePrimFns = fromList $ allDocs $
\ does not have an `n`-th element, or if it is not a list or vector, then\
\ an exception is thrown."
, \case
- [Number n, vs] -> case floatingOrInteger n of
- Left (_ :: Double) -> throwErrorHere $ OtherError "nth: first argument was not an integer"
+ [Number n, vs] -> case Num.isInt n of
+ Left _ -> throwErrorHere $ OtherError "nth: first argument was not an integer"
Right i -> do
xs <- hoistEitherWith (const (toLangError . OtherError $ "nth: first argument must be sequential")) $ fromRad vs
case xs `atMay` i of
@@ -283,6 +283,13 @@ purePrimFns = fromList $ allDocs $
, numBinop (+) "+" "Adds two numbers together."
, numBinop (*) "*" "Multiplies two numbers together."
, numBinop (-) "-" "Substracts one number from another."
+ , ( "/"
+ , "Divides one number by another. Throws an exception if the second argument is 0."
+ , twoArg "/" $ \case
+ (Number x, Number y) | y /= 0 -> pure $ Number (x / y)
+ (Number _, Number _) -> throwErrorHere $ OtherError "Can't divide by 0"
+ _ -> throwErrorHere $ TypeError "/: expects two numbers"
+ )
, ( "<"
, "Checks if a number is strictly less than another."
, \case
@@ -298,9 +305,9 @@ purePrimFns = fromList $ allDocs $
, ( "integral?"
, "Checks if a number is an integer."
, oneArg "integral?" $ \case
- Number n -> case floatingOrInteger n of
- Left (_ :: Double) -> pure $ Boolean False
- Right (_ :: Integer) -> pure $ Boolean True
+ Number n -> case Num.isInteger n of
+ Left _ -> pure $ Boolean False
+ Right _ -> pure $ Boolean True
_ -> throwErrorHere $ TypeError "integral?: expecting number"
)
, ( "foldl"
@@ -496,7 +503,7 @@ purePrimFns = fromList $ allDocs $
kw = Keyword . Ident
- numBinop :: (Scientific -> Scientific -> Scientific)
+ numBinop :: (Rational -> Rational -> Rational)
-> Text
-> Text
-> (Text, Text, [Value] -> Lang m Value)
diff --git a/test/spec/Radicle/Internal/TestCapabilities.hs b/test/spec/Radicle/Internal/TestCapabilities.hs
index 88ccc9e..4f10325 100644
--- a/test/spec/Radicle/Internal/TestCapabilities.hs
+++ b/test/spec/Radicle/Internal/TestCapabilities.hs
@@ -6,7 +6,6 @@ import Protolude hiding (TypeError)
import qualified Crypto.Random as CryptoRand
import qualified Data.Map.Strict as Map
-import Data.Scientific (floatingOrInteger)
import qualified Data.Sequence as Seq
import GHC.Exts (fromList)
import qualified System.FilePath.Find as FP
@@ -15,6 +14,7 @@ import Radicle
import Radicle.Internal.Core (addBinding)
import Radicle.Internal.Crypto
import Radicle.Internal.Effects.Capabilities
+import qualified Radicle.Internal.Number as Num
import Radicle.Internal.PrimFns (allDocs)
import qualified Radicle.Internal.UUID as UUID
@@ -116,8 +116,8 @@ clientPrimFns = fromList . allDocs $ [sendPrimop, receivePrimop]
, "Mocked version of `receive!` that retrieves values from a map with chains as keys."
, \case
[String url, Number n] -> do
- case floatingOrInteger n of
- Left (_ :: Float) -> throwErrorHere . OtherError
+ case Num.isInt n of
+ Left _ -> throwErrorHere . OtherError
$ "receive!: expecting int argument"
Right r -> do
chains <- lift $ gets worldStateRemoteChains
diff --git a/test/spec/Radicle/Tests.hs b/test/spec/Radicle/Tests.hs
index a4abc19..c762ddb 100644
--- a/test/spec/Radicle/Tests.hs
+++ b/test/spec/Radicle/Tests.hs
@@ -42,21 +42,21 @@ test_eval =
, testCase "vector syntax creates vectors" $ do
"[]" `succeedsWith` Vec Empty
- "[1 2]" `succeedsWith` Vec (fromList [Number 1, Number 2])
- "[1 :a [:b 2]]" `succeedsWith` Vec (fromList [Number 1, kw "a", Vec (fromList [kw "b", Number 2])])
+ "[1 2]" `succeedsWith` Vec (fromList [int 1, int 2])
+ "[1 :a [:b 2]]" `succeedsWith` Vec (fromList [int 1, kw "a", Vec (fromList [kw "b", int 2])])
, testCase "vectors evaluate their elements" $
- "[(+ 1 2) (* 3 4)]" `succeedsWith` Vec (fromList [Number 3, Number 12])
+ "[(+ 1 2) (* 3 4)]" `succeedsWith` Vec (fromList [int 3, int 12])
, testCase "'dict' creates a Dict with given key/vals" $ do
let prog1 = [s|(dict 'why "not")|]
prog1 `succeedsWith` Dict (fromList [(Atom $ [ident|why|], String "not")])
let prog2 = [s|(dict 3 1)|]
- prog2 `succeedsWith` Dict (fromList [(Number 3, Number 1)])
+ prog2 `succeedsWith` Dict (fromList [(int 3, int 1)])
, testCase "Dicts evaluate both keys are arguments" $ do
- "{(+ 1 1) (+ 1 1)}" `succeedsWith` Dict (fromList [(Number 2, Number 2)])
- "{(+ 1 1) :a (+ 0 2) :b}" `succeedsWith` Dict (fromList [(Number 2, kw "a")])
+ "{(+ 1 1) (+ 1 1)}" `succeedsWith` Dict (fromList [(int 2, int 2)])
+ "{(+ 1 1) :a (+ 0 2) :b}" `succeedsWith` Dict (fromList [(int 2, kw "a")])
, testCase "'cons' conses an element" $ do
let prog = [s|(cons #t (list #f))|]
@@ -75,10 +75,10 @@ test_eval =
"(drop 2 [1 2 3 4])" `succeedsWith` toRad [3 :: Int, 4]
, testCase "'nth' extracts elements from lists and vectors" $ do
- "(nth 0 [0 1 2])" `succeedsWith` Number 0
- "(nth 1 [0 1 2])" `succeedsWith` Number 1
- "(nth 0 '(0 1 2))" `succeedsWith` Number 0
- "(nth 1 '(0 1 2))" `succeedsWith` Number 1
+ "(nth 0 [0 1 2])" `succeedsWith` int 0
+ "(nth 1 [0 1 2])" `succeedsWith` int 1
+ "(nth 0 '(0 1 2))" `succeedsWith` int 0
+ "(nth 1 '(0 1 2))" `succeedsWith` int 1
, testCase "'take n' takes n elements of a sequence" $ do
"(take 0 [0 1 2])" `succeedsWith` Vec (fromList [])
@@ -112,7 +112,7 @@ test_eval =
, testCase "'integral?' returns true for whole numbers and false fractional numbers" $ do
"(integral? 0)" `succeedsWith` Boolean True
"(integral? 1)" `succeedsWith` Boolean True
- "(integral? 3.14159265359)" `succeedsWith` Boolean False
+ "(integral? 1/3)" `succeedsWith` Boolean False
, testCase "'member?' returns true if list contains element" $ do
let prog = [s|(member? #t (list #f #t))|]
@@ -158,27 +158,27 @@ test_eval =
, testProperty "'string-length' returns length of a string" $ \x -> do
let prog = "(string-length " <> renderPrettyDef (asValue (String x)) <> ")"
res = runTest' prog
- expected = Right $ Number $ fromIntegral $ T.length x
+ expected = Right $ int $ fromIntegral $ T.length x
info = "Expected:\n" <> prettyEither (Right (String x)) <>
"Got:\n" <> prettyEither (runTest' prog)
counterexample (toS info) $ res == expected
, testCase "'foldl' foldls the list" $ do
let prog = [s|(foldl (fn [x y] (- x y)) 0 (list 1 2 3))|]
- prog `succeedsWith` Number (-6)
+ prog `succeedsWith` int (-6)
, testCase "'foldr' foldrs the list" $ do
let prog = [s|(foldr (fn [x y] (- x y)) 0 (list 1 2 3))|]
- prog `succeedsWith` Number 2
+ prog `succeedsWith` int 2
, testCase "'map' maps over the list" $ do
let prog = [s|(map (fn [x] (+ x 1)) (list 1 2))|]
- prog `succeedsWith` List [Number 2, Number 3]
+ prog `succeedsWith` List [int 2, int 3]
, testCase "'map' (and co.) don't over-eval elements of argument list" $ do
let prog = [s|(map (fn [x] (cons 1 x)) (list (list 1)))
|]
- prog `succeedsWith` List [List [Number 1, Number 1]]
+ prog `succeedsWith` List [List [int 1, int 1]]
, testCase "'eval' evaluates the list" $ do
let prog = [s|(head (eval (quote #t) (get-current-env)))|]
@@ -219,7 +219,7 @@ test_eval =
(def y 3)
y))
|]
- prog `succeedsWith` Number 3
+ prog `succeedsWith` int 3
, testCase "lambdas throw WrongNumberOfArgs if given wrong number of args" $ do
let prog = [s| ((fn [x] 3)) |]
@@ -254,8 +254,8 @@ test_eval =
"(do)" `succeedsWith` List []
, testCase "'do' returns the result of the last argument" $ do
- "(do 1)" `succeedsWith` Number 1
- "(do 1 2 3)" `succeedsWith` Number 3
+ "(do 1)" `succeedsWith` int 1
+ "(do 1 2 3)" `succeedsWith` int 3
, testCase "'do' runs effects in order" $ do
let prog = [s|
@@ -264,7 +264,7 @@ test_eval =
(write-ref r 2))
(read-ref r)
|]
- prog `succeedsWith` Number 2
+ prog `succeedsWith` int 2
, testCase "'string?' is true for strings" $ do
let prog = [s|(string? "hi")|]
@@ -315,19 +315,19 @@ test_eval =
, testCase "'+' sums the list of numbers" $ do
let prog1 = [s|(+ 2 (+ 2 3))|]
- prog1 `succeedsWith` Number 7
+ prog1 `succeedsWith` int 7
let prog2 = [s|(+ -1 -2 -3)|]
- prog2 `succeedsWith` Number (- 6)
+ prog2 `succeedsWith` int (- 6)
, testCase "'-' subtracts the list of numbers" $ do
let prog1= [s|(- (+ 2 3) 1)|]
- prog1 `succeedsWith` Number 4
+ prog1 `succeedsWith` int 4
let prog2 = [s|(- -1 -2 -3)|]
- prog2 `succeedsWith` Number 4
+ prog2 `succeedsWith` int 4
, testCase "'*' multiplies the list of numbers" $ do
let prog = [s|(* 2 3)|]
- prog `succeedsWith` Number 6
+ prog `succeedsWith` int 6
, testProperty "'>' works" $ \(x, y) -> do
let prog = [i|(> #{renderPrettyDef . asValue $ Number x} #{renderPrettyDef . asValue $ Number y})|]
@@ -385,7 +385,7 @@ test_eval =
(read-ref x)
|]
res = runTest' prog
- res @?= Right (Number 6)
+ res @?= Right (int 6)
, testCase "mutations to refs are persisted beyond a lambda's scope" $ do
let prog = [s|
@@ -409,8 +409,8 @@ test_eval =
(ref-incer foo)
(read-ref foo)
|]
- runTest' prog @?= Right (Number 1)
- runTest' prog2 @?= Right (Number 1)
+ runTest' prog @?= Right (int 1)
+ runTest' prog2 @?= Right (int 1)
, testCase "muliple refs mutated in multiple lambdas" $ do
let prog = [s|
@@ -429,7 +429,7 @@ test_eval =
(c1)
(list (c1) (c2))
|]
- runTest' prog @?= Right (List [Number 3, Number 1])
+ runTest' prog @?= Right (List [int 3, int 1])
, testProperty "read-ref . ref == id" $ \(v :: Value) -> do
let derefed = runTest' $ toS [i|(read-ref (ref #{renderPrettyDef v}))|]
@@ -442,16 +442,18 @@ test_eval =
runTest' "(show 'a)" @?= Right (String "a")
runTest' "(show ''a)" @?= Right (String "(quote a)")
runTest' "(show \"hello\")" @?= Right (String "\"hello\"")
- runTest' "(show 42)" @?= Right (String "42.0")
+ runTest' "(show 42)" @?= Right (String "42")
+ runTest' "(show 1/3)" @?= Right (String "1/3")
runTest' "(show #t)" @?= Right (String "#t")
runTest' "(show #f)" @?= Right (String "#f")
- runTest' "(show (list 'a 1 \"foo\" (list 'b ''x 2 \"bar\")))" @?= Right (String "(a 1.0 \"foo\" (b (quote x) 2.0 \"bar\"))")
+ runTest' "(show (list 'a 1 \"foo\" (list 'b ''x 2 \"bar\")))" @?= Right (String "(a 1 \"foo\" (b (quote x) 2 \"bar\"))")
+ runTest' "(show [1 :a])" @?= Right (String "[1 :a]")
runTest' "eval" @?= Right (PrimFn [ident|base-eval|])
- runTest' "(show (dict 'a 1))" @?= Right (String "{a 1.0}")
+ runTest' "(show (dict 'a 1))" @?= Right (String "{a 1}")
runTest' "(show (fn [x] x))" @?= Right (String "(fn [x] x)")
, testCase "'read' works" $
- runTest' "(read \"(:hello 42)\")" @?= Right (List [Keyword [ident|hello|], Number 42])
+ runTest' "(read \"(:hello 42)\")" @?= Right (List [Keyword [ident|hello|], int 42])
, testCase "'to-json' works" $ do
runTest' "(to-json (dict \"foo\" #t))" @?= Right (String "{\"foo\":true}")
@@ -467,7 +469,7 @@ test_eval =
(+ n (triangular (- n 1))))))
(triangular 10)
|]
- runTest' prog @?= Right (Number 55)
+ runTest' prog @?= Right (int 55)
, testCase "def-rec errors when defining a non-function" $
failsWith "(def-rec x 42)" (OtherError "def-rec can only be used to define functions")
@@ -532,7 +534,7 @@ test_parser =
"(++ \"merge\" \"d\")" ~~> List [Atom [ident|++|], String "merge", String "d"]
, testCase "parses numbers" $ do
- "0.15" ~~> Number 0.15
+ "1/3" ~~> Number (1 % 3)
"2000" ~~> Number 2000
, testCase "parses identifiers" $ do
@@ -543,7 +545,7 @@ test_parser =
"evaluate" ~~> Atom [ident|evaluate|]
, testCase "parses dicts" $ do
- "{:foo 3}" ~~> Dict (Map.singleton (Keyword [ident|foo|]) (Number 3))
+ "{:foo 3}" ~~> Dict (Map.singleton (Keyword [ident|foo|]) (int 3))
, testCase "a dict litteral with an odd number of forms is a syntax error" $
assertBool "succesfully parsed a dict with 3 forms" (isLeft $ parseTest "{:foo 3 4}")
@@ -575,16 +577,16 @@ test_pretty =
r @?= "(fn arg1 arg2)"
, testCase "dicts try to fit on one line" $ do
- let r = renderPretty (apl 80) (asValue (Dict $ Map.fromList [ (kw "k1", Number 1)
- , (kw "k2", Number 2)
+ let r = renderPretty (apl 80) (asValue (Dict $ Map.fromList [ (kw "k1", int 1)
+ , (kw "k2", int 2)
]))
- r @?= "{:k1 1.0 :k2 2.0}"
+ r @?= "{:k1 1 :k2 2}"
, testCase "dicts split with key-val pairs" $ do
- let r = renderPretty (apl 5) (asValue (Dict $ Map.fromList [ (kw "k1", Number 1)
- , (kw "k2", Number 2)
+ let r = renderPretty (apl 5) (asValue (Dict $ Map.fromList [ (kw "k1", int 1)
+ , (kw "k2", int 2)
]))
- r @?= "{:k1 1.0\n :k2 2.0}"
+ r @?= "{:k1 1\n :k2 2}"
, testProperty "read . pprint == identity for strings" $ \(t :: Text) ->
let v = asValue (String t)
pp = renderPrettyDef v
@@ -624,7 +626,7 @@ test_repl_primops =
(+ foo bar)
|]
files = Map.singleton "foo.rad" "(def foo 42) (def bar 8)"
- runFiles files prog @?= Right (Number 50)
+ runFiles files prog @?= Right (int 50)
, testCase "generating and verifying cryptographic signatures works" $
let prog = [s|
(def my-keys (gen-key-pair! (default-ecc-curve)))
@@ -795,7 +797,7 @@ test_macros =
, ":quit"
, "x"
]
- output = [ "0.0" ]
+ output = [ "0" ]
(_, result) <- runInRepl input
result @==> output
]
@@ -813,6 +815,9 @@ kw = Keyword . unsafeToIdent
atom :: Text -> Value
atom = Atom . unsafeToIdent
+int :: Integer -> Value
+int = Number . fromIntegral
+
-- -- | Like 'parse', but uses "(test)" as the source name and the default set of
-- -- primops.
parseTest :: MonadError Text m => Text -> m Value