{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE OverloadedStrings #-}
module Cek.Dhall
(
DhallExpr(..)
, DhallVal(..)
, DhallEnv(..)
, emptyDhallEnv
, extendDhallEnv
, lookupDhallEnv
, DhallFrame(..)
, DhallCont
, emptyDhallCont
, DhallState(..)
, stepDhallCEK
, evalDhallCEK
) where
import Data.Text (Text)
import Data.Word (Word64)
import GHC.Generics (Generic)
data DhallExpr
= DVar !Text
| DLam !Text !DhallExpr
| DApp !DhallExpr !DhallExpr
| DLet !Text !DhallExpr !DhallExpr
| DNat !Word64
| DInt !Integer
| DBool !Bool
| DText !Text
| DRecord ![(Text, DhallExpr)]
| DField !DhallExpr !Text
| DList ![DhallExpr]
| DOpPlus !DhallExpr !DhallExpr
| DOpTextAppend !DhallExpr !DhallExpr
deriving stock (DhallExpr -> DhallExpr -> Bool
(DhallExpr -> DhallExpr -> Bool)
-> (DhallExpr -> DhallExpr -> Bool) -> Eq DhallExpr
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: DhallExpr -> DhallExpr -> Bool
== :: DhallExpr -> DhallExpr -> Bool
$c/= :: DhallExpr -> DhallExpr -> Bool
/= :: DhallExpr -> DhallExpr -> Bool
Eq, Int -> DhallExpr -> ShowS
[DhallExpr] -> ShowS
DhallExpr -> String
(Int -> DhallExpr -> ShowS)
-> (DhallExpr -> String)
-> ([DhallExpr] -> ShowS)
-> Show DhallExpr
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> DhallExpr -> ShowS
showsPrec :: Int -> DhallExpr -> ShowS
$cshow :: DhallExpr -> String
show :: DhallExpr -> String
$cshowList :: [DhallExpr] -> ShowS
showList :: [DhallExpr] -> ShowS
Show, (forall x. DhallExpr -> Rep DhallExpr x)
-> (forall x. Rep DhallExpr x -> DhallExpr) -> Generic DhallExpr
forall x. Rep DhallExpr x -> DhallExpr
forall x. DhallExpr -> Rep DhallExpr x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. DhallExpr -> Rep DhallExpr x
from :: forall x. DhallExpr -> Rep DhallExpr x
$cto :: forall x. Rep DhallExpr x -> DhallExpr
to :: forall x. Rep DhallExpr x -> DhallExpr
Generic)
data DhallVal
= VNat !Word64
| VInt !Integer
| VBool !Bool
| VText !Text
| VClosure !Text !DhallExpr !DhallEnv
| VRecord ![(Text, DhallVal)]
| VList ![DhallVal]
deriving stock (DhallVal -> DhallVal -> Bool
(DhallVal -> DhallVal -> Bool)
-> (DhallVal -> DhallVal -> Bool) -> Eq DhallVal
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: DhallVal -> DhallVal -> Bool
== :: DhallVal -> DhallVal -> Bool
$c/= :: DhallVal -> DhallVal -> Bool
/= :: DhallVal -> DhallVal -> Bool
Eq, Int -> DhallVal -> ShowS
[DhallVal] -> ShowS
DhallVal -> String
(Int -> DhallVal -> ShowS)
-> (DhallVal -> String) -> ([DhallVal] -> ShowS) -> Show DhallVal
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> DhallVal -> ShowS
showsPrec :: Int -> DhallVal -> ShowS
$cshow :: DhallVal -> String
show :: DhallVal -> String
$cshowList :: [DhallVal] -> ShowS
showList :: [DhallVal] -> ShowS
Show, (forall x. DhallVal -> Rep DhallVal x)
-> (forall x. Rep DhallVal x -> DhallVal) -> Generic DhallVal
forall x. Rep DhallVal x -> DhallVal
forall x. DhallVal -> Rep DhallVal x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. DhallVal -> Rep DhallVal x
from :: forall x. DhallVal -> Rep DhallVal x
$cto :: forall x. Rep DhallVal x -> DhallVal
to :: forall x. Rep DhallVal x -> DhallVal
Generic)
newtype DhallEnv = DhallEnv { DhallEnv -> [(Text, DhallVal)]
unDhallEnv :: [(Text, DhallVal)] }
deriving stock (DhallEnv -> DhallEnv -> Bool
(DhallEnv -> DhallEnv -> Bool)
-> (DhallEnv -> DhallEnv -> Bool) -> Eq DhallEnv
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: DhallEnv -> DhallEnv -> Bool
== :: DhallEnv -> DhallEnv -> Bool
$c/= :: DhallEnv -> DhallEnv -> Bool
/= :: DhallEnv -> DhallEnv -> Bool
Eq, Int -> DhallEnv -> ShowS
[DhallEnv] -> ShowS
DhallEnv -> String
(Int -> DhallEnv -> ShowS)
-> (DhallEnv -> String) -> ([DhallEnv] -> ShowS) -> Show DhallEnv
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> DhallEnv -> ShowS
showsPrec :: Int -> DhallEnv -> ShowS
$cshow :: DhallEnv -> String
show :: DhallEnv -> String
$cshowList :: [DhallEnv] -> ShowS
showList :: [DhallEnv] -> ShowS
Show, (forall x. DhallEnv -> Rep DhallEnv x)
-> (forall x. Rep DhallEnv x -> DhallEnv) -> Generic DhallEnv
forall x. Rep DhallEnv x -> DhallEnv
forall x. DhallEnv -> Rep DhallEnv x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. DhallEnv -> Rep DhallEnv x
from :: forall x. DhallEnv -> Rep DhallEnv x
$cto :: forall x. Rep DhallEnv x -> DhallEnv
to :: forall x. Rep DhallEnv x -> DhallEnv
Generic)
emptyDhallEnv :: DhallEnv
emptyDhallEnv :: DhallEnv
emptyDhallEnv = [(Text, DhallVal)] -> DhallEnv
DhallEnv []
extendDhallEnv :: Text -> DhallVal -> DhallEnv -> DhallEnv
extendDhallEnv :: Text -> DhallVal -> DhallEnv -> DhallEnv
extendDhallEnv !Text
x !DhallVal
v (DhallEnv [(Text, DhallVal)]
bs) = [(Text, DhallVal)] -> DhallEnv
DhallEnv ((Text
x, DhallVal
v) (Text, DhallVal) -> [(Text, DhallVal)] -> [(Text, DhallVal)]
forall a. a -> [a] -> [a]
: [(Text, DhallVal)]
bs)
lookupDhallEnv :: Text -> DhallEnv -> Maybe DhallVal
lookupDhallEnv :: Text -> DhallEnv -> Maybe DhallVal
lookupDhallEnv !Text
x (DhallEnv [(Text, DhallVal)]
bs) = Text -> [(Text, DhallVal)] -> Maybe DhallVal
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup Text
x [(Text, DhallVal)]
bs
data DhallFrame
= DFrameArg !DhallExpr !DhallEnv
| DFrameFun !DhallVal
| DFrameLet !Text !DhallExpr !DhallEnv
| DFrameField !Text
| DFrameOpPlusR !DhallExpr !DhallEnv
| DFrameOpPlusL !Word64
| DFrameOpTextR !DhallExpr !DhallEnv
| DFrameOpTextL !Text
deriving stock (DhallFrame -> DhallFrame -> Bool
(DhallFrame -> DhallFrame -> Bool)
-> (DhallFrame -> DhallFrame -> Bool) -> Eq DhallFrame
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: DhallFrame -> DhallFrame -> Bool
== :: DhallFrame -> DhallFrame -> Bool
$c/= :: DhallFrame -> DhallFrame -> Bool
/= :: DhallFrame -> DhallFrame -> Bool
Eq, Int -> DhallFrame -> ShowS
[DhallFrame] -> ShowS
DhallFrame -> String
(Int -> DhallFrame -> ShowS)
-> (DhallFrame -> String)
-> ([DhallFrame] -> ShowS)
-> Show DhallFrame
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> DhallFrame -> ShowS
showsPrec :: Int -> DhallFrame -> ShowS
$cshow :: DhallFrame -> String
show :: DhallFrame -> String
$cshowList :: [DhallFrame] -> ShowS
showList :: [DhallFrame] -> ShowS
Show, (forall x. DhallFrame -> Rep DhallFrame x)
-> (forall x. Rep DhallFrame x -> DhallFrame) -> Generic DhallFrame
forall x. Rep DhallFrame x -> DhallFrame
forall x. DhallFrame -> Rep DhallFrame x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. DhallFrame -> Rep DhallFrame x
from :: forall x. DhallFrame -> Rep DhallFrame x
$cto :: forall x. Rep DhallFrame x -> DhallFrame
to :: forall x. Rep DhallFrame x -> DhallFrame
Generic)
type DhallCont = [DhallFrame]
emptyDhallCont :: DhallCont
emptyDhallCont :: [DhallFrame]
emptyDhallCont = []
data DhallState
= DStateEval !DhallExpr !DhallEnv !DhallCont
| DStateReturn !DhallVal !DhallCont
deriving stock (DhallState -> DhallState -> Bool
(DhallState -> DhallState -> Bool)
-> (DhallState -> DhallState -> Bool) -> Eq DhallState
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: DhallState -> DhallState -> Bool
== :: DhallState -> DhallState -> Bool
$c/= :: DhallState -> DhallState -> Bool
/= :: DhallState -> DhallState -> Bool
Eq, Int -> DhallState -> ShowS
[DhallState] -> ShowS
DhallState -> String
(Int -> DhallState -> ShowS)
-> (DhallState -> String)
-> ([DhallState] -> ShowS)
-> Show DhallState
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> DhallState -> ShowS
showsPrec :: Int -> DhallState -> ShowS
$cshow :: DhallState -> String
show :: DhallState -> String
$cshowList :: [DhallState] -> ShowS
showList :: [DhallState] -> ShowS
Show, (forall x. DhallState -> Rep DhallState x)
-> (forall x. Rep DhallState x -> DhallState) -> Generic DhallState
forall x. Rep DhallState x -> DhallState
forall x. DhallState -> Rep DhallState x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. DhallState -> Rep DhallState x
from :: forall x. DhallState -> Rep DhallState x
$cto :: forall x. Rep DhallState x -> DhallState
to :: forall x. Rep DhallState x -> DhallState
Generic)
stepDhallCEK :: DhallState -> Either Text (Maybe DhallState)
stepDhallCEK :: DhallState -> Either Text (Maybe DhallState)
stepDhallCEK (DStateEval DhallExpr
expr DhallEnv
env [DhallFrame]
cont) = case DhallExpr
expr of
DVar Text
x -> case Text -> DhallEnv -> Maybe DhallVal
lookupDhallEnv Text
x DhallEnv
env of
Just DhallVal
val -> Maybe DhallState -> Either Text (Maybe DhallState)
forall a. a -> Either Text a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (DhallState -> Maybe DhallState
forall a. a -> Maybe a
Just (DhallVal -> [DhallFrame] -> DhallState
DStateReturn DhallVal
val [DhallFrame]
cont))
Maybe DhallVal
Nothing -> Text -> Either Text (Maybe DhallState)
forall a b. a -> Either a b
Left (Text
"Unbound variable in Dhall expression: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
x)
DLam Text
x DhallExpr
body ->
Maybe DhallState -> Either Text (Maybe DhallState)
forall a. a -> Either Text a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (DhallState -> Maybe DhallState
forall a. a -> Maybe a
Just (DhallVal -> [DhallFrame] -> DhallState
DStateReturn (Text -> DhallExpr -> DhallEnv -> DhallVal
VClosure Text
x DhallExpr
body DhallEnv
env) [DhallFrame]
cont))
DApp DhallExpr
e1 DhallExpr
e2 ->
Maybe DhallState -> Either Text (Maybe DhallState)
forall a. a -> Either Text a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (DhallState -> Maybe DhallState
forall a. a -> Maybe a
Just (DhallExpr -> DhallEnv -> [DhallFrame] -> DhallState
DStateEval DhallExpr
e1 DhallEnv
env (DhallExpr -> DhallEnv -> DhallFrame
DFrameArg DhallExpr
e2 DhallEnv
env DhallFrame -> [DhallFrame] -> [DhallFrame]
forall a. a -> [a] -> [a]
: [DhallFrame]
cont)))
DLet Text
x DhallExpr
e1 DhallExpr
e2 ->
Maybe DhallState -> Either Text (Maybe DhallState)
forall a. a -> Either Text a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (DhallState -> Maybe DhallState
forall a. a -> Maybe a
Just (DhallExpr -> DhallEnv -> [DhallFrame] -> DhallState
DStateEval DhallExpr
e1 DhallEnv
env (Text -> DhallExpr -> DhallEnv -> DhallFrame
DFrameLet Text
x DhallExpr
e2 DhallEnv
env DhallFrame -> [DhallFrame] -> [DhallFrame]
forall a. a -> [a] -> [a]
: [DhallFrame]
cont)))
DNat Word64
n ->
Maybe DhallState -> Either Text (Maybe DhallState)
forall a. a -> Either Text a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (DhallState -> Maybe DhallState
forall a. a -> Maybe a
Just (DhallVal -> [DhallFrame] -> DhallState
DStateReturn (Word64 -> DhallVal
VNat Word64
n) [DhallFrame]
cont))
DInt Integer
i ->
Maybe DhallState -> Either Text (Maybe DhallState)
forall a. a -> Either Text a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (DhallState -> Maybe DhallState
forall a. a -> Maybe a
Just (DhallVal -> [DhallFrame] -> DhallState
DStateReturn (Integer -> DhallVal
VInt Integer
i) [DhallFrame]
cont))
DBool Bool
b ->
Maybe DhallState -> Either Text (Maybe DhallState)
forall a. a -> Either Text a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (DhallState -> Maybe DhallState
forall a. a -> Maybe a
Just (DhallVal -> [DhallFrame] -> DhallState
DStateReturn (Bool -> DhallVal
VBool Bool
b) [DhallFrame]
cont))
DText Text
t ->
Maybe DhallState -> Either Text (Maybe DhallState)
forall a. a -> Either Text a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (DhallState -> Maybe DhallState
forall a. a -> Maybe a
Just (DhallVal -> [DhallFrame] -> DhallState
DStateReturn (Text -> DhallVal
VText Text
t) [DhallFrame]
cont))
DRecord [(Text, DhallExpr)]
fields -> do
vals <- ((Text, DhallExpr) -> Either Text (Text, DhallVal))
-> [(Text, DhallExpr)] -> Either Text [(Text, DhallVal)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (\(Text
f, DhallExpr
e) -> DhallEnv -> DhallExpr -> Either Text DhallVal
evalDhallCEKWithEnv DhallEnv
env DhallExpr
e Either Text DhallVal
-> (DhallVal -> Either Text (Text, DhallVal))
-> Either Text (Text, DhallVal)
forall a b. Either Text a -> (a -> Either Text b) -> Either Text b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \DhallVal
v -> (Text, DhallVal) -> Either Text (Text, DhallVal)
forall a. a -> Either Text a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Text
f, DhallVal
v)) [(Text, DhallExpr)]
fields
pure (Just (DStateReturn (VRecord vals) cont))
DField DhallExpr
recExpr Text
fieldName ->
Maybe DhallState -> Either Text (Maybe DhallState)
forall a. a -> Either Text a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (DhallState -> Maybe DhallState
forall a. a -> Maybe a
Just (DhallExpr -> DhallEnv -> [DhallFrame] -> DhallState
DStateEval DhallExpr
recExpr DhallEnv
env (Text -> DhallFrame
DFrameField Text
fieldName DhallFrame -> [DhallFrame] -> [DhallFrame]
forall a. a -> [a] -> [a]
: [DhallFrame]
cont)))
DList [DhallExpr]
items -> do
vals <- (DhallExpr -> Either Text DhallVal)
-> [DhallExpr] -> Either Text [DhallVal]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (DhallEnv -> DhallExpr -> Either Text DhallVal
evalDhallCEKWithEnv DhallEnv
env) [DhallExpr]
items
pure (Just (DStateReturn (VList vals) cont))
DOpPlus DhallExpr
e1 DhallExpr
e2 ->
Maybe DhallState -> Either Text (Maybe DhallState)
forall a. a -> Either Text a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (DhallState -> Maybe DhallState
forall a. a -> Maybe a
Just (DhallExpr -> DhallEnv -> [DhallFrame] -> DhallState
DStateEval DhallExpr
e1 DhallEnv
env (DhallExpr -> DhallEnv -> DhallFrame
DFrameOpPlusR DhallExpr
e2 DhallEnv
env DhallFrame -> [DhallFrame] -> [DhallFrame]
forall a. a -> [a] -> [a]
: [DhallFrame]
cont)))
DOpTextAppend DhallExpr
e1 DhallExpr
e2 ->
Maybe DhallState -> Either Text (Maybe DhallState)
forall a. a -> Either Text a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (DhallState -> Maybe DhallState
forall a. a -> Maybe a
Just (DhallExpr -> DhallEnv -> [DhallFrame] -> DhallState
DStateEval DhallExpr
e1 DhallEnv
env (DhallExpr -> DhallEnv -> DhallFrame
DFrameOpTextR DhallExpr
e2 DhallEnv
env DhallFrame -> [DhallFrame] -> [DhallFrame]
forall a. a -> [a] -> [a]
: [DhallFrame]
cont)))
stepDhallCEK (DStateReturn DhallVal
val [DhallFrame]
cont) = case [DhallFrame]
cont of
[] -> Maybe DhallState -> Either Text (Maybe DhallState)
forall a. a -> Either Text a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe DhallState
forall a. Maybe a
Nothing
(DFrameArg DhallExpr
e2 DhallEnv
env : [DhallFrame]
rest) ->
Maybe DhallState -> Either Text (Maybe DhallState)
forall a. a -> Either Text a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (DhallState -> Maybe DhallState
forall a. a -> Maybe a
Just (DhallExpr -> DhallEnv -> [DhallFrame] -> DhallState
DStateEval DhallExpr
e2 DhallEnv
env (DhallVal -> DhallFrame
DFrameFun DhallVal
val DhallFrame -> [DhallFrame] -> [DhallFrame]
forall a. a -> [a] -> [a]
: [DhallFrame]
rest)))
(DFrameFun DhallVal
funVal : [DhallFrame]
rest) -> case DhallVal
funVal of
VClosure Text
x DhallExpr
body DhallEnv
closureEnv ->
let !newEnv :: DhallEnv
newEnv = Text -> DhallVal -> DhallEnv -> DhallEnv
extendDhallEnv Text
x DhallVal
val DhallEnv
closureEnv
in Maybe DhallState -> Either Text (Maybe DhallState)
forall a. a -> Either Text a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (DhallState -> Maybe DhallState
forall a. a -> Maybe a
Just (DhallExpr -> DhallEnv -> [DhallFrame] -> DhallState
DStateEval DhallExpr
body DhallEnv
newEnv [DhallFrame]
rest))
DhallVal
_ -> Text -> Either Text (Maybe DhallState)
forall a b. a -> Either a b
Left Text
"Type error: Attempted to apply non-function Dhall value"
(DFrameLet Text
x DhallExpr
e2 DhallEnv
env : [DhallFrame]
rest) ->
let !newEnv :: DhallEnv
newEnv = Text -> DhallVal -> DhallEnv -> DhallEnv
extendDhallEnv Text
x DhallVal
val DhallEnv
env
in Maybe DhallState -> Either Text (Maybe DhallState)
forall a. a -> Either Text a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (DhallState -> Maybe DhallState
forall a. a -> Maybe a
Just (DhallExpr -> DhallEnv -> [DhallFrame] -> DhallState
DStateEval DhallExpr
e2 DhallEnv
newEnv [DhallFrame]
rest))
(DFrameField Text
fieldName : [DhallFrame]
rest) -> case DhallVal
val of
VRecord [(Text, DhallVal)]
fields -> case Text -> [(Text, DhallVal)] -> Maybe DhallVal
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup Text
fieldName [(Text, DhallVal)]
fields of
Just DhallVal
fieldVal -> Maybe DhallState -> Either Text (Maybe DhallState)
forall a. a -> Either Text a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (DhallState -> Maybe DhallState
forall a. a -> Maybe a
Just (DhallVal -> [DhallFrame] -> DhallState
DStateReturn DhallVal
fieldVal [DhallFrame]
rest))
Maybe DhallVal
Nothing -> Text -> Either Text (Maybe DhallState)
forall a b. a -> Either a b
Left (Text
"Record field not found: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
fieldName)
DhallVal
_ -> Text -> Either Text (Maybe DhallState)
forall a b. a -> Either a b
Left Text
"Type error: Cannot project field from non-record Dhall value"
(DFrameOpPlusR DhallExpr
e2 DhallEnv
env : [DhallFrame]
rest) -> case DhallVal
val of
VNat Word64
n1 -> Maybe DhallState -> Either Text (Maybe DhallState)
forall a. a -> Either Text a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (DhallState -> Maybe DhallState
forall a. a -> Maybe a
Just (DhallExpr -> DhallEnv -> [DhallFrame] -> DhallState
DStateEval DhallExpr
e2 DhallEnv
env (Word64 -> DhallFrame
DFrameOpPlusL Word64
n1 DhallFrame -> [DhallFrame] -> [DhallFrame]
forall a. a -> [a] -> [a]
: [DhallFrame]
rest)))
DhallVal
_ -> Text -> Either Text (Maybe DhallState)
forall a b. a -> Either a b
Left Text
"Type error: Left operand of + must be a Natural"
(DFrameOpPlusL Word64
n1 : [DhallFrame]
rest) -> case DhallVal
val of
VNat Word64
n2 -> Maybe DhallState -> Either Text (Maybe DhallState)
forall a. a -> Either Text a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (DhallState -> Maybe DhallState
forall a. a -> Maybe a
Just (DhallVal -> [DhallFrame] -> DhallState
DStateReturn (Word64 -> DhallVal
VNat (Word64
n1 Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
+ Word64
n2)) [DhallFrame]
rest))
DhallVal
_ -> Text -> Either Text (Maybe DhallState)
forall a b. a -> Either a b
Left Text
"Type error: Right operand of + must be a Natural"
(DFrameOpTextR DhallExpr
e2 DhallEnv
env : [DhallFrame]
rest) -> case DhallVal
val of
VText Text
t1 -> Maybe DhallState -> Either Text (Maybe DhallState)
forall a. a -> Either Text a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (DhallState -> Maybe DhallState
forall a. a -> Maybe a
Just (DhallExpr -> DhallEnv -> [DhallFrame] -> DhallState
DStateEval DhallExpr
e2 DhallEnv
env (Text -> DhallFrame
DFrameOpTextL Text
t1 DhallFrame -> [DhallFrame] -> [DhallFrame]
forall a. a -> [a] -> [a]
: [DhallFrame]
rest)))
DhallVal
_ -> Text -> Either Text (Maybe DhallState)
forall a b. a -> Either a b
Left Text
"Type error: Left operand of ++ must be Text"
(DFrameOpTextL Text
t1 : [DhallFrame]
rest) -> case DhallVal
val of
VText Text
t2 -> Maybe DhallState -> Either Text (Maybe DhallState)
forall a. a -> Either Text a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (DhallState -> Maybe DhallState
forall a. a -> Maybe a
Just (DhallVal -> [DhallFrame] -> DhallState
DStateReturn (Text -> DhallVal
VText (Text
t1 Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
t2)) [DhallFrame]
rest))
DhallVal
_ -> Text -> Either Text (Maybe DhallState)
forall a b. a -> Either a b
Left Text
"Type error: Right operand of ++ must be Text"
evalDhallCEKWithEnv :: DhallEnv -> DhallExpr -> Either Text DhallVal
evalDhallCEKWithEnv :: DhallEnv -> DhallExpr -> Either Text DhallVal
evalDhallCEKWithEnv DhallEnv
env DhallExpr
expr = DhallState -> Either Text DhallVal
go (DhallExpr -> DhallEnv -> [DhallFrame] -> DhallState
DStateEval DhallExpr
expr DhallEnv
env [DhallFrame]
emptyDhallCont)
where
go :: DhallState -> Either Text DhallVal
go DhallState
st = case DhallState -> Either Text (Maybe DhallState)
stepDhallCEK DhallState
st of
Left Text
err -> Text -> Either Text DhallVal
forall a b. a -> Either a b
Left Text
err
Right Maybe DhallState
Nothing -> case DhallState
st of
DStateReturn DhallVal
v [] -> DhallVal -> Either Text DhallVal
forall a. a -> Either Text a
forall (f :: * -> *) a. Applicative f => a -> f a
pure DhallVal
v
DhallState
_ -> Text -> Either Text DhallVal
forall a b. a -> Either a b
Left Text
"Internal CEK invariant violation"
Right (Just DhallState
nextSt) -> DhallState -> Either Text DhallVal
go DhallState
nextSt
evalDhallCEK :: DhallExpr -> Either Text DhallVal
evalDhallCEK :: DhallExpr -> Either Text DhallVal
evalDhallCEK = DhallEnv -> DhallExpr -> Either Text DhallVal
evalDhallCEKWithEnv DhallEnv
emptyDhallEnv