Is there a way to refer to the result of the previous command in PSCi? - read-eval-print-loop

Coming from Haskell, I'm accustomed to using it to refer to the result of the previous command in GHCi:
ghci> 1+1
2
ghci> :type it
it :: Num a => a
ghci> it*2
4
I went looking to see if there was something similar in the PureScript REPL, but I couldn't find anything about it on the "Differences from Haskell" page in the PureScript docs (which makes a certain kind of sense now that I've had some time to think about it, since it is a specific feature of GHCi, not of the Haskell language itself), or in the PSCi documentation. In GHCi I can :show bindings to discover that it has been bound, but PSCi's :show command doesn't accept the bindings argument, and from the built-in PSCi help :? it's not obvious to me if there's a different command that does the same thing. (I know PSCI's :browse lets you examine the functions from a specific module, so since errors reported in PSCi often begin with Error found: in module $PSCI I tried :browse $PSCI, but this produces an indentation error.)
Some errors I've encountered seem to suggest that PSCi is trying to bind the result of a computation to the name it (I've cut some whitespace but otherwise this is copied verbatim from a fresh spago repl session in a toy project—note the final line, in value declaration it):
> import Prelude
> 1+'a'
Error found:
in module $PSCI
at :1:3 - 1:6 (line 1, column 3 - line 1, column 6)
Could not match type
Char
with type
Int
while checking that type Char
is at least as general as type Int
while checking that expression 'a'
has type Int
in value declaration it
But after a successful computation, it isn't available:
> 1+1
2
> it
Error found:
at <internal>:0:0 - 0:0 (line 0, column 0 - line 0, column 0)
The value of it is undefined here, so this reference is not allowed.
This is a CycleInDeclaration error, which is different from the UnknownName error produced when trying to use some other undefined name like x or foo. This all makes me suspect there is something special going on with it and I'm just missing something obvious about how to make it available for use in the REPL, but as someone brand new to PureScript, I'm not sure where to look next.

Related

Error_Generator expression_spyder(python 3.6)

I develop mathematical model using gurobi solver, in python.
I get the following error while running:
SyntaxError: Generator expression must be parenthesized if not sole argument
My constraint is:
My code is:
for s in S:
m.addConstr(sum(x[s,s0,c,i] for s0 in S0 for c in C for i in D,s!=p) == 1,'C_3')
First of all, everything comes whenever you add that comma: ,s!=p.
I just emulated your code with a model I'm working on, and I obviously got the same error. Look around (e.g. if else in a list comprehension), and you will see that the only mistake you had was that the iterator within the generator wasn't well specified. That means, you had to use an if clause in order to achieve what you wanted:
for s in S:
m.addConstr(
quicksum(x[s,s0,c,i] for s0 in S0 for c in C for i in D if s!=p) == 1,
'C_3_'+str(s) )
By the way, as included in the code, you should use quicksum instead of sum. Furthermore, I would suggest to try changing the order of the iterators; in other words, it's not the same for a computer to enumerate a list of 5 elements 1000 times than to enumerate 5 times a list of 1000 elements, and this is something quite important in Python timing.
As a side note, I got into this question while looking for this:
TypeError: unsupported operand type(s) for +: 'generator' and 'generator'

ocaml: Basic syntax for function of several arguments

I am learning OCaml and I'm a complete beginner at this point. I'm trying to get used to the syntax and I just spent 15 minutes debugging a stupid syntax error.
let foo a b = "bar";;
let biz = foo 2. -1.;;
I was getting an error This expression has type 'a -> string but an expression was expected of type int. I resolved the error, but it prompted me to learn what is the best way to handle this syntax peculiarity.
Basically OCaml treats what I intended as the numeric constant -1. as two separate tokens: - and 1. and I end up passing just 1 argument to foo. In other languages I'm familiar with this doesn't happen because arguments are separated with a comma (or in Scheme there are parentheses).
What is the usual way to handle this syntax peculiarity in OCaml? Is it surrounding the number with parentheses (foo 2. (-1.)) or there is some other way?
There is an unary minus operator ~-. that can be used to avoid this issue: foo ~-.1. (and its integer counterpart ~-) but it is generally simpler to add parentheses around the problematic expression.

Datalog code not working in DrRacket

