Phil 455: Homework 6 Extra

Here are definitions of the two auxiliary functions addCarry and mulDigit used in Homework 6 Problem 9. These in turn call other auxiliary functions, that are also supplied.

addCarry(zs, carry) =def dissect zs {
    λ empty. carry;
    λ v ⁀ vs if digit(v). dissect addAux2(v, carry) {
        λ c1 ⁀ c2 if digit(c1). c1 ⁀ addCarry(vs, c2)
    }
}

addAux2(v, carry) =def dissect v {
    λ "0". carry ⁀ "0";
    λ "1" if carry = "0". "10";
    λ "1" if carry = "1". "20";
    λ "1" if carry = "2". "01";
    λ "2" if carry = "0". "20";
    λ "2" if carry = "1". "01";
    λ "2" if carry = "2". "11"
}

addAux3(v, u, carry) =def dissect v {
    λ "0". addAux2(u, carry);
    λ "1" if u = "0" and carry = "0". "10";
    λ "1" if u = "0" and carry = "1". "20";
    λ "1" if u = "0" and carry = "2". "01";
    λ "1" if u = "1" and carry = "0". "20";
    λ "1" if u = "1" and carry = "1". "01";
    λ "1" if u = "1" and carry = "2". "11";
    λ "1" if u = "2" and carry = "0". "01";
    λ "1" if u = "2" and carry = "1". "11";
    λ "1" if u = "2" and carry = "2". "21";
    λ "2" if u = "0" and carry = "0". "20";
    λ "2" if u = "0" and carry = "1". "01";
    λ "2" if u = "0" and carry = "2". "11";
    λ "2" if u = "1" and carry = "0". "01";
    λ "2" if u = "1" and carry = "1". "11";
    λ "2" if u = "1" and carry = "2". "21";
    λ "2" if u = "2" and carry = "0". "11";
    λ "2" if u = "2" and carry = "1". "21";
    λ "2" if u = "2" and carry = "2". "02"
}

mulDigits(u, y, z, carry) =def dissect mulAux2(u, y) {
    λ v1 ⁀ v2 if digit(v1). dissect addAux3(v1, z, carry) {
        λ ws if v2 = "0". ws;
        λ w1 ⁀ "0" if v2 = "1". w1 ⁀ "1";
        λ w1 ⁀ "1" if v2 = "1". w1 ⁀ "2";
        λ w1 ⁀ "0" if v2 = "2". w1 ⁀ "2"
    }
}

mulAux2(u, y) =def dissect u {
    λ "0". "00";
    λ "1". y ⁀ "0";
    λ "2" if y = "0". "00";
    λ "2" if y = "1". "20";
    λ "2" if y = "2". "11"
}