Hello world won't compile with "The value or constructor is not defined" - f#

let prefix prefixString baseString =
prefixString + " " + baseString
prefix "Hello" "World"
With the code above I'm getting the error Stuff.fs(34,1): error FS0039: The value or constructor 'prefix' is not defined.
I'm not entirely sure why this is happening, as I'm watching a video series on F# in which literally the same code is being compiled and ran successfully. Is there something wrong with my environment?

In the comment, you mentioned that you are running the snippet using "run selection" command. This command runs the selected piece of code in F# Interactive which initially contains no definitions. So, if you select and run just the last line, you will get:
> prefix "Hello" "World";;
stdin(1,1): error FS0039: The value or constructor 'prefix' is not defined
This is because F# Interactive does not know what the definition of prefix is - it does not look for it automatically in your file. You can fix this by selecting everything and running all code in a single interaction, or you can first run the definition and then the last line, i.e.:
> let prefix prefixString baseString =
prefixString + " " + baseString;;
val prefix : prefixString:string -> baseString:string -> string
> prefix "Hello" "World";;
val it : string = "Hello World"
Note that when you run the first command, F# Interactive will print the type of the defined functions, so you can see what has just been defined.
The fact that F# Interactive has its own separate "state of the world" is quite important, as it also means that you need to re-run functions after you change them so that subsequent commands use the new definition.

Related

Rascal: parsing string with multiple "_"s

