Inverse of a bijective function programmatically

This commit is contained in:
Chris Allen 2014-08-15 15:51:50 -05:00
parent 3044bb2b85
commit 1e534f2fa8

View File

@ -1176,3 +1176,20 @@ g xs = map last $ groupBy (<) xs
λ> map last $ groupBy (<) [1, 2, 1, 2, 3, 1, 2, 3, 4, 5, 6, 1, 1, 2, 3, 4]
[2,3,6,1,4]
```
## Programmatically finding the inverse of a bijective function
tel:
If the range is finite and the domain has decidable equality
```haskell
class Finite a where
-- x :: a iff elem x universe = True
universe :: [a]
inv :: (Eq b, Finite a) => (a -> b) -> (b -> a)
inv f b = let [a] = [ a | a <- universe, f a == b ] in a
```
The pattern matching above will fail if f is not bijective.