Commit graph

48 commits

Author SHA1 Message Date
Sebastian Ullrich
99bc069fdd feat(library/compiler/ir_interpreter): implement IR interpreter 2019-09-12 18:22:02 +02:00
Leonardo de Moura
ec0e74f5f8 chore(library/init/lean): export as C functions 2019-08-16 20:52:10 -07:00
Leonardo de Moura
14f05d2001 feat(library/init/lean/compiler): register [implementedBy] attribute using new attribute manager 2019-06-26 15:55:51 -07:00
Leonardo de Moura
7e60c6063e chore(library/compiler): remove dead code 2019-06-26 11:08:23 -07:00
Leonardo de Moura
45dc2cd49a feat(library/init/lean/compiler): [export] attribute in Lean 2019-06-24 15:48:12 -07:00
Leonardo de Moura
9da080d398 feat(library/compiler/init_attribute): switch to @[init] attribute in Lean 2019-06-18 16:03:52 -07:00
Leonardo de Moura
51a9208ea9 chore(library/compiler): remove environment extension: llnf_code
We don't need it anymore. It was used by the old IR compiler
2019-05-27 21:28:22 -07:00
Leonardo de Moura
356a4fafcd chore(library/compiler): remove old code
@kha I am removing the old IR compiler. If there is a disaster with
the new one implemented in Lean, I will put it back.
2019-05-27 21:28:22 -07:00
Leonardo de Moura
9b3421b63b feat(library/compiler/closed_term_cache): remove C++ implementation and use Lean one 2019-05-15 15:33:46 -07:00
Leonardo de Moura
f6b3bc868a fix(library/init/lean/environment, library/compiler): compilation error and add [implementedBy] attribute 2019-05-10 07:22:56 -07:00
Leonardo de Moura
15d89b24a3 feat(library/compiler): [init] attribute
TODO: use attribute when emitting code in the backends.
2019-03-18 15:33:29 -07:00
Leonardo de Moura
8e9b0d2799 fix(library/compiler): inferred types for stage2 declarations 2019-03-06 17:24:43 -08:00
Leonardo de Moura
d4dce78b0e chore(library/compiler): delete compiler/builtin module
It has been replaced with `compiler/extern_attribute`
2019-02-12 18:23:09 -08:00
Leonardo de Moura
09d194d5b7 feat(library/compiler): emit external declarations
We only skip `inline` and `adhoc` externs.
2019-02-12 15:42:59 -08:00
Leonardo de Moura
348ccf533c feat(library/compiler): borrowed annotations 2019-02-11 10:08:47 -08:00
Leonardo de Moura
7e8f9e6f66 feat(library/compiler): add [extern] attribute 2019-02-09 18:53:44 -08:00
Leonardo de Moura
bf15ee48fd refactor(library/compiler): export_name ==> export_attribute 2019-02-09 17:59:46 -08:00
Leonardo de Moura
9aed74a5e0 refactor(library/compiler): move extension for storing LLNF code 2019-02-09 17:59:46 -08:00
Leonardo de Moura
4339afc802 chore(library/compiler): [extname] ==> [export] 2019-02-08 10:25:21 -08:00
Leonardo de Moura
b4a7bb4f4c refactor(library/compiler): [cppname] ==> [extname]
We will use this atttribute for all backends.
2019-02-06 12:53:12 -08:00
Leonardo de Moura
70bb89b213 feat(library/compiler): add [cppname] attribute 2019-01-24 14:40:12 -08:00
Leonardo de Moura
b55b1deaf5 chore(library/compiler): remove ir.cpp
There is only one missing transformation: insert explicit reference counting instructions.
We will implement this transformation at `llnf.cpp`. After that, we are
ready to emit C++ code.
2018-11-15 16:53:44 -08:00
Leonardo de Moura
5acdd68ad5 feat(library/compiler/init_module): initialize builtin module 2018-11-15 13:33:02 -08:00
Leonardo de Moura
a2817c3644 refactor(library/compiler): move closed term cache to separate module 2018-11-13 15:01:57 -08:00
Leonardo de Moura
8de7637f75 feat(library/compiler): add to_ir skeleton 2018-11-05 17:04:23 -08:00
Leonardo de Moura
d871c4f7d8 feat(library/compiler): replace simp_inductive with llnf 2018-10-29 13:07:46 -07:00
Leonardo de Moura
a161eec8f2 feat(library/compiler): add llnf (low level normal form) skeleton 2018-10-27 12:36:30 -07:00
Leonardo de Moura
356928c873 feat(library/compiler): add extract_closed skeleton 2018-10-19 16:14:59 -07:00
Leonardo de Moura
5d726eb210 feat(library/compiler/compiler): switch to new compiler frontend
We also rename `vm_compiler` module to `emit_bytecode`.
We will eventually replace this module with the new IR emitter.
2018-10-08 17:38:17 -07:00
Leonardo de Moura
6b8008a222 feat(library/compiler): new compiler entry point (skeleton) 2018-10-05 17:30:27 -07:00
Leonardo de Moura
135a8d7508 chore(library/compiler): remove old compiler steps that have already been replaced 2018-10-05 17:30:27 -07:00
Leonardo de Moura
e18af852c8 feat(library/compiler): add code specialization skeleton 2018-10-05 17:30:27 -07:00
Leonardo de Moura
81e9e95570 chore(library/compiler): erase_irrelevant ==> old_erase_irrelevant 2018-10-01 14:17:11 -07:00
Leonardo de Moura
54b4908349 chore(library/compiler): remove step from old compiler 2018-09-29 16:48:24 -07:00
Leonardo de Moura
5c2286f3c1 refactor(library/compiler): we preserve type information, but we do not preserve type correctness
The compiler applies transformations `t ==> s` where `t` and `s` are
only provably equal, but not definitionally equal. Moreover, in
dependent type theory `C[t]` may be type correct, but `C[s]` is not
when `t` is provably equal to `s`, but not definitionally equal.
Consider the following example:
```
fun n : nat,
  let v : nat      := 0+n in
  let b : bv (0+n) := bv.mk v in
  b
```
If we replace the first `0+n` with `n`, we produce the type incorrect
term
```
fun n : nat,
  let v : nat      := n in
  let b : bv (0+n) := bv.mk v in
  b
```
This is incorrect because `b : bv (0+n)` and `bv.mk v : bv n`, and
`0+n` is not definitionally equal to `n`.

