[Haskell-cafe] Source code location in IO?

MarLinn monkleyon at googlemail.com
Tue Jun 21 18:27:17 UTC 2016

>> Is it compatible with the semantics of Haskell to have a function 
>> sourceLocation :: IO String which when run returns the source file 
>> location at which it is used? 
> It's totally compatible with Haskell semantics, since Haskell does not 
> give any semantics to IO. Anything can happen when you do IO. But it's 
> still a bit weird. :) 

I think the weird feeling stems exactly from the fact that IO has no 
well-defined syntax. It's our convenient catch-all for anything remotely 

Nondeterminism? Put it in IO! (Instead of a purely nondeterministic context)
Non-destructive read access to a file? Put it in IO! (instead of adding 
Maybe to a nondeterministic context)
Deterministic fire-and-forget write access to a log file? Put it in IO! 
(instead of a context that just provides an ordering)
Firing missiles or calling the system to do an "rm -rf /"? Put it in IO! 
(instead of a 

So let's just view IO as a shorthand for some unknown as-yet unspecified 
monad stack. (But note that most of the contexts I mentioned don't even 
need to be monadic)
This is not to say IO is not good to have in this form. It's just not a 
helpful context to answer this question.

 From this vantage point a possibly better form of the question would 
be: Could we define a context which contains the required semantics that 
is consistent with Haskell syntax?
Something like

     thisLine :: SourceInfo Int

Of course once you have such a context you can just assume it's part of 
IO because everything is part of IO.

To answer the modified question just think about these facts:

1. We can define contexts with 100% determinism across almost any 
transformation including changing all the source code around it, the 
compiler, the language version, and all optimizations. (pure functions, 
i.e. the Identity context)
2. We can also define contexts with almost 100% non-determinism even 
across two consecutive invocations with nothing else changed. (calls to 
external random number generators)
3. The context we are searching for would be deterministic across an 
intermediate subset of meta-transformations.

So if these special semantics were not allowed we would have an 
inconsistent scale of determinism across meta-transformations, and that 
would need some serious explaining. Ergo it should be allowed.

This trail of thought does bring up two interesting follow-up questions 
though: Is the hidden structure of the IO stack tightly bound to a 
theory of consistency across meta-transformations? And do we even have a 
suitable theory of such transformations if we would want to refine IO? I 
have no idea.


More information about the Haskell-Cafe mailing list