Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Literal casting #2457

Merged
merged 14 commits into from
Nov 3, 2023
Merged

Literal casting #2457

merged 14 commits into from
Nov 3, 2023

Conversation

lukaszcz
Copy link
Collaborator

@lukaszcz lukaszcz commented Oct 20, 2023

  • Closes Automatic casting of integer literals to the right type #2453
  • Closes Inferring the types of number literals doesn't work when the literals are used with generic functions #2432
  • Any nonnegative literal n is replaced with fromNat {_} {{_}} n where fromNat is the builtin conversion function defined in the Natural trait in Stdlib.Trait.Natural.
  • Any negative literal -n is replaced with fromInt {_} {{_}} -n where fromInt is the builtin conversion function defined in the Integral trait in Stdlib.Trait.Integral.
  • Before resolving instance holes, it is checked whether the type holes introduced for fromNat and fromInt have been inferred. If not, an attempt is made to unify them with Nat or Int. This allows to type-check e.g. 1 == 1 (there is no hint in the context as to what the type of 1 should be, so it is decided to be Nat after inferring the hole fails).

@lukaszcz lukaszcz added enhancement New feature or request syntax labels Oct 20, 2023
@lukaszcz lukaszcz added this to the 0.5.4 milestone Oct 20, 2023
@lukaszcz lukaszcz self-assigned this Oct 20, 2023
@lukaszcz lukaszcz force-pushed the literal-casting branch 2 times, most recently from 5a6d56b to ccc678b Compare October 23, 2023 16:53
@lukaszcz lukaszcz marked this pull request as ready for review October 23, 2023 17:12
@lukaszcz lukaszcz requested review from paulcadman and janmasrovira and removed request for paulcadman October 23, 2023 17:12
@lukaszcz lukaszcz force-pushed the literal-casting branch 3 times, most recently from 0a9a639 to 25a5396 Compare October 24, 2023 09:24
@paulcadman
Copy link
Collaborator

I get an evaluation error in the REPL if I type 1 == 1 which does not occur on the main branch:

Stdlib.Prelude> 1 == 1
/repl:1:1: error:
evaluation error: no matching case branch: case mkNatural Int (λ(_X : Int)
  λ(_X' : Int) + _X _X') (λ(_X : Int)
  λ(_X' : Int) * _X _X') (λ(_X : Int)
  λ(_X' : Int) / _X _X') (λ(_X : Int)
  λ(_X' : Int) % _X _X') (λ(x : Int) x) of {
  mkEq (_X : Type) (a : naturalNatI → naturalNatI → Bool) := a
}

It works if I evaluate let x1 : Nat := 1; x2 : Nat := 1 in x1 == x2 or let x1 : Int := 1; x2 : Int := 1 in x1 == x2 instead.

@lukaszcz
Copy link
Collaborator Author

I get an evaluation error in the REPL if I type 1 == 1 which does not occur on the main branch:

Stdlib.Prelude> 1 == 1
/repl:1:1: error:
evaluation error: no matching case branch: case mkNatural Int (λ(_X : Int)
  λ(_X' : Int) + _X _X') (λ(_X : Int)
  λ(_X' : Int) * _X _X') (λ(_X : Int)
  λ(_X' : Int) / _X _X') (λ(_X : Int)
  λ(_X' : Int) % _X _X') (λ(x : Int) x) of {
  mkEq (_X : Type) (a : naturalNatI → naturalNatI → Bool) := a
}

It works if I evaluate let x1 : Nat := 1; x2 : Nat := 1 in x1 == x2 or let x1 : Int := 1; x2 : Int := 1 in x1 == x2 instead.

Hmm, strange. 1 == 1 works if you evaluate a file with:

main : Bool := 1 == 1;

I'll look into that next week.

@lukaszcz
Copy link
Collaborator Author

lukaszcz commented Nov 2, 2023

I get an evaluation error in the REPL if I type 1 == 1 which does not occur on the main branch:

This was a very subtle error in how nameIds are generated in the REPL. That's why I don't like these effects so much -- sometimes it's not immediately clear how running them actually works.

--- a/src/Juvix/Compiler/Pipeline/Repl.hs
+++ b/src/Juvix/Compiler/Pipeline/Repl.hs
@@ -26,12 +26,11 @@ arityCheckExpression ::
   Sem r Internal.Expression
 arityCheckExpression p = do
   scopeTable <- gets (^. artifactScopeTable)
-  runNameIdGenArtifacts
-    . runBuiltinsArtifacts
+  runBuiltinsArtifacts
     . runScoperScopeArtifacts
     . runStateArtifacts artifactScoperState
-    $ Scoper.scopeCheckExpression scopeTable p
-      >>= Internal.fromConcreteExpression
+    $ runNameIdGenArtifacts (Scoper.scopeCheckExpression scopeTable p)
+      >>= runNameIdGenArtifacts . Internal.fromConcreteExpression
       >>= Internal.arityCheckExpression

@lukaszcz lukaszcz merged commit 485f6e7 into main Nov 3, 2023
4 checks passed
@lukaszcz lukaszcz deleted the literal-casting branch November 3, 2023 09:01
paulcadman added a commit to anoma/juvix-stdlib that referenced this pull request Nov 7, 2023
* Adapts to anoma/juvix#2457

---------

Co-authored-by: Paul Cadman <git@paulcadman.dev>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
3 participants