I want to implement this deterministic finite automaton in Java and I want the program the program to recognize the language of the automaton.
Automaton
I already got this code, but I donĀ“t no how to make the switch the best way :
int estado = 0;
char c = 0;
//else ir para erro
switch(aOpcao) {
case 1:
if(c == '/')
estado = 1;
break;
case 2:
if(c == '!')
estado = 7;
if(c == '#')
estado = 6;
break;
case 3:
if(c == 'a' || c == 'b' || c == 'c' || c == 'd' || c == 'e' || c == 'f'
|| c == 'g' || c == 'h' || c == 'i' || c == 'j' || c == 'k' ||
c == 'l' || c == 'm' || c == 'n' || c == 'o' || c == 'p' || c == 'q'
|| c == 'r' || c == 's' || c == 't' || c == 'u' || c == 'v' || c == 'w'
|| c == 'x' || c == 'y' || c == 'z' )
estado = 8;
if(c == '!')
estado = 7;
if(c == ',')
estado = 14;
if(c == 'a' || c == 'b' || c == 'c' || c == 'd' || c == 'e' || c == 'f'
|| c == 'g' || c == 'h' || c == 'i' || c == 'j' || c == 'k' ||
c == 'l' || c == 'm' || c == 'n' || c == 'o' || c == 'p' || c == 'q'
|| c == 'r' || c == 's' || c == 't' || c == 'u' || c == 'v' || c == 'w'
|| c == 'x' || c == 'y' || c == 'z' )
estado = 13;
break;
if you are looking for a better way to check if c is alphabet or not for those two long if statement.
then one simple way in java is to change it to
if( (c >= 'a' && c <= 'z') )
if you are talking about how to completely translate the above code into java. You have read java switch() statement and java documentation by yourself. Your question seems like a homework problem.
Related
For some reason dafny reports that the ensure condition for PreorderTraversalChildrenAreLater might not always hold even though the quantified expression always holds... later on in the same lemma. Ideally, once I've shown the existence k, I was attempting to show that all child elements of the root.repr will appear later in the PreorderTraversal.
ensures forall k :: 0 <= k < |PreorderTraversal(root)| ==> forall child :: child in PreorderTraversal(root)[k].repr && child != PreorderTraversal(root)[k] ==> exists j :: k < j < |PreorderTraversal(root)| && PreorderTraversal(root)[j] == child
function PreorderTraversal(root: TreeNode): seq<TreeNode>
reads root.repr
requires root.Valid()
ensures forall x :: x in root.repr ==> x in PreorderTraversal(root)
ensures forall k :: 0 <= k < |PreorderTraversal(root)| ==> PreorderTraversal(root)[k] in root.repr && PreorderTraversal(root)[k].Valid()
// ensures forall k :: 0 <= k < |PreorderTraversal(root)| ==> PreorderTraversal(root)[k] in root.repr
{
if root.left != null && root.right != null then [root]+PreorderTraversal(root.left)+PreorderTraversal(root.right) else if root.left != null then [root]+PreorderTraversal(root.left) else if root.right != null then [root]+PreorderTraversal(root.right) else [root]
}
lemma {:verify true} PreorderTraversalChildrenAreLater(root: TreeNode)
requires root.Valid()
//the following does not verify
ensures forall x :: x in root.repr ==> exists k: nat :: 0 <= k < |PreorderTraversal(root)| && PreorderTraversal(root)[k] == x
{
// var what := PreorderTraversal(root);
assert forall x :: x in root.repr ==> x in PreorderTraversal(root);
forall x | x in root.repr
ensures exists k: nat :: 0 <= k < |PreorderTraversal(root)| && PreorderTraversal(root)[k] == x
{
assert x in PreorderTraversal(root);
seqbusiness(PreorderTraversal(root), x);
}
// but it verifies here, at least I get the green checkmark
assert forall x :: x in root.repr ==> exists k: nat :: 0 <= k < |PreorderTraversal(root)| && PreorderTraversal(root)[k] == x;
}
lemma seqbusiness<A>(s: seq<A>, elem: A)
requires elem in s
ensures exists k:nat :: 0 <= k < |s| && s[k] == elem
{
}
class TreeNode {
var val: int;
var left: TreeNode?;
var right: TreeNode?;
ghost var repr: set<TreeNode>;
constructor(val: int, left: TreeNode?, right: TreeNode?)
requires left != null ==> left.Valid()
requires right != null ==> right.Valid()
requires left != null && right != null ==> left.repr !! right.repr
ensures this.val == val
ensures this.left == left
ensures this.right == right
ensures left != null ==> this !in left.repr
ensures right != null ==> this !in right.repr
ensures Valid()
{
this.val := val;
this.left := left;
this.right := right;
var leftRepr := if left != null then {left}+left.repr else {};
var rightRepr := if right != null then {right}+right.repr else {};
this.repr := {this} + leftRepr + rightRepr;
}
predicate Valid()
reads this, repr
decreases repr
{
this in repr &&
(this.left != null ==>
(this.left in repr
&& this !in this.left.repr
&& this.left.repr < repr
&& this.left.Valid()
))
&& (this.right != null ==>
(this.right in repr
&& this !in this.right.repr
&& this.right.repr < repr
&& this.right.Valid())) &&
(this.left != null && this.right != null ==> this.left.repr !! this.right.repr && this.repr == {this} + this.left.repr + this.right.repr)
&& (this.left != null && this.right == null ==> this.repr == {this} + this.left.repr)
&& (this.right != null && this.left == null ==> this.repr == {this} + this.right.repr)
&& (this.right == null && this.left == null ==> this.repr == {this})
}
}
I agree it doesn't verify as is and its not clear why. However, I did manage to get it to go through by lifting out a predicate as follows:
predicate WithinRootPreorder(root:TreeNode, x: TreeNode)
reads root.repr
requires root.Valid()
requires x in root.repr {
exists k: nat :: 0 <= k < |PreorderTraversal(root)| && PreorderTraversal(root)[k] == x
}
lemma {:verify true} PreorderTraversalChildrenAreLater(root: TreeNode)
//reads root.repr
requires root.Valid()
ensures root.repr == old(root.repr)
//the following does not verify
ensures forall x :: x in root.repr ==> WithinRootPreorder(root,x)
{
...
assert forall x :: x in root.repr ==> WithinRootPreorder(root,x);
}
That seemed to work for me.
I'm trying to define an operation on classes and then prove properties about it.
//the following resolves the error with substring, but creates problems down the line
//predicate isSubstring<A(!new)>(sub: seq<A>, super: seq<A>) {
predicate isSubstring<A>(sub: seq<A>, super: seq<A>) {
|sub| <= |super| && exists xs: seq<A> :: IsSuffix(xs, super) && sub <= xs
}
predicate IsSuffix<T>(xs: seq<T>, ys: seq<T>) {
|xs| <= |ys| && xs == ys[|ys| - |xs|..]
}
However, the IsSubstring method produces the following error.
a exists expression involved in a predicate definition is not allowed to depend on the set of allocated references, but values of 'xs' may contain references (see documentation for 'older' parameters
I am aware that I can set (!new) restriction on the type variable A. That resolves the issue at the predicate. However I am presented with another issue.
lemma AllChildrenTraversalsAreSubstrings(root: TreeNode)
requires root.Valid()
ensures forall x :: x in root.repr && x in PreorderTraversal(root) ==> isSubstring(PreorderTraversal(x), PreorderTraversal(root))
{
forall x | x in root.repr && x in PreorderTraversal(root)
ensures isSubstring(PreorderTraversal(x), PreorderTraversal(root))
{
if x == root {
}else if x == root.left || x == root.right {
PreorderTraversalSubstrings(root);
}else {
if root.left != null && x in root.left.repr {
AllChildrenTraversalsAreSubstrings(root.left);
}
if root.right != null && x in root.right.repr {
AllChildrenTraversalsAreSubstrings(root.right);
}
}
}
}
In the forall ensure it reports:
type parameter (A) passed to predicate isSubstring must support no references (got TreeNode)
function method PreorderTraversal(root: TreeNode): seq<TreeNode>
reads root.repr
requires root.Valid()
// ensures forall x :: x in PreorderTraversal(root) ==> x.Valid()
ensures forall x :: x in root.repr ==> x in PreorderTraversal(root)
ensures forall k :: 0 <= k < |PreorderTraversal(root)| ==> PreorderTraversal(root)[k] in root.repr && PreorderTraversal(root)[k].Valid()
ensures injectiveSeq(PreorderTraversal(root))
ensures forall k :: 0 <= k < |PreorderTraversal(root)| ==> PreorderTraversal(root)[k] in root.repr
// ensures forall k :: 0 <= k < |PreorderTraversal(root)| ==> forall child :: child in PreorderTraversal(root)[k].repr && child != child in PreorderTraversal(root)[k] ==> exists j :: k < j < |PreorderTraversal(root)| && PreorderTraversal(root)[j] == child
{
if root.left != null && root.right != null then [root]+PreorderTraversal(root.left)+PreorderTraversal(root.right) else if root.left != null then [root]+PreorderTraversal(root.left) else if root.right != null then [root]+PreorderTraversal(root.right) else [root]
}
lemma PreorderTraversalSubstrings(root: TreeNode)
requires root.Valid()
ensures root.left != null ==> isSubstring(PreorderTraversal(root.left), PreorderTraversal(root))
ensures root.right != null ==> isSubstring(PreorderTraversal(root.right), PreorderTraversal(root))
{
if root.left != null && root.right != null {
calc {
PreorderTraversal(root);
[root]+PreorderTraversal(root.left)+PreorderTraversal(root.right);
}
assert |PreorderTraversal(root.left)| < |PreorderTraversal(root)|;
assert |PreorderTraversal(root.right)| < |PreorderTraversal(root)|;
assert IsSuffix(PreorderTraversal(root.left)+PreorderTraversal(root.right), PreorderTraversal(root));
assert IsSuffix(PreorderTraversal(root.right), PreorderTraversal(root));
assert PreorderTraversal(root.left) <= PreorderTraversal(root.left)+PreorderTraversal(root.right);
}else if root.left != null && root.right == null {
calc {
PreorderTraversal(root);
[root]+PreorderTraversal(root.left);
}
assert |PreorderTraversal(root.left)| < |PreorderTraversal(root)|;
assert IsSuffix(PreorderTraversal(root.left), PreorderTraversal(root));
}else if root.left == null && root.right != null {
calc {
PreorderTraversal(root);
[root]+PreorderTraversal(root.right);
}
assert |PreorderTraversal(root.right)| < |PreorderTraversal(root)|;
assert IsSuffix(PreorderTraversal(root.right), PreorderTraversal(root));
}
}
class TreeNode {
var val: int;
var left: TreeNode?;
var right: TreeNode?;
ghost var repr: set<TreeNode>;
constructor(val: int, left: TreeNode?, right: TreeNode?)
requires left != null ==> left.Valid()
requires right != null ==> right.Valid()
requires left != null && right != null ==> left.repr !! right.repr
ensures this.val == val
ensures this.left == left
ensures this.right == right
ensures left != null ==> this !in left.repr
ensures right != null ==> this !in right.repr
ensures Valid()
{
this.val := val;
this.left := left;
this.right := right;
var leftRepr := if left != null then {left}+left.repr else {};
var rightRepr := if right != null then {right}+right.repr else {};
this.repr := {this} + leftRepr + rightRepr;
}
predicate Valid()
reads this, repr
decreases repr
{
this in repr &&
(this.left != null ==>
(this.left in repr
&& this !in this.left.repr
&& this.left.repr < repr
&& this.left.Valid()
))
&& (this.right != null ==>
(this.right in repr
&& this !in this.right.repr
&& this.right.repr < repr
&& this.right.Valid())) &&
(this.left != null && this.right != null ==> this.left.repr !! this.right.repr && this.repr == {this} + this.left.repr + this.right.repr)
&& (this.left != null && this.right == null ==> this.repr == {this} + this.left.repr)
&& (this.right != null && this.left == null ==> this.repr == {this} + this.right.repr)
&& (this.right == null && this.left == null ==> this.repr == {this})
}
}
Oddly enough,PreorderTraversalSubstrings verifies just fine even with the (!new) restriction but the ensure statement of the forall in AllChildrenTraversalsAreSubstrings throws the above error. How should I proceed? Switching to a datatype would make my life easier but I'm trying to verify programs involving classes.
Do I define the binary tree datatype and then assert that all operations on it are equivalent to the valid class tree version? Is that even possible if existence quantifier expressions can't refer to allocated value?
I'm trying to prove a little lemma for a larger proof, the lemma definition is below:
lemma LoopLemma(a: seq<int>, b: seq<int>, c: seq<int>, k:int, i:int, j:int)
requires 0 <= i < |a| && 0<= j < |b| && 0 <= k < |c| && i +j ==k && |a| + |b| == |c|
requires Sorted(c[..k]) && Sorted(b) && Sorted(a)
requires multiset(c[..k]) == multiset(a[..i]+b[..j])
ensures Sorted(c[..k]+[b[j]]) && Sorted(c[..k]+[a[i]])
{
assert multiset(c[..k]) == multiset(a[..i]+b[..j]);
var q:=a[..i]+b[..j];
var c1 := c[..k];
assert Sorted(c1);
assert multiset(c1) == multiset(q);
assert |q| == i + j;
assert |c1| == k == i + j;
calc {
multiset(c1) == multiset(q);
==
forall l :: l in multiset(c1) ==> l in multiset(q);
== {assert forall l :: l in multiset(q) ==> exists r :: 0 <= r <|q| && l == q[r]; assert forall l :: l in multiset(c1) ==> exists r :: 0 <= r <|c1| && l == c1[r];}
forall l :: 0<=l <|c1| ==> exists r :: 0 <= r < |q| && q[r] == c1[l];
}
}
I get "the calculation step between the previous line and this line might not hold" for the last step, and I don't understand why.
All I'm saying there is that if the multisets of two sequences are equal, for any entry in the first sequence exists an entry in the second sequence with the same value.
I tried some simpler examples (where I define the sequences explicitly) and it worked. Maybe I don't understand something about multisets?
Any suggestions will help.
Additional hint that needed here is every element in sequence is in multiset i.e forall i :: 0 <= i < |c[..k]| ==> c[..k][i] in multiset(c[..k]) (hint in forward direction of reasoning). Following snippet verifies.
predicate Sorted(a: seq<int>)
{
if |a| <= 1 then true else (a[0] <= a[1]) && Sorted(a[1..])
}
lemma LoopLemma(a: seq<int>, b: seq<int>, c: seq<int>, k: int, i: int, j: int)
requires 0 <= i < |a| && 0 <= j < |b| && 0 <= k < |c| && i + j == k && |a| + |b| == |c|
requires Sorted(c[..k]) && Sorted(a) && Sorted(b)
requires multiset(c[..k]) == multiset(a[..i] + b[..j])
{
var s := a[..i] + b[..j];
calc {
multiset(c[..k]) == multiset(s);
forall e :: e in multiset(c[..k]) ==> e in multiset(s);
{
assert forall e :: e in multiset(c[..k]) ==>
exists r :: 0 <= r < |s| && s[r] == e;
// assert forall e :: e in multiset(c[..k]) ==>
// exists r :: 0 <= r < |c[..k]| && c[..k][r] == e;
assert forall i :: 0 <= i < |c[..k]| ==> c[..k][i] in multiset(c[..k]);
}
forall i :: 0 <= i < |c[..k]| ==> exists r :: 0 <= r < |s| && s[r] == c[..k][i];
}
}
Could somebody please help me out in writing a Dafny predicate that checks if a string is sorted in the order 'b' -> 'a' -> 'd'
i.e:
"bbbaaaaad" == true
"abd" == false
"bad" == true
The predicate should have the following form:
predicate sortedbad(s:string)
{
[???]
}
Thanks
Here is one way to do it.
predicate bad_compare(c1:char, c2:char) {
c1 == 'b' || (c1 == 'a' && c2 != 'b') || c2 == 'd'
}
predicate sortedbad(s:string)
{
forall i, j | 0 <= i <= j < |s| :: bad_compare(s[i], s[j])
}
lemma Test()
{
assert sortedbad("bbbbaaaadddd");
var s := "bbbbbbda";
assert !bad_compare(s[6], s[7]);
assert !sortedbad(s);
}
execute immediate 'insert into ' || rec.destinationtable || '(' || rec.destinationcolumn || ',' || rec.destinationcolumn1 || ') values ( select ' || rec.sourcecolumn || ' from ' || rec.sourcetable||' ,select ' || rec.sourcecolumn || ' from ' || rec.sourcetable||')';
it is showing "ORA-00936: missing expression" error in ORACLE 11g
You should use brackets when your are using select from multiple tables
Try
execute immediate 'insert into ' || rec.destinationtable || '(' || rec.destinationcolumn || ',' || rec.destinationcolumn1 || ') values ( (select ' || rec.sourcecolumn || ' from ' || rec.sourcetable||' ),(select ' || rec.sourcecolumn || ' from ' || rec.sourcetable||'))';
use like this
execute immediate 'insert into ' || rec.destinationtable ||
'(' || rec.destinationcolumn || ',' || rec.destinationcolumn1 || ')
( select ' || rec.sourcecolumn || ' from ' || rec.sourcetable||' ,
select ' || rec.sourcecolumn || ' from ' || rec.sourcetable||')';
if you are specifying the cloumn names to which insertion is to be done, omit the 'values' phrase in the query.
'insert into '
|| rec.destinationtable
|| '(' || rec.destinationcolumn
|| ',' || rec.destinationcolumn1
|| ') values ( select '
|| rec.sourcecolumn
|| ' from '
|| rec.sourcetable
|| ' ,select '
|| rec.sourcecolumn
|| ' from '
|| rec.sourcetable || ')';
If you execute the above expression, you get something like this:
insert into [destinationtable]
([destinationcolumn]
,[destinationcolumn1]
)
values (
select [sourcecolumn] from [sourcetable]
,select [sourcecolumn] from [sourcetable]
)
which is not a valid insert statement.
You are using the VALUES clause and supplying two expressions, but those expressions are themselves subqueries, so they need to be surrouned by parentheses, e.g.:
insert into [destinationtable]
([destinationcolumn]
,[destinationcolumn1]
)
values (
( select [sourcecolumn] from [sourcetable] )
, ( select [sourcecolumn] from [sourcetable] )
)
So you need to include them in your expression, e.g.:
execute immediate
'insert into '
|| rec.destinationtable
|| '(' || rec.destinationcolumn
|| ',' || rec.destinationcolumn1
|| ') values ( ( select '
|| rec.sourcecolumn
|| ' from '
|| rec.sourcetable
|| ' ) , ( select '
|| rec.sourcecolumn
|| ' from '
|| rec.sourcetable || ' ) )';
To make the code somewhat clearer you could do this:
DECLARE
source1 VARCHAR2(1000);
source2 VARCHAR2(1000);
BEGIN
source1 := 'select ' || rec.sourcecolumn || ' from ' || rec.sourcetable;
source2 := 'select ' || rec.sourcecolumn || ' from ' || rec.sourcetable;
execute immediate
'insert into ' || rec.destinationtable
|| '(' || rec.destinationcolumn
|| ',' || rec.destinationcolumn1
|| ') values ('
|| '(' || source1 || ')'
|| ', (' || source2 || ')'
|| ')';
END;