I am trying to run this prolog code in DrRacket: http://www.anselm.edu/homepage/mmalita/culpro/graf1.html
#lang datalog
arc(a,b).
arc(b,c).
arc(a,c).
arc(a,d).
arc(b,e).
arc(e,f).
arc(b,f).
arc(f,g).
pathall(X,X,[]).
pathall(X,Y,[X,Z|L]):- arc(X,Z),pathall(Z,Y,L). % error on this line;
pathall(a,g)?
However, it is giving following error:
read: expected a `]' to close `['
I suspect '|' symbol is not being read as head-tail separator of the list. Additionally, [] is also giving error (if subsequent line is removed):
#%app: missing procedure expression;
probably originally (), which is an illegal empty application in: (#%app)
How can these be corrected so that the code works and searches for paths between a and g ?
The Datalog module in DrRacket is not an implementation of Prolog, and the syntax that you have used is not allowed (see the manual for the syntax allowed).
In particular terms cannot be data structures like lists ([]). To run a program like that of above you need a Prolog interpreter with data structures.
What you can do is define for instance a predicate path, like in the example that you have linked:
path(X,Y):- arc(X,Y).
path(X,Y):- arc(X,Z),path(Z,Y).
and, for instance, ask if a path exists or not, as in:
path(a,g)?
or print all the paths to a certain node with
path(X,g)?
etc.

Why does whitespace in my F# code cause errors?

I've been tinkering with the F# Interactive.
I keep getting weird results, but here's one I can't explain:
The following code returns 66, which is the value I expect.
> let f x = 2*x*x-5*x+3;;
> f 7;;
The following code throws a syntax error:
> let f x = 2*x*x - 5*x +3;;
stdin(33,21): error FS0003: This value is not a function and cannot be applied
As you can see, the only difference is that there are some spaces between the symbols in the second example.
Why does the first code example work while the second one results in a syntax error?
The problem here is the use of +3. When dealing with a +/- prefix on a number expression white space is significant
x+3: x plus 3
x +3: syntax error: x followed by the positive value 3
I've run into this several times myself (most often with -). It's a bit frustrating at first but eventually you learn to spot it.
It's not a feature without meaning though. It's necessary to allow application of negative values to functions
myFunc x -3: call function myFunc with parameters x and -3
The error message says that you are trying to call a function x with the argument +3 ( unary + on 3) and since x is not a function, hence the This value is not a function and cannot be applied

Can we peek out partially inferred typing info. from Ocaml toplevel/compiler for a program that does not compile?

I would like to know, in Ocaml, whether a partial typing info. can be drawn by some existed functionality of toplevel/compiler, for a program that does not compile? Let me explain.
In Ocaml, it's well known that inferred typed can be retrieved by -annot file. However, sometimes we have a piece of code that does not compile due to some typing error. It gives a error exported to the toplevel, of this pattern
"This expression has type A, but was expected type B"
An artificial example would be
# let x =
let y = 5 in
not y;;
Characters 32-33:
not y;;
^
Error: This expression has type int
but an expression was expected of type bool
The programmer of this piece of code should understand well the 2nd part of this message, i.e.,
"y is expected of type bool", because of the "not y" part. However, she/he might have some difficulty to understand the 1st part of this error message: how this "y" is inferred to have type "int"? Thus it would be interesting to have a partial set of inferred types, before the type conflicts are raised. For the example above, one would like the interpreter tells that the first "y" (from "let y = 5") is of type int, by which I will know the reason why the second "y" (from "not y") is infered to be of type int.
Could you tell me whether the described functionality is already provided by some ocaml interpreter/compiler?
In general words, my question is: can ocaml toplevel, or its interpreter, yield partially inferred types that user can retrieve in order to more efficiently find the source of their typing error?
This question might not make sense because of the non-uniqueness of the partially inferred type annotation. However, the example example should show that at least for some cases, and some partially inferred types have its usage.
Thank you for your ideas.
The type annotations by generated by the -annot switch are available even if the program did not compile. You'll see types for the expressions that the compiler got through, and some of them may be incomplete. This doesn't tell you the compiler's reasoning for inferring the types, but it does tell you how far the compiler went and lets you explore what it's inferred.
For example, with this source code:
let x = [(let y = 5 in not y); true];;
x has the type _a list (the compiler hasn't gotten far enough to figure out _a).
y has the type int.
not has the type bool -> bool.
The error message is that the second occurrence of y has the type int (and we've seen where it was inferred) but the context expects the type bool (and we can see that, since not is a function whose argument type is bool).
I don't know how to see these types from the toplevel, but if you have a source file with your code, you can run ocamlc -c -annot, open the source in a suitable editor (such as Emacs) and view the inferred types whether the compilation succeeded or not.

Resources