cs3102 Spring 2023

Problem Set 1 (Jupyter Part): (Un)natural Numbers

Purpose
The goal of this part of Problem Set 1 is to explore our constructive recursive definition of Natural Numbers. The programs you will write will be similar to both "definitions" and "proofs", but as executable code. (Of course, one should be very careful in considering a "program" to be a "proof", especially without proving the program is correct.)

Defining Natural Numbers

We will use Python strings to represent our Natural Numbers. The main point of this is to show that the "0" and "S" used in the definition are just symbols: we only use string concatenation, extraction, and comparison to build our Natural Number represetation. Of course, this is a ridiculously silly way to represent Natural Numbers if we were actually doing computations on them (especially as Python already provides a built-in integer datatype, which could be naturally used to approximate the Natural Numbers).

(feel free to replace the emojis we used to your favorites, but be wary of multi-byte unicode characters!)

Defining Equalty

Here's a definition of equality using our Natural Numbers from Class 2.

Note that we could have just defined equality using Python string equality, but that would be tied to our string representation and assume we know what Python's string equality does. Although we are using strings for our representation now, we don't want to rely on anything other than the makeZero, isZero, makeSuccessor and getPredecessor functions we defined.

Problem J1. Define a function addNumbers that takes as input two Natural Numbers (as represented here) and returns a Natural Number that represents the sum of the two inputs.

Problem J2. Define a function greaterNumber that takes as input two Natural Numbers (as represented here) and returns a Boolean that denotes whether the first input is greater than the second.

Unnatural Trees

Next, we explore a recursive constructive definition of trees.

We will say that a Tree is either the empty tree, which we will represent with an acorn: '🌰', or it will be a pair of Trees where the first element of the pair is the left branch and the second element of the pair is the right branch.

We can make a few example trees.

Problem J3. Define treeSize, treeHeight, and leafCount functions which that all take a single Tree as their input, and output an int (the regular Python integer type, not our (un)Natural Number type from the earlier problems) that represents the total number of nodes in the tree, the height of the tree, and the total number of leaves in the tree respectively. You may use standard int operations including +, -, *, <, etc.).

(Hint: a leaf is a Tree whose children are both empty trees, so it may be helful to also write an isLeaf function.)

Bijections

We can show two sets are the same size by demonstrating a bijection between the elements in these sets. Intuitively, a bijection shows two sets to be the same size --- since it is injective every input element maps to a unique output element; since it is surjective, every possible output element is mapped to by some input. Combined, these two properties cause elements to form matched pairs.

Here, we are going to show that the number of binary strings of length n is equal to 2_n_ by giving a bijection with the natural numbers up to 2_n_, i.e., by making these matched pairs. To do this, your goal is to write a function bitstringToNaturalNumber which will convert a binary string to a Natural Number (using our constructive definition from earlier in this notebook).

Your function should be a one-to-one mapping: there should be some binary string input that produces every natural number, and there should not be any two different binary string inputs that ouput the same natural number. (Note, however, that it is not necessary to assume any particular way of interpreting a bitstring as a number.)

Problem J4. Write this bitstringToNaturalNumber function.

The following code checks that every bitstring of length n maps to a different natual number:

End of Jupyter Notebook for Problem Set 1

Make sure to save your completed notebook, and submit it along with your PDF file with the answer to the other problems in ps1.tex.