I try to parse a string containing multiple "_"s, but I get a CallFailed exception.
I have tried to create a small as possible example of the problem syntax.
layout Layout = WhitespaceAndComment* !>> [\ \t\n\r#];
lexical WhitespaceAndComment = [\ \t\n\r] | #category="Comment" "#" ![\n]* $;
syntax SourceList = sourceList: "$"? "{"? Id sourceFile "}"?;
lexical Id = ([a-zA-Z/.\-][a-zA-Z0-9_/.]* !>> [a-zA-Z0-9_/.]) \ Reserved;
keyword Reserved =
"$" | "{" | "}" ;
I am unable to parse this small example.
rascal>try { parse(#SourceList, "test"); } catch CallFailed(m, e): println("<m> : <e>");
|prompt:///|(25,9,<1,25>,<1,34>) : [type(sort("SourceList"),(sort("SourceList"):choice(sort("SourceList"),{prod(label("sourceList",sort("SourceList")),[opt(lit("$")),layouts("$default$"),opt(lit("{")),layouts("$default$"),label("sourceFile",lex("Id")),layouts("$default$"),opt(lit("}"))],{})}),layouts("$default$"):choice(layouts("$default$"),{prod(layouts("$default$"),[],{})}),empty():choice(empty(),{prod(empty(),[],{})}),lex("Id"):choice(lex("Id"),{prod(lex("Id"),[conditional(seq([\char-class([range(45,47),range(65,90),range(97,122)]),conditional(\iter-star(\char-class([range(46,57),range(65,90),range(95,95),range(97,122)])),{\not-follow(\char-class([range(46,57),range(65,90),range(95,95),range(97,122)]))})]),{delete(keywords("Reserved"))})],{})}),keywords("Reserved"):choice(keywords("Reserved"),{prod(keywords("Reserved"),[lit("$")],{}),prod(keywords("Reserved"),[lit("}")],{}),prod(keywords("Reserved"),[lit("{")],{})}))),"${test}"]
ok
A changed sourcefile from "test" to "${test}" gives exactly the same output.
The complete syntax in which SourceList is embedded has many more rules. But then I get the following results.
set(${TARGET_NAME}_DEPS
GenConfiguration_OBJ_TN_Common # accept
${COMMON_BB_PCMDEPS} # reject
COMMON_BB_PCMDEPS # accept
COMMON_BB_PCM_DEPS # reject
)
for which I want to have a solution.
What is wrong with the minimal example? Why is test or ${test} not accepted?
BTW: I am using the latest unstable. Does it make sense to install and try the stable release?
I've tried to reproduce your problem, but it seems to work here:
rascal>parse(#SourceList, "test")
SourceList: (SourceList) `test`
The unstable version is fine at the moment. In fact it's high time to release a stable version. So for now you're better off with the unstable version.
The CallFailed exception is confusing. It means that a function is called which can not be matched OR not be found. So maybe parse is not in scope by not importing ParseTree, or you have a different function called parse which does not have a type[Tree] and str as the expected parameters in scope. As long as the ParseTree module is imported, you're call to parse should be fine.
Please let me know if you have made progress. Perhaps a restart of Eclipse might clear up something as well.

Enclosing a python Path variable in quotes?

I need help fixing a Python script, but know ABSOLUTELY NOTHING about Python (though I am an experienced programmer so I get the jargon.)
I have this script that is trying to write output to a generated path, but when I run it, it gives me an error saying something about "E:\Program", and when I check, sure enough, it creates a folder named "Program" in the root of my E: drive. I'm CERTAIN it is trying to write to "E:\Program Files" but the space is terminating the command.
I did find where "path" is assigned:
path = tkm.path_archuncomp + bs.path + bs.name + '.ext'
How would I enclose that path in quotes that are assigned to the variable?
Inserting the quotes character appears to be the same as languages like C. Simply use "slash" notion:
path = \" + some_variable + \"
Unfortunately for me, repairing the script is more complicated than that, and doing so only created errors, so I can't confirm that was done properly. :(

Is it possible to trace the current line in an F# script with FSI library

We are using FSharp compiler service FSI evaluation session to execute a DSL. To be precise we are using F# code to simulate G-Code for a CNC machine. As each line of the FSI script moves the machine to a different location our users would like to see the current line of the script that is executing synced to the position of the machine.
Is it possible to get a callback from the FSI evaluation session indicating the current line being executed?
Use the LINE directive
let x = "this is on line " + __LINE__
result
val x : string = "this is on line 42"

Rhino: Retrieving the Line No and Column No

I am running my own proxy objects which extend org.mozilla.javascript.ScriptableObject.
I also have my own functions which extend org.mozilla.javascript.Function.
My desire is to have any exceptions thrown here return the line no and if possible the column number where they occurred in the evaluated script. Is this possible? I only have access to the context and the scope.
Whenever an exception is thrown from a script, Rhino throws RhinoException which already has line and column number (and more). However when you execute the script you need to provide the line number that will be used by Rhino as the starting line number. The actual line number of where the exception/error occurred will be relative to this number. So something line this:
//-- Define a simple test script to test if things are working or not.
String testScript = "function simpleJavascriptFunction() {" +
" this line has syntax error." +
"}" +
"simpleJavascriptFunction();";
//-- Compile the test script.
Script compiledScript = Context.getCurrentContext().compileString(testScript, "My Test Script", 2, null);
//-- Execute the test script.
compiledScript.exec(Context.getCurrentContext(), anyJavascriptScope);
In the above code, the starting line number is set to 2 (third parameter of the call to compileString()). When this is executed, Rhino will throw a RhinoException that will have the lineNumber property set to the value '3' (the first line is treated as the second line b/c we passed 2).
Hope this helps.

Powershell: inconsistent/strange behavior with quote parsing?

all! I'm trying to compile a program using PowerShell, but the command is being parsed strangely.
This command executes correctly in cmd.exe:
dmd -od"bin" -of"bin\convHull.exe" -I"src" "src\concSort.d" "src\fileParser.d" "src\main.d" "src\pointLogic.d" "src\quickHull.d" "src\stupidHull.d" -D -O -release
But PowerShell executes it as: (blue, navy, purple texts as they appear in PowerShell ISE)
dmd -od"bin" -of"bin\convHull.exe" -I"src" "src\concSort.d" "src\fileParser.d" "src\main.d" "src\pointLogic.d" "src\quickHull.d" "src\stupidHull.d" -D -O -release
This spits the following error:
The string starting:
At line:1 char:147
+ dmd -od"bin" -of"bin\convHull.exe" -I"src" "src\concSort.d" "src\fileParser.d" "src\main.d"
"src\pointLogic.d" "src\quickHull.d" "src\stupidHull.d <<<< " -D -O -release
is missing the terminator: ".
At line:1 char:163
So it seems to be interpreting a period as a quote. This is peculiar. Has anyone else had this problem with PowerShell?
Things I've tried:
escaping quotes
making sure all quotes are "straight quotes" and not angled
putting a space before quotes (parses correctly, but the program doesn't understand the arguments.)
Thanks,
Charles.
I believe this should do the trick (newlines added for clarity only, and removal of extra quotes):
dmd '-od"bin"' '-of"bin\convHull.exe"' '-I"src"'
src\concSort.d src\fileParser.d src\main.d src\pointLogic.d src\quickHull.d src\stupidHull.d
-D -O -release
Note that in the case where a quote (") is to be passed as part of the argument itself, I surrounded the entire argument with single quotes ('). From the experimentation below it can be seen that only -of"..." needs the quotes about it.
Happy coding.
I can't find a good reference on this exact production, however note the following parsings:
-x"w." -> error: " expected (last " is special)
-x"w."" -> -x"w and ."" (the . starts a new token and the " in that starts
a quote; however, the quotes are not removed)
'-x"w."' -> -x"w." (extra quote fine, neither special)
-x"w" -> -x"w" (no . and " not special)
-x"w"" -> -x"w"" (no . and " not special)
a".b" -> a.b (didn't start with `-`, quotes removed)
a".b -> error: " expected (" is special)
So it does indeed appear to have something to do with the . and - combination (and it might not be exclusive). From the above I believe that a token starting with - does not include the . character as a valid character in the token so the lexer terminates said token and starts a new token -- easily provable with a good EBNF reference, which I don't have.
The best I can find is Appendix C: The PowerShell Grammar:
The ParameterToken rule is used to match cmdlet parameters such as -foo or -
boolProp: . Note that this rule will also match --foobar, so this rule has
to be checked before the --token rule.
<ParameterToken> = -[:letter:]+[:]{0 |1}
However, this is incomplete at best and does not even include a definition of "letter".
I don't have the executable, but this seems to want to work.
$cmd = #'
dmd -od"bin" -of"bin\convHull.exe" -I"src" "src\concSort.d" "src\fileParser.d" "src\main.d" "src\pointLogic.d" "src\quickHull.d" "src\stupidHull.d" -D -O -release
'#
&$cmd

Resources