use of nars.term.atom.Int in project narchy by automenta.
the class Builtin method registerFunctors.
public static void registerFunctors(NAR nar) {
for (Concept t : Builtin.statik) {
nar.on(t);
}
nar.on(Functor.f1("varIntro", (x) -> {
Pair<Term, Map<Term, Term>> result = DepIndepVarIntroduction.the.apply(x, nar.random());
return result != null ? result.getOne() : Null;
}));
nar.on(Functor.f1((Atom) $.the("termlinkRandom"), (Term t) -> {
@Nullable Concept c = nar.conceptualize(t);
if (c == null)
return Null;
@Nullable PriReference<Term> tl = c.termlinks().sample(nar.random());
if (tl == null)
return Null;
return tl.get();
}));
// nar.on(Functor.f("service", (TermContainer c) ->
// $.sete(
// nar.services().map(
// (e) ->
// $.p(e, $.the(e.getValue().state())))
// .toArray(Term[]::new)
// )
// ));
/**
* subterm, but specifically inside an ellipsis. otherwise pass through
*/
nar.on(Functor.f("esubterm", (Subterms c) -> {
Term x = c.sub(0, null);
if (x == null)
return Null;
Term index = c.sub(1, Null);
if (index == Null)
return Null;
int which;
if (index != null) {
if (index instanceof Variable)
return Null;
which = $.intValue(index, -1);
if (which < 0) {
return Null;
}
} else {
// random
which = nar.random().nextInt(x.subs());
}
return x.sub(which);
}));
nar.on(Functor.f2((Atom) $.the("without"), (Term container, Term content) -> Op.without(container, x -> x.equals(content), nar.random())));
nar.on(Functor.f2((Atom) $.the("withoutPosOrNeg"), (Term container, Term content) -> Op.without(container, x -> x.unneg().equals(content), nar.random())));
/**
* TODO rename this to 'dropAnyCommutive'
* remove an element from a commutive conjunction (or set), at random, and try re-creating
* the compound. wont necessarily work in all situations.
* TODO move the type restriction to another functor to wrap this
*
* this also filter a single variable (depvar) from being a result
*/
nar.on(Functor.f1((Atom) $.the("dropAnySet"), (Term t) -> {
Op oo = t.op();
if (oo == INT) {
if (t instanceof Int.IntRange) {
// select random location in the int and split either up or down
Int.IntRange i = (Int.IntRange) t;
Random rng = nar.random();
if (i.min + 1 == i.max) {
// arity=2
return Int.the(rng.nextBoolean() ? i.min : i.max);
} else if (i.min + 2 == i.max) {
// arity=3
switch(rng.nextInt(4)) {
case 0:
return Int.the(i.min);
case 1:
return Int.range(i.min, i.min + 1);
case 2:
return Int.range(i.min + 1, i.min + 2);
case 3:
return Int.the(i.max);
default:
throw new UnsupportedOperationException();
}
} else {
int split = // midpoint, deterministic
(i.max + i.min) / 2;
// rng.nextInt(i.max-i.min-2);
return (rng.nextBoolean()) ? Int.range(i.min, split + 1) : Int.range(split + 1, i.max);
}
}
// cant drop int by itself
return Null;
}
if (!oo.in(SETi.bit | SETe.bit | SECTi.bit | SECTe.bit))
// returning the original value may cause feedback loop in callees expcting a change in value
return Null;
int size = t.subs();
switch(size) {
case 0:
assert (false) : "empty set impossible here";
return Null;
case 1:
return Null;
/* can't shrink below one element */
case 2:
int n = nar.random().nextInt(2);
return oo.the(t.sub(n));
default:
Term[] y = Terms.dropRandom(nar.random(), t.subterms());
return oo.the(y);
}
}));
/**
* depvar cleaning from commutive conj
*/
nar.on(Functor.f1((Atom) $.the("ifConjCommNoDepVars"), (Term t) -> {
if (!t.hasAny(VAR_DEP))
return t;
Op oo = t.op();
if (oo != CONJ)
return t;
SortedSet<Term> s = t.subterms().toSetSorted();
if (!s.removeIf(x -> x.unneg().op() == VAR_DEP))
return t;
return CONJ.the(t.dt(), s);
}));
/**
* drops a random contained event, whether at first layer or below
*/
nar.on(Functor.f1((Atom) $.the("dropAnyEvent"), (Term t) -> {
Op oo = t.op();
if (oo != CONJ)
// returning the original value may cause feedback loop in callees expcting a change in value
return Null;
FasterList<LongObjectPair<Term>> ee = Conj.eventList(t);
ee.remove(nar.random().nextInt(ee.size()));
return Conj.conj(ee);
// }
// if (r instanceof Variable /*&& r.op()!=VAR_DEP*/)
// return Null; //HACK dont allow returning a variable as an event during decomposition HACK TODO make more careful and return the only result if one subterm is a non-returnable variable
// return r;
}));
nar.on(Functor.f2((Atom) $.the("conjEvent"), (Term c, Term when) -> {
if (c.op() != CONJ || !(when instanceof Atom))
return Null;
// extract earliest or latest &| timeslice of events
throw new TODO();
// if (c.dt() == DTERNAL || c.dt() == 0) {
// return c.sub(nar.random().nextInt(c.subs())); //choose a subterm at random
// }
// assert (c.subs() == 2);
// int target;
// switch (when.toString()) {
// case "early":
// target = 0;
// break;
// case "late":
// target = 1;
// break;
// default:
// throw new UnsupportedOperationException();
// }
// if (c.dt() < 0)
// target = 1 - target;
// return c.sub(target);
}));
/**
* similar to without() but special handling for CONJ sub-events
*/
nar.on(Functor.f2((Atom) $.the("conjWithout"), (Term conj, Term event) -> {
if (conj.op() != CONJ || conj.impossibleSubTerm(event))
return Null;
FasterList<LongObjectPair<Term>> events = Conj.eventList(conj);
IntArrayList found = new IntArrayList(2);
int es = events.size();
assert (es > 1);
for (int i = 0; i < es; i++) {
if (event.equalsRoot(events.get(i).getTwo())) {
found.add(i);
}
}
int fs = found.size(), r;
switch(fs) {
case 0:
return Null;
case 1:
r = found.get(0);
break;
default:
r = found.get(nar.random().nextInt(fs));
break;
}
events.remove(r);
return Conj.conj(events);
// } else {
// return nullToNull(Op.without(conj, event::equalsRoot, nar.random()));
// }
}));
/**
* extracts only the events preceding the specified events
*/
nar.on(Functor.f2((Atom) $.the("conjDropIfLatest"), (Term conj, Term event) -> Conj.conjDrop(conj, event, false)));
nar.on(Functor.f2((Atom) $.the("conjDropIfEarliest"), (Term conj, Term event) -> Conj.conjDrop(conj, event, true)));
nar.on(Functor.f1Concept("belief", nar, (c, n) -> $.quote(n.belief(c, n.time()))));
nar.on(Functor.f1Concept("goal", nar, (c, n) -> $.quote(n.goal(c, n.time()))));
nar.on(f0("self", nar::self));
nar.on(Functor.f1("the", what -> {
if (what instanceof Atom) {
switch(what.toString()) {
case "sys":
return $.p($.quote(nar.emotion.summary()), $.quote(nar.concepts.summary()), $.quote(nar.emotion.summary()), $.quote(nar.exe.toString()));
}
}
Object x = nar.concept(what);
if (x == null)
x = what;
return $.quote($.p($.quote(x.getClass().toString()), $.quote(x.toString())));
}));
// /** slice(<compound>,<selector>)
// selector :-
// a specific integer value index, from 0 to compound size
// (a,b) pair of integers, a range of indices */
nar.on(Functor.f("slice", (args) -> {
if (args.subs() == 2) {
Term x = args.sub(0);
if (x.subs() > 0) {
int len = x.subs();
Term index = args.sub(1);
Op o = index.op();
if (o == INT) {
// specific index
int i = ((Int) index).id;
if (i >= 0 && i < len)
return x.sub(i);
else
return False;
} else if (o == PROD && index.subs() == 2) {
Term start = (index).sub(0);
if (start.op() == INT) {
Term end = (index).sub(1);
if (end.op() == INT) {
int si = ((Int) start).id;
if (si >= 0 && si < len) {
int ei = ((Int) end).id;
if (ei >= 0 && ei <= len) {
if (si == ei)
return Op.EmptyProduct;
if (si < ei) {
return $.p(Arrays.copyOfRange(x.subterms().arrayClone(), si, ei));
}
}
}
// TODO maybe reverse order will return reversed subproduct
return False;
}
}
}
}
}
return null;
}));
}
use of nars.term.atom.Int in project narchy by automenta.
the class Functor method f2Int.
public static Functor f2Int(String termAtom, boolean commutive, @Nullable IntPredicate identityComponent, IntPredicate zeroIfArgIs, IntIntToIntFunction ff) {
Atom f = fName(termAtom);
return f2(f, (xt, yt) -> {
boolean xi = xt.op() == INT;
boolean yi = yt.op() == INT;
if (xi && yi) {
int xid = ((Int) xt).id;
int yid = ((Int) yt).id;
return Int.the(ff.apply(xid, yid));
} else {
if (identityComponent != null) {
if (xi) {
int xid = ((Int) xt).id;
if (zeroIfArgIs.test(xid))
return Int.the(0);
if (identityComponent.test(xid))
return yt;
}
if (yi) {
int yid = ((Int) yt).id;
if (zeroIfArgIs.test(yid))
return Int.the(0);
if (identityComponent.test(yid))
return xt;
}
}
if (commutive && xt.compareTo(yt) > 0) {
return $.func(f, yt, xt);
}
return null;
}
});
}
use of nars.term.atom.Int in project narchy by automenta.
the class ArithmeticIntroduction method apply.
public static Term apply(Term x, @Nullable Anon anon, Random rng) {
if ((anon == null && !x.hasAny(INT)) || x.complexity() < 3)
return x;
// find all unique integer subterms
IntHashSet ints = new IntHashSet();
x.recurseTerms((t) -> {
Int it = null;
if (t instanceof Anom) {
Anom aa = ((Anom) t);
Term ta = anon.get(aa);
if (ta.op() == INT)
it = ((Int) ta);
} else if (t instanceof Int) {
it = (Int) t;
}
if (it == null)
return;
ints.add((it.id));
});
// Set<Term> ints = ((Compound) x).recurseTermsToSet(INT);
int ui = ints.size();
if (ui <= 1)
// nothing to do
return x;
// increasing so that relational comparisons can assume that 'a' < 'b'
int[] ii = ints.toSortedArray();
// potential mods to select from
// FasterList<Supplier<Term[]>> mods = new FasterList(1);
IntObjectHashMap<List<Supplier<Term[]>>> mods = new IntObjectHashMap(ii.length);
Variable v = $.varDep("x");
// test arithmetic relationships
for (int a = 0; a < ui; a++) {
int ia = ii[a];
for (int b = a + 1; b < ui; b++) {
int ib = ii[b];
assert (ib > ia);
if (ib - ia < ia && (ia != 0)) {
mods.getIfAbsentPut(ia, FasterList::new).add(() -> new Term[] { Int.the(ib), $.func("add", v, $.the(ib - ia)) });
mods.getIfAbsentPut(ib, FasterList::new).add(() -> new Term[] { Int.the(ia), $.func("add", v, $.the(ia - ib)) });
} else if ((ia != 0 && ia != 1) && (ib != 0 && ib != 1) && Util.equals(ib / ia, (((float) ib) / ia), Float.MIN_NORMAL)) {
mods.getIfAbsentPut(ia, FasterList::new).add(() -> new Term[] { Int.the(ib), $.func("mul", v, $.the(ib / ia)) });
} else if (ia == -ib) {
// negation (x * -1)
mods.getIfAbsentPut(ia, FasterList::new).add(() -> new Term[] { Int.the(ib), $.func("mul", v, $.the(-1)) });
mods.getIfAbsentPut(ib, FasterList::new).add(() -> new Term[] { Int.the(ia), $.func("mul", v, $.the(-1)) });
}
}
}
if (mods.isEmpty())
return x;
// TODO fair select randomly if multiple of the same length
RichIterable<IntObjectPair<List<Supplier<Term[]>>>> mkv = mods.keyValuesView();
int ms = mkv.maxBy(e -> e.getTwo().size()).getTwo().size();
mkv.reject(e -> e.getTwo().size() < ms);
// convention: choose lowest base
MutableList<IntObjectPair<List<Supplier<Term[]>>>> mmm = mkv.toSortedListBy(IntObjectPair::getOne);
IntObjectPair<List<Supplier<Term[]>>> m = mmm.get(0);
int base = m.getOne();
Term baseTerm = Int.the(base);
if (anon != null)
baseTerm = anon.put(baseTerm);
Term yy = x.replace(baseTerm, v);
for (Supplier<Term[]> s : m.getTwo()) {
Term[] mm = s.get();
if (anon != null)
mm[0] = anon.put(mm[0]);
yy = yy.replace(mm[0], mm[1]);
}
Term y = CONJ.the(yy, SIM.the(baseTerm, v));
if (y.op() != CONJ) {
// something happened
return null;
}
if (x.isNormalized()) {
y = y.normalize();
}
return y;
}
use of nars.term.atom.Int in project narchy by automenta.
the class DefaultTermizer method obj2term.
@Nullable
Term obj2term(@Nullable Object o) {
if (o == null)
return NULL;
if (o instanceof Term)
return (Term) o;
if (o instanceof String)
return $.quote(o);
if (o instanceof Boolean)
return ((Boolean) o) ? Termizer.TRUE : Termizer.FALSE;
if (o instanceof Character)
return $.quote(String.valueOf(o));
if (o instanceof Number)
return number((Number) o);
if (o instanceof Class) {
Class oc = (Class) o;
return classTerm(oc);
// if (metadata) {
// Package p = oc.getPackage();
// if (p != null) {
//
// Term cterm = termClassInPackage(oc);
//
// if (reportClassInPackage(oc)) { //TODO use a method for other class exclusions
// Term pkg = packages.get(p);
// if (pkg == null) {
// pkg = termPackage(p);
// packages.put(p, pkg);
// termClassInPackage(cterm, PACKAGE);
// }
//
// //TODO add recursive superclass ancestry?
// }
//
// return cterm;
// }
// }
// return PRIMITIVE;
}
if (o instanceof int[]) {
return $.p((int[]) o);
}
// noinspection IfStatementWithTooManyBranches
if (o instanceof Object[]) {
List<Term> arg = Arrays.stream((Object[]) o).map(this::term).collect(Collectors.toList());
if (arg.isEmpty())
return EMPTY;
return $.p(arg);
}
if (o instanceof List) {
if (((Collection) o).isEmpty())
return EMPTY;
// TODO can this be done with an array to avoid duplicate collection allocation
Collection c = (Collection) o;
List<Term> arg = $.newArrayList(c.size());
for (Object x : c) {
Term y = term(x);
arg.add(y);
}
if (arg.isEmpty())
return EMPTY;
return $.p(arg);
/*} else if (o instanceof Stream) {
return Atom.quote(o.toString().substring(17));
}*/
}
if (o instanceof Set) {
Collection<Term> arg = (Collection<Term>) ((Collection) o).stream().map(this::term).collect(Collectors.toList());
if (arg.isEmpty())
return EMPTY;
return $.sete(arg);
} else if (o instanceof Map) {
Map mapo = (Map) o;
Set<Term> components = $.newHashSet(mapo.size());
mapo.forEach((k, v) -> {
Term tv = obj2term(v);
Term tk = obj2term(k);
if ((tv != null) && (tk != null)) {
components.add($.inh(tv, tk));
}
});
if (components.isEmpty())
return EMPTY;
return $.sete(components);
}
return instanceTerm(o);
// //ensure package is term'ed
// String pname = p.getName();
// int period = pname.length()-1;
// int last = period;
// Term child = cterm;
// while (( period = pname.lastIndexOf('.', period)) != -1) {
// String parname = pname.substring(0, last);
// Term parent = packages.get(parname);
// if (parent == null) {
// parent = Atom.the(parname);
// nar.believe( Inheritance.make(child, parent) );
// packages.put()
// last = period;
// child = parent;
// }
// else {
// break;
// }
// }
}
Aggregations