Why doesn't the FParsec example run? - f#

So I'm running the following code from the FParsec sample, but it doesn't seem to want to run.
namespace Test
open FParsec.CharParsers
module Stuff =
let main = run pfloat "1.25E3"
let str s = pstring s
let floatBetweenBrackets = str "[" >>. pfloat .>> str "]"
The problem is the last line - I get this error:
Error 1 Expecting a type supporting the operator '>>.' but given a function type. You may be missing an argument to a function. C:\Users\...\Documents\Visual Studio 2013\Projects\Solution2\ConsoleApplication1\load.fs 6 42 Formatting
The code is from here:
http://www.quanttec.com/fparsec/tutorial.html#parsing-a-float-between-brackets
If it matters, the version of F# I'm running is:
Microsoft (R) F# Interactive version 12.0.30110.0

>>. is defined in FParsec.Primitives, so you need to open that as well. Alternatively, you can just open FParsec, since
Opening the FParsec namespace also automatically opens the Primitives, CharParsers and Error modules.
This answer explains why the error you're seeing is what it is.

Related

F# FSI "Unexpected compiler generated literal in interaction" with "#I __SOURCE_DIRECTORY__"

If I create a new F# file 'Test.fsx' in VSCode with the line
#I __SOURCE_DIRECTORY__
attempting to run the code in FSI generates the error
Test.fsx(2,4): error FS0010: Unexpected compiler generated literal in interaction. Expected incomplete structured construct at or before this point, ';', ';;' or other token.
Any idea what might be going wrong?
It is a known issue in VS2019, which has been fixed in VS2022.
https://github.com/dotnet/fsharp/issues/13467
You may replace the code with
#I "."

F# NUnit test sets value as null

I have 2 files: Asm.fs, AsmTest.fs
Asm.fs
namespace Assembler
[<Measure>] type ln
[<Measure>] type col
[<Measure>] type port
type Addr = int<ln> * int<col> * int<port>
module Asm =
let emptyAddr : Addr = (-1<ln>, -1<col>, -1<port>)
AsmTest.fs
module Assembler.Tests
[<Test>]
let someTest() =
let x = Asm.emptyAddr
When I debug someTest() code, I get that x = null, what am I doing wrong?
P.S. files in the different projects visual studio projects. Asm.fs in the Assembler project and AsmTest.fs in the AssemblerTest project.
I found an interesting behavior. My problem will be resolved, if I add some file (even empty) to the Assembler project. Can anyone explain this behavior?
The debugger sometimes has issues showing the correct values. For me however, having exactly the same Asm.fs and AsmTest.fs like this:
module Assembler.Tests
open NUnit.Framework
[<Test>]
let someTest() =
let x = Asm.emptyAddr
Assert.IsNotNull x
the test passes and if I set a breakpoint at the assertion, x is correctly shown as {(-1, -1, -1)}
As the code that you show does not compile as it is (Block following this let is unfinished. in someTest), could you try my test above and/or show your complete test method?
Using your code I can reproduce the behaviour. If I set my projects to console applications, my test will fail as well. So, it seems that for console projects, not having any other file or an [<EntryPoint>] surprisingly skips the initialization of module values. For me, the compiler at least issues a warning Main module of program is empty where I use x in the test. The solution to the problem is therefore:
make sure you treat warnings as errors
have an [<EntryPoint>] for console applications
use library projects for libraries

F# Data Type Provider ; error on CSVProvider initialization

I am trying to set up a tiny F# console app with FSharp.Data referenced in the solution. I got the following error at runtime :
An unhandled exception of type 'System.TypeInitializationException' occurred in Anot_F1.exe
for this code (error in line 4) :
1 open FSharp.Data
2 type Anot_lines = CsvProvider<"anot1.csv",Separators=";">
3 let ll = Anot_lines.Load("anot1.csv")
4 for r in ll.Rows do
5 printfn "%A" r.ToString
In debug mode after line 3, I can see that the variable ll contains the proper Headers but does not show the rows.
My CSV file is :
tline;tcol;bline;bcol;anot
3;1;4;16;"Barack Obama has ... The US president"
3;1;3;12;"Barack Obama"
3;18;3;26;"ratcheted"
4;102;4;109;"agencies"
4;289;4;306;"financial pressure"
4;1;4;320;"The US president ...ure on the regime"
4;1;4;16;"The US president"
I am new to F# and especially have no experience on using type providers.
Any help greatly appreciated.
The issue is with line 3, you're using a method that loads CSV data from a URL. You need to use the GetSample() method. Also note that the "%A" format placeholder can print any value and doesn't require a ToString() call.
let ll = Anot_lines.GetSample()
for r in ll.Rows do
printfn "%A" r

Simple correct implementation file in verbose syntax?

UPDATE
The general question is: how to use verbose syntax of F# correctly? Verbose syntax is the syntax which is close to OCaml syntax, i.e. syntax with many commas etc.
OLD TEXT
I want to turn light syntax off in F# to have verbose syntax which is closer to OCaml.
I wrote the following code
#light "off"
let k=3.14;;
and got an error on let:
Unexpected keyword 'let' or 'use' in implementation file
What is correct implementation file structure without light syntax?
The problem is that you have written this inside a .fsi file - which is an FSharp Interface definition file; it has nothing to do with fsi.exe (FSharp Interactive).
The message "Unexpected keyword 'let' or 'use' in implementation file" is a tell - interface definitions were expected. Simply use a .fs extension.
If you want reuse ML code, consider changing the file extension to .ml, and add a #nowarn "62" directive at the beginning to ignore the legacy warning.
#nowarn "62"
#light "off"
let div2 = 2;;
let f x =
let r = x % div2 in
if r = 1 then
begin "Odd" end
else
begin "Even" end
I don't see anything wrong but... why the two ;? Are you compiling it or running in fsi?

Error while creating a F# Library

I created a new F# library with say the following expressions:
module Module1
let x = 2 + 2
When trying to run this, I get an error
Unexpected start of structured construct in definition. Expected '=' or other token.
When you say "run", do you mean select all -> ALT + ENTER to send to FSI? If so, I think that is your issue. FSI doesn't handle file-level module declarations. You need to either not select module Module1 when you send it to FSI, or change it to a module expression:
module Module1 =
let x = 2 + 2

Resources