We considered two approaches to address this problem:
1- A relaxed type checker (see deleted file `ctype_checker.cpp`).
This approach does not solve the problem, see example in the end
of the commit.

2- Introduce `lc_cast`-applications to "fix" type problems.  However,
it seems it is too expensive to detect all places that require a
`lc_ast` application. We did that in a few places, but there is major
omission: when we simplify `x : t := v` to `x : t := w` (like in the
example above), we did not find an efficient way to repair all
affected places. We have implemented the function `replace_fvar_with`,
but we would have to execute after each `x : t := v` ==> `x : t := w`
where `v` is not definitionally equal to `w`. We considered grouping
many `replace_fvar_with` together, but it would still be very
expensive. Basically, for any occurrence of `x` we would have to check
whether the resulting type changed or not. Another issue is that the
restrictions considered at `replace_fvar_with` seem to restrictive:
for example `x` cannot occur in lambda/let-decl types. This happens
very frequently in code that uses types such as `decidable p`.
Remark: note that the Lean3 simplifier does not have support for
simplyfing let-decls for this reason. It offers only two options:
expand the `let`, or skip it.

So, this commit gives up the idea of ensuring that each compiler step
produces a type correct Lean term. That is, we preserve type
information, but we do not guarantee the terms are type correct after
we apply compiler transformations.

Here are examples that demonstrate the `ctype_checker` approach does
not work.
```
def C : bool → Type → Type
| tt α := nat × α
| ff α := string × α

def ex  (α β : Type) (x : nat × α) (h₂ : α = β) : nat × β  :=
let f : Π (b : bool) (h₁ : b = tt) (α β : Type) (x : C b α) (h₂ : α = β), nat × β
      := λ (b : bool) (h₁ : b = tt) (α β : Type) (x : C b α) (h₂ : α = β),
         let x₁ : C b β := @eq.ndrec Type α  (λ z : Type, C b z) x β h₂ in
         @eq.ndrec bool b (λ z : bool, C z β) x₁ tt h₁
in f tt rfl α β x h₂
```
Now, suppose we want to put in LCNF. Then, we have to decide whether
each `eq.ndrec` will become a cast or not. Now, suppose we use the
compiler type checker `is_def_eq`.
Then, the first `eq.ndrec` application is `@eq.ndrec Type α  (λ z :
Type, C b z) x β h₂`, and we have that `x : C b α` and the expected
type is `C b β`, both types are stuck since they reduce to
`bool.cases_on b (string × α) (nat × α)` and  `bool.cases_on b (string
× β) (nat × β)` respectively. Thus, they are considered definitionally
equal, and we do not introduce a cast.

