-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluator.h
More file actions
44 lines (34 loc) · 1.68 KB
/
evaluator.h
File metadata and controls
44 lines (34 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#pragma once
#include "Expression.h"
#include "Visitor.h"
#include "Environment.h"
#include <unordered_set>
// Evaluator for lambda expressions using the visitor pattern
class Evaluator : public IVisitor {
private:
std::shared_ptr<Expression> result;
Environment& environment;
// Helper methods for evaluation
std::shared_ptr<Expression> substitute(
const std::shared_ptr<Expression>& expr,
const std::string& var,
const std::shared_ptr<Expression>& replacement);
std::unordered_set<std::string> getFreeVariables(const std::shared_ptr<Expression>& expr);
std::string generateFreshVariable(const std::unordered_set<std::string>& usedVars,
const std::string& hint = "x");
public:
explicit Evaluator(Environment& env) : environment(env) {}
// Visitor pattern implementation
void visit(Variable& variable) override;
void visit(Abstraction& abstraction) override;
void visit(Application& application) override;
void visit(NamedReference& reference) override;
// Evaluate using normal order reduction (outermost, leftmost redex first)
std::shared_ptr<Expression> evaluateNormalOrder(const std::shared_ptr<Expression>& expr);
// Evaluate using applicative order reduction (innermost redexes first)
std::shared_ptr<Expression> evaluateApplicativeOrder(const std::shared_ptr<Expression>& expr);
// Perform a single beta reduction step
std::shared_ptr<Expression> betaReduce(const std::shared_ptr<Expression>& expr);
// Check if an expression is in normal form (cannot be reduced further)
bool isNormalForm(const std::shared_ptr<Expression>& expr);
};