Skip to content

Commit

Permalink
refactoring: Int64 -> Int
Browse files Browse the repository at this point in the history
There is really no point in being anal about the 64-bitness of
integers here, in t. Switching makes interfacing with other
libraries nicier as well.
  • Loading branch information
supki committed Oct 7, 2024
1 parent ecff98c commit 057fee8
Show file tree
Hide file tree
Showing 11 changed files with 25 additions and 35 deletions.
11 changes: 0 additions & 11 deletions src/T/Embed.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ instance Embed Bool where
embed _name = Bool

instance Embed Int where
embed _name =
Int . fromIntegral

instance Embed Int64 where
embed _name = Int

instance Embed Double where
Expand Down Expand Up @@ -80,13 +76,6 @@ instance Eject Bool where
Left (TypeError (varE name) Type.Bool (typeOf value) (sexp value))

instance Eject Int where
eject name = \case
Int n ->
pure (fromIntegral n)
value ->
Left (TypeError (varE name) Type.Int (typeOf value) (sexp value))

instance Eject Int64 where
eject name = \case
Int n ->
pure n
Expand Down
4 changes: 2 additions & 2 deletions src/T/Exp.hs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ true :: Exp
true =
litE_ (Bool True)

int :: Int64 -> Exp
int :: Int -> Exp
int =
litE_ . Int

Expand Down Expand Up @@ -159,7 +159,7 @@ keyE_ exp key =
data Literal
= Null
| Bool Bool
| Int Int64
| Int Int
| Double Double
| String Text
| Regexp Pcre.Regex
Expand Down
3 changes: 3 additions & 0 deletions src/T/Parse.hs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,9 @@ litP = do
, Bool True <$ symbol "true"
]
numberP =
-- here, `fromIntegral` is slightly problematic because it can
-- truncate (enormous) Integer values nonsensically, e.g. to 0
-- for large enough Integers.
map (either (Int . fromIntegral) Double) integerOrDouble
stringP =
map String stringLiteral
Expand Down
2 changes: 0 additions & 2 deletions src/T/Prelude.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ module T.Prelude
, FilePath
, HashMap
, Int
, Int64
, IO
, Maybe(..)
, NonEmpty(..)
Expand Down Expand Up @@ -95,7 +94,6 @@ import Data.Foldable
import Data.Functor.Classes (Eq1(..), eq1)
import Data.HashMap.Strict (HashMap)
import Data.Hashable (Hashable)
import Data.Int (Int64)
import Data.List (foldl')
import Data.List.NonEmpty (NonEmpty(..))
import Data.Maybe (mapMaybe)
Expand Down
16 changes: 8 additions & 8 deletions src/T/Render.hs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ evalExp = \case
_ :< Idx exp expIdx -> do
xs <- enforceArray exp
idx <- enforceInt expIdx
case xs !? fromIntegral idx of
case xs !? idx of
Nothing ->
throwError (OutOfBounds expIdx (sexp xs) (sexp idx))
Just x ->
Expand All @@ -227,7 +227,7 @@ evalExp = \case
Just x ->
pure x

enforceInt :: (Ctx m, MonadError Error m) => Exp -> m Int64
enforceInt :: (Ctx m, MonadError Error m) => Exp -> m Int
enforceInt exp = do
v <- evalExp exp
case v of
Expand Down Expand Up @@ -282,7 +282,7 @@ data Path = Path

data Lookup
= K (Ann :+ Name)
| I (Ann :+ Int64)
| I (Ann :+ Int)
deriving (Show, Eq)

topLevel :: Ann :+ Name -> Path
Expand Down Expand Up @@ -311,7 +311,7 @@ evalLValue =
let
path =
path0 {lookups = I (idxAnn :+ idx) : path0.lookups}
pure $ case xs !? fromIntegral idx of
pure $ case xs !? idx of
Nothing -> do
let
key =
Expand Down Expand Up @@ -399,10 +399,10 @@ insertVar Path {var = (ann :+ name), lookups} v = do
go (Value.Array xs) (I (ann0 :+ idx) : path) =
-- this is pretty similar to records except the lack of the aforementioned special
-- treatment.
case xs !? fromIntegral idx of
case xs !? idx of
Just v1 -> do
v2 <- go v1 path
pure (Value.Array (xs // [(fromIntegral idx, v2)]))
pure (Value.Array (xs // [(idx, v2)]))
Nothing ->
throwError (OutOfBounds (ann0 :< Lit Null) (sexp xs) (sexp idx))
go v0 (I (ann0 :+ _idx) : _path) =
Expand Down Expand Up @@ -432,8 +432,8 @@ build chunk =
loopRecord :: Maybe Text -> Int -> Int -> Value
loopRecord key len idx =
Value.Record $ HashMap.fromList
[ ("length", Value.Int (fromIntegral len))
, ("index", Value.Int (fromIntegral idx))
[ ("length", Value.Int len)
, ("index", Value.Int idx)
, ("first", Value.Bool (idx == 0))
, ("last", Value.Bool (idx == len - 1))
, ("key", maybe Value.Null Value.String key)
Expand Down
2 changes: 1 addition & 1 deletion src/T/SExp.hs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ instance To Bool where
False -> "false"
True -> "true"

instance To Int64 where
instance To Int where
sexp =
Var . fromString . show

Expand Down
8 changes: 4 additions & 4 deletions src/T/Stdlib/Fun.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ functions =
[ Fun "empty?" nullB
, Fun "length" lengthB

, Fun "floor" (flip embed0 (floor @Double @Int64))
, Fun "ceiling" (flip embed0 (ceiling @Double @Int64))
, Fun "round" (flip embed0 (round @Double @Int64))
, Fun "int->double" (flip embed0 (fromIntegral @Int64 @Double))
, Fun "floor" (flip embed0 (floor @Double @Int))
, Fun "ceiling" (flip embed0 (ceiling @Double @Int))
, Fun "round" (flip embed0 (round @Double @Int))
, Fun "int->double" (flip embed0 (fromIntegral @Int @Double))

, Fun "upper-case" (flip embed0 Text.toUpper)
, Fun "lower-case" (flip embed0 Text.toLower)
Expand Down
4 changes: 2 additions & 2 deletions src/T/Stdlib/Op.hs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ operators =
, Op "<>" (flip embed0 ((<>) @Text)) Infixr 6
]

combineNumbers :: (Int64 -> Int64 -> Int64) -> (Double -> Double -> Double) -> Name -> Value
combineNumbers :: (Int -> Int -> Int) -> (Double -> Double -> Double) -> Name -> Value
combineNumbers intOp doubleOp name =
Lam $ \case
_ann :+ Int n0 ->
Expand Down Expand Up @@ -108,7 +108,7 @@ divide :: Name -> Value
divide =
combineNumbers div (/)

predicateNumbers :: (Int64 -> Int64 -> Bool) -> (Double -> Double -> Bool) -> Name -> Value
predicateNumbers :: (Int -> Int -> Bool) -> (Double -> Double -> Bool) -> Name -> Value
predicateNumbers intOp doubleOp name =
Lam $ \case
_ann :+ Int n0 ->
Expand Down
2 changes: 1 addition & 1 deletion src/T/Value.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import T.Type qualified as Type
data Value
= Null
| Bool Bool
| Int Int64
| Int Int
| Double Double
| String Text
| Regexp Pcre.Regex
Expand Down
2 changes: 1 addition & 1 deletion test/T/ParseSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ true :: Exp
true =
litE_ (Bool True)

int :: Int64 -> Exp
int :: Int -> Exp
int =
litE_ . Int

Expand Down
6 changes: 3 additions & 3 deletions test/T/SExpSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ spec = do

it "int" $ do
render (sexp (Value.Int 42)) `shouldBe` "42"
render (sexp (42 :: Int64)) `shouldBe` "42"
render (sexp (42 :: Int)) `shouldBe` "42"

it "double" $ do
render (sexp (Value.Double 4.2)) `shouldBe` "4.2"
Expand All @@ -123,12 +123,12 @@ spec = do

it "array" $ do
render (sexp (Value.Array [Value.Int 1, Value.Int 2, Value.Int 3])) `shouldBe` "[1 2 3]"
render (sexp ([1, 2, 3] :: [Int64])) `shouldBe` "[1 2 3]"
render (sexp ([1, 2, 3] :: [Int])) `shouldBe` "[1 2 3]"

it "record" $ do
render (sexp (Value.Record [("foo", Value.Int 4), ("bar", Value.Int 7)])) `shouldBe`
"{\"bar\" 7 \"foo\" 4}"
render (sexp ([("foo", 4), ("bar", 7)] :: HashMap Text Int64)) `shouldBe`
render (sexp ([("foo", 4), ("bar", 7)] :: HashMap Text Int)) `shouldBe`
"{\"bar\" 7 \"foo\" 4}"

it "lam" $
Expand Down

0 comments on commit 057fee8

Please sign in to comment.