The second `eq.ndrec` application is
`@eq.ndrec bool b (λ z : bool, C z β) x₁ tt h₁`, and we have
`x₁ : C b β` and the expected type is `nat × β`, again they are definitionally equal since `C b β`.
Thus, in LCNF, the example above would be:
```
def ex  (α β : Type) (x : nat × α) (h₂ : α = β) : nat × β  :=
let f : Π (b : bool) (h₁ : b = tt) (α β : Type) (x : C b α) (h₂ : α = β), nat × β
      := λ (b : bool) (h₁ : b = tt) (α β : Type) (x : C b α) (h₂ : α = β), x
in f tt rfl α β x h₂
```

The compiler type checker (`ctype_checker`) would say this is a type correct term.
Now, suppose we reduce `f tt rfl α β x h₂` using `csimp`, this term
would reduce to  `x`.
Then, `f` is eliminated since it is not used anymore and we have the new simplified term
```
def ex  (α β : Type) (x : nat × α) (h₂ : α = β) : nat × β  := x
```
`ctype_checker` will report it to be type incorrect. Since, `x : nat × α`
and the result type is `nat × β`, and both are not stuck.
This example shows that by applying simple reduction rules (zeta/beta
in the example above) we can transform a type correct term into a type
incorrect one.

The type `any` is also problematic.  Consider the following example.
```
def ex (x : nat) : real :=
let f : any -> real := fun z, z in
f x
```
It is type correct. If we apply `csimp`, we will get.
```
def ex (x : nat) : real := x
```
which is type incorrect. Again, reduction may transform a type correct term into a type incorrect one.
Note that, I am not using anything fancy here. We are just treating
`any` as definitionally equal to any type.
2018-09-29 12:50:53 -07:00
Leonardo de Moura
d8af3dc906 feat(library/compiler): add ctype_checker
It is just a big wishlist at this point.
The goal is to use it instead of the kernel type_checker.
2018-09-28 15:37:41 -07:00
Leonardo de Moura
52d1abf0bc feat(library/compiler): add cse to new compiler stack 2018-09-14 17:48:18 -07:00
Leonardo de Moura
1c0a6367c8 feat(library/compiler): new dead let removal 2018-09-14 08:41:55 -07:00
Leonardo de Moura
9b21287a3e feat(library/compiler/lcnf): add lean compiler normal form 2018-09-11 18:10:10 -07:00
Leonardo de Moura
58e91559d0 feat(*): use new inductive datatype module 2018-09-06 18:09:22 -07:00
Leonardo de Moura
9a671d7c7d chore(library/compiler): delete rec_fn_macro 2018-05-31 17:11:25 -07:00
Jared Roesch
e65d90ac79 feat(*): C++ code generator
in progress move of Lean.native to init
2016-12-05 16:11:41 -08:00
Leonardo de Moura
df9352ea6e feat(library/compiler): better support for numeric constants 2016-05-12 16:33:37 -07:00
Leonardo de Moura
f2af5828ba refactor(library/compiler): preprocess_rec ==> preprocess 2016-05-12 16:03:39 -07:00
Leonardo de Moura
705317ae77 feat(library/compiler): generate bytecode 2016-05-11 19:21:47 -07:00
Leonardo de Moura
e53bfb9d0a feat(library/compiler): add new compilation step where we reduce cases_on, constructor and projection applications into a basic primitives 2016-05-11 14:17:32 -07:00
Leonardo de Moura
1820bdc430 feat(library/compiler): generate better auxiliary function names 2016-05-11 10:26:51 -07:00
Leonardo de Moura
de9df69ef6 refactor(src): move compiler folder to library 2016-05-09 13:28:00 -07:00
Renamed from src/compiler/init_module.cpp (Browse further)