rascal icon indicating copy to clipboard operation
rascal copied to clipboard

False positive: addition and subtraction on datetime values

Open rodinaarssen opened this issue 3 years ago • 13 comments

Describe the bug

The type checker flags addition and subtraction on datetime values as errors.

To Reproduce

datetime hoi = now();
datetime doei = now();
datetime added = hoi + doei;
datetime subtracted = doei - hoi;

Screenshots image

rodinaarssen avatar May 18 '22 14:05 rodinaarssen

Oops, you are completely right: two missing cases. Thanks!

PaulKlint avatar May 18 '22 15:05 PaulKlint

Solving this in the type checker is trivial (have already fixed it). For the compiler I have to study this a bit more: to my surprise IDateTime does not provide add or subtract operations so their implementation must be hidden in the interpreter somewhere ...

PaulKlint avatar May 18 '22 15:05 PaulKlint

I should have checked, but it seems that only subtraction is allowed in the interpreter..

rascal>now() + now()
|prompt:///|(8,3,<1,8>,<1,11>): insert into collection not supported on datetime and datetime
Advice: |http://tutor.rascal-mpl.org/Errors/Static/UnsupportedOperation/UnsupportedOperation.html|
ok
rascal>now() - now()
Duration: duration(0,0,0,0,0,0,0)

rodinaarssen avatar May 18 '22 15:05 rodinaarssen

https://github.com/usethesource/rascal/blob/d110bafba97d8dfe4e863754544fa47aa61630ae/src/org/rascalmpl/interpreter/result/DateTimeResult.java#L357

rodinaarssen avatar May 18 '22 15:05 rodinaarssen

Indeed!

This means that I will have to port that code to the runtime of the compiler.

PaulKlint avatar May 18 '22 15:05 PaulKlint

Unfortunately yes; this part of the DateTime API is waiting patiently on the introduction of "units and dimensions" in vallang and Rascal, such that datetime - datetime will be an int[Duration] or a real[Duration] in seconds, years, ...

jurgenvinju avatar May 18 '22 15:05 jurgenvinju

When we have that, the code can move into IDateTime, but before that time it seems clunky to move it there. Of course, it is possible, but we'd have to add more "bootstrap" code in terms of manual declarations of data types for the representation of differences.

jurgenvinju avatar May 18 '22 15:05 jurgenvinju

Fair enough, let's not go into this rat hole for now :-)

PaulKlint avatar May 18 '22 15:05 PaulKlint

As part of this rewriting I came across very strange stuff around datetimes.

For example there want is also a datetime library, that also has a duration adt.

DavyLandman avatar May 18 '22 16:05 DavyLandman

Yes, way back when we decided that a Duration ADT was a bridge too far for the - operator to produce so I think we settled for a tuple if I recall correctly. For the library that made less sense. Hence we got stuck with two representations for durations. I believe, it was always @mahills perspective that this would be rewritten eventually using a dimension/unit type system.

jurgenvinju avatar May 19 '22 07:05 jurgenvinju

That is what confused me, it's the inverse, the - operator returns a ADT, while the library returns a tuple.

see:

https://github.com/usethesource/rascal/blob/d110bafba97d8dfe4e863754544fa47aa61630ae/src/org/rascalmpl/interpreter/result/DateTimeResult.java#L368-L377

and:

https://github.com/usethesource/rascal/blob/d110bafba97d8dfe4e863754544fa47aa61630ae/src/org/rascalmpl/library/Prelude.java#L420-L425

DavyLandman avatar May 19 '22 08:05 DavyLandman

thanks for the references. that's exactly right. let's leave it for now until we have the dimensions. otherwise, we'd have yet another breaking change in the library and language when we introduce that.

jurgenvinju avatar May 19 '22 09:05 jurgenvinju

True, I would be surprised if many people use this feature, but I agree to leave it as is.

DavyLandman avatar May 19 '22 09:05 DavyLandman