Dafny invalid Ident - syntax

I am getting an invalid Ident error in the forall line (from is to the first i), does anybody know why? This is unusual.
predicate SumMaxToRight(v:array<int>,i:int,s:int)
reads v
requires 0<=i<v.Length
{forall l, is {:induction l} :: 0<=l<=i && is==i+1 ==> Sum(v,l,is)<=s}
Version 3.0.0.

It looks like is is a keyword in Dafny now, so it cannot be used as a variable name.

Related

Fixing TypeError: unsupported format string passed to numpy.ndarray.__format__

trying to print a loop over x for values of q, but keep getting this syntax error. Can anyone help with this?
Because it's a vector/array, not a single value. You may try the following:
np.set_printoptions(precision=2)
print(f'x = {x} -> q = {q}')
You could also take a look at numpy.array2string function.

It is applied to too many arguments; maybe you forgot a `;'

I am trying to write a code that calculate the size of a list.
Here is what I've done:
let rec l = function
| [] -> 0
| t::q -> 1 + l q
print_int(l ([1;2;3;4]))
The problem is that it's saying me :
It is applied to too many arguments; maybe you forgot a `;'.
When I put the double semicolon ;; at the end of the definition of l it works well, yet I've read that ;; is not useful at all if you are not coding in the REPL, so here I don't see why it's giving me this error.
The following
print_int(l [1;2;3;4])
is a toplevel expression. Such expression needs to be preceded by ;;:
;; print_int(l [1;2;3;4])
Another option is to make this toplevel expression a binding with
let () = print_int(l [1;2;3;4])
When parsing the code the parser advances until it hits l q. At this point there could be more arguments that should get applied to the function l. So the parser keeps going and the next thing it finds is the value print_int. Another argument to l. Which gives you your error.
The parser has no way of knowing that you had finished the code for the function l. In the top level the special token ;; is used to tell the parser that the input is finished and it should evaluate the code now. After that it starts paring the remaining input again.
Now why doesn't compiled code also have the ';;' token?
Simply because its not needed. In compiled code the line print_int(l [1;2;3;4]) is not valid input. That would be a statement you want to execute and functional languages have no such thing. Instead print_int(l [1;2;3;4]) is an expression that returns a value, () in this case, and you have to tell the compiler what to do with that value. A let () = tells the compiler to match it against (). And the let ... also tells the compiler that the previous let rec l ... has finished. So no special ;; token is needed.
Or think of it this way: In the top level there is an implicit let _ = if your input doesn't start with let. That way you can just type in some expression and see what it evaluates to without having to type let _ = every time. The ';;' token still means "evaluate now" though and is still needed.

Swi-Prolog asserta error

i must assert in SWI this kind of CLP(FD) rules:
asserta(schedule(A,B) :- V = [S0,S1,S2],V ins 0..sup).
but i get this error:
ERROR: Syntax error: Operator expected
ERROR: asserta(schedule(A,B) :- V = [S0,S1,S2],V
ERROR: ** here **
ERROR: ins 0..sup) .
why? thank you!
There are two errors here:
You need to have CLP(FD) loaded at the time when reading the text.
So there needs to be a use_module(library(clpfd)) either as a directive like in a line
:- use_module(library(clpfd)).
or entered as a goal on the toplevel. This is necessary because you are using (ins)/2 in operator form.
The other problem is the missing parentheses. It should rather read:
..., asserta( ( schedule(A,B) :- V = [_,_,_], V ins 0..sup ) ), ...
As an aside, I do not think that asserting such a rule makes much sense. Dynamic databases are rarely used together with asserting rules as this one.

XSB Prolog partial order tabling

I'm trying an example from XSB Version 3.3.5 manual (from "Partial Order Answer Subsumption"):
:- table sp(_,_,po(</2)).
sp(X,Y,1):- edge(X,Y).
sp(X,Z,N):- sp(X,Y,N1),edge(Y,Z),N is N1 + 1.
And I'm getting
++Error[XSB/Runtime/P]: [Syntax] :- table sp ( _ , _ , po ( >/ <--- HERE? ************
++ 2 ) )
Any ideas what's wrong?
Also, there is no error with
:- table sp(_,_,lattice(min/3)).
I would try this (since it's a syntax error)
:- table sp(_,_,po('<'/2)).
sp(X,Y,1):- edge(X,Y).
sp(X,Z,N):- sp(X,Y,N1),edge(Y,Z),N is N1 + 1.
It's strange that the operator 'reversed' in the error message (or the error message loose 1 character?). The table directive could generate the error 'inside' (directives are library predicate calls) or the error could be generated before, consulting.
Could be a syntax error due to change in operator declaration (i.e. some declaration like op(N,xfx,<) or op(M,xfy,/) changed N regards M), or the sample could be misaligned on current table/3 specification.
I'd try (similar to what thanosQR suggest), the simplest thing, changing the directive and removing the (redundant?) arity indication:
:- table sp(_,_,po(<)).

translate(list1, list2) in prolog

I was trying a functor translate([3,5,1,3],[three,five,one,three]) which does the operation of printing numbers. I get a strange warning while executing like this,
35 ?- translate([1,2,3],[a,b,c]).
ERROR: write/2: stream `a' does not exist
domains
list1=integer*
list2=symbol*
predicates
translate(list1,list2)
means(integer,symbol)
clauses
translate([],[]).
translate([],_):-
write("\nError in Input").
translate(_,[]):-
write("\nError in Input").
translate([Head1|Tail1],[Head2|Tail2]):-
write(Head2," = "),
means(Head1,Name),
write(Name,"\n"),
translate(Tail1,Tail2).
means(0,zero).
means(1,one).
means(2,two).
means(3,three).
means(4,four).
means(5,five).
means(6,six).
means(7,seven).
means(8,eight).
means(9,nine).
What exactly is the problem? This is the expected value.
translate([1,2,3],[a,b,c])
a = one
b = two
c = three
Yes
Variables need to be uppercase:
translate([1,2,3],[A,B,C]).
When you enter the translate([Head1|Tail1],[Head2|Tail2]) clause, a unifies with Head2, and then you try to satisfy write(Head2, "="), which is write(a, "=").
write/2 takes as first argument a Stream and writes the second argument to that Stream.
Presumably you want to use - if you want output at all - something like
writef('Head2 = %w', [Head2])
(I got the formatting from here.)

Resources