This isn’t a homework question, by the way. It got brought up in class but my teacher couldn’t think of any. Thanks.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
How do you define the identity functions ? If you’re only considering the syntax, there are different identity functions, which all have the correct type:
There are even weirder functions:
If you restrict yourself to a pure subset of OCaml (which rules out f7 and f8), all the functions you can build verify an observational equation that ensures, in a sense, that what they compute is the identity : for all value
f : 'a -> 'a, we have thatf x = xThis equation does not depend on the specific function, it is uniquely determined by the type. There are several theorems (framed in different contexts) that formalize the informal idea that “a polymorphic function can’t change a parameter of polymorphic type, only pass it around”. See for example the paper of Philip Wadler, Theorems for free!.
The nice thing with those theorems is that they don’t only apply to the
'a -> 'acase, which is not so interesting. You can get a theorem out of the('a -> 'a -> bool) -> 'a list -> 'a listtype of a sorting function, which says that its application commutes with the mapping of a monotonous function.More formally, if you have any function
swith such a type, then for all typesu, v, functionscmp_u : u -> u -> bool,cmp_v : v -> v -> bool,f : u -> v, and listli : u list, and ifcmp_u u u'impliescmp_v (f u) (f u')(f is monotonous), you have :This is indeed true when
sis exactly a sorting function, but I find it impressive to be able to prove that it is true of any functionswith the same type.Once you allow non-termination, either by diverging (looping indefinitely, as with the
let rec f x = f xfunction given above), or by raising exceptions, of course you can have anything : you can build a function of type'a -> 'b, and types don’t mean anything anymore. UsingObj.magic : 'a -> 'bhas the same effect.There are saner ways to lose the equivalence to identity : you could work inside a non-empty environment, with predefined values accessible from the function. Consider for example the following function :
You still that the property that for all x,
f x = x: if you only consider the return value, your function still behaves as the identity. But once you consider side-effects, you’re not equivalent to the (side-effect-free) identity anymore : if I knowcounter, I can write a separating function that returnstruewhen given this functionf, and would return false for pure identity functions.If counter is hidden (for example by a module signature, or simply
let f = let counter = ... in fun x -> ...), and no other function can observe it, then we again can’t distinguishfand the pure identity functions. So the story is much more subtle in presence of local state.