Skip to content

Commit

Permalink
update comments
Browse files Browse the repository at this point in the history
edit typos
  • Loading branch information
noti0na1 committed Nov 20, 2016
1 parent 00288af commit 0bef09f
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 46 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# LambdaCalculus-java
Lambda calculus implemented in Java

See examples in [com.notnl.lambda.examples](https://github.com/noti0na1/LambdaCalculus-java/tree/master/src/com/notnl/lambda/example)

What is Lambda Calculus: https://en.wikipedia.org/wiki/Lambda_calculus

See examples in [com.notnl.lambda.examples](https://github.com/noti0na1/LambdaCalculus-java/tree/master/src/com/notnl/lambda/example)
More materials: [A Tutorial Introduction to the Lambda Calculus](www.inf.fu-berlin.de/lehre/WS03/alpi/lambda.pdf)

//TODO

Expand Down
47 changes: 27 additions & 20 deletions src/com/notnl/lambda/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,30 @@
import java.util.Set;

/**
* Created by noti0 on 2016/11/18.
* An application
* apply a function with certain argument
*/
public class Application extends Expression {

private Expression function;

private Expression arguement;
private Expression argument;

/**
* @param function
* @param arguement
* @param argument
*/
public Application(Expression function, Expression arguement) {
public Application(Expression function, Expression argument) {
this.function = function;
this.arguement = arguement;
this.argument = argument;
}

/**
* @return
*/
@Override
public boolean reducible() {
return this.function.reducible() || this.arguement.reducible() || this.function instanceof Function;
return this.function.reducible() || this.argument.reducible() || this.function instanceof Function;
}

/**
Expand All @@ -51,12 +52,12 @@ public boolean reducible() {
@Override
public Expression reduce() {
if (this.function.reducible()) {
return new Application(this.function.reduce(), this.arguement);
} else if (this.arguement.reducible()) {
return new Application(this.function, this.arguement.reduce());
return new Application(this.function.reduce(), this.argument);
} else if (this.argument.reducible()) {
return new Application(this.function, this.argument.reduce());
} else if (this.function instanceof Function) {
Function fun = (Function) this.getFunction();
return subst(fun.getBody(), fun.getName(), this.getArguement());
return subst(fun.getBody(), fun.getName(), this.getargument());
}
return this;
}
Expand Down Expand Up @@ -90,15 +91,15 @@ public void setFunction(Expression function) {
/**
* @return
*/
public Expression getArguement() {
return arguement;
public Expression getargument() {
return argument;
}

/**
* @param arguement
* @param argument
*/
public void setArguement(Expression arguement) {
this.arguement = arguement;
public void setargument(Expression argument) {
this.argument = argument;
}

/**
Expand All @@ -113,15 +114,17 @@ public String toString() {
apply.append(this.function.toString());
}
apply.append(' ');
if (this.arguement instanceof Variable) {
apply.append(this.arguement);
if (this.argument instanceof Variable) {
apply.append(this.argument);
} else {
apply.append('(').append(this.arguement).append(')');
apply.append('(').append(this.argument).append(')');
}
return apply.toString();
}

/**
* substitute variable in expression with replacement
*
* @param expr
* @param name
* @param repl
Expand All @@ -138,7 +141,7 @@ private static Expression subst(Expression expr, Variable name, Expression repl)
Application app = (Application) expr;
return new Application(
subst(app.getFunction(), name, repl),
subst(app.getArguement(), name, repl));
subst(app.getargument(), name, repl));
} else if (expr instanceof Function) {
Function fun = (Function) expr;
Set<Variable> fvs = freeVariables(repl);
Expand All @@ -152,6 +155,8 @@ private static Expression subst(Expression expr, Variable name, Expression repl)
}

/**
* get unique name
*
* @param name
* @param usedNames
* @return
Expand All @@ -165,6 +170,8 @@ private static Variable uniqueName(Variable name, Set<Variable> usedNames) {
}

/**
* get all free variables
*
* @param expr
* @return
*/
Expand All @@ -176,7 +183,7 @@ private static Set<Variable> freeVariables(Expression expr) {
} else if (expr instanceof Application) {
Application app = (Application) expr;
vars = freeVariables(app.getFunction());
vars.addAll(freeVariables(app.getArguement()));
vars.addAll(freeVariables(app.getargument()));
} else if (expr instanceof Function) {
Function fun = (Function) expr;
vars = freeVariables(fun.getBody());
Expand Down
15 changes: 12 additions & 3 deletions src/com/notnl/lambda/Expression.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,34 @@
package com.notnl.lambda;

/**
* Created by noti0 on 2016/11/18.
* An Expression
*/
public abstract class Expression {

/**
* return if it can be reduced
*
* @return
*/
public abstract boolean reducible();

/**
* reduce it for one step
*
* @return
*/
public abstract Expression reduce();

/**
* fully reduce it
* caution: this may cause infinite loop
*
* @return
*/
public abstract Expression deepReduce();

/**
*
* display reduce steps
*/
public void printReduceSteps() {
System.out.println(this.toString());
Expand All @@ -47,7 +54,9 @@ public void printReduceSteps() {
}

/**
* @param n
* display certain reduce steps
*
* @param n number of steps
*/
public void printReduceSteps(int n) {
if (n == 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/com/notnl/lambda/Function.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package com.notnl.lambda;

/**
*
* A lambda function
*/
public class Function extends Expression {

Expand Down
46 changes: 33 additions & 13 deletions src/com/notnl/lambda/Lambda.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
package com.notnl.lambda;

/**
* Created by noti0 on 2016/11/18.
* A useful class to create lambda expression
*/
public class Lambda {

/**
* create a variable
*
* @param name
* @return
*/
Expand All @@ -30,6 +32,8 @@ public static Variable var(String name) {
}

/**
* create a lambda function
*
* @param name
* @param body
* @return
Expand All @@ -39,6 +43,8 @@ public static Function lambda(String name, String body) {
}

/**
* create a lambda function
*
* @param name
* @param body
* @return
Expand All @@ -48,6 +54,8 @@ public static Function lambda(String name, Expression body) {
}

/**
* create a lambda function
*
* @param name
* @param body
* @return
Expand All @@ -57,6 +65,8 @@ public static Function lambda(Variable name, Expression body) {
}

/**
* create a lambda function
*
* @param name
* @param body
* @return
Expand All @@ -75,6 +85,8 @@ public static Function lambda(Variable name, Expression body) {
}

/**
* create a lambda function
*
* @param name
* @param body
* @return
Expand All @@ -84,38 +96,46 @@ public static Function lambda(Variable name, Expression body) {
}

/**
* apply a function with argument
*
* @param function
* @param arguement
* @param argument
* @return
*/
public static Application apply(String function, String arguement) {
return new Application(new Variable(function), new Variable(arguement));
public static Application apply(String function, String argument) {
return new Application(new Variable(function), new Variable(argument));
}

/**
* apply a function with argument
*
* @param function
* @param arguement
* @param argument
* @return
*/
public static Application apply(String function, Expression arguement) {
return new Application(new Variable(function), arguement);
public static Application apply(String function, Expression argument) {
return new Application(new Variable(function), argument);
}

/**
* apply a function with argument
*
* @param function
* @param arguement
* @param argument
* @return
*/
public static Application apply(Expression function, String arguement) {
return new Application(function, new Variable(arguement));
public static Application apply(Expression function, String argument) {
return new Application(function, new Variable(argument));
}

/**
* apply a function with argument
*
* @param function
* @param arguement
* @param argument
* @return
*/
public static Application apply(Expression function, Expression arguement) {
return new Application(function, arguement);
public static Application apply(Expression function, Expression argument) {
return new Application(function, argument);
}
}
3 changes: 2 additions & 1 deletion src/com/notnl/lambda/Variable.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
package com.notnl.lambda;

/**
* Created by noti0 on 2016/11/18.
* A variable
* its name should be String
*/
public class Variable extends Expression {

Expand Down
Loading

0 comments on commit 0bef09f

Please sign in to comment.