4 ms·
What does it mean for a type class to be "law-abiding"? I'm coming from Rust so I'm curious what the "issue" is with FromIterator.
by cjstadler 6y ago
What does it mean for a type class to be "law-abiding"? I'm coming from Rust so I'm curious what the "issue" is with FromIterator.
- joppy 6y agoI don’t know if there is an “issue” with fromIterator, the comment is just that it is ad-hoc: there is no law for it to satisfy. Law-abiding means that some equation holds, for example a monoid instance should be associative, so ((a <> b) <> c) should always produce the same result as (a <> (b <> c)). There is nothing checking that my implementation of <> abides by this law, but other programmers (and optimisers or compilers, maybe?) can make use of it, and might write incorrect code if my instance does not abide by the law. Another example would be functors: it should be the case that (fmap f) . (fmap g) is the same as fmap (f . g), for all functions f and g that make sense in the equation (output type of g must match input type of f).