Last active
September 19, 2019 11:51
-
-
Save acutmore/a9e09be531f565ba3415200400a0ce31 to your computer and use it in GitHub Desktop.
logic in typescript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 DIV_4< | |
numerator extends BITS_4, | |
divisor extends BITS_4, | |
/* variables: */ | |
count extends BITS_4 = N_0, | |
remainder extends BITS_4 = numerator | |
> = { | |
// remainder < divisor | |
1: {count:count, remainder:remainder}, | |
// remainder >= divisor | |
0: DIV_4<numerator, divisor, ADD_4<count, N_1>, SUB_4<remainder, divisor>> | |
}[ | |
COMP_4<remainder, divisor>['less'] extends 1 ? 1 : 0 | |
]; | |
interface assert { true: equals<DIV_4<N_4, N_2>, {count:N_2, remainder:N_0}> } | |
interface assert { true: equals<DIV_4<N_9, N_2>, {count:N_4, remainder: N_1}> } | |
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 SUBTRACT_RESULT = FULL_SUBTRACTOR<0, regA[0], regB[0]>, | |
b extends SUBTRACT_RESULT = FULL_SUBTRACTOR<a['borrow'], regA[1], regB[1]>, | |
c extends SUBTRACT_RESULT = FULL_SUBTRACTOR<b['borrow'], regA[2], regB[2]>, | |
d extends SUBTRACT_RESULT = FULL_SUBTRACTOR<c['borrow'], regA[3], regB[3]>, | |
> = [a['diff'], b['diff'], c['diff'], d['diff']]; | |
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 ADDER_RESULT = FULL_ADDER<0, regA[0], regB[0]>, | |
b extends ADDER_RESULT = FULL_ADDER<a['carry'], regA[1], regB[1]>, | |
c extends ADDER_RESULT = FULL_ADDER<b['carry'], regA[2], regB[2]>, | |
d extends ADDER_RESULT = FULL_ADDER<c['carry'], regA[3], regB[3]>, | |
> = [a['sum'], b['sum'], c['sum'], d['sum']]; | |
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 SUBTRACT_RESULT = HALF_SUBTRACTOR<a, b>, | |
subtractor2 extends SUBTRACT_RESULT = HALF_SUBTRACTOR<subtractor1['diff'], borrow>, | |
> = { | |
diff:subtractor2['diff'], | |
borrow:OR<subtractor1['borrow'], subtractor2['borrow']> | |
}; | |
interface assert { true: equals<FULL_SUBTRACTOR<0, 0, 0>, {diff:0, borrow:0}> } | |
interface assert { true: equals<FULL_SUBTRACTOR<1, 0, 0>, {diff:1, borrow:1}> } | |
interface assert { true: equals<FULL_SUBTRACTOR<0, 1, 0>, {diff:1, borrow:0}> } | |
interface assert { true: equals<FULL_SUBTRACTOR<0, 0, 1>, {diff:1, borrow:1}> } | |
interface assert { true: equals<FULL_SUBTRACTOR<1, 1, 0>, {diff:0, borrow:0}> } | |
interface assert { true: equals<FULL_SUBTRACTOR<1, 0, 1>, {diff:0, borrow:1}> } | |
interface assert { true: equals<FULL_SUBTRACTOR<0, 1, 1>, {diff:0, borrow:0}> } | |
interface assert { true: equals<FULL_SUBTRACTOR<1, 1, 1>, {diff:1, borrow:1}> } | |
type FULL_ADDER< | |
carry extends B, | |
a extends B, | |
b extends B, | |
/* variables: */ | |
adder1 extends ADDER_RESULT = HALF_ADDER<a, b>, | |
adder2 extends ADDER_RESULT = HALF_ADDER<carry, adder1['sum']> | |
> = { | |
sum:adder2['sum'], | |
carry:OR<adder2['carry'], adder1['carry']> | |
}; | |
interface assert { true: equals<FULL_ADDER<0, 0, 0>, {sum:0, carry:0}> } | |
interface assert { true: equals<FULL_ADDER<1, 0, 0>, {sum:1, carry:0}> } | |
interface assert { true: equals<FULL_ADDER<0, 1, 0>, {sum:1, carry:0}> } | |
interface assert { true: equals<FULL_ADDER<0, 0, 1>, {sum:1, carry:0}> } | |
interface assert { true: equals<FULL_ADDER<1, 1, 0>, {sum:0, carry:1}> } | |
interface assert { true: equals<FULL_ADDER<1, 0, 1>, {sum:0, carry:1}> } | |
interface assert { true: equals<FULL_ADDER<0, 1, 1>, {sum:0, carry:1}> } | |
interface assert { true: equals<FULL_ADDER<1, 1, 1>, {sum:1, carry:1}> } | |
type SUBTRACT_RESULT = {diff: B, borrow: B}; | |
type ADDER_RESULT = {sum: B, carry: B}; | |
type HALF_SUBTRACTOR<a extends B, b extends B> = { | |
diff: XOR<a, b>, | |
borrow: AND<NOT<a>, b> | |
}; | |
interface assert { true: equals<HALF_SUBTRACTOR<0, 0>, {diff:0, borrow:0}> } | |
interface assert { true: equals<HALF_SUBTRACTOR<1, 0>, {diff:1, borrow:0}> } | |
interface assert { true: equals<HALF_SUBTRACTOR<0, 1>, {diff:1, borrow:1}> } | |
interface assert { true: equals<HALF_SUBTRACTOR<1, 1>, {diff:0, borrow:0}> } | |
type HALF_ADDER<a extends B, b extends B> = {sum:XOR<a, b>, carry:AND<a, b>}; | |
interface assert { true: equals<HALF_ADDER<0, 0>, {sum:0, carry:0}> } | |
interface assert { true: equals<HALF_ADDER<1, 0>, {sum:1, carry:0}> } | |
interface assert { true: equals<HALF_ADDER<0, 1>, {sum:1, carry:0}> } | |
interface assert { true: equals<HALF_ADDER<1, 1>, {sum:0, carry: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, | |
/* variables: */ | |
c0 extends COMP_RESULT = COMP<a[0], b[0]>, | |
c1 extends COMP_RESULT = COMP<a[1], b[1]>, | |
c2 extends COMP_RESULT = COMP<a[2], b[2]>, | |
c3 extends COMP_RESULT = COMP<a[3], b[3]>, | |
> = { | |
less: OR_4< | |
c3['grt'], | |
AND<c3['eq'], c2['grt']>, | |
AND_4<1, c3['eq'], c2['eq'], c1['grt']>, | |
AND_4<c3['eq'], c2['eq'], c1['eq'], c0['grt']> | |
>, | |
eq: AND_4<c0['eq'], c1['eq'], c2['eq'], c3['eq']>, | |
grt: OR_4< | |
c3['less'], | |
AND<c3['eq'], c2['less']>, | |
AND_4<1, c3['eq'], c2['eq'], c1['less']>, | |
AND_4<c3['eq'], c2['eq'], c1['eq'], c0['less']> | |
>, | |
}; | |
interface assert { true: equals<COMP_4<N_1, N_5>, {less:1, eq:0, grt:0}> } | |
interface assert { true: equals<COMP_4<N_9, N_9>, {less:0, eq:1, grt:0}> } | |
interface assert { true: equals<COMP_4<N_11, N_3>, {less:0, eq:0, grt:1}> } | |
type COMP_RESULT = {less: B, eq: B, grt: B}; | |
type COMP<a extends B, b extends B, | |
/* variables */ | |
aLessThanB extends B = AND<NOT<b>, a>, | |
aGreaterThanB extends B = AND<b, NOT<a>>, | |
equal extends B = NOR<aLessThanB, aGreaterThanB> | |
> = {less: aLessThanB, eq: equal, grt: aGreaterThanB}; | |
interface assert { true: equals<COMP<1, 0>, {less:1, eq:0, grt:0}> } | |
interface assert { true: equals<COMP<0, 0>, {less:0, eq:1, grt:0}> } | |
interface assert { true: equals<COMP<1, 1>, {less:0, eq:1, grt:0}> } | |
interface assert { true: equals<COMP<0, 1>, {less:0, eq:0, grt:1}> } | |
type equals<a extends any, b extends any> = 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