Skip to content

Instantly share code, notes, and snippets.

@ZenLiuCN
Last active December 28, 2024 06:48
Show Gist options
  • Save ZenLiuCN/70377d18fbb19d44e3fbc340e290604e to your computer and use it in GitHub Desktop.
Save ZenLiuCN/70377d18fbb19d44e3fbc340e290604e to your computer and use it in GitHub Desktop.
tree caclulus implemant in java
package io.github.zenliucn;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.IntFunction;
import java.util.function.Predicate;
import java.util.function.UnaryOperator;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* @author Zen.Liu
* @since 2024-12-27
*/
public interface TreeCalcX {
Logger log = LoggerFactory.getLogger(TreeCalcX.class);
record N(List<N> v) implements List<N> {
@Override
public int size() {
return v.size();
}
@Override
public boolean isEmpty() {
return v.isEmpty();
}
@Override
public boolean contains(Object o) {
return v.contains(o);
}
@Override
public Iterator<N> iterator() {
return v.iterator();
}
@Override
public Object[] toArray() {
return v.toArray();
}
@Override
public <T> T[] toArray( T[] a) {
return v.toArray(a);
}
@Override
public boolean add(N n) {
return v.add(n);
}
@Override
public boolean remove(Object o) {
return v.remove(o);
}
@Override
public boolean containsAll( Collection<?> c) {
return v.containsAll(c);
}
@Override
public boolean addAll( Collection<? extends N> c) {
return v.addAll(c);
}
@Override
public boolean addAll(int index, Collection<? extends N> c) {
return v.addAll(index, c);
}
@Override
public boolean removeAll( Collection<?> c) {
return v.removeAll(c);
}
@Override
public boolean retainAll( Collection<?> c) {
return v.retainAll(c);
}
@Override
public void replaceAll(UnaryOperator<N> operator) {
v.replaceAll(operator);
}
@Override
public void sort(Comparator<? super N> c) {
v.sort(c);
}
@Override
public void clear() {
v.clear();
}
@Override
public N get(int index) {
return v.get(index);
}
@Override
public N set(int index, N element) {
return v.set(index, element);
}
@Override
public void add(int index, N element) {
v.add(index, element);
}
@Override
public N remove(int index) {
return v.remove(index);
}
@Override
public int indexOf(Object o) {
return v.indexOf(o);
}
@Override
public int lastIndexOf(Object o) {
return v.lastIndexOf(o);
}
@Override
public ListIterator<N> listIterator() {
return v.listIterator();
}
@Override
public ListIterator<N> listIterator(int index) {
return v.listIterator(index);
}
@Override
public List<N> subList(int fromIndex, int toIndex) {
return v.subList(fromIndex, toIndex);
}
@Override
public Spliterator<N> spliterator() {
return v.spliterator();
}
@Override
public <T> T[] toArray(IntFunction<T[]> generator) {
return v.toArray(generator);
}
@Override
public boolean removeIf(Predicate<? super N> filter) {
return v.removeIf(filter);
}
@Override
public Stream<N> stream() {
return v.stream();
}
@Override
public Stream<N> parallelStream() {
return v.parallelStream();
}
@Override
public void forEach(Consumer<? super N> action) {
v.forEach(action);
}
N() {
this(new ArrayList<>(2));
}
public N v1() {
if (size() < 1) return leaf;
return v.get(0);
}
public N v2() {
if (size() < 2) return leaf;
return v.get(1);
}
@Override
public String toString() {
return v.size() == 0 ? "●" : switch (v.size()) {
case 1 -> "▲" + "(" + v.get(0) + ")";
case 2 -> "■" + v.stream().map(N::toString).collect(Collectors.joining(",", "[", "]"));
default -> "λ" + v.stream().map(N::toString).collect(Collectors.joining(",", "[", "]"));
};
}
static final int magic = 3721;
@Override
public int hashCode() {
return v.size() == 0 ? magic : v.hashCode();
}
@Override
public boolean equals(Object obj) {
return obj instanceof N x && x.hashCode() == this.hashCode();
}
N push(N v) {
this.v.add(v);
return this;
}
N pushExpand(N v) {
this.v.addAll(v.v);
return this;
}
N pop() {
return v.remove(v.size() - 1);
}
}
N leaf = new N();
static N $(N... v) {
if (v.length == 0) return leaf;
return new N(Arrays.asList(v));
}
static N apply(N v1, N v2) {
log.debug("arg\n{}\n{}", v1, v2);
var exp = new N().push(v2).pushExpand(v1);
var todo = new N().push(exp);
log.debug("\nexpr {}\ntodo {}", exp, todo);
while (todo.size() != 0) {
var f = todo.pop();
if (f.size() < 3) continue;
log.debug("\nf {}\ntodo {}", f, todo);
todo.push(f);
var a = f.pop();
var b = f.pop();
var c = f.pop();
log.debug("\na {}\nb {}\nc {}\nf {}", a, b, c, f);
switch (a.size()) {
case 0 -> f.pushExpand(b);
case 1 -> {
var r = new N().push(c).pushExpand(b);
f.push(r).push(c).pushExpand(a.v1());
todo.push(r);
}
case 2 -> {
switch (c.size()) {
case 0 -> f.pushExpand(a.v2());
case 1 -> f.push(c.v1()).pushExpand(a.v1());
case 2 -> f.push(c.v1()).push(c.v2()).pushExpand(b);
}
}
}
log.debug("\ntodo {}\nexp {}", todo, exp);
}
return exp;
}
static void main(String[] args) {
var $false = leaf;
var $true = $(leaf);
var $NOT = $(leaf,$($($false, leaf),$true));
System.out.println($false);
System.out.println($true);
var NF = apply($NOT, $false);
System.out.println(NF);
System.out.println(NF.equals($true));
var NT = apply($NOT, $true);
System.out.println(NT);
System.out.println(NT.equals($false));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment