{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE GADTs #-}
module Swarm.Language.Value (
Value (..),
stripVResult,
prettyValue,
valueToTerm,
Env,
) where
import Data.Aeson (FromJSON, ToJSON)
import Data.Bool (bool)
import Data.List (foldl')
import Data.Map qualified as M
import Data.Set qualified as S
import Data.Set.Lens (setOf)
import Data.Text (Text)
import GHC.Generics (Generic)
import Swarm.Language.Context
import Swarm.Language.Pretty (prettyText)
import Swarm.Language.Syntax
import Prelude
data Value where
VUnit :: Value
VInt :: Integer -> Value
VText :: Text -> Value
VDir :: Direction -> Value
VBool :: Bool -> Value
VRobot :: Int -> Value
VInj :: Bool -> Value -> Value
VPair :: Value -> Value -> Value
VClo :: Var -> Term -> Env -> Value
VCApp :: Const -> [Value] -> Value
VDef :: Bool -> Var -> Term -> Env -> Value
VResult :: Value -> Env -> Value
VBind :: Maybe Var -> Term -> Term -> Env -> Value
VDelay :: Term -> Env -> Value
VRef :: Int -> Value
deriving (Value -> Value -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Value -> Value -> Bool
$c/= :: Value -> Value -> Bool
== :: Value -> Value -> Bool
$c== :: Value -> Value -> Bool
Eq, Int -> Value -> ShowS
[Value] -> ShowS
Value -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Value] -> ShowS
$cshowList :: [Value] -> ShowS
show :: Value -> String
$cshow :: Value -> String
showsPrec :: Int -> Value -> ShowS
$cshowsPrec :: Int -> Value -> ShowS
Show, forall x. Rep Value x -> Value
forall x. Value -> Rep Value x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Value x -> Value
$cfrom :: forall x. Value -> Rep Value x
Generic, Value -> Parser [Value]
Value -> Parser Value
forall a.
(Value -> Parser a) -> (Value -> Parser [a]) -> FromJSON a
parseJSONList :: Value -> Parser [Value]
$cparseJSONList :: Value -> Parser [Value]
parseJSON :: Value -> Parser Value
$cparseJSON :: Value -> Parser Value
FromJSON, [Value] -> Encoding
[Value] -> Value
Value -> Encoding
Value -> Value
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
toEncodingList :: [Value] -> Encoding
$ctoEncodingList :: [Value] -> Encoding
toJSONList :: [Value] -> Value
$ctoJSONList :: [Value] -> Value
toEncoding :: Value -> Encoding
$ctoEncoding :: Value -> Encoding
toJSON :: Value -> Value
$ctoJSON :: Value -> Value
ToJSON)
stripVResult :: Value -> Value
stripVResult :: Value -> Value
stripVResult (VResult Value
v Env
_) = Value -> Value
stripVResult Value
v
stripVResult Value
v = Value
v
prettyValue :: Value -> Text
prettyValue :: Value -> Text
prettyValue = forall a. PrettyPrec a => a -> Text
prettyText forall b c a. (b -> c) -> (a -> b) -> a -> c
. Value -> Term
valueToTerm
valueToTerm :: Value -> Term
valueToTerm :: Value -> Term
valueToTerm Value
VUnit = forall ty. Term' ty
TUnit
valueToTerm (VInt Integer
n) = forall ty. Integer -> Term' ty
TInt Integer
n
valueToTerm (VText Text
s) = forall ty. Text -> Term' ty
TText Text
s
valueToTerm (VDir Direction
d) = forall ty. Direction -> Term' ty
TDir Direction
d
valueToTerm (VBool Bool
b) = forall ty. Bool -> Term' ty
TBool Bool
b
valueToTerm (VRobot Int
r) = forall ty. Int -> Term' ty
TRobot Int
r
valueToTerm (VInj Bool
s Value
v) = Term -> Term -> Term
TApp (forall ty. Const -> Term' ty
TConst (forall a. a -> a -> Bool -> a
bool Const
Inl Const
Inr Bool
s)) (Value -> Term
valueToTerm Value
v)
valueToTerm (VPair Value
v1 Value
v2) = Term -> Term -> Term
TPair (Value -> Term
valueToTerm Value
v1) (Value -> Term
valueToTerm Value
v2)
valueToTerm (VClo Text
x Term
t Env
e) =
forall k a b. (k -> a -> b -> b) -> b -> Map k a -> b
M.foldrWithKey
(\Text
y Value
v -> Bool -> Text -> Maybe Polytype -> Term -> Term -> Term
TLet Bool
False Text
y forall a. Maybe a
Nothing (Value -> Term
valueToTerm Value
v))
(Text -> Maybe Type -> Term -> Term
TLam Text
x forall a. Maybe a
Nothing Term
t)
(forall k a. Ord k => Map k a -> Set k -> Map k a
M.restrictKeys (forall t. Ctx t -> Map Text t
unCtx Env
e) (forall a. Ord a => a -> Set a -> Set a
S.delete Text
x (forall a s. Getting (Set a) s a -> s -> Set a
setOf forall ty. Traversal' (Syntax' ty) Text
freeVarsV (forall ty. SrcLoc -> Term' ty -> ty -> Syntax' ty
Syntax' SrcLoc
NoLoc Term
t ()))))
valueToTerm (VCApp Const
c [Value]
vs) = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' Term -> Term -> Term
TApp (forall ty. Const -> Term' ty
TConst Const
c) (forall a. [a] -> [a]
reverse (forall a b. (a -> b) -> [a] -> [b]
map Value -> Term
valueToTerm [Value]
vs))
valueToTerm (VDef Bool
r Text
x Term
t Env
_) = Bool -> Text -> Maybe Polytype -> Term -> Term
TDef Bool
r Text
x forall a. Maybe a
Nothing Term
t
valueToTerm (VResult Value
v Env
_) = Value -> Term
valueToTerm Value
v
valueToTerm (VBind Maybe Text
mx Term
c1 Term
c2 Env
_) = Maybe Text -> Term -> Term -> Term
TBind Maybe Text
mx Term
c1 Term
c2
valueToTerm (VDelay Term
t Env
_) = DelayType -> Term -> Term
TDelay DelayType
SimpleDelay Term
t
valueToTerm (VRef Int
n) = forall ty. Int -> Term' ty
TRef Int
n
type Env = Ctx Value