{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE OverloadedStrings #-}

{- |
Module      : Cek.Dhall
Description : Felleisen-Friedman CEK abstract machine for Dhall expression normalization
Copyright   : (c) Nadia Yvette Chambers, 2026
License     : EUPL-1.2
Maintainer  : Nadia Chambers <nadia.yvette.chambers@ik.me>
Stability   : experimental
Portability : non-portable (GHC extensions)

Implements a tailored Felleisen-Friedman CEK machine for the normalization and
evaluation of Dhall language constructs (natural numbers, integers, booleans, text,
records, projections, lists, and closures).

= Architectural Context

Per Note [Felleisen-Friedman CEK Abstract Machine] [ref:note-core-architecture],
this module models Dhall strong normalization through tail-recursive transition steps:
* Record fields are evaluated inside-out.
* Projections ('DField') resolve against fully evaluated record values.
* Operators ('DOpPlus', 'DOpTextAppend') evaluate operands strictly before combining.
-}
module Cek.Dhall
  ( -- * Dhall AST
    DhallExpr(..)
    -- * Value Domain
  , DhallVal(..)
  , DhallEnv(..)
  , emptyDhallEnv
  , extendDhallEnv
  , lookupDhallEnv
    -- * Continuation Frames
  , DhallFrame(..)
  , DhallCont
  , emptyDhallCont
    -- * Machine State & Evaluator
  , DhallState(..)
  , stepDhallCEK
  , evalDhallCEK
  ) where

import Data.Text (Text)
import Data.Word (Word64)
import GHC.Generics (Generic)

-- | Abstract syntax for normalized Dhall expressions.
data DhallExpr
  = DVar !Text                           -- ^ Variable identifier
  | DLam !Text !DhallExpr                -- ^ Lambda abstraction (\(\lambda x. e\))
  | DApp !DhallExpr !DhallExpr           -- ^ Function application
  | DLet !Text !DhallExpr !DhallExpr     -- ^ Let-binding (@let x = e1 in e2@)
  | DNat !Word64                         -- ^ Natural literal (@+42@)
  | DInt !Integer                        -- ^ Signed integer literal (@-7@)
  | DBool !Bool                          -- ^ Boolean literal (@True@, @False@)
  | DText !Text                          -- ^ Text literal (@"hello"@)
  | DRecord ![(Text, DhallExpr)]         -- ^ Record literal (@{ x = 1, y = 2 }@)
  | DField !DhallExpr !Text              -- ^ Field selection (@r.x@)
  | DList ![DhallExpr]                   -- ^ List literal (@[1, 2, 3]@)
  | DOpPlus !DhallExpr !DhallExpr        -- ^ Natural addition (@n + m@)
  | DOpTextAppend !DhallExpr !DhallExpr  -- ^ Text concatenation (@s1 ++ s2@)
  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)

-- | Normalized Dhall values.
data DhallVal
  = VNat !Word64                          -- ^ Natural number value
  | VInt !Integer                         -- ^ Integer value
  | VBool !Bool                           -- ^ Boolean value
  | VText !Text                           -- ^ Text string value
  | VClosure !Text !DhallExpr !DhallEnv   -- ^ Lexical closure \(\langle x, e, E \rangle\)
  | VRecord ![(Text, DhallVal)]           -- ^ Fully evaluated record value
  | VList ![DhallVal]                     -- ^ Evaluated list value
  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)

-- | Lexical environment for Dhall evaluation.
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)

-- | Construct an empty Dhall environment.
emptyDhallEnv :: DhallEnv
emptyDhallEnv :: DhallEnv
emptyDhallEnv = [(Text, DhallVal)] -> DhallEnv
DhallEnv []

-- | Extend a Dhall environment with a variable binding.
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)

-- | Lookup a variable in a Dhall environment.
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

-- | Continuation frames for Dhall evaluation contexts.
data DhallFrame
  = DFrameArg !DhallExpr !DhallEnv              -- ^ Waiting for function argument
  | DFrameFun !DhallVal                         -- ^ Waiting for function body to apply
  | DFrameLet !Text !DhallExpr !DhallEnv        -- ^ Waiting for let-bound definition
  | DFrameField !Text                           -- ^ Waiting for record expression to project field
  | DFrameOpPlusR !DhallExpr !DhallEnv          -- ^ Waiting for right operand of addition
  | DFrameOpPlusL !Word64                       -- ^ Waiting to add left natural
  | DFrameOpTextR !DhallExpr !DhallEnv          -- ^ Waiting for right operand of text append
  | DFrameOpTextL !Text                         -- ^ Waiting to append left 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)

-- | Dhall continuation stack.
type DhallCont = [DhallFrame]

-- | Construct an empty Dhall continuation stack.
emptyDhallCont :: DhallCont
emptyDhallCont :: [DhallFrame]
emptyDhallCont = []

-- | Active state of the Dhall CEK abstract machine.
data DhallState
  = DStateEval !DhallExpr !DhallEnv !DhallCont   -- ^ Control expression \(C\), environment \(E\), continuation \(K\)
  | DStateReturn !DhallVal !DhallCont           -- ^ Value \(V\) being delivered to continuation \(K\)
  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)

-- | Advance the Dhall CEK machine by a single deterministic transition step.
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
    -- Evaluate record fields in order
    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 -- Terminal state: value computed
  (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"

-- | Evaluate a 'DhallExpr' under a specific lexical environment.
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

-- | Evaluate a Dhall expression to its final normalized value under the empty environment.
evalDhallCEK :: DhallExpr -> Either Text DhallVal
evalDhallCEK :: DhallExpr -> Either Text DhallVal
evalDhallCEK = DhallEnv -> DhallExpr -> Either Text DhallVal
evalDhallCEKWithEnv DhallEnv
emptyDhallEnv