BrainGrid

Grammar

🧩 Shape-Safe Symbolic Differentiation with Algebraic Data Types

Used in: 1 reposβ€’Updated: recently

Grammar

Below is the approximate BNF grammar for Kotlinβˆ‡. This is incomplete and subject to change without notice.

#Numerical types:

1  𝔹 = "True" | "False"
2  𝔻 = "1" | ...  | "9"
3  β„• =  𝔻  | 𝔻"0" | 𝔻ℕ
4  β„€ = "0" | β„•    | -β„•
5  β„š = β„• | β„€"/"β„•
6  ℝ = β„• | β„•"."β„• | "-"ℝ
7  β„‚ = ℝ + ℝi
8  ℍ = ℝ + ℝi + ℝj + ℝk
9  T = 𝔹 | β„• | β„€ | β„š | ℝ | β„‚ | ℍ
10  n = β„• < 100*
11vec = [Tⁿ]
12mat = [[Tⁿ]ⁿ]

βˆ— To increase n, see DimGen.kt.

#DSL

1       type = "Double" | "Float" | "Int" | "BigInteger" | "BigDouble";
2        nat = "1" | ... | "99";
3     output = "Fun<" type "Real>" | "VFun<" type "Real," nat ">" | "MFun<" type "Real," nat "," nat ">";
4        int = "0" | nat int;
5      float = int "." int;
6        num = type "(" int ")" | type "(" float ")";
7        var = "x" | "y" | "z" | "ONE" | "ZERO" | "E" | "Var()";
8     signOp = "+" | "-";
9      binOp = signOp | "*" | "/" | "pow";
10     trigOp = "sin" | "cos" | "tan" | "asin" | "acos" | "atan" | "asinh" | "acosh" | "atanh";
11    unaryOp = signOp | trigOp | "sqrt" | "log" | "ln" | "exp";
12        exp = var | num | unaryOp exp | var binOp exp | "(" exp ")";
13    expList = exp | exp "," expList;
14      linOp = signOp | "*" | " dot ";
15        vec = "Vec(" expList ")" | "Vec" nat "(" expList ")";
16     vecExp = vec | signOp vecExp | exp "*" vecExp | vec linOp vecExp | vecExp ".norm(" int ")";
17        mat = "Mat" nat "x" nat "(" expList ")";
18     matExp = mat | signOp matExp | exp linOp matExp | vecExp linOp matExp | mat linOp matExp;
19     anyExp = exp | vecExp | matExp | derivative | invocation;
20   bindings = exp " to " exp | exp " to " exp "," bindings;
21 invocation = anyExp "(" bindings ")";
22 derivative = "d(" anyExp ") / d(" exp ")" | anyExp ".d(" exp ")" | anyExp ".d(" expList ")";
23   gradient = exp ".grad()";

Operational Semantics

Below we provide an operational semantics for Kotlinβˆ‡.

1                 v = a | ... | z | vv
2                 c = 1 | ... | 9 | cc | c.c
3                 e = v | c | e βŠ• e | e βŠ™ e | (e) | (e).d(v) | e(e = e)
4                 
5       d(e) / d(v) = e.d(v)
6      Plus(e₁, eβ‚‚) = e₁ βŠ• eβ‚‚
7     Times(e₁, eβ‚‚) = e₁ βŠ™ eβ‚‚
8           c₁ βŠ• cβ‚‚ = c₁ + cβ‚‚
9           c₁ βŠ™ cβ‚‚ = c₁ * cβ‚‚
10       e₁(eβ‚‚ = e₃) = e₁[eβ‚‚ β†’ e₃]
11           
12    (e₁ βŠ• eβ‚‚).d(v) =    e₁.d(v)   βŠ•   eβ‚‚.d(v)
13    (e₁ βŠ™ eβ‚‚).d(v) = e₁.d(v) βŠ™ eβ‚‚ βŠ• e₁ βŠ™ eβ‚‚.d(v)
14          v₁.d(v₁) = 1
15          v₁.d(vβ‚‚) = 0
16            c.d(v) = 0
17
18     e.d(v₁).d(vβ‚‚) = e.d(vβ‚‚).d(v₁) †
19
20(e₁ βŠ• eβ‚‚)[e₃ β†’ eβ‚„] = e₁[e₃ β†’ eβ‚„] βŠ• eβ‚‚[e₃ β†’ eβ‚„]
21(e₁ βŠ™ eβ‚‚)[e₃ β†’ eβ‚„] = e₁[e₃ β†’ eβ‚„] βŠ™ eβ‚‚[e₃ β†’ eβ‚„]
22       e₁[e₁ β†’ eβ‚‚] = eβ‚‚
23       e₁[eβ‚‚ β†’ e₃] = e₁

In the notation above, we use subscripts to denote conditional inequality. If we have two nonterminals with matching subscripts in the same production, i.e. eβ‚˜, eβ‚™ where m = n, then eβ‚˜ = eβ‚™ must be true. If we have two nonterminals with different subscripts in the same production, i.e. eβ‚˜, eβ‚™ where m β‰  n, either eβ‚˜ = eβ‚™ or eβ‚˜ β‰  eβ‚™ may be true. Subscripts have no meaning across multiple productions.

† See Clairaut-Schwarz. Commutativity holds for twice-differentiable functions, however it is possible to have a continuously differentiable function whose partials are defined everywhere, but whose permuted partials are unequal βˆ‚FΒ²/βˆ‚xβˆ‚y β‰  βˆ‚FΒ²/βˆ‚yβˆ‚x. Some examples of continuously differentiable functions whose partials do not commute may be found in Tolstov (1949, 1949).

Reduction semantics

Our reduction semantics can be described as follows:

Sub loosely corresponds to Ξ·-reduction in the untyped Ξ»-calculus, however f βˆ‰ Ξ© is disallowed, and we assume all variables are bound by Inv. Let us consider what happens under f∢ {a, b, c} ↦ abc, g∢ {a, b, c} ↦ ac under Ξ© ∢= {(a, 1), (b, 2), (c, 2)}:

We can view the above process as acting on a dataflow graph, where Inv backpropagates Ξ©, and Sub returns concrete values Y, here depicted on g:

Quick Actions

Details

Author
breandan
Slug
breandan/grammar