This PR implements zero cost `BaseIO` by erasing the `IO.RealWorld` parameter from argument lists and structures. This is a **major breaking change for FFI**. Concretely: - `BaseIO` is defined in terms of `ST IO.RealWorld` - `EIO` (and thus `IO`) is defined in terms of `EST IO.RealWorld` - The opaque `Void` type is introduced and the trivial structure optimization updated to account for it. Furthermore, arguments of type `Void s` are removed from the argument lists of the C functions. - `ST` is redefined as `Void s -> ST.Out s a` where `ST.Out` is a pair of `Void s` and `a` This together has the following major effects on our generated code: - Functions that return `BaseIO`/`ST`/`EIO`/`IO`/`EST` now do not take the dummy world parameter anymore. To account for this FFI code needs to delete the dummy world parameter from the argument lists. - Functions that return `BaseIO`/`ST` now return their wrapped value directly. In particular `BaseIO UInt32` now returns a `uint32_t` instead of a `lean_object*`. To account for this FFI code might have to change the return type and does not need to call `lean_io_result_mk_ok` anymore but can instead just `return` values right away (same with extracting values from `BaseIO` computations. - Functions that return `EIO`/`IO`/`EST` now only return the equivalent of an `Except` node which reduces the allocation size. The `lean_io_result_mk_ok`/`lean_io_result_mk_error` functions were updated to account for this already so no change is required. Besides improving performance by dropping allocation (sizes) we can now also do fun new things such as: ```lean @[extern "malloc"] opaque malloc (size : USize) : BaseIO USize ```
83 lines
3.4 KiB
Text
83 lines
3.4 KiB
Text
import Lean
|
||
|
||
/-- `erased α` is the same as `α`, except that the elements
|
||
of `erased α` are erased in the VM in the same way as types
|
||
and proofs. This can be used to track data without storing it
|
||
literally. -/
|
||
def Erased (α : Sort u) : Sort max 1 u :=
|
||
Σ's : α → Prop, ∃ a, (fun b => a = b) = s
|
||
|
||
namespace Erased
|
||
|
||
/-- Erase a value. -/
|
||
@[inline]
|
||
def mk {α} (a : α) : Erased α :=
|
||
⟨fun b => a = b, a, rfl⟩
|
||
|
||
open Lean.Compiler
|
||
set_option pp.explicit true
|
||
set_option pp.funBinderTypes true
|
||
set_option pp.letVarTypes true
|
||
set_option trace.Compiler.result true
|
||
/--
|
||
trace: [Compiler.result] size: 1
|
||
def Erased.mk (α : lcErased) (a : lcAny) : PSigma lcErased lcAny :=
|
||
let _x.1 : PSigma lcErased lcAny := PSigma.mk ◾ ◾ ◾ ◾;
|
||
return _x.1
|
||
---
|
||
trace: [Compiler.result] size: 5
|
||
def _private.lean.run.erased.0._eval._lam_0 (_x.1 : Array
|
||
Lean.Name) (_x.2 : PUnit) (_y.3 : Lean.Elab.Term.Context) (_y.4 : lcAny) (_y.5 : Lean.Meta.Context) (_y.6 : lcAny) (_y.7 : Lean.Core.Context) (_y.8 : lcAny) (_y.9 : lcVoid) : EST.Out
|
||
Lean.Exception lcAny PUnit :=
|
||
let _x.10 : EST.Out Lean.Exception lcAny PUnit := compile _x.1 _y.7 _y.8 _y.9;
|
||
cases _x.10 : EST.Out Lean.Exception lcAny PUnit
|
||
| EST.Out.ok (a.11 : PUnit) (a.12 : lcVoid) =>
|
||
let _x.13 : EST.Out Lean.Exception lcAny PUnit := @EST.Out.ok ◾ ◾ ◾ _x.2 a.12;
|
||
return _x.13
|
||
| EST.Out.error (a.14 : Lean.Exception) (a.15 : lcVoid) =>
|
||
return _x.10
|
||
[Compiler.result] size: 1
|
||
def _private.lean.run.erased.0._eval._closed_0 : String :=
|
||
let _x.1 : String := "Erased";
|
||
return _x.1
|
||
[Compiler.result] size: 1
|
||
def _private.lean.run.erased.0._eval._closed_1 : String :=
|
||
let _x.1 : String := "mk";
|
||
return _x.1
|
||
[Compiler.result] size: 3
|
||
def _private.lean.run.erased.0._eval._closed_2 : Lean.Name :=
|
||
let _x.1 : String := _eval._closed_1.2;
|
||
let _x.2 : String := _eval._closed_0.2;
|
||
let _x.3 : Lean.Name := Lean.Name.mkStr2 _x.2 _x.1;
|
||
return _x.3
|
||
[Compiler.result] size: 2
|
||
def _private.lean.run.erased.0._eval._closed_3 : Array Lean.Name :=
|
||
let _x.1 : Nat := 1;
|
||
let _x.2 : Array Lean.Name := Array.mkEmpty ◾ _x.1;
|
||
return _x.2
|
||
[Compiler.result] size: 3
|
||
def _private.lean.run.erased.0._eval._closed_4 : Array Lean.Name :=
|
||
let _x.1 : Lean.Name := _eval._closed_2.2;
|
||
let _x.2 : Array Lean.Name := _eval._closed_3.2;
|
||
let _x.3 : Array Lean.Name := Array.push ◾ _x.2 _x.1;
|
||
return _x.3
|
||
[Compiler.result] size: 9
|
||
def _private.lean.run.erased.0._eval (a.1 : Lean.Elab.Command.Context) (a.2 : lcAny) (a.3 : lcVoid) : EST.Out
|
||
Lean.Exception lcAny PUnit :=
|
||
let _x.4 : String := _eval._closed_0.2;
|
||
let _x.5 : String := _eval._closed_1.2;
|
||
let _x.6 : Lean.Name := _eval._closed_2.2;
|
||
let _x.7 : Nat := 1;
|
||
let _x.8 : Array Lean.Name := _eval._closed_3.2;
|
||
let _x.9 : Array Lean.Name := _eval._closed_4.2;
|
||
let _x.10 : PUnit := PUnit.unit;
|
||
let _f.11 : Lean.Elab.Term.Context →
|
||
lcAny →
|
||
Lean.Meta.Context →
|
||
lcAny →
|
||
Lean.Core.Context → lcAny → lcVoid → EST.Out Lean.Exception lcAny PUnit := _eval._lam_0.2 _x.9 _x.10;
|
||
let _x.12 : EST.Out Lean.Exception lcAny PUnit := Lean.Elab.Command.liftTermElabM._redArg _f.11 a.1 a.2 a.3;
|
||
return _x.12
|
||
-/
|
||
#guard_msgs in
|
||
run_meta Lean.Compiler.compile #[``Erased.mk]
|