Caterpillar

Daily Thought - 2024-07-06

< back to list

So, how do you track I/O in a functional language? Haskell does it by encoding I/O operations into a monad. (If you don't know what a monad is, then for the purpose of this discussion, just think it as a container for data that describes a series of I/O operations.) That monad is returned to the runtime, which executes the operations.

I think I understand why Haskell does it like that, but it's not how I want to do it in Caterpillar. Instead, I want to represent anything that can do I/O (like a file, for example) as values. A function that writes to a file needs to take the value representing that file as an argument, and return an updated version, which represents the file after it's been written to.

That way, the developer, the compiler, or the runtime, can clearly see the file value going through that function. I think this requires linear (or affine) types though, and that's probably the reason Haskell uses the (seemingly) more complicated monad-based technique. (I don't have enough room left to explain linear types today, but I'm sure I'll get to them sooner or later.)

<< previous thoughtnext thought >>