Skip to content

Instantly share code, notes, and snippets.

@acutmore
Last active September 19, 2019 11:51
Show Gist options
  • Select an option

  • Save acutmore/a9e09be531f565ba3415200400a0ce31 to your computer and use it in GitHub Desktop.

Select an option

Save acutmore/a9e09be531f565ba3415200400a0ce31 to your computer and use it in GitHub Desktop.
logic in typescript
// ASHEMBLER
type B = 1 | 0;
type BITS_4 = [B, B, B, B];
// note: 'assert' interfaces function as unit tests
interface assert {
true: 1;
false: 0;
}
interface assert { true: 0 } // expected error
interface assert { false: 1 } // expected error
interface assert { true: any } // expected error
interface assert { false: any } // expected error
interface assert { true: 1 } // good: no error
interface assert { false: 0 } // good: no error
type MUL_4<
regA extends BITS_4,
regB extends BITS_4,
/* variables: */
sum0 extends BITS_4 = BITWISE_AND<[regA[0], regA[0], regA[0], regA[0]], regB>,
sum1 extends BITS_4 = ADD_4<[0, AND<regA[1],regB[0]>, AND<regA[1],regB[1]>, AND<regA[1],regB[2]>], sum0>,
sum2 extends BITS_4 = ADD_4<[0, 0, AND<regA[2],regB[0]>, AND<regA[2],regB[1]>], sum1>,
sum3 extends BITS_4 = ADD_4<[0, 0, 0, AND<regA[3], regB[0]>], sum2>
> = sum3;
interface assert { true: equals<MUL_4<N_5, N_0>, N_0> }
interface assert { true: equals<MUL_4<N_2, N_3>, N_6> }
interface assert { true: equals<MUL_4<N_3, N_3>, N_9> }
interface assert { true: equals<MUL_4<N_3, N_5>, N_15> }
type SUB_4<
regA extends BITS_4,
regB extends BITS_4,
/* variables: */
a extends [B, B] = FULL_SUBTRACTOR<0, regA[0], regB[0]>,
b extends [B, B] = FULL_SUBTRACTOR<a[1], regA[1], regB[1]>,
c extends [B, B] = FULL_SUBTRACTOR<b[1], regA[2], regB[2]>,
d extends [B, B] = FULL_SUBTRACTOR<c[1], regA[3], regB[3]>,
> = [a[0], b[0], c[0], d[0]];
interface assert { true: equals<SUB_4<N_5, N_2>, N_3> }
type ADD_4<
regA extends BITS_4,
regB extends BITS_4,
/* variables: */
a extends [B, B] = FULL_ADDER<0, regA[0], regB[0]>,
b extends [B, B] = FULL_ADDER<a[1], regA[1], regB[1]>,
c extends [B, B] = FULL_ADDER<b[1], regA[2], regB[2]>,
d extends [B, B] = FULL_ADDER<c[1], regA[3], regB[3]>,
> = [a[0], b[0], c[0], d[0]];
interface assert { true: equals<ADD_4<N_2, N_3>, N_5> }
type N_0 = [0, 0, 0, 0];
type N_1 = [1, 0, 0, 0];
type N_2 = [0, 1, 0, 0];
type N_3 = [1, 1, 0, 0];
type N_4 = [0, 0, 1, 0];
type N_5 = [1, 0, 1, 0];
type N_6 = [0, 1, 1, 0];
type N_7 = [1, 1, 1, 0];
type N_8 = [0, 0, 0, 1];
type N_9 = [1, 0, 0, 1];
type N_10 = [0, 1, 0, 1];
type N_11 = [1, 1, 0, 1];
type N_12 = [0, 0, 1, 1];
type N_13 = [1, 0, 1, 1];
type N_14 = [0, 1, 1, 1];
type N_15 = [1, 1, 1, 1];
type FULL_SUBTRACTOR<
borrow extends B,
a extends B,
b extends B,
/* variables: */
subtractor1 extends [B, B] = HALF_SUBTRACTOR<a, b>,
subtractor2 extends [B, B] = HALF_SUBTRACTOR<subtractor1[0], borrow>,
> = [subtractor2[0], OR<subtractor1[1], subtractor2[1]>];
interface assert { true: equals<FULL_SUBTRACTOR<0, 0, 0>, [0, 0]> }
interface assert { true: equals<FULL_SUBTRACTOR<1, 0, 0>, [1, 1]> }
interface assert { true: equals<FULL_SUBTRACTOR<0, 1, 0>, [1, 0]> }
interface assert { true: equals<FULL_SUBTRACTOR<0, 0, 1>, [1, 1]> }
interface assert { true: equals<FULL_SUBTRACTOR<1, 1, 0>, [0, 0]> }
interface assert { true: equals<FULL_SUBTRACTOR<1, 0, 1>, [0, 1]> }
interface assert { true: equals<FULL_SUBTRACTOR<0, 1, 1>, [0, 0]> }
interface assert { true: equals<FULL_SUBTRACTOR<1, 1, 1>, [1, 1]> }
type FULL_ADDER<
carry extends B,
a extends B,
b extends B,
/* variables: */
adder1 extends [B, B] = HALF_ADDER<a, b>,
adder2 extends [B, B] = HALF_ADDER<carry, adder1[0]>
> = [adder2[0], OR<adder2[1], adder1[1]>];
interface assert { true: equals<FULL_ADDER<0, 0, 0>, [0, 0]> }
interface assert { true: equals<FULL_ADDER<1, 0, 0>, [1, 0]> }
interface assert { true: equals<FULL_ADDER<0, 1, 0>, [1, 0]> }
interface assert { true: equals<FULL_ADDER<0, 0, 1>, [1, 0]> }
interface assert { true: equals<FULL_ADDER<1, 1, 0>, [0, 1]> }
interface assert { true: equals<FULL_ADDER<1, 0, 1>, [0, 1]> }
interface assert { true: equals<FULL_ADDER<0, 1, 1>, [0, 1]> }
interface assert { true: equals<FULL_ADDER<1, 1, 1>, [1, 1]> }
type HALF_SUBTRACTOR<a extends B, b extends B> = [
XOR<a, b>,
AND<NOT<a>, b>
];
interface assert { true: equals<HALF_SUBTRACTOR<0, 0>, [0, 0]> }
interface assert { true: equals<HALF_SUBTRACTOR<1, 0>, [1, 0]> }
interface assert { true: equals<HALF_SUBTRACTOR<0, 1>, [1, 1]> }
interface assert { true: equals<HALF_SUBTRACTOR<1, 1>, [0, 0]> }
type HALF_ADDER<a extends B, b extends B> = [XOR<a, b>, AND<a, b>];
interface assert { true: equals<HALF_ADDER<0, 0>, [0, 0]> }
interface assert { true: equals<HALF_ADDER<1, 0>, [1, 0]> }
interface assert { true: equals<HALF_ADDER<0, 1>, [1, 0]> }
interface assert { true: equals<HALF_ADDER<1, 1>, [0, 1]> }
type BITWISE_AND<a extends BITS_4, b extends BITS_4> = [
AND<a[0], b[0]>,
AND<a[1], b[1]>,
AND<a[2], b[2]>,
AND<a[3], b[3]>,
];
interface assert { true: equals<
BITWISE_AND<
[0, 0, 1, 1],
[1, 0, 1, 0]
>,
[0, 0, 1, 0]>
}
type COMP_4<
a extends BITS_4,
b extends BITS_4,
/* parameters: */
c0 extends [B, B, B] = COMP<a[0], b[0]>,
c1 extends [B, B, B] = COMP<a[1], b[1]>,
c2 extends [B, B, B] = COMP<a[2], b[2]>,
c3 extends [B, B, B] = COMP<a[3], b[3]>,
> = [
OR_4<
c3[2],
AND<c3[1], c2[2]>,
AND_4<1, c3[1], c2[1], c1[2]>,
AND_4<c3[1], c2[1], c1[1], c0[2]>
>,
AND_4<c0[1], c1[1], c2[1], c3[1]>,
OR_4<
c3[0],
AND<c3[1], c2[0]>,
AND_4<1, c3[1], c2[1], c1[0]>,
AND_4<c3[1], c2[1], c1[1], c0[0]>
>,
];
interface assert { true: equals<COMP_4<N_1, N_5>, [1, 0, 0]> }
interface assert { true: equals<COMP_4<N_9, N_9>, [0, 1, 0]> }
interface assert { true: equals<COMP_4<N_11, N_3>, [0, 0, 1]> }
type COMP<a extends B, b extends B,
/* parameters */
aLessThanB extends B = AND<NOT<b>, a>,
aGreaterThanB extends B = AND<b, NOT<a>>,
equal extends B = NOR<aLessThanB, aGreaterThanB>
> = [aLessThanB, equal, aGreaterThanB];
interface assert { true: equals<COMP<1, 0>, [1, 0, 0]> }
interface assert { true: equals<COMP<0, 0>, [0, 1, 0]> }
interface assert { true: equals<COMP<1, 1>, [0, 1, 0]> }
interface assert { true: equals<COMP<0, 1>, [0, 0, 1]> }
type equals<a extends B[], b extends B[]> = a extends b ? 1 : 0;
interface assert { true: equals<[0, 1], [0, 1]> }
interface assert { false: equals<[0, 1], [1, 1]> }
type XOR<a extends B, b extends B> = AND<NAND<a, b>, OR<a, b>>;
interface assert { true: XOR<1, 0> }
interface assert { true: XOR<0, 1> }
interface assert { false: XOR<1, 1> }
interface assert { false: XOR<0, 0> }
type AND_4<a extends B, b extends B, c extends B, d extends B> = AND<AND<a, b>, AND<c, d>>;
interface assert { true: AND_4<1, 1, 1, 1> }
interface assert { false: AND_4<1, 1, 1, 0> }
interface assert { false: AND_4<0, 1, 0, 1> }
interface assert { false: AND_4<0, 0, 0, 0> }
type AND<a extends B, b extends B> = NOT<NAND<a, b>>;
interface assert { true: AND<1, 1> }
interface assert { false: AND<1, 0> }
interface assert { false: AND<0, 1> }
interface assert { false: AND<0, 0> }
type NOR<a extends B, b extends B> = NOT<OR<a, b>>;
interface assert { true: NOR<0, 0> }
interface assert { false: NOR<1, 0> }
interface assert { false: NOR<0, 1> }
interface assert { false: NOR<1, 1> }
type OR_4<a extends B, b extends B, c extends B, d extends B> = OR<OR<a, b>, OR<c, d>>;
interface assert { true: OR_4<1, 1, 1, 1> }
interface assert { true: OR_4<1, 1, 1, 0> }
interface assert { true: OR_4<0, 0, 0, 1> }
interface assert { false: OR_4<0, 0, 0, 0> }
type OR<a extends B, b extends B> = NAND<NOT<a>, NOT<b>>;
interface assert { true: OR<1, 1> }
interface assert { true: OR<1, 0> }
interface assert { true: OR<0, 1> }
interface assert { false: OR<0, 0> }
type NOT<a extends B> = NAND<a, 1>;
interface assert { true: NOT<0> }
interface assert { false: NOT<1> }
type NAND<a extends B, b extends B> = {
1: {
1: 0;
0: 1;
};
0: {
1: 1;
0: 1;
};
}[a][b];
interface assert { true: NAND<0, 0> }
interface assert { true: NAND<1, 0> }
interface assert { true: NAND<0, 1> }
interface assert { false: NAND<1, 1> }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment