Discriminated union type checking in F# with differernt return types - visual-studio

I am trying to write an interpreter in F#. I want to check the type of expressions.
Here is my discriminated union for the expressions
type Expr =
| Integer of int
| String of string
| Boolean of bool
This is the method i am using to check the types with
let checkType (e:Expr) =
match e with
| String s -> s
| Integer i -> i
| Boolean b -> b
I want the method to determine wether an expression is a string,integer or boolean.
However, visual studio gives me the following error on line 4 of the checkType method:
This expression was expected to have type string but here has type int
Am i missing something?

To expand on John Palmer's comment:
F# expects each function to have a single return type. For example, you can write a function that takes an int and returns an int, which would be a function of type int -> int. A function that parses strings into ints would be of type string -> int. And so on.
Now, what is the return type of the checkType function you've written? Since you don't specify a return type, the compiler looks at the type of the values you can return from the function -- every possible code branch must return a value, and they must all be the same type. So it looks at your match statement, sees that its first branch returns a string, and says, "Ah ha! I've figured out the return type of this function; this is a function that takes an Expr and returns a string. The function's type is Expr -> string."
Then it looks at the second branch of your match statement, and says, "Wait a minute. This is a function that returns a string, but in this code branch it's returning an int. That's not valid: the code that calls this function needs to know what type to expect it to return." And so you get the error.
Now, if you were to swap the order of your match statement cases, checking for Integer i first, then the compiler would evaluate your function as having type Expr -> int (taking an Expr input and returning an int output), and throw an error on the | String s -> s line. This time, the error would be "Wait a minute, this is a function that returns an int, so the expression s here should have been of type int. But instead, it's of type string. That's not valid."
Or delete the | String s -> s line, and you'll see an error "This expression was expected to have type int but here has type bool." Same thing: each function can have only one return type. If you want to return multiple different possible types from a function, that's what Discriminated Unions are for.
For more reading on F# types, see http://fsharpforfunandprofit.com/series/understanding-fsharp-types.html.

You can wrap all of the expressions into an Option type like Some or String:
let checkType (e:Expr) =
match e with
| String e -> "we are string: " + string e
| Integer e-> "we are integer: " + string e
| Boolean e -> "we are boolean: " + string e

Related

F# record: ref vs mutable field

While refactoring my F# code, I found a record with a field of type bool ref:
type MyType =
{
Enabled : bool ref
// other, irrelevant fields here
}
I decided to try changing it to a mutable field instead
// Refactored version
type MyType =
{
mutable Enabled : bool
// other fields unchanged
}
Also, I applied all the changes required to make the code compile (i.e. changing := to <-, removing incr and decr functions, etc).
I noticed that after the changes some of the unit tests started to fail.
As the code is pretty large, I can't really see what exactly changed.
Is there a significant difference in implementation of the two that could change the behavior of my program?
Yes, there is a difference. Refs are first-class values, while mutable variables are a language construct.
Or, from a different perspective, you might say that ref cells are passed by reference, while mutable variables are passed by value.
Consider this:
type T = { mutable x : int }
type U = { y : int ref }
let t = { x = 5 }
let u = { y = ref 5 }
let mutable xx = t.x
xx <- 10
printfn "%d" t.x // Prints 5
let mutable yy = u.y
yy := 10
printfn "%d" !u.y // Prints 10
This happens because xx is a completely new mutable variable, unrelated to t.x, so that mutating xx has no effect on x.
But yy is a reference to the exact same ref cell as u.y, so that pushing a new value into that cell while referring to it via yy has the same effect as if referring to it via u.y.
If you "copy" a ref, the copy ends up pointing to the same ref, but if you copy a mutable variable, only its value gets copied.
You have the difference not because one is first-value, passed by reference/value or other things. It's because a ref is just a container (class) on its own.
The difference is more obvious when you implement a ref by yourself. You could do it like this:
type Reference<'a> = {
mutable Value: 'a
}
Now look at both definitions.
type MyTypeA = {
mutable Enabled: bool
}
type MyTypeB = {
Enabled: Reference<bool>
}
MyTypeA has a Enabled field that can be directly changed or with other word is mutable.
On the other-side you have MyTypeB that is theoretically immutable but has a Enabled that reference to a mutable class.
The Enabled from MyTypeB just reference to an object that is mutable like the millions of other classes in .NET. From the above type definitions, you can create objects like these.
let t = { MyTypeA.Enabled = true }
let u = { MyTypeB.Enabled = { Value = true }}
Creating the types makes it more obvious, that the first is a mutable field, and the second contains an object with a mutable field.
You find the implementation of ref in FSharp.Core/prim-types.fs it looks like this:
[<DebuggerDisplay("{contents}")>]
[<StructuralEquality; StructuralComparison>]
[<CompiledName("FSharpRef`1")>]
type Ref<'T> =
{
[<DebuggerBrowsable(DebuggerBrowsableState.Never)>]
mutable contents: 'T }
member x.Value
with get() = x.contents
and set v = x.contents <- v
and 'T ref = Ref<'T>
The ref keyword in F# is just the built-in way to create such a pre-defined mutable Reference object, instead that you create your own type for this. And it has some benefits that it works well whenever you need to pass byref, in or out values in .NET. So you should use ref. But you also can use a mutable for this. For example, both code examples do the same.
With a reference
let parsed =
let result = ref 0
match System.Int32.TryParse("1234", result) with
| true -> result.Value
| false -> result.Value
With a mutable
let parsed =
let mutable result = 0
match System.Int32.TryParse("1234", &result) with
| true -> result
| false -> result
In both examples you get a 1234 as an int parsed. But the first example will create a FSharpRef and pass it to Int32.TryParse while the second example creates a field or variable and passes it with out to Int32.TryParse

Get variable type (ADO Object or String) in ASP Classic?

I'm trying to write a function in ASP Classic that accepts a parameter; but the parameter can either be a String, or an ADODB.Command Object. How can the function determine which type the parameter is?
So...
Function myfunction( input )
If is_ADODBCommand( input ) Then
' do stuff to object'
ElseIf is_string( input ) Then
' do stuff to string'
End If
End Function
Basically, please tell me how to do is_ADODBCommand and is_string
You can use VarType for primitive types.
is_string = (VarType(input) = vbString)
For object types, you can use TypeName since VarType returns a generic type specifier vbObject which gives no clue about the object's kind.
is_ADODBCommand = (TypeName(input) = "Command")

What is the type of `main0` function?

What is the type of main0 function?
I'm writing gtk3 library. However the g_application_run() function needs argv.
Of course, ATS language can use argv on main0 function. But what is the type?
The following code is declared in prelude/basics_dyn.dats:
//
symintr main0
//
fun
main_void_0
((*void*)): void = "ext#mainats_void_0"
fun
main_argc_argv_0
{n:int | n >= 1}
(argc: int n, argv: !argv(n)): void = "ext#mainats_argc_argv_0"
//
overload main0 with main_void_0
overload main0 with main_argc_argv_0
//
As you can see, main0 is overloaded with main_void_0 and main_argc_argv_0. The type argv(n) is essentially for a linear string array of size n that ends with the null value. Note that the null value is not counted as part of the size.

How to detect snprintf failure?

I am using snprintf to format string using user-defined format (also given as string). The code looks like this:
void DataPoint::valueReceived( QVariant value ) {
// Get the formating QVariant, which is only considered valid if it's string
QVariant format = this->property("format");
if( format.isValid() && format.type()==QMetaType::QString && !format.isNull() ) {
// Convert QString to std string
const std::string formatStr = format.toString().toStdString();
LOGMTRTTIINFO(pointName<<"="<<value.toString().toUtf8().constData()<<"=>"<<formatStr<<"["<<formatStr.length()<<'\n');
// The attempt to catch exceptions caused by invalid formating string
try {
if( value.type() == QMetaType::QString ) {
// Treat value as string (values are allways ASCII)
const std::string array = value.toString().toStdString();
const char* data = (char*)array.c_str();
// Assume no more than 10 characters are added during formating.
char* result = (char*)calloc(array.length()+10, sizeof(char));
snprintf(result, array.length()+10, formatStr.c_str(), data);
value = result;
}
// If not string, then it's a number.
else {
double data = value.toDouble();
char* result = (char*)calloc(30, sizeof(char));
// Even 15 characters is already longer than largest number you can make any sense of
snprintf(result, 30, formatStr.c_str(), data);
LOGMTRTTIINFO(pointName<<"="<<data<<"=>"<<formatStr<<"["<<formatStr.length()<<"]=>"<<result<<'\n');
value = result;
}
} catch(...) {
LOGMTRTTIERR("Format error in "<<pointName<<'\n');
}
}
ui->value->setText(value.toString());
}
As you can see I assumed there will be some exception. But there's not, invalid formatting string results in gibberish. This is what I get if I try to format double using %s:
So is there a way to detect that invalid formatting option was selected, such as formatting number as string or vice-versa? And what if totally invalid formatting string is given?
You ask if it's possible to detect format/argument mismatch at run-time, right? Then the short and only answer is no.
To expand on that "no" it's because Variable-argument functions (functions using the ellipsis ...) have no kind of type-safety. The compiler will convert some types of arguments to others (e.g. char or short will be converted to int, float will be converted to double), and if you use a literal string for the format some compilers will be able to parse the string and check the arguments you pass.
However since you pass a variable string, that can change at run-time, the compiler have no possibility for any kind of compile-time checking, and the function must trust that the format string passed is using the correct formatting for the arguments passed. If it's not then you have undefined behavior.
It should be noted that snprintf might not actually fail when being passed mismatching format specifier and argument value.
For example if using the %d format to print an int value, but then passing a double value, the snprintf would happily extract sizeof(int) bytes from the double value, and interpret it as an int value. The value printed will be quite unexpected, but there won't be a "failure" as such. Only undefined behavior (as mentioned above).
Thus it's not really possible to detect such errors or problems at all. At least not through the code. This is something that needs proper testing and code-review to catch.
What happens when snprintf fails? When snprintf fails, POSIX requires that errno is set:
If an output error was encountered, these functions shall return a negative value and set errno to indicate the error.
Also you can find some relevant information regarding how to handle snprintf failures Here.

lifetime not long enough rust

I want to open a file, replace some characters, and make some splits. Then I want to return the list of strings. however I get error: broken does not live long enough. My code works when it is in main, so it is only an issue with lifetimes.
fn tokenize<'r>(fp: &'r str) -> Vec<&'r str> {
let data = match File::open(&Path::new(fp)).read_to_string(){
Ok(n) => n,
Err(e) => fail!("couldn't read file: {}", e.desc)
};
let broken = data.replace("'", " ' ").replace("\"", " \" ").replace(" ", " ");
let mut tokens = vec![];
for t in broken.as_slice().split_str(" ").filter(|&x| *x != "\n"){
tokens.push(t)
}
return tokens;
}
How can I make the value returned by this function live in the scope of the caller?
The problem is that your function signature says "the result has the same lifetime as the input fp", but that's simply not true. The result contains references to data, which is allocated inside your function; it has nothing to do with fp! As it stands, data will cease to exist at the end of your function.
Because you're effectively creating new values, you can't return references; you need to transfer ownership of that data out of the function. There are two ways I can think of to do this, off the top of my head:
Instead of returning Vec<&str>, return Vec<String>, where each token is a freshly-allocated string.
Return data inside a wrapper type which implements the splitting logic. Then, you can have fn get_tokens(&self) -> Vec<&str>; the lifetime of the slices can be tied to the lifetime of the object which contains data.

Resources