Skip to content

Commit 5da271c

Browse files
committed
add parametric attribute
1 parent 2ef0ed7 commit 5da271c

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

lean/extra/attributes.lean

+28-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import lean.extra.attrs.dummy
22
import lean.extra.attrs.tag
3+
import lean.extra.attrs.parametric
34
/-
45
# Attributes
56
@@ -11,7 +12,7 @@ we will bounce between this file and the files in the `attrs/` folder which
1112
contain the implementations of the attributes. We'll see you at
1213
[`./attrs/tag.lean`](./attrs/tag.lean).
1314
14-
## Using `myTag`
15+
## Tag attributes with `myTag`
1516
1617
see that we've created a tagging infrastructure based on Lean's `TagAttribute`s, which exists
1718
explicitly to allow us to create 'simple' attributes that wish to keep track of
@@ -37,6 +38,26 @@ This simplified mechanism exists to allow us to easily tag definitions of intere
3738
-- decl: tag2 | find? OfNat.ofNat.{0} Int 2 (Int.instOfNatInt 2)
3839

3940

41+
/-
42+
## Parametric attributes with `myParam`
43+
44+
A parametric attribute is like a tag attribute, while adding support for
45+
parameters in the attribute.
46+
47+
We shall add an attribute called `myParam`, which recieves two parameters,
48+
a priority, denoted by a natural number, and an optional tag `important`.
49+
50+
51+
We'll see you at [`./attrs/parametric.lean`](./attrs/dummy.lean).
52+
53+
-/
54+
55+
56+
57+
@[myParam 10] def h1 (x : Nat) := 2*x + 1
58+
@[myParam 20 important] def h2 (x : Nat) := 2*x + 1
59+
60+
4061
/-
4162
## Using `dummy_attr`
4263
@@ -77,5 +98,11 @@ class bar
7798
## Modifying the `value` with the `around` attribute
7899
79100
We're going to write an attribute that will modify a given definition
101+
102+
103+
## Implementing `sym`:
104+
Scoped environment extension maintains scoping information, so it keeps track of
105+
whether the tag is local or not.
80106
-/
81107

108+

lean/extra/attrs/parameteric.lean

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import Lean
2+
open Lean
3+
4+
-- The name of the syntax is important. The name is
5+
-- used to connect to the corresponding attribute.
6+
syntax (name := myParam) "myParam" num "important"? : attr
7+
8+
initialize fooParamAttr : ParametricAttribute (Nat × Bool) ←
9+
registerParametricAttribute {
10+
name := `myParam /-
11+
The attribute name must match the `syntax` declaration name.
12+
-/
13+
descr := "parametric attribute containing a priority and flag"
14+
getParam := fun _ stx =>
15+
match stx with
16+
| `(attr| myParam $prio:num) =>
17+
return (prio.toNat, false)
18+
| `(attr| myParam $prio:num important) =>
19+
return (prio.toNat, true)
20+
| _ => throwError "unexpected foo attribute"
21+
afterSet := fun declName val => do
22+
IO.println s!"set attribute [myParam] at {declName}; priority: {val.1}; important? {val.2}"
23+
}

0 commit comments

Comments
 (0)