-
Notifications
You must be signed in to change notification settings - Fork 350
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
fail to synthesize if with constant natural numbers #770
Comments
This is not a bug. Lean is working as expected. There is a lot going on in this example.
There are also many ways to address this issue.
|
FYI I use this feature all the time to get local mutation. |
It would still be doable, you would just have to explicitly use the BTW, I am not pushing for this solution, but I have seen users suggesting it. |
I don't agree with option 1 "Novice users would not use do blocks in pure code.". I am a Novice user and I just did :-) So at a minmum we need a better error message pointing the user to these 2 possible solutions. But I still argue it is inconsistent at best that my first code block in my original bug rep[ort works, but the second does not. So the 2 fixes I've found so far are, add a return statement: def u (x : Nat) : Nat := do
let y := x + x
return y * (if y > 100 then 1 else 5) remove the do def u (x : Nat) : Nat :=
let y := x + x
y * (if y > 100 then 1 else 5) |
Not sure you are a novice user ;)
They are not the same. In the first one, you had
In both cases, you are avoiding the |
No I really am a Novice so much so I didn't even know numerals were polymorphic. Now that you pointed that out I see there's another fix: def u (x : Nat) : Nat := do
let y := x + x
y * (if y > 100 then 1 else (5 : Nat)) So how can we improve this error message so I would have realized that is what is going on ? I don't even know how to read this message:
I can guess "HMul" is what the multiply elaborated to, Nat is the type that 'y' has already but |
And where do we write about the fact that numerals are polymorphic in the reference docs? I think dependent_type_theory.md brilliantly discussed polymorphism using List as an example which made all kinds of sense, but it missed the opportunity to really blow my mind and point out that numbers are polymorphic too! And what made me try this example in the first place? I was exploring what you can do with the second part of a let statement, how much code can that second part expand to, and apparently, it can be a lot of code - with nested if-expressions I could have 10 pages of code where the bound variable |
You are not the standard novice either. The standard novice user would start using Lean in pure code, read the tutorial, and prove theorems. The
We have to prioritize the issues and focus on the ones that are blocking the Mathlib port.
It is the identity monad. If you jump to its definition, you will find def Id (type : Type u) : Type u := type
namespace Id
instance : Monad Id where
pure x := x
bind x f := f x
map f x := f x
Here: https://leanprover.github.io/theorem_proving_in_lean4/type_classes.html#numerals |
Just to record my preferred solution here: I believe pure |
I don't like this one. It is extra complexity, a new keyword, and the benefit looks small to me. |
In Haskell, I used to use macro "puredo" cmds:doSeq : term => `(Id.run do $cmds)
example : Array Nat := puredo
let mut xs := #[]
for i in [:10] do xs := xs.push i
return xs |
Only where it helps elaboration by hiding |
(Strictly speaking, this part is independent from the "separate keyword" proposal) |
Note that your suggestion does not fix the issue here. macro "puredo" cmds:doSeq : term => `(Id.run do $cmds)
def u (x : Nat) : Nat := puredo
let y := x + x
y * (if y > 100 then 1 else 5) would still produce the same error. |
It may work, but this kind of change can easily trigger unforeseen problems. |
I was thinking that the |
Yes, |
I second this. Also, @dselsam your example usage of the pure
is very questionable. What separates pure programming and monadic programming conceptually is precisely the forbidding of things like mutations. In a functional language, If one is using mutations, they should be doing so in a monadic context. And if one doesn't want the algorithm to pollute the function signature with an The magical injection of a monadic context that Lean currently supports may be a little convenient (though is typing Thus, I really think that the idea of a pure |
@tydeu Yeah, the error message "can't use do in a pure context" would solve the problem. |
Thanks @leodemoura , looks great. |
Prerequisites
Description
I can write this:
but if I change the final y to a constant
5
it no longer compiles:Steps to Reproduce
See above.
Expected behavior: [What you expect to happen]
should work
Actual behavior: [What actually happens]
Reproduces how often: [What percentage of the time does it reproduce?]
Versions
Lean (version 4.0.0-nightly-2021-11-07, commit d5e05f3, Release)
Additional Information
Any additional information, configuration or data that might be necessary to reproduce the issue.
The text was updated successfully, but these errors were encountered: