Skip to content

Instantly share code, notes, and snippets.

@Superstar64
Last active November 26, 2021 03:33
Show Gist options
  • Save Superstar64/a92fffb39c079d9e33dfd7d6d0286472 to your computer and use it in GitHub Desktop.
Save Superstar64/a92fffb39c079d9e33dfd7d6d0286472 to your computer and use it in GitHub Desktop.
A Calculus of Constructions Type Checker in Java
/* Copyright (C) Freddy A Cubas "superstar64"
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
// https://en.wikipedia.org/wiki/Calculus_of_constructions
// https://www.staff.city.ac.uk/~ross/papers/debruijn.html
import java.util.Optional;
import java.util.function.Function;
interface Void {
<A> A absurd();
}
abstract class Term<A> {
<B> Term<B> inhabited() {
throw new RuntimeException("Term not inhabited: " + this.toString());
}
abstract void unify(Term<A> right);
void unifyRight(Type<A> left) {
mismatch(left, this);
}
void unifyRight(Kind<A> left) {
mismatch(left, this);
}
void unifyRight(Variable<A> left) {
mismatch(left, this);
}
void unifyRight(Call<A> left) {
mismatch(left, this);
}
void unifyRight(Lambda<A> left) {
mismatch(left, this);
}
void unifyRight(Forall<A> left) {
mismatch(left, this);
}
static <A> void mismatch(Term<A> left, Term<A> right)
{
throw new RuntimeException("Type mismatch between \"" + left.toString() + "\" and \"" + right.toString());
}
abstract <B> Term<B> substitute(Function<A, Term<B>> f);
static <A, B> Function<Optional<A>, Term<Optional<B>>> traverse(Function<A, Term<B>> f)
{
return o -> o.isPresent() ? f.apply(o.get()).substitute(x -> new Variable<>(Optional.of(x))) : new Variable<>(Optional.empty());
}
abstract Term<A> reduce();
Term<A> reduceCall(Term<A> argument) {
return new Call<A>(this, argument);
}
Term<A> typeCheckCall(Term<A> argumentType, Term<A> argument) {
throw new RuntimeException("Not a function type: " + this.toString());
}
abstract Term<A> typeCheck(Function<A, Term<A>> enviroment);
}
class Type<A> extends Term<A> {
<B> Term<B> inhabited() {
return new Type<B>();
}
void unify(Term<A> right) {
right.unifyRight(this);
}
void unifyRight(Type<A> left) {}
<B> Term<B> substitute(Function<A, Term<B>> f) {
return new Type<B>();
}
Term<A> reduce() {
return this;
}
Term<A> typeCheck(Function<A, Term<A>> enviroment) {
return new Kind<>();
}
public String toString() {
return "P";
}
}
class Kind<A> extends Term<A> {
<B> Term<B> inhabited() {
return new Kind<B>();
}
void unify(Term<A> right) {
right.unifyRight(this);
}
void unifyRight(Kind<A> left) {}
<B> Term<B> substitute(Function<A, Term<B>> f) {
return new Kind<B>();
}
Term<A> reduce() {
return this;
}
Term<A> typeCheck(Function<A, Term<A>> enviroment) {
throw new RuntimeException("Type of T");
}
public String toString() {
return "T";
}
}
class Variable<A> extends Term<A> {
final A name;
Variable(A name) {
this.name = name;
}
void unify(Term<A> right) {
right.unifyRight(this);
}
void unifyRight(Variable<A> left) {
if(!left.name.equals(this.name)) {
mismatch(left, this);
}
}
<B> Term<B> substitute(Function<A, Term<B>> f) {
return f.apply(name);
}
Term<A> reduce() {
return this;
}
Term<A> typeCheck(Function<A, Term<A>> enviroment) {
return enviroment.apply(name);
}
public String toString() {
return "V(" + name.toString() + ")";
}
}
class Call<A> extends Term<A> {
final Term<A> function;
final Term<A> argument;
Call(Term<A> function, Term<A> argument) {
this.function = function;
this.argument = argument;
}
void unify(Term<A> right) {
right.unifyRight(this);
}
void unifyRight(Call<A> left) {
left.function.unify(this.function);
left.argument.unify(this.argument);
}
<B> Term<B> substitute(Function<A, Term<B>> f) {
return new Call<B>(function.substitute(f), argument.substitute(f));
}
Term<A> reduce() {
return function.reduce().reduceCall(argument.reduce());
}
Term<A> typeCheck(Function<A, Term<A>> enviroment) {
return function.typeCheck(enviroment).typeCheckCall(argument.typeCheck(enviroment), argument.reduce());
}
public String toString() {
return "(" + function.toString() + ")" + "(" + argument.toString() + ")";
}
}
class Lambda<A> extends Term<A> {
final Term<A> argumentType;
final Term<Optional<A>> bound;
Lambda(Term<A> argumentType, Term<Optional<A>> bound) {
this.argumentType = argumentType;
this.bound = bound;
}
void unify(Term<A> right) {
right.unifyRight(this);
}
void unifyRight(Lambda<A> left) {
left.argumentType.unify(this.argumentType);
left.bound.unify(this.bound);
}
<B> Term<B> substitute(Function<A, Term<B>> f) {
return new Lambda<B>(argumentType.substitute(f), bound.substitute(traverse(f)) );
}
Term<A> reduce() {
return new Lambda<A>(argumentType.reduce(), bound.reduce());
}
Term<A> reduceCall(Term<A> argument) {
return bound.substitute(o -> o.isPresent() ? new Variable<>(o.get()) : argument).reduce();
}
Term<A> typeCheck(Function<A, Term<A>> enviroment) {
argumentType.typeCheck(enviroment).inhabited();
Term<A> argumentType = this.argumentType.reduce();
return new Forall<>(argumentType, bound.typeCheck(o ->
(o.isPresent() ? enviroment.apply(o.get()) : argumentType).substitute(x -> new Variable<>(Optional.of(x)))));
}
public String toString() {
return "λ _ : (" + argumentType.toString() + "). " + bound.toString();
}
}
class Forall<A> extends Term<A> {
final Term<A> argumentType;
final Term<Optional<A>> resultType;
Forall(Term<A> argumentType, Term<Optional<A>> resultType) {
this.argumentType = argumentType;
this.resultType = resultType;
}
void unify(Term<A> right) {
right.unifyRight(this);
}
void unifyRight(Forall<A> left) {
left.argumentType.unify(this.argumentType);
left.resultType.unify(this.resultType);
}
<B> Term<B> substitute(Function<A, Term<B>> f) {
return new Forall<B>(argumentType.substitute(f), resultType.substitute(traverse(f)) );
}
Term<A> reduce() {
return new Forall<A>(argumentType.reduce(), resultType.reduce());
}
Term<A> typeCheckCall(Term<A> argumentType, Term<A> argument)
{
this.argumentType.unify(argumentType);
return resultType.substitute(o -> o.isPresent() ? new Variable<>(o.get()) : argument).reduce();
}
Term<A> typeCheck(Function<A, Term<A>> enviroment) {
argumentType.typeCheck(enviroment).inhabited();
Term<A> argumentType = this.argumentType.reduce();
return resultType.typeCheck(o -> (o.isPresent() ? enviroment.apply(o.get()) : argumentType).substitute(x -> new Variable<>(Optional.of(x)))).inhabited();
}
public String toString() {
return "∀ _ : (" + argumentType.toString() + "). " + resultType.toString();
}
}
public class TypeSystem {
public static void main(String[] $) {
Term<Void> id = new Lambda<>(new Type<>(), new Lambda<>(new Variable<>(Optional.empty()), new Variable<>(Optional.empty())) );
System.out.println(id);
Term<Void> endo = id.typeCheck(v -> v.absurd());
System.out.println(endo);
Term<Void> p = endo.typeCheck(v -> v.absurd());
System.out.println(p);
Term<Void> idUniversal = new Call<>(id, endo);
System.out.println(idUniversal.typeCheck(v -> v.absurd()));
System.out.println(idUniversal);
System.out.println(idUniversal.reduce());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment