Commit graph

71 commits

Author SHA1 Message Date
Leonardo de Moura
d4a67baa8e refactor: rename MonadFinally.finally' => MonadFinally.tryFinally' 2020-10-22 17:40:30 -07:00
Leonardo de Moura
02521397ac refactor: rename MonadExceptOf.catch => MonadExceptOf.tryCatch 2020-10-22 17:27:15 -07:00
Leonardo de Moura
82ee2e361b chore: cleanup 2020-10-21 18:43:47 -07:00
Leonardo de Moura
1495f403a1 chore: use builtin_initialize instead of initialize at src/Lean 2020-10-19 15:17:02 -07:00
Leonardo de Moura
437f4670ed fix: expand doIf notation before lifting nested methods 2020-10-19 11:32:51 -07:00
Leonardo de Moura
c05f73577a fix: expand doElem macros *before* lifting nested methods 2020-10-19 11:26:14 -07:00
Leonardo de Moura
7bfa39ae45 fix: for .. in .. do notation and universe constraints
We use `MProd` instead of `Prod` to group values when expanding the
`do` notation. `MProd` is a universe monomorphic product.
The motivation is to generate simpler universe constraints in code
that was not written by the user but generated by the `do` macro.
Note that we are not really restricting the macro power since the
`HasBind.bind` combinator already forces values computed by monadic
actions to be in the same universe.

The new test cannot be compiled without this modication.
2020-10-18 18:05:00 -07:00
Leonardo de Moura
63e982768a feat: expand nested dos 2020-10-15 17:11:50 -07:00
Leonardo de Moura
3cfff9df14 chore: remove workarounds 2020-10-15 15:34:36 -07:00
Leonardo de Moura
d1ad5eb51a chore: add workarounds 2020-10-15 14:56:38 -07:00
Leonardo de Moura
827625a377 perf: add temporary hack for performance issue
The compiler frontend implemented in C++ is eagerly inlining local
functions. The new test would take an absurd amount of time without
the new hack.
We remove this hack when we re-implement the compiler frontend in Lean.
2020-10-15 13:37:29 -07:00
Leonardo de Moura
38c79298ed chore: cleanup 2020-10-14 17:56:52 -07:00
Leonardo de Moura
9a3d785ae3 chore: move to new frontend 2020-10-14 17:38:17 -07:00
Leonardo de Moura
9af0a0e18b feat: add withReader method
@Kha `withReader` is a well-behaved version of `adaptReader`. `adaptReader` is
too general, and it often produces counterintuitive elaboration
errors.

Here are two super annoying issues I hit all the time:
1- `adaptReader` + polymorphic code
```
def ex1 : ReaderT Nat IO Unit :=
adaptReader (fun x => x + 1) $
  IO.println "foo" -- 3 Errors here failed to synthesize `Monad ?m` and  `MonadIO ?m`, and don't know how to synthesize `Type → Type`
```

2- `adaptReader` and notation that requires the expected type
```
structure Context :=
(x y : Nat)

def ex2 : ReaderT Context IO Nat :=
adaptReader (fun s => { s with x := 10 }) $ -- Error at the structure instance
  ...
```
In the example above, I have to write `fun (s : Context) => ...` to
fix the problem.

The two problems above happen in the old and new frontends. However,
there is a new problem specific for the new frontend. In the new
frontend, a `do` is only elaborated when the expected type is known.
So, `adaptReader (fun ctx => ...) do ...` seldom works :(

As I said above, the issue is that `adaptReader` is too general. Its
type is
```
  {ρ ρ' : Type u_1} → {m m' : Type u_1 → Type u_2} → [MonadReaderAdapter ρ ρ' m m'] → {α : Type u_1} → (ρ' → ρ) → m α → m' α
```

`withReader` is a simpler version of `adaptReader`
```
withReader : {ρ : Type u_1} → {m : Type u_1 → Type u_2} → [MonadWithReader ρ m] → {α : Type u_1} → (ρ → ρ) → m α → m α
```
It doesn't have any of the problems above. Moreover, I managed to replace
every single instance of `adaptReader` with `withReader` at the stdlib
and tests. We don't need the `adaptReader` generality.
2020-10-13 15:00:17 -07:00
Leonardo de Moura
67e9c83f54 fix: for result type 2020-10-12 11:01:59 -07:00
Leonardo de Moura
7c6b10012b chore: add helper function 2020-10-11 19:58:07 -07:00
Leonardo de Moura
0b81ffb569 refactor: factor out nested do term support and document code
We currently use the nested `do` terms for two combinators: `catch`
and `finally`. We may want to support more in the future.
2020-10-09 11:59:14 -07:00
Leonardo de Moura
8a6cb1842f feat: expand doTry
@Kha I did not implement support for reassignments and `continue`,
`break`, `return` inside the `finally` clause. It is doable, but it
feels like unnecessary complexity. We currently don't have any
instance in our code base where this would be useful.
2020-10-08 19:39:36 -07:00
Leonardo de Moura
9ec583e808 chore: use (doElem| ...) quotation instead of auxDo idiom 2020-10-08 13:54:49 -07:00
Leonardo de Moura
a3a5190004 feat: expand doElem macros 2020-10-08 13:42:56 -07:00
Leonardo de Moura
608de7b592 feat: expand doReassignArrow 2020-10-08 13:31:27 -07:00
Leonardo de Moura
c1469643ca chore: cleanup doLetArrowToCode 2020-10-08 13:00:27 -07:00
Leonardo de Moura
09dcf718c1 feat: expand doHave 2020-10-08 11:56:03 -07:00
Leonardo de Moura
426f139ea3 chore: remove unnecessary [inline] 2020-10-08 11:29:56 -07:00
Leonardo de Moura
91aaab9e0d fix: error message location 2020-10-07 17:43:23 -07:00
Leonardo de Moura
d9d8e95987 feat: elaborate doElems at doLetArrow
@Kha The Rust `let+return` example works in Lean too :)
The Rust function
```rust
fn f (x : i32) -> i32 {
    let y = if x == 0 { println!("x is zero"); return 100 } else { x + 1 };
    println!("y: {}", y)
    y
}
```
The Lean version is
```lean
def f (x : Nat) : IO Nat := do
let y ← if x == 0 then IO.println "x is zero"; return 100 else pure (x + 1)
IO.println ("y: " ++ toString y)
return y
```

The main missing feature now is the `try-catch-finally` `do` element.
2020-10-07 17:30:25 -07:00
Leonardo de Moura
ac07999e95 chore: cleanup do expander, and make sure it can handle the "easy" doLetArrows 2020-10-07 17:00:07 -07:00
Leonardo de Moura
5b76155318 feat: new return semantics
`return e` is not equivalent to `pure e` anymore.
Now, `return e` means "return value `e` as the result of the root `do` block".
2020-10-07 14:07:58 -07:00
Leonardo de Moura
a841842de1 chore: Code.action => Code.seq 2020-10-07 10:19:04 -07:00
Leonardo de Moura
e70dd03340 chore: remove forInMap 2020-10-07 10:01:04 -07:00
Leonardo de Moura
ac1c0714a1 fix: expand macros in patterns before retrieving pattern variables 2020-10-07 09:15:05 -07:00
Leonardo de Moura
2fda7f4f9f chore: add TODO 2020-10-06 19:12:08 -07:00
Leonardo de Moura
294a750110 feat: expand doMatch 2020-10-06 19:07:47 -07:00
Leonardo de Moura
cda4de474b fix: weird error message in do notation
cc @Kha
2020-10-06 15:12:55 -07:00
Leonardo de Moura
db1b110f7e fix: use let* to avoid bad error messages in do notation
cc @Kha
2020-10-06 15:01:05 -07:00
Leonardo de Moura
82dad3c86f feat: add Code.returnAction 2020-10-06 13:19:55 -07:00
Leonardo de Moura
5110b1212d test: do notation error messages 2020-10-06 11:35:44 -07:00
Leonardo de Moura
13591e59d5 chore: improve regular 'for' error message 2020-10-06 11:30:14 -07:00
Leonardo de Moura
7f5b454382 chore: improve for type mismatch error message 2020-10-06 10:50:03 -07:00
Leonardo de Moura
5cabc917f5 chore: improve error message 2020-10-06 10:45:12 -07:00
Leonardo de Moura
0447966b72 chore: adjust elaborator to new syntax 2020-10-06 06:53:12 -07:00
Leonardo de Moura
83f5329aa3 chore: make missing case explicit 2020-10-05 19:13:15 -07:00
Leonardo de Moura
eac3ca9286 feat: let rec in do notation 2020-10-05 19:10:06 -07:00
Leonardo de Moura
954f38d4c2 chore: remove leftover 2020-10-05 17:00:50 -07:00
Leonardo de Moura
54e10776d4 feat: improve error messages for do notation
@Kha we are getting better error message, but we need to figure out a
way to suppress error messages that come from the "plumbing".
For example, in the following example.
```
def f4 (b : Bool) (n : Nat) (v : Vector Nat n) : Vector Nat (n+1) := do
if b then
  v := Vector.cons 1 v
Vector.cons 1 v
```
We get the nice "invalid reassignment" error at `v := ...`, but
we also get the nasty
```
error: application type mismatch
  jp✝ v
argument
  v
has type
  Vector Nat (n + 1)
but is expected to have type
  Vector Nat n
failed to synthesize instance
  CoeT (Vector Nat (n + 1)) v (Vector Nat n)
```
Where `jp✝` is a joinpoint created by the do-notation macro expander.
I thought about using `withoutErrToSorry` when elaborating the expanded
`do` term.
We may also try to add more helper hints such as `ensureTypeOf!` and
use them around "plumbing" code (e.g., "joinpoint goto"s)
2020-10-05 16:33:59 -07:00
Leonardo de Moura
eafd9bc0ad feat: expand for x in xs notation 2020-10-05 15:37:34 -07:00
Leonardo de Moura
60edde82b0 fix: special support for Id 2020-10-05 11:50:07 -07:00
Leonardo de Moura
fb83e8e79b feat: allow any variable to be reassigned 2020-10-05 11:25:19 -07:00
Leonardo de Moura
6e7a883b21 fix: make sure all joinpoints have at least 1 argument 2020-10-04 18:07:00 -07:00
Leonardo de Moura
d8c2d0b551 fix: CodeBlock concatenation 2020-10-04 17:59:56 -07:00