{-# LANGUAGE BlockArguments      #-}
{-# LANGUAGE ImportQualifiedPost #-}
{-# LANGUAGE OverloadedStrings   #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications    #-}
{-# LANGUAGE ViewPatterns        #-}

module Dovetail.Core.Data.Number where

import Data.Foldable (fold)
import Data.Text (Text)
import Dovetail
import Dovetail.Evaluate (builtIn)

env :: forall ctx. Env ctx
env :: Env ctx
env = do
  let _ModuleName :: ModuleName
_ModuleName = Text -> ModuleName
ModuleName Text
"Data.Number"

  [Env ctx] -> Env ctx
forall (t :: * -> *) m. (Foldable t, Monoid m) => t m -> m
fold
    [ -- nan :: Number
      ModuleName -> Text -> Double -> Env ctx
forall ctx a. ToValue ctx a => ModuleName -> Text -> a -> Env ctx
builtIn @ctx @Double
        ModuleName
_ModuleName Text
"nan"
        (Double
0 Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
0)
    -- isNaN :: Number -> Boolean
    , ModuleName -> Text -> (Double -> Eval ctx Bool) -> Env ctx
forall ctx a. ToValue ctx a => ModuleName -> Text -> a -> Env ctx
builtIn @ctx @(Double -> Eval ctx Bool)
        ModuleName
_ModuleName Text
"isNaN"
        \Double
a -> 
          Bool -> Eval ctx Bool
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Double -> Bool
forall a. RealFloat a => a -> Bool
isNaN Double
a)
    -- infinity :: Number
    , ModuleName -> Text -> Double -> Env ctx
forall ctx a. ToValue ctx a => ModuleName -> Text -> a -> Env ctx
builtIn @ctx @Double
        ModuleName
_ModuleName Text
"infinity"
        (Double -> Double
forall a. Fractional a => a -> a
recip Double
0)
    -- isFinite :: Number -> Boolean
    , ModuleName -> Text -> (Double -> Eval ctx Bool) -> Env ctx
forall ctx a. ToValue ctx a => ModuleName -> Text -> a -> Env ctx
builtIn @ctx @(Double -> Eval ctx Bool)
        ModuleName
_ModuleName Text
"isFinite"
        \Double
a -> 
          Bool -> Eval ctx Bool
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Bool -> Bool
not (Double -> Bool
forall a. RealFloat a => a -> Bool
isInfinite Double
a))
    -- fromStringImpl :: Fn4 String (Number -> Boolean) (forall a. a -> Maybe a) (forall a. Maybe a) (Maybe Number)
    , ModuleName
-> Text
-> (Text
    -> (Double -> Eval ctx Bool)
    -> (Double -> Eval ctx (Value ctx))
    -> Value ctx
    -> Eval ctx (Value ctx))
-> Env ctx
forall ctx a. ToValue ctx a => ModuleName -> Text -> a -> Env ctx
builtIn @ctx @(Text -> (Double -> Eval ctx Bool) -> (Double -> Eval ctx (Value ctx)) -> Value ctx -> Eval ctx (Value ctx))
        ModuleName
_ModuleName Text
"fromStringImpl"
        \Text
_s Double -> Eval ctx Bool
_isFinite Double -> Eval ctx (Value ctx)
_just Value ctx
_nothing ->
          EvaluationErrorType ctx -> Eval ctx (Value ctx)
forall x (m :: * -> *) a.
(MonadError (EvaluationError x) m,
 MonadReader (EvaluationContext x) m) =>
EvaluationErrorType x -> m a
throwErrorWithContext (Text -> EvaluationErrorType ctx
forall ctx. Text -> EvaluationErrorType ctx
OtherError Text
"fromStringImpl is not implemented")
    ]