This is my program:
modify :: Integer -> Integer
modify a = a 100
x = x where modify(x) = 101
In ghci, this compiles successfully but when I try to print x the terminal gets stuck. Is it not possible to find input from function output in Haskell?
CodePudding user response:
x = x where modify(x) = 101
is valid syntax but is equivalent to
x = x where f y = 101
where x = x
is a recursive definition, which will get stuck in an infinite loop (or generate a <<loop>>
exception), and f y = 101
is a definition of a local function, completely unrelated to the modify
function defined elsewhere.
If you turn on warnings you should get a message saying "warning: the local definition of modify
shadows the outer binding", pointing at the issue.
Further, there is no way to invert a function like you'd like to do. First, the function might not be injective. Second, even if it were such, there is no easy way to invert an arbitrary function. We could try all the possible inputs but that would be extremely inefficient.