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

Add builtin integer type to the surface language #1948

Merged
merged 38 commits into from
Apr 13, 2023
Merged

Add builtin integer type to the surface language #1948

merged 38 commits into from
Apr 13, 2023

Conversation

paulcadman
Copy link
Collaborator

@paulcadman paulcadman commented Mar 29, 2023

This PR adds a builtin integer type to the surface language that is compiled to the backend integer type.

Inductive definition

The Int type is defined in the standard library as:

builtin int
type Int :=
  | --- ofNat n represents the integer n
    ofNat : Nat -> Int
  | --- negSuc n represents the integer -(n + 1)
    negSuc : Nat -> Int;

New builtin functions defined in the standard library

intToString : Int -> String;
+ : Int -> Int -> Int;
neg : Int -> Int;
* : Int -> Int -> Int;
- : Int -> Int -> Int;
div : Int -> Int -> Int;
mod : Int -> Int -> Int;

== : Int -> Int -> Bool;
<= : Int -> Int -> Bool;
< : Int -> Int -> Bool;

Additional builtins required in the definition of the other builtins:

negNat : Nat -> Int;
intSubNat : Nat -> Nat -> Int;
nonNeg : Int -> Bool;

REPL types of literals

In the REPL, non-negative integer literals have the inferred type Nat, negative integer literals have the inferred type Int.

Stdlib.Prelude> :t 1
Nat
Stdlib.Prelude> :t -1
Int
:t let x : Int := 1 in x
Int

The standard library Prelude

The definitions of *, +, div and mod are not exported from the standard library prelude as these would conflict with the definitions from Stdlib.Data.Nat.

Stdlib.Prelude

open import Stdlib.Data.Int hiding {+;*;div;mod} public;

@paulcadman paulcadman added enhancement New feature or request language labels Mar 29, 2023
@paulcadman paulcadman added this to the 0.4 - Geb integration milestone Mar 29, 2023
@paulcadman paulcadman self-assigned this Mar 29, 2023
@paulcadman paulcadman temporarily deployed to github-pages March 29, 2023 18:05 — with GitHub Actions Inactive
@paulcadman paulcadman force-pushed the builtin-int branch 2 times, most recently from a147928 to c22da7d Compare April 3, 2023 11:00
@paulcadman paulcadman temporarily deployed to github-pages April 3, 2023 11:12 — with GitHub Actions Inactive
@paulcadman paulcadman temporarily deployed to github-pages April 3, 2023 15:42 — with GitHub Actions Inactive
@paulcadman paulcadman temporarily deployed to github-pages April 4, 2023 18:05 — with GitHub Actions Inactive
@paulcadman paulcadman temporarily deployed to github-pages April 6, 2023 17:55 — with GitHub Actions Inactive
@paulcadman paulcadman temporarily deployed to github-pages April 6, 2023 20:31 — with GitHub Actions Inactive
@paulcadman paulcadman temporarily deployed to github-pages April 6, 2023 21:12 — with GitHub Actions Inactive
@paulcadman paulcadman temporarily deployed to github-pages April 6, 2023 22:11 — with GitHub Actions Inactive
@paulcadman paulcadman temporarily deployed to github-pages April 6, 2023 22:29 — with GitHub Actions Inactive
@paulcadman paulcadman temporarily deployed to github-pages April 11, 2023 11:03 — with GitHub Actions Inactive
@paulcadman paulcadman temporarily deployed to github-pages April 11, 2023 11:26 — with GitHub Actions Inactive
@paulcadman paulcadman temporarily deployed to github-pages April 11, 2023 12:05 — with GitHub Actions Inactive
@paulcadman paulcadman temporarily deployed to github-pages April 11, 2023 12:28 — with GitHub Actions Inactive
@paulcadman paulcadman temporarily deployed to github-pages April 11, 2023 12:45 — with GitHub Actions Inactive
@paulcadman paulcadman temporarily deployed to github-pages April 11, 2023 13:04 — with GitHub Actions Inactive
@paulcadman paulcadman temporarily deployed to github-pages April 11, 2023 14:38 — with GitHub Actions Inactive
@paulcadman paulcadman temporarily deployed to github-pages April 11, 2023 15:15 — with GitHub Actions Inactive
@paulcadman paulcadman temporarily deployed to github-pages April 11, 2023 19:12 — with GitHub Actions Inactive
@paulcadman paulcadman changed the title Builtin Integers Add builtin integer type to the surface language Apr 12, 2023
@lukaszcz lukaszcz temporarily deployed to github-pages April 12, 2023 14:50 — with GitHub Actions Inactive
@lukaszcz
Copy link
Collaborator

In the REPL:

Stdlib.Prelude> :type div 4 -2
Nat

The operations on Nats can somehow be used with integers. It shouldn't be possible or the result should have type Int.

@lukaszcz lukaszcz temporarily deployed to github-pages April 12, 2023 15:35 — with GitHub Actions Inactive
@paulcadman
Copy link
Collaborator Author

In the REPL:

Stdlib.Prelude> :type div 4 -2
Nat

The operations on Nats can somehow be used with integers. It shouldn't be possible or the result should have type Int.

Thanks for catching this. I've now fixed it and added a negative type checking test.

The REPL now reports:

Stdlib.Prelude> div 1 -2
/<repl>:1:8-9: error:
The expression -2 has type:
  Int
but is expected to have type:
  Nat

@lukaszcz
Copy link
Collaborator

In the REPL:

Stdlib.Prelude> :type div 4 -2
Nat

The operations on Nats can somehow be used with integers. It shouldn't be possible or the result should have type Int.

Thanks for catching this. I've now fixed it and added a negative type checking test.

The REPL now reports:

Stdlib.Prelude> div 1 -2
/<repl>:1:8-9: error:
The expression -2 has type:
  Int
but is expected to have type:
  Nat

This doesn't solve the problem completely:

Stdlib.Prelude> let f : String -> Nat := \{_ := 0} in f 2
0

Should give an error that 2 is expected to have type String. I guess one also needs to check if the inferred type is actually a subtype of the hint type in src/Juvix/Compiler/Internal/Translation/FromInternal/Analysis/TypeChecking/Checker.hs:626.

@paulcadman paulcadman temporarily deployed to github-pages April 12, 2023 16:52 — with GitHub Actions Inactive
@paulcadman
Copy link
Collaborator Author

This doesn't solve the problem completely:

Stdlib.Prelude> let f : String -> Nat := \{_ := 0} in f 2
0

Should give an error that 2 is expected to have type String. I guess one also needs to check if the inferred type is actually a subtype of the hint type in src/Juvix/Compiler/Internal/Translation/FromInternal/Analysis/TypeChecking/Checker.hs:626.

Thank you - I've tried to solve the problem again.

@paulcadman paulcadman temporarily deployed to github-pages April 12, 2023 17:51 — with GitHub Actions Inactive
@paulcadman paulcadman temporarily deployed to github-pages April 12, 2023 18:08 — with GitHub Actions Inactive
@lukaszcz lukaszcz temporarily deployed to github-pages April 12, 2023 18:51 — with GitHub Actions Inactive
@paulcadman paulcadman merged commit ea09ec3 into main Apr 13, 2023
@paulcadman paulcadman deleted the builtin-int branch April 13, 2023 07:16
lukaszcz pushed a commit to anoma/juvix-stdlib that referenced this pull request Apr 14, 2023
This PR provides the implementations of builtins defined in
anoma/juvix#1948
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request language
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Negative number literals should not be typed as Nat Add an integer (Int) type in the surface language
2 participants