Skip to content

Commit 964bcad

Browse files
committed
better examples
1 parent a642840 commit 964bcad

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

src/Lean/Compiler/ImplementedByAttr.lean

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,34 @@ import Lean.Elab.InfoTree
1212
namespace Lean.Compiler
1313

1414
/--
15-
Instructs the compiler to use a different function as implementation for a function.
16-
Similarly to `@[extern]`, this causes the function to not be compiled and all compiler related
17-
attributes (e.g. `noncomputable`, `@[inline]`) to be ignored.
15+
Instructs the compiler to use a different function as the implementation of a function. With the
16+
exception of tactics that call native code such as `native_decide`, the kernel and type checking
17+
are unaffected. When this attribute is used on a function, the function is not compiled and all
18+
compiler-related attributes (e.g. `noncomputable`, `@[inline]`) are ignored. Calls to this
19+
function are replaced by calls to its implementation.
1820
1921
The most common use cases of `@[implemented_by]` are to provide an efficient unsafe implementation
20-
and to make an unsafe function accessible through an opaque function:
22+
and to make an unsafe function accessible in safe code through an opaque function:
2123
2224
```
23-
unsafe def fooUnsafe (x : Bool) : UInt8 := unsafeCast x
25+
unsafe def testEqImpl (as bs : Array Nat) : Bool :=
26+
ptrEq as bs || as == bs
2427
25-
@[implemented_by fooUnsafe]
26-
def foo : Bool → UInt8 | false => 0 | true => 1
28+
@[implemented_by testEqImpl]
29+
def testEq (as bs : Array Nat) : Bool :=
30+
as == bs
2731
28-
unsafe def barUnsafe (x : Nat) : Nat := ...
32+
unsafe def printAddrImpl {α : Type u} (x : α) : IO Unit :=
33+
IO.println s!"Address: {ptrAddrUnsafe x}"
2934
30-
@[implemented_by barUnsafe]
31-
opaque bar (x : Nat) : Nat
35+
@[implemented_by printAddrImpl]
36+
opaque printAddr {α : Type u} (x : α) : IO Unit
3237
```
3338
34-
Note: the provided implementation will not be checked to be correct, thus making it possible to
35-
prove `False` with `native_decide` using incorrect implementations. For a safer variant of this
36-
attribute that however only works for providing safe implementations, see `@[csimp]`.
39+
The provided implementation is not checked to be equivalent to the original definition. This makes
40+
it possible to prove `False` with `native_decide` using incorrect implementations. For a safer
41+
variant of this attribute that however doesn't work for unsafe implementations, see `@[csimp]`,
42+
which requires a proof that the two functions are equal.
3743
-/
3844
@[builtin_doc]
3945
builtin_initialize implementedByAttr : ParametricAttribute Name ← registerParametricAttribute {

0 commit comments

Comments
 (0)