plutils

programming language utilities
git clone git://git.rr3.xyz/plutils
Log | Files | Refs | Submodules

commit 76c59d52a1a5db2e10db04071b1a35189bc35dec
parent 0d9ba56d82d9a5993d54fb79f38c4e39af7f1b8e
Author: Robert Russell <robert@rr3.xyz>
Date:   Sat, 26 Jul 2025 19:01:27 -0700

Add Environment.eval back

Diffstat:
Msrc/PlUtils/Environment.hs | 10++++++++++
1 file changed, 10 insertions(+), 0 deletions(-)

diff --git a/src/PlUtils/Environment.hs b/src/PlUtils/Environment.hs @@ -6,8 +6,10 @@ module PlUtils.Environment ( assign, lookup, define, + eval, ) where +import Data.Maybe (fromMaybe) import Naturals.NatMap (NatMap) import Naturals.NatMap qualified as NatMap import Prelude hiding (lookup) @@ -46,3 +48,11 @@ define :: (Level -> v) -> Environment v -> Environment v define makeVal env = let (l, env') = bind env in assign l (makeVal l) env' + +-- | Evaluate an index in an environment, falling back to making a value with +-- the given function in case the environment doesn't have a substitution for +-- the index. +eval :: (Level -> v) -> Index -> Environment v -> v +eval makeVal i env = + let l = idxToLvl (depth env) i + in fromMaybe (makeVal l) $ lookup l env