Skip to content

Commit 7b5a622

Browse files
committed
Quote pattern matching runtime spec
1 parent 33a6f9e commit 7b5a622

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
---
2+
layout: doc-page
3+
title: "Quote Matching Spec"
4+
---
5+
6+
## Evaluation sematics
7+
8+
Bellow we provide an informal description of the pattern evailatuion.
9+
We use `'{..}` for expression, `'[..]` for types and `{{..}}` for patterns nested in expressions.
10+
The semantics are defined as a list of reduction rules that are tried one by one until one matches.
11+
12+
Operations:
13+
* `s =?= p` checks if a scrutinee `s` matches the pattern `p` while accumulating extracted parts of the code.
14+
* `isColosedUnder(x1, .., xn)('{e})` returns true if and only if all the references in `e` to names defined in the patttern are contained in the set `{x1, ... xn}`.
15+
* `lift(x1, .., xn)('{e})` returns `(y1, ..., yn) => [xi = $yi]'{e}` where `yi` is an `Expr` of the type of `xi`.
16+
* `withEnv(x1 -> y1, ..., xn -> yn)(matching)` evaluates mathing recording that `xi` is equivalent to `yi`.
17+
* `matched` denotes that the the match succedded and `matched('{e})` denotes that a matech succeded and extracts `'{e}`
18+
* `&&&` matches if both sides match. Concatenates the extracted expressions of both sides.
19+
20+
```scala
21+
'{ e } =?= '{ $x } && typeOf('{e}) <:< typeOf('{$x}) && isColosedUnder()('{e}) ===> matched('{e})
22+
'{ e } =?= '{ $y(x1, ..., xn) } && isColosedUnder(x1, ... xn)('{e}) ===> matched(lift(x1, ..., xn)('{e}))
23+
24+
'{ lit } =?= '{ lit } ===> matched
25+
'{ e: T } =?= '{ p: P } ===> '{e} =?= '{p} &&& '[T] =?= '[P]
26+
'{ e } =?= '{ p: P } ===> '{e} =?= '{p}
27+
'{ e.x } =?= '{ p.x } ===> '{e} =?= '{p}
28+
'{ x } =?= '{ x } ===> matched
29+
'{e0.f(e1, ..., en)} =?= '{p0.f(p1, ..., p2)} ===> '{e0} =?= '{p0} &&& '{e1} =?= '{p1} &&& ... %% '{en} =?= '{pn}
30+
'{e.f[T1, ..., Tn]} =?= '{p.f[P1, ..., Pn]} ===> '{e} =?= '{p} &&& '[T1] =?= '{P1} &&& ... %% '[Tn] =?= '[Pn]
31+
32+
'{ if e0 then e1 else e2 } =?= '{ if p0 then p1 else p2 } ===> '{e0} =?= '{p0} &&& '{e1} =?= '{p1} &&& '{e2} =?= '{p2}
33+
'{ while e0 do e1 } =?= '{ while p0 do p1 } ===> '{e0} =?= '{p0} &&& '{e1} =?= '{p1}
34+
35+
'{ e0 = e1 } =?= '{ p0 = p1 } && '{e0} =?= '{p0} ===> '{e1} =?= '{p1}
36+
'{ new T } =?= '{ new T } ===> matched
37+
'{ this } =?= '{ this } ===> matched
38+
'{ e.super[T] } =?= '{ p.super[T] } ===> '{e} =?= '{p}
39+
'{ Seq(e0, ..., en): _* } =?= '{ Seq(p0, ..., pn): _* } ===> '{e0} =?= '{p0} &&& ... &&& '{en} =?= '{pn}
40+
41+
'[T] =?= '[P] && T <:< P ===> matched
42+
'[ T0[T1, ..., Tn] ] =?= '[ P0[P1, ..., Pn] ] ===> '[T0] =?= '[P0] &&& ... &&& '[Tn] =?= '[Pn]
43+
'[T @annot] =?= '[P] ===> '[T] =?= '[P]
44+
'[T] =?= '[P @annot] ===> '[T] =?= '[P]
45+
46+
'{ {e0; e1; ...; en}; em } =?= '{ {p0; p1; ...; pm}; em } ===> '{ e0; {e1; ...; en; em} } =?= '{ p0; {p1; ...; pm; em} }
47+
'{ val x: T = e1; e2 } =?= '{ val y: P = p1; p2 } ===> withEnv(x -> y)('[T] =?= '[P] &&& '{e1} =?= '{p1} &&& '{e2} =?= '{p2})
48+
'{ def x0(x1: T1, ..., xn: Tn): T0 = e1; e2 } =?= '{ def y0(y1: P1, ..., yn: Pn): P0 = p1; p2 } ===> withEnv(x0 -> y0, ..., xn -> yn)('[T0] =?= '[P0] &&& ... &&& '[Tn] =?= '[Pn] &&& '{e1} =?= '{p1} &&& '{e2} =?= '{p2})
49+
50+
'{ e1; e2 } =?= '{ p1; p2 } ===> '{e1} =?= '{p1} &&& '{e2} =?= '{p2}
51+
52+
'{ e0 match { case u1 => e1; ...; case un => en } } =?= '{ p0 match { case q1 => p1; ...; case qn => pn } } ===>
53+
'{e0} =?= '{p0} &&& ... &&& '{en} =?= '{pn} &&& '{{u1}} =?= '{{q1}} &&& ... &&& '{{un}} =?= '{{qn}}
54+
55+
'{ try e0 catch { case u1 => e1; ...; case un => en } finally ef } =?= '{ try p0 catch { case q1 => p1; ...; case qn => pn } finally pf } ===> '{e0} =?= '{p0} &&& ... &&& '{en} =?= '{pn} &&& '{{u1}} =?= '{{q1}} &&& ... &&& '{{un}} =?= '{{qn}} &&& '{ef} =?= '{pf}
56+
57+
'{{ _ }} =?= '{{ _ }} ===> matched
58+
'{{ x @ e }} =?= '{{ y @ p }} ===> withEnv(x -> y)('{{e}} =?= '{{p}})
59+
'{{ e0(e1, ..., en)(using i1, ..., im ) }} =?= '{{ p0(p1, ..., pn)(using q1, ..., 1m) }} ===> '{{e0}} =?= '{{p0}} &&& ... &&& '{{en}} =?= '{{pn}} &&& '{i1} =?= '{q1} &&& ... &&& '{im} =?= '{qm}
60+
'{{ e1 | ... | en }} =?= '{{ p1 | ... | pn }} ===> '{{e1}} =?= '{{p1}} &&& ... &&& '{{en}} =?= '{{pn}}
61+
'{{ e: T }} =?= '{{ p: U }} ===> '{{e}} =?= '{{p}} &&& '[T] =?= [U]
62+
'{{ e: T }} =?= '{{ p: U }} ===> '{{e}} =?= '{{p}} &&& '[T] =?= [U]
63+
'{{ `x` }} =?= '{{ `y` }} ===> matched
64+
```

0 commit comments

Comments
 (0)