eval :: [Lam.BindingWithId CodeDbId]
     -> Map CodeDbId (Eval.Val CodeDbId)
     -> Either [Error] (Eval.Val CodeDbId)
eval bindingsWithIds initialEnv = do
  resolved <- (_Left %~ pure . ParseError) $ Lam.resolveVars (Lam.GlobalNames Builtins.functionIds Builtins.allArgIds) bindingsWithIds

  let maybeMainExp = Lam.bindingExp =<< List.find ((== "main") . Lam.bindingName) bindingsWithIds
  mainExp <- maybe (Left [RuntimeError $ Eval.UndefinedVar (CodeDbId "toplevel") "main"]) Right maybeMainExp

  let toplevelEnv = let bindingsEnv = Map.fromList . concat $ bindingContentsEnvItems <$> bindingsWithIds
                     in Map.unions [bindingsEnv, Builtins.builtinsEnv]

  iterateTrail mempty (evalWithTrail resolved toplevelEnv initialEnv mainExp)
eval :: [Lam.BindingWithId CodeDbId]
     -> Map CodeDbId (Eval.Val CodeDbId)
     -> Either [Error] (Eval.Val CodeDbId)
eval bindingsWithIds initialEnv = do
  resolved <- (_Left %~ pure . ParseError) $ Lam.resolveVars (Lam.GlobalNames Builtins.functionIds Builtins.allArgIds) bindingsWithIds

  let maybeMainExp = Lam.bindingExp =<< List.find ((== "main") . Lam.bindingName) bindingsWithIds
  mainExp <- maybe (Left [RuntimeError $ Eval.UndefinedVar (CodeDbId "toplevel") "main"]) Right maybeMainExp

  let toplevelEnv = let bindingsEnv = Map.fromList . concat $ bindingContentsEnvItems <$> bindingsWithIds
                     in Map.unions [bindingsEnv, Builtins.builtinsEnv]

  iterateTrail mempty (evalWithTrail resolved toplevelEnv initialEnv mainExp)
eval bindingsWithIds initialEnv = do
  resolved <- (_Left %~ pure . ParseError) $ Lam.resolveVars (Lam.GlobalNames Builtins.functionIds Builtins.allArgIds) bindingsWithIds

  let maybeMainExp = Lam.bindingExp =<< List.find ((== "main") . Lam.bindingName) bindingsWithIds
  mainExp <- maybe (Left [RuntimeError $ Eval.UndefinedVar (CodeDbId "toplevel") "main"]) Right maybeMainExp

  let toplevelEnv = let bindingsEnv = Map.fromList . concat $ bindingContentsEnvItems <$> bindingsWithIds
                     in Map.unions [bindingsEnv, Builtins.builtinsEnv]

  iterateTrail mempty (evalWithTrail resolved toplevelEnv initialEnv mainExp)