jstolarek / slicer

Companion code for paper "Imperative Functional Programs that Explain their Work", Wilmer Ricciotti, Jan Stolarek, Roly Perera and James Cheney, ICFP 2017, Oxford, UK
http://dl.acm.org/citation.cfm?id=3110258
GNU General Public License v3.0
6 stars 0 forks source link

Add support for exceptions in TML programs #23

Closed jstolarek closed 7 years ago

jstolarek commented 7 years ago

I implemented support for exceptions. What works:

What does not work:

What needs attention:

See Note [Evaluation of exceptions] for more details

jamescheney commented 7 years ago

Thanks, but I don't think it's necessary (and may not be correct) to try to track the types of thrown exceptions. I will try to explain more clearly what the type system for exceptions should be like in the paper, but you can see what I had in mind here:

http://www.inf.ed.ac.uk/teaching/courses/epl/2016/lectures/lec16.pdf

(I think there is a similar discussion in TAPL but think you have my copy.)

jstolarek commented 7 years ago

I should have made sure we discussed how the type system for exceptions works in ML-like languages.

Yes, I was aware that what I'm implementing is different from presentation in TAPL. The reason I allowed raising arbitrary expressions as exceptions values was that this is what the rules in our paper say. Secondary reason was that we don't really have a proper support for data types and pattern matching. So I would either have to rewrite these parts of the interpreter or come up with some sort of hack to allow defining new values of error data type. Again, something that can be done but will take time.

I'm also nor sure the proposed type system is correct, for example if you call a function in an expression it may raise an exception that has a different type than expected

Sorry, I don't follow. Could you give an example?

jamescheney commented 7 years ago

The reason I allowed raising arbitrary expressions as exceptions values was that this is what the rules in our paper say.

We didn't give typing rules, though. I would suggest that we pick a specific type (say, string) for exceptions for now, since the issue of defining extensible exception types and slicing for general pattern matching is somewhat orthogonal to the control-flow issues of exception handling, which are our focus here.

Sorry, I don't follow. Could you give an example?

Consider "map". It takes a function as an argument. So we can do this:

let foo (f,g) = (map f [1,2,3], map g [1,2,3])
let bar (x) = raise "abc"
in try (foo(bar,\x -> raise 123)) with x -> x + 1

I believe something like this will be well typed according to your rules, because the raise inside "try" raises an int and the handler handles an int. But the actual exception that gets raised will be of type string. The types of f and g do not tell us anything about what kinds of exceptions they (may) raise. Dealing with this correctly would require some kind of effect annotations on the functions/types to characterize what exceptions they might raise (as in Java).

jstolarek commented 7 years ago

We didn't give typing rules, though.

True - I had to come up with an ad-hoc solution. And indeed, your example will derail my type system.

For now I will assume that strings are the only allowed values of exceptions and keep the hack with ExnTy. Fix coming. I'll start separate tickets to track further improvements to the exceptions mechanism.

jstolarek commented 7 years ago

Updated - please review.

jamescheney commented 7 years ago

Thanks, looks fine.