I didn't have any problems understanding how fix function works in Haskell, but I just can't wrap my head around Fix data type. I believe I get the idea behind it, but I just can't understand how Fix data type definition works under the hood. So, Fix is defined like this:
newtype Fix f = Fix { unfix :: f (Fix f) } The way I parse this definition in my head is that Fix as a data type is a record with one field, unfix, which has a type of f (Fix f). So, in order to construct this type, you use Fix type constructor with an argument in form of f (Fix f), correct?
If my understanding so far is indeed correct, then I need an explanation why the following is accepted by the compiler:
data T a = Dummy t = Fix Dummy Here, the argument to Fix is not in the form of f (Fix f), but the compiler doesn't reject the code, but why? f (Fix f) seems like two terms thingy to me, one term for f and another for Fix f, but Dummy is just one term, so for me the code above should not work for that reason alone. Apparently, the compiler does something under the hood that I'm missing in my reasoning and this is what I need help with.
And the bonus question is why Fix 1 is also accepted by the compiler and what's going on in this case?
Dummyhave?Fixhas a single field, so there's only supposed to be a single term. That term's type has to be of the formf (Fix f), not the term itself. (Althoughf (Fix f)is also a single type-level expression, not "two terms"; it'sfapplied toFix f, notfand then a separateFix f)