Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds tracing for quantification #65

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions language-reference-manual/lrm.trlc
Original file line number Diff line number Diff line change
Expand Up @@ -1519,8 +1519,8 @@ section "Quantification" {
Grammar Quantified_Expression {
bnf = '''
quantified_expression ::=
quantifier IDENTIFIER_name 'in' IDENTIFIER_component_name '=>'
expression_PREDICATE
'(' quantifier IDENTIFIER_name 'in' IDENTIFIER_component_name '=>'
expression_PREDICATE ')'

quantifier ::= 'forall' | 'exists'
'''
Expand All @@ -1546,14 +1546,6 @@ section "Quantification" {
quantified expression is Boolean.'''
}

Dynamic_Semantics Quantification_Evaluation {
text = '''During evaluation of the quantified expression, each element
of the array is evaluated in sequence and its
value is bound to the declared name. The
predicated is then evaluated with this
binding.'''
}

Dynamic_Semantics Universal_Quantification_Semantics {
text = '''For universal *(forall)* quantification the final value is
true iff all predicates evaluate to
Expand All @@ -1569,6 +1561,14 @@ section "Quantification" {
vacuously false.)*'''
}

Note Quantification_Evaluation {
text = '''During evaluation of the quantified expression, each element
of the array is evaluated in sequence and its
value is bound to the declared name. The
predicated is then evaluated with this
binding.'''
}

Recommendation Quantification_Short_Circuit_Evaluation {
text = '''In general Quantification is equivalent to a chain of `and`
or `or` expressions, however it is left
Expand All @@ -1580,6 +1580,7 @@ section "Quantification" {
'''For static analysis or linting, use non-short-circuit
semantics, as using a quantifier as a
complex guard is not reasonable.''']
untraced_reason = "Non-normative text"
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package Foo

type T {
a Integer[0..*]
}

checks T {
(exists item in a => item == 7), warning "there is no 7 in a"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package Foo

T Bar {
a = [7]
}

T Kitten {
a = []
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
T Kitten {
^^^^^^ rbt-existential-quantification-semantics/foo.trlc:7: check warning: there is no 7 in a
Processed 1 model and 1 requirement file and found 1 warning
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rbt-existential-quantification-semantics/foo.trlc:7:3: trlc check warning: there is no 7 in a
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
T Kitten {
^^^^^^ rbt-existential-quantification-semantics/foo.trlc:7: check warning: there is no 7 in a
{
"Bar": {
"a": [
7
]
},
"Kitten": {
"a": []
}
}
Processed 1 model and 1 requirement file and found 1 warning
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
T Kitten {
^^^^^^ rbt-existential-quantification-semantics/foo.trlc:7: check warning: there is no 7 in a
Processed 1 model and 1 requirement file and found 1 warning
11 changes: 11 additions & 0 deletions tests-system/rbt-quantification-naming-scope/foo.rsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package Foo

type T {
a Integer[1..10]
}

checks T {
(forall item in a => item > 0), warning "All items must be positive"
(forall item in a => item < 10), warning "All items must be less than 10"
(forall a in a => a != 7), warning "No sevens allowed"
}
3 changes: 3 additions & 0 deletions tests-system/rbt-quantification-naming-scope/output
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(forall a in a => a != 7), warning "No sevens allowed"
^ rbt-quantification-naming-scope/foo.rsl:10: error: shadows Composite_Component a from foo.rsl:4
Processed 1 model and 0 requirement files and found 1 error
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rbt-quantification-naming-scope/foo.rsl:10:13: trlc error: shadows Composite_Component a from foo.rsl:4
3 changes: 3 additions & 0 deletions tests-system/rbt-quantification-naming-scope/output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(forall a in a => a != 7), warning "No sevens allowed"
^ rbt-quantification-naming-scope/foo.rsl:10: error: shadows Composite_Component a from foo.rsl:4
Processed 1 model and 0 requirement files and found 1 error
3 changes: 3 additions & 0 deletions tests-system/rbt-quantification-naming-scope/output.smtlib
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(forall a in a => a != 7), warning "No sevens allowed"
^ rbt-quantification-naming-scope/foo.rsl:10: error: shadows Composite_Component a from foo.rsl:4
Processed 1 model and 0 requirement files and found 1 error
9 changes: 9 additions & 0 deletions tests-system/rbt-quantification-object-1/foo.rsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package Foo

type Potato {
a String
}

checks Potato {
(forall char in a => char != "a"), warning "a should not contain the letter a"
}
3 changes: 3 additions & 0 deletions tests-system/rbt-quantification-object-1/output
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(forall char in a => char != "a"), warning "a should not contain the letter a"
^ rbt-quantification-object-1/foo.rsl:8: error: you can only quantify over arrays
Processed 1 model and 0 requirement files and found 1 error
1 change: 1 addition & 0 deletions tests-system/rbt-quantification-object-1/output.brief
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rbt-quantification-object-1/foo.rsl:8:21: trlc error: you can only quantify over arrays
3 changes: 3 additions & 0 deletions tests-system/rbt-quantification-object-1/output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(forall char in a => char != "a"), warning "a should not contain the letter a"
^ rbt-quantification-object-1/foo.rsl:8: error: you can only quantify over arrays
Processed 1 model and 0 requirement files and found 1 error
3 changes: 3 additions & 0 deletions tests-system/rbt-quantification-object-1/output.smtlib
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(forall char in a => char != "a"), warning "a should not contain the letter a"
^ rbt-quantification-object-1/foo.rsl:8: error: you can only quantify over arrays
Processed 1 model and 0 requirement files and found 1 error
9 changes: 9 additions & 0 deletions tests-system/rbt-quantification-object-2/foo.rsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package Foo

type Potato {
a String
}

checks Potato {
(forall char in b => char != "a"), warning "b should not contain the letter a"
}
3 changes: 3 additions & 0 deletions tests-system/rbt-quantification-object-2/output
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(forall char in b => char != "a"), warning "b should not contain the letter a"
^ rbt-quantification-object-2/foo.rsl:8: error: unknown symbol b
Processed 1 model and 0 requirement files and found 1 error
1 change: 1 addition & 0 deletions tests-system/rbt-quantification-object-2/output.brief
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rbt-quantification-object-2/foo.rsl:8:21: trlc error: unknown symbol b
3 changes: 3 additions & 0 deletions tests-system/rbt-quantification-object-2/output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(forall char in b => char != "a"), warning "b should not contain the letter a"
^ rbt-quantification-object-2/foo.rsl:8: error: unknown symbol b
Processed 1 model and 0 requirement files and found 1 error
3 changes: 3 additions & 0 deletions tests-system/rbt-quantification-object-2/output.smtlib
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(forall char in b => char != "a"), warning "b should not contain the letter a"
^ rbt-quantification-object-2/foo.rsl:8: error: unknown symbol b
Processed 1 model and 0 requirement files and found 1 error
11 changes: 11 additions & 0 deletions tests-system/rbt-quantified-expression/foo.rsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package Foo

type T {
a Integer[1..*]
}

checks T {
(forall item in a => item > 0), warning "All items must be positive", a
(exists item in a => item == 7), warning "7 is a lucky number", a
(forall item in a => 42), warning "All items must be positive", a
}
5 changes: 5 additions & 0 deletions tests-system/rbt-quantified-expression/foo.trlc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package Foo

T Bar {
a = [1, 2, 3, 7]
}
3 changes: 3 additions & 0 deletions tests-system/rbt-quantified-expression/output
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(forall item in a => 42), warning "All items must be positive", a
^^ rbt-quantified-expression/foo.rsl:10: error: expected expression of type Builtin_Boolean, got Builtin_Integer instead
Processed 1 model and 1 requirement file and found 1 error
1 change: 1 addition & 0 deletions tests-system/rbt-quantified-expression/output.brief
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rbt-quantified-expression/foo.rsl:10:26: trlc error: expected expression of type Builtin_Boolean, got Builtin_Integer instead
3 changes: 3 additions & 0 deletions tests-system/rbt-quantified-expression/output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(forall item in a => 42), warning "All items must be positive", a
^^ rbt-quantified-expression/foo.rsl:10: error: expected expression of type Builtin_Boolean, got Builtin_Integer instead
Processed 1 model and 1 requirement file and found 1 error
3 changes: 3 additions & 0 deletions tests-system/rbt-quantified-expression/output.smtlib
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(forall item in a => 42), warning "All items must be positive", a
^^ rbt-quantified-expression/foo.rsl:10: error: expected expression of type Builtin_Boolean, got Builtin_Integer instead
Processed 1 model and 1 requirement file and found 1 error
1 change: 1 addition & 0 deletions tests-system/rbt-quantified-expression/tracing
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
LRM.Quantification_Type
christophkloeffel marked this conversation as resolved.
Show resolved Hide resolved
9 changes: 9 additions & 0 deletions tests-system/rbt-universal-quantification-semantics/foo.rsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package Foo

type T {
a Integer[0..*]
}

checks T {
(forall item in a => item > 0), warning "All items must be positive"
}
13 changes: 13 additions & 0 deletions tests-system/rbt-universal-quantification-semantics/foo.trlc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package Foo

T Bar {
a = [-1]
christophkloeffel marked this conversation as resolved.
Show resolved Hide resolved
}

T Kitten {
a = []
}

T Potato {
a = [-1, 1]
}
5 changes: 5 additions & 0 deletions tests-system/rbt-universal-quantification-semantics/output
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
T Bar {
^^^ rbt-universal-quantification-semantics/foo.trlc:3: check warning: All items must be positive
T Potato {
^^^^^^ rbt-universal-quantification-semantics/foo.trlc:11: check warning: All items must be positive
Processed 1 model and 1 requirement file and found 2 warnings
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
rbt-universal-quantification-semantics/foo.trlc:3:3: trlc check warning: All items must be positive
rbt-universal-quantification-semantics/foo.trlc:11:3: trlc check warning: All items must be positive
21 changes: 21 additions & 0 deletions tests-system/rbt-universal-quantification-semantics/output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
T Bar {
^^^ rbt-universal-quantification-semantics/foo.trlc:3: check warning: All items must be positive
T Potato {
^^^^^^ rbt-universal-quantification-semantics/foo.trlc:11: check warning: All items must be positive
{
"Bar": {
"a": [
-1
]
},
"Kitten": {
"a": []
},
"Potato": {
"a": [
-1,
1
]
}
}
Processed 1 model and 1 requirement file and found 2 warnings
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
T Bar {
^^^ rbt-universal-quantification-semantics/foo.trlc:3: check warning: All items must be positive
T Potato {
^^^^^^ rbt-universal-quantification-semantics/foo.trlc:11: check warning: All items must be positive
Processed 1 model and 1 requirement file and found 2 warnings
4 changes: 4 additions & 0 deletions trlc/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -2046,6 +2046,8 @@ def __init__(self, mh, location,
n_variable,
n_source,
n_expr):
# lobster-trace: LRM.Quantified_Expression
# lobster-trace: LRM.Quantification_Type
super().__init__(location, typ)
assert isinstance(typ, Builtin_Boolean)
assert isinstance(universal, bool)
Expand Down Expand Up @@ -2077,6 +2079,8 @@ def to_string(self):

def evaluate(self, mh, context):
# lobster-trace: LRM.Null_Is_Invalid
# lobster-trace: LRM.Universal_Quantification_Semantics
# lobster-trace: LRM.Existential_Quantification_Semantics
assert isinstance(mh, Message_Handler)
assert context is None or isinstance(context, dict)

Expand Down
3 changes: 3 additions & 0 deletions trlc/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1064,6 +1064,7 @@ def parse_primary(self, scope):
return self.parse_name(scope)

def parse_quantified_expression(self, scope):
# lobster-trace: LRM.Quantified_Expression
assert isinstance(scope, ast.Scope)

if self.peek_kw("forall"):
Expand All @@ -1078,6 +1079,7 @@ def parse_quantified_expression(self, scope):
self.match("IDENTIFIER")
t_qv = self.ct
if scope.contains(t_qv.value):
# lobster-trace: LRM.Quantification_Naming_Scope
pdef = scope.lookup(self.mh, t_qv)
self.mh.error(t_qv.location,
"shadows %s %s from %s" %
Expand All @@ -1092,6 +1094,7 @@ def parse_quantified_expression(self, scope):
field)
n_source.set_ast_link(self.ct)
if not isinstance(field.n_typ, ast.Array_Type):
# lobster-trace: LRM.Quantification_Object
self.mh.error(self.ct.location,
"you can only quantify over arrays")
n_var = ast.Quantified_Variable(t_qv.value,
Expand Down
Loading