Challenge: Given an expressions made of additions and multiplications, output an expression that is a sum of products. The output must be equivalent to the input modulo the law of distribution. For example, \$1 + ((2 + 5\times 6) \times(3+4))\$ becomes \$1 + 2 \times 3 + 2 \times 4 + 5\times6\times3 + 5 \times 6 \times 4 \$. This is code-golf.
This task is useful in automatic theorem proving, since the conversion to disjunctive normal forms is exactly the same task. (Oops, that gives away a Mathematica builtin!)
Clarifications:
- You can assume the numbers are whatever you want, integers, floats, or even just strings of symbols, as long as it's convenient.
- You can use the law of distribution, commutativity and associativity of addition and multiplication. But you cannot use any other property of addition or multiplication. So e.g. \$3 \times (1 + 3) \to 3 + 3 \times 3\$ is not acceptable.
- This means that answers are not unique, the test cases are just a reference.
- Parentheses are optional, you won't need them anyway.
- You can assume any reasonable input form.
Test cases:
2 -> 2 (Corner case, you can ignore this.)
1+3 -> 1+3
1+(3*4) -> 1+3*4
1+3*4 -> 1+3*4
(1+3)*4 -> 1*4+3*4
(1+2+3)*(4+5+6) -> 1*4+2*4+3*4+1*5+2*5+3*5+1*6+2*6+3*6
(1+2*(3+4))*((5+6)*7)+8 -> 1*5*7+1*6*7+2*3*5*7+2*3*6*7+2*4*5*7+2*4*6*7+8
P(1, M(3, 4))
instead of1+(3*4))
? \$\endgroup\$f(1*(2+3))="1*2+1*3"
? \$\endgroup\$