aboutsummaryrefslogtreecommitdiffhomepage
path: root/std/Unary.bruijn
blob: cf72e3b1c5f6f73873f464c8bff28d091814e7c6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# MIT License, Copyright (c) 2022 Marvin Borner
# classic Church style numerals

:import std/Logic .

zero [[0]]

# returns true if a unary number is zero
zero? [0 [[[0]]] [[1]]] ⧗ Unary → Boolean

=?‣ zero?

:test (=?(+0u)) (true)
:test (=?(+42u)) (false)

# adds 1 to a unary number
inc [[[1 (2 1 0)]]] ⧗ Unary → Unary

++‣ inc

:test (++(+0u)) ((+1u))
:test (++(+1u)) ((+2u))
:test (++(+42u)) ((+43u))

# subs 1 from a unary number
dec [[[2 [[0 (1 3)]] [1] [0]]]] ⧗ Unary → Unary

--‣ dec

:test (--(+0u)) ((+0u))
:test (--(+1u)) ((+0u))
:test (--(+42u)) ((+41u))

# adds two unary numbers
add [[[[3 1 (2 1 0)]]]] ⧗ Unary → Unary → Unary

…+… add

:test ((+0u) + (+2u)) ((+2u))
:test ((+5u) + (+3u)) ((+8u))

# muls two unary numbers
mul [[[2 (1 0)]]] ⧗ Unary → Unary → Unary

…⋅… mul

:test ((+0u) ⋅ (+2u)) ((+0u))
:test ((+2u) ⋅ (+3u)) ((+6u))

# exponentiates two unary numbers
# gives 1 if exponent is 0
exp [[0 1]] ⧗ Unary → Unary → Unary

…^… exp

:test ((+1u) ^ (+0u)) ((+1u))
:test ((+2u) ^ (+3u)) ((+8u))