Started by pk, Dec 26, 2024, 06:47 PM

Previous topic - Next topic

pk(OP)

Please feel free to add your Tau demos to this post.
I'll start with a simple implementation of integer arithmetic with addition and multiplication.
I'm attaching the .tau demo file and screenshots of its output.

https://pastebin.com/raw/vYW7dEwt



pk(OP)

One more example:

cmp(int[x](1),int[x](1)):=T
cmp(int[x](1),int[y](1)):=F
sides(sqr()):=int[4](1)
sides(tri()):=int[3](1)
sides(rect()):=int[4](1)
same(x,y):=cmp(sides(x),sides(y))
n same(tri(),sqr())
n same(sqr(),tri())
n same(tri(),tri())
n same(sqr(),sqr())

n same(tri(),rect())
n same(rect(),sqr())

quit
 


pk(OP)

Thanks to S1r_4zdr3w for pointing out an unhandled edge case in the multiplication recurrence relation (while multiplying by zero). Please see the fixed code below.

# This is an example implementation of addition and multiplication arithmetic in Tau (defined here between 0 and 20).
# Range can be easily extended by adding more entries to the input successor relation.
version

# input

succ(int[0](1)) := int[1](1)
succ(int[1](1)) := int[2](1)
succ(int[2](1)) := int[3](1)
succ(int[3](1)) := int[4](1)
succ(int[4](1)) := int[5](1)
succ(int[5](1)) := int[6](1)
succ(int[6](1)) := int[7](1)
succ(int[7](1)) := int[8](1)
succ(int[8](1)) := int[9](1)
succ(int[9](1)) := int[10](1)
succ(int[10](1)) := int[11](1)
succ(int[11](1)) := int[12](1)
succ(int[12](1)) := int[13](1)
succ(int[13](1)) := int[14](1)
succ(int[14](1)) := int[15](1)
succ(int[15](1)) := int[16](1)
succ(int[16](1)) := int[17](1)
succ(int[17](1)) := int[18](1)
succ(int[18](1)) := int[19](1)
succ(int[19](1)) := int[20](1)
pred(int[t](1)) := int[t-1](1)
add(x, int[0](1)) := x
add(int[0](1), x) := x
add(x, y) := add(pred(x), succ(y))
mul(x, int[0](1)) := int[0](1)
mul(int[0](1), x) := int[0](1)
mul(x, int[1](1)) := x
mul(int[1](1), x) := x
mul(x, y) := add(y, mul(pred(x), y))

# addition and multiplication examples

n add(int[7](1),int[10](1))

n mul(int[5](1),int[3](1))

# overflow examples

n add(int[22](1),int[3](1))

n mul(int[3](1),int[8](1))


# done
quit

pk(OP)

I added gt and lt functions to my previous demo (note that a predecessor relation is also required for these, added as well - it's called "pred" here).

cmp(int[x](1),int[x](1)):=T
cmp(int[x](1),int[y](1)):=F
pred(int[t](1)):=int[t-1](1)
gt(int[0](1),int[x](1)):=F
gt(int[x](1),int[x](1)):=F
gt(x,y):=cmp(pred(x),y) || gt(pred(x),y)
lt(int[x](1),int[x](1)):=F
lt(int[x](1),int[y](1)):=gt(int[y](1),int[x](1))

So now we can also check whether a shape has more/less sides than the other:

sides(sqr()):=int[4](1)
sides(tri()):=int[3](1)
sides(rect()):=int[4](1)
greater(x,y):=gt(sides(x),sides(y))
less(x,y):=lt(sides(x),sides(y))

n greater(tri(),sqr())
n greater(sqr(),tri())
n greater(tri(),tri())
n greater(sqr(),sqr())
n greater(tri(),rect())
n greater(rect(),tri())
n greater(rect(),sqr())

n less(tri(),sqr())
n less(sqr(),tri())
n less(tri(),tri())
n less(sqr(),sqr())
n less(tri(),rect())
n less(rect(),tri())
n less(rect(),sqr())