Assignment 2

Problems

Problem 1

Integers, like booleans, do not have a natural associative operation. However, both addition and multiplication are associative operations on integers. Write Semigroup instances for the Sum and Product types wrapping Int in the starter code, mirroring the implementations of Any and All.

See solution to Problem 3.

Problem 2

The default given above has \(O(n)\) performance, which is less efficient than the best general implementation. Using the associative property of <>, provide a more efficient implementation, stimes', with time complexity \(O(\log n)\).

Problem 3

While \(O(\log n)\) is the best possible performance in general, specific instances can perform even better. Extend your instances for Sum and Product with \(O(1)\) implementations of stimes.