patterns: drop unspecified type elements at T ?: {N:pat}

Emit a <_> pattern for unspecified elements rather than patterns
that would match metadata like record label and arity.
This commit is contained in:
Emery Hemingway 2024-01-06 16:48:12 +02:00
parent 6d2a401a2b
commit 59ece65f3b
2 changed files with 28 additions and 21 deletions

View File

@ -172,10 +172,10 @@ proc fieldCount(T: typedesc): int =
for _, _ in fieldPairs(default T):
inc result
proc lookup[T](bindings: openArray[(int, Pattern)]; i: int; _: T): Pattern =
proc lookup(bindings: openArray[(int, Pattern)]; i: int): Pattern =
for (j, b) in bindings:
if i == j: return b
return dropType(T)
return drop()
proc grab*(typ: static typedesc; bindings: sink openArray[(int, Pattern)]): Pattern =
## Construct a `Pattern` from type `typ` that selectively captures fields.
@ -186,7 +186,7 @@ proc grab*(typ: static typedesc; bindings: sink openArray[(int, Pattern)]): Patt
rec.fields.setLen(fieldCount typ)
var i: int
for _, f in fieldPairs(default typ):
rec.fields[i] = lookup(bindings, i, f)
rec.fields[i] = lookup(bindings, i)
inc i
result = rec.toPattern
elif typ is tuple:
@ -194,7 +194,7 @@ proc grab*(typ: static typedesc; bindings: sink openArray[(int, Pattern)]): Patt
arr.items.setLen(fieldCount typ)
var i: int
for _, f in fieldPairs(default typ):
arr.items[i] = lookup(bindings, i, f)
arr.items[i] = lookup(bindings, i)
inc i
result = arr.toPattern
else:

View File

@ -3,7 +3,7 @@
import std/[options, tables, unittest]
import preserves, syndicate
import preserves, syndicate, syndicate/protocols/gatekeeper
import ./test_schema
@ -74,23 +74,30 @@ suite "captures":
checkpoint $pat
check pat.matches pr
suite "later-than":
let
obsA = parsePreserves"""<Observe <rec later-than [<lit 1704113731.419243>]> #!#f>"""
obsB = parsePreserves"""<Observe <rec Observe [<rec rec [<lit later-than> <arr [<rec lit [<bind <_>>]>]>]> <_>]> #!#f>"""
patA = """<rec later-than [<lit 1704113731.419243>]>""".parsePreserves.preservesTo(Pattern).get
patB = """<rec Observe [<rec rec [<lit later-than> <arr [<rec lit [<bind <_>>]>]>]> <_>]>""".parsePreserves.preservesTo(Pattern).get
suite "protocol":
test "Observe":
let pat = ?:Observe
const text = """<rec Observe [<bind <_>> <bind <_>>]>"""
check $pat == text
patC = grab obsA
test "later-than":
let
obsA = parsePreserves"""<Observe <rec later-than [<lit 1704113731.419243>]> #!#f>"""
obsB = parsePreserves"""<Observe <rec Observe [<rec rec [<lit later-than> <arr [<rec lit [<bind <_>>]>]>]> <_>]> #!#f>"""
patA = """<rec later-than [<lit 1704113731.419243>]>""".parsePreserves.preservesTo(Pattern).get
patB = """<rec Observe [<rec rec [<lit later-than> <arr [<rec lit [<bind <_>>]>]>]> <_>]>""".parsePreserves.preservesTo(Pattern).get
test $patC:
check patC.matches obsA
patC = grab obsA
test $patB:
checkpoint $obsA
check patB.matches obsA
test $patC:
check patC.matches obsA
suite "Observe":
let pat = ?:Observe
const text = """<rec Observe [<bind <_>> <bind <_>>]>"""
check $pat == text
test $patB:
checkpoint $obsA
check patB.matches obsA
test "TransportConnection":
let
pat = TransportConnection ?: { 2: ?:Rejected}
text = """<rec connect-transport [<_> <_> <rec rejected [<bind <_>>]>]>"""
check $pat == text