3.17 Problem: Binary Addition

► |  | Redex: Practical Semantics Engineering

1 | Amb: A Redex Tutorial
2 | Long Tutorial
3 | Extended Exercises
4 | The Redex Reference
5 | Automated Testing Benchmark
| Bibliography
| Index

▼ | 3 | Extended Exercises

3.1 | Problem: Objects
3.2 | Solution: Objects
3.3 | Problem: Types
3.4 | Solution: Types
3.5 | Problem: Missionaries and Cannibals
3.6 | Solution: Missionaries and Cannibals
3.7 | Problem: Towers of Hanoi
3.8 | Solution: Towers of Hanoi
3.9 | Problem: GC
3.10 | Solution: GC
3.11 | Problem: Finite State Machines
3.12 | Solution: Finite State Machines
3.13 | Problem: Threads
3.14 | Solution: Threads
3.15 | Problem: Contracts
3.16 | Solution: Contracts
3.17 | Problem: Binary Addition
3.18 | Solution: Binary Addition

top contents ← prev up next →

### 3.17 Problem: Binary Addition🔗ℹ

Redex can also model hardware scenarios.

Here is a language of bit expressions:

> (define-language L
> (e ::=
> (or e e)
> (and e e)
> (not e)
> (append e ...)
> (add e e)
> v)
> (v ::= (b ...))
> (b ::= 0 1)
> (n ::= natural))

Your task is to design a standard reduction system that mimics addition on sequences of bits (binary digits).

Hints Raw values are sequences of booleans, not just one. For or, you can reduce sequences that have more than one element into appends of ors that each have a single element, and then actually handle or’s logic for arrays that have just one element. This also works for and and not. This idea doesn’t work for add.

Here are some test cases to get you started:

> (module+ test
> (test-->> red (term (or (1 1 0 0) (0 1 0 1))) (term (1 1 0 1)))
> (test-->> red (term (not (0 1))) (term (1 0)))
> (test-->> red (term (append (1 0) (0 1))) (term (1 0 0 1)))
> (test-->> red (term (or (1 1 0 0) (0 1 0 1))) (term (1 1 0 1)))
> (test-->> red (term (and (1 1) (0 1))) (term (0 1)))
> (test-->> red (term (and (0 0) (0 1))) (term (0 0))))

For testing add, we suggest comparing it to Racket’s + operation using this helper function:

> ; v -> n (in L)
> ; convert a sequence of bits to a natural number
> (module+ test
> (test-equal (to-nat (term ())) 0)
> (test-equal (to-nat (term (0))) 0)
> (test-equal (to-nat (term (1))) 1)
> (test-equal (to-nat (term (0 1))) 1)
> (test-equal (to-nat (term (1 0))) 2)
> (test-equal (to-nat (term (1 1))) 3)
> (test-equal (to-nat (term (1 1 1))) 7)
> (test-equal (to-nat (term (0 1 1 1))) 7)
> (test-equal (to-nat (term (0 1 1 0))) 6))
> (define (to-nat bs)
> (for/sum ([b (in-list (reverse bs))]
> [i (in-naturals)])
> (* b (expt 2 i))))

top contents ← prev up next →