I'd like to write a function that is given a list of option references, and checks that each reference is not None. That is, I'd like a function
let check rs =
List.for_all (fun r -> (!r) != None) rs
let _ =
check [ref (Some 5); ref (Some true)]
This doesn't work. The compiler can't find a type for the list [ref (Some 5); ref (Some true)], even though the check function seems inherently ok, with its nice polymorphic 'a option ref list -> bool type.
Is there any way to make this work?
My real situation, from which I've distilled the toy example above, is that I'm parsing a number of command-line arguments into a bunch of references. Some of the arguments are strings and some are ints, etc. Initially all of the references are set to None, and when the parser finds a command-line argument it sets the corresponding reference to Some .... Having done the parsing, I find myself wanting to iterate over a subset of the references to make sure they're not still None, because I want the corresponding command-line arguments to be mandatory.
OCaml doesn't support heterogeneous containers. You can try to solve your problem like this:
type arg =
| Unset
| Int of int
| Bool of bool
(* etc *)
let check rs =
List.for_all (fun r -> (!r) <> Unset) rs
let _ =
check [ref (Int 5); ref (Bool true); ref Unset]
One way to look at your problem is that your list of parameters can't be given a type in OCaml:
# [ ref (Some 5); ref (Some true) ];;
Error: This expression has type bool option ref
but an expression was expected of type int option ref
Type bool is not compatible with type int
If you're willing to wrap your references inside an object interface, you can have the list:
# class cr x = object method is_set = !x <> None end;;
class cr : 'a option ref -> object method is_set : bool end
# let reflist = [ new cr (ref (Some 5)); new cr (ref (Some true))];;
val reflist : cr list = [<obj>; <obj>]
Then you can check all the elements of the list:
# List.for_all (fun x -> x#is_set) reflist;;
- : bool = true
Related
I meet an error about subtyping.
For this code, List.map (fun ((String goal_feat):> Basic.t) -> goal_feat) (goal_feats_json:> Basic.t list).
I meet the following error in vscode:
This expression cannot be coerced to type
Yojson.Basic.t =
[ Assoc of (string * Yojson.Basic.t) list
| Bool of bool
| Float of float
| Int of int
| List of Yojson.Basic.t list
| Null
| String of string ];
it has type [< String of 'a ] -> 'b but is here used with type
[< Yojson.Basic.t ].
While compiling, I meet the following error.
Error: Syntax error: ')' expected.
If I change the code to List.map (fun ((String goal_feat): Basic.t) -> goal_feat) (goal_feats_json:> Basic.t list), which useq explicit type cast instead of subtyping, then the error disappeared. I can not understand what is the problem with my code when i use subtyping. Much appreciation to anyone who could give me some help.
First of all, most likely the answer that you're looking for is
let to_strings xs =
List.map (function `String x -> x | _ -> assert false) (xs :> t list)
The compiler is telling you that your function is handling only one case and you're passing it a list that may contain many other things, so there is a possibility for runtime error. So it is better to indicate to the compiler that you know that only the variants tagged with String are expected. This is what we did in the example above. Now our function has type [> Yojson.Basic.t].
Now back to your direct question. The syntax for coercion is (expr : typeexpr), however in the fun ((String goal_feat):> Basic.t) -> goal_feat snippet, String goal_feat is a pattern, and you cannot coerce a pattern, so we shall use parenthesized pattern here it to give it the right, more general, type1, e.g.,
let exp xs =
List.map (fun (`String x : t) -> x ) (xs :> t list)
This will tell the compiler that the parameter of your function shall belong to a wider type and immediately turn the error into warning 8,
Warning 8: this pattern-matching is not exhaustive.
Here is an example of a case that is not matched:
(`Bool _|`Null|`Assoc _|`List _|`Float _|`Int _)
which says what I was saying in the first part of the post. It is usually a bad idea to leave warning 8 unattended, so I would suggest you to use the first solution, or, otherwise, find a way to prove to the compiler that your list doesn't have any other variants, e.g., you can use List.filter_map for that:
let collect_strings : t list -> [`String of string] list = fun xs ->
List.filter_map (function
| `String s -> Some (`String s)
| _ -> None) xs
And a more natural solution would be to return untagged strings (unless you really need the to be tagged, e.g., when you need to pass this list to a function that is polymorphic over [> t] (Besides, I am using t for Yojson.Basic.t to make the post shorter, but you should use the right name in your code). So here is the solution that will extract strings and make everyone happy (it will throw away values with other tags),
let collect_strings : t list -> string list = fun xs ->
List.filter_map (function
| `String s -> Some s
| _ -> None) xs
Note, that there is no need for type annotations here, and we can easily remove them to get the most general polymoprhic type:
let collect_strings xs =
List.filter_map (function
| `String s -> Some s
| _ -> None) xs
It will get the type
[> `String a] list -> 'a list
which means, a list of polymorphic variants with any tags, returning a list of objects that were tagged with the String tag.
1)It is not a limitation that coercion doesn't work on patterns, moreover it wouldn't make any sense to coerce a pattern. The coercion takes an expression with an existing type and upcasts (weakens) it to a supertype. A function parameter is not an expression, so there is nothing here to coerce. You can just annotate it with the type, e.g., fun (x : #t) -> x will say that our function expects values of type [< t] which is less general than the unannotated type 'a. To summarize, coercion is needed when you have a function that accepts an value that have a object or polymorphic variant type, and in you would like at some expressions to use it with a weakened (upcasted type) for example
type a = [`A]
type b = [`B]
type t = [a | b]
let f : t -> unit = fun _ -> ()
let example : a -> unit = fun x -> f (x :> t)
Here we have type t with two subtypes a and b. Our function f is accepting the base type t, but example is specific to a. In order to be able to use f on an object of type a we need an explicit type coercion to weaken (we lose the type information here) its type to t. Notice that, we do not change the type of x per se, so the following example still type checks:
let rec example : a -> unit = fun x -> f (x :> t); example x
I.e., we weakened the type of the argument to f but the variable x is still having the stronger type a, so we can still use it as a value of type a.
I'm new to ML, but in other languages that use type inference, I have learned the habit of omitting the type of a thing whenever the inference on the right hand side is obvious to a human reader, and explicitly declaring the type of a thing whenever the inference is not obvious to a human. I like this convention, and would like to continue with it in my ML code.
I have the following example function declarations, that are equivalent:
fun hasFour [] = false
| hasFour (x::xs) = (x = 4) orelse hasFour xs
is equivalent to
val rec hasFour: int list -> bool =
fn [] => false
| (x::xs) => (x = 4) orelse hasFour xs
I like the latter form not only because it's easier for me to figure out what type the function is when I read it, but also because it explicitly declares the type of the function, and hence, if I screw something up in my implementation, there is no chance of accidentally declaring something that's syntactically valid but the wrong type, which is harder to debug later.
My question is: I want to use fun instead of val rec, because anonymous fn's can only take one parameter. So the latter technique is not syntactically valid for a function like int -> int -> bool. Is there a way to explicitly declare the type in fun? Or have I named all the preferred alternatives in this post, and should simply follow one of these patterns? The only way I could find to use fun with explicit type declaration was to add a type declaration to each parameter in the pattern, which is quite ugly and horrible, like so:
fun hasFour ([]:int list):bool = false
| hasFour (x::xs) = (x = 4) orelse hasFour xs
A colleague showed me some code following a pattern like this:
fun hasFour [] = false
| hasFour (x::xs) = (x = 4) orelse hasFour xs
val _ = op hasFour: int list -> bool
By declaring an unnamed variable and setting it to an instance of the function with a forced type, we effectively achieve the desired result, but the val _ must appear below the fully defined function, where it's less obvious to a human reader, unless you simply get used to this pattern and learn to expect it.
I asked a very similar question, Can I annotate the complete type of a fun declaration?, recently.
Your current solution would have been a nice answer to that.
You can have multiple curried arguments with multiple fn, e.g. like:
val rec member : ''a -> ''a list -> bool =
fn x => fn [] => false
| y::ys => x = y orelse member x ys
Or you can do as you currently do, or as matt suggests:
local
fun member _ [] = false
| member x (y::ys) = x = y orelse member x ys
in
val member = member : ''a -> ''a list -> bool
end
But the combination of using fun and having the complete type signature listed first is yet elusive.
For production-like code, the norm is to collect type signatures in a module signature. See ML for the Working Programmer, ch. 7: Signatures and abstraction, pp. 267-268. Although I imagine you'd want to use Ocaml then.
Some functions in the List module fail when the argument is an empty list. List.rev is an example. The problem is the dreaded Value Restriction.
I met the same problem while trying to define a function that returns a list with all but the last element of a list:
let takeAllButLast (xs: 'a list) =
xs |> List.take (xs.Length - 1)
The function works well with nonempty lists, but a version that would handle empty lists fails:
let takeAllButLast (xs: 'a list) =
if List.isEmpty xs then []
else xs |> List.take (xs.Length - 1)
takeAllButLast []
error FS0030: Value restriction. The value 'it' has been inferred to have generic type
val it : '_a list, etc.
I tried several things: making it an inline function, not specifying a type for the argument, specifying a type for the returned value, making the function depend on a type argument, and using the Option type to obtain an intermediate result later converted to list<'a>. Nothing worked.
For example, this function has the same problem:
let takeAllButLast<'a> (xs: 'a list) =
let empty : 'a list = []
if List.isEmpty xs then empty
else xs |> List.take (xs.Length - 1)
A similar question was asked before in SO: F# value restriction in empty list but the only answer also fails when the argument is an empty list.
Is there a way to write a function that handles both empty and nonempty lists?
Note: The question is not specific to a function that returns all but the last element of a list.
The function itself is completely fine. The function does not "fail".
You do not need to modify the body of the function. It is correct.
The problem is only with the way you're trying to call the function: takeAllButLast []. Here, the compiler doesn't know what type the result should have. Should it be string list? Or should it be int list? Maybe bool list? No way for the compiler to know. So it complains.
In order to compile such call, you need to help the compiler out: just tell it what type you expect to get. This can be done either from context:
// The compiler gleans the result type from the type of receiving variable `l`
let l: int list = takeAllButLast []
// Here, the compiler gleans the type from what function `f` expects:
let f (l: int list) = printfn "The list: %A" l
f (takeAllButLast [])
Or you can declare the type of the call expression directly:
(takeAllButLast [] : int list)
Or you can declare the type of the function, and then call it:
(takeAllButLast : int list -> int list) []
You can also do this in two steps:
let takeAllButLast_Int : int list -> int list = takeAllButLast
takeAllButLast_Int []
In every case the principle is the same: the compiler needs to know from somewhere what type you expect here.
Alternatively, you can give it a name and make that name generic:
let x<'a> = takeAllButLast [] : 'a list
Such value can be accessed as if it was a regular value, but behind the scenes it is compiled as a parameterless generic function, which means that every access to it will result in execution of its body. This is how List.empty and similar "generic values" are implemented in the standard library.
But of course, if you try to evaluate such value in F# interactive, you'll face the very same gotcha again - the type must be known - and you'll have to work around it anyway:
> x // value restriction
> (x : int list) // works
I'm trying to write a function that checks whether a set (denoted by a list) is a subset of another.
I already wrote a helper function that gives me the intersection:
let rec intersect_helper a b =
match a, b with
| [], _ -> []
| _, [] -> []
| ah :: at, bh :: bt ->
if ah > bh then
intersect_helper a bt
else if ah < bh then
intersect_helper at b
else
ah :: intersect_helper at bt
I'm trying to use this inside of the subset function (if A is a subset of B, then A = A intersect B):
let subset a_ b_ =
let a = List.sort_uniq a_
and b = List.sort_uniq b_
in intersect_helper a b;;
Error: This expression has type 'a list -> 'a list but an expression was expected of type 'b list
What exactly is wrong here? I can use intersect_helper perfectly fine by itself, but calling it with lists here does not work. From what I know about 'a, it's just a placeholder for the first argument type. Shouldn't the lists also be of type 'a list?
I'm glad you could solve your own problem, but your code seems exceedingly intricate to me.
If I understood correctly, you want a function that tells whether a list is a subset of another list. Put another way, you want to know whether all elements of list a are present in list b.
Thus, the signature of your function should be
val subset : 'a list -> 'a list -> bool
The standard library comes with a variety of functions to manipulate lists.
let subset l1 l2 =
List.for_all (fun x -> List.mem x l2) l1
List.for_all checks that all elements in a list satisfy a given condition. List.mem checks whether a value is present in a list.
And there you have it. Let's check the results:
# subset [1;2;3] [4;2;3;5;1];;
- : bool = true
# subset [1;2;6] [4;2;3;5;1];;
- : bool = false
# subset [1;1;1] [1;1];; (* Doesn't work with duplicates, though. *)
- : bool = true
Remark: A tiny perk of using List.for_all is that it is a short-circuit operator. That means that it will stop whenever an item doesn't match, which results in better performance overall.
Also, since you specifically asked about sets, the standard library has a module for them. However, sets are a bit more complicated to use because they need you to create new modules using a functor.
module Int = struct
type t = int
let compare = Pervasives.compare
end
module IntSet = Set.Make(Int)
The extra overhead is worth it though, because now IntSet can use the whole Set interface, which includes the IntSet.subset function.
# IntSet.subset (IntSet.of_list [1;2;3]) (IntSet.subset [4;2;3;5;1]);;
- : bool = true
Instead of:
let a = List.sort_uniq a_
Should instead call:
let a = List.sort_uniq compare a_
Given a the .Net Type, say typeof<string>, at runtime how does one created the equivalent of string list = []?
My motivation is that when using FSharpValue.MakeRecord to create a record based on values parsed the values need to be passed as a obj[]. I've been up casting the arguments using box and that has worked except for with lists. The issue I've encountered is that a empty untyped list list can't be boxed and then unboxed. The specific error returned is:
System.InvalidCastException: Unable to cast object of type
'Microsoft.FSharp.Collections.FSharpList`1[System.Object]'
to type
'Microsoft.FSharp.Collections.FSharpList`1[System.String]'.
An empty typed list can be boxed and unboxed so I've tried to find a way to cast a list to a runtime type, e.g. a Type returned by typeof<> but with no luck.
type Test = {Names : string list}
// fails
let genericList = []
{Names = unbox (box genericList)}
//works
let typedList : string list = []
{Names = unbox (box typedList)}
//works
let genericNonEmptyList = ["Bill"]
{Names = unbox (box genericNonEmptyList)}
Using reflection you can get the List module and call the generic empty method:
open System
open System.Reflection
let emptyList (t:Type) =
Assembly.GetAssembly(typeof<_ list>)
.GetType("Microsoft.FSharp.Collections.ListModule")
.GetMethod("Empty", BindingFlags.Static ||| BindingFlags.Public)
.MakeGenericMethod(t)
.Invoke(null, [||])
use as follows:
let itemType = typeof<string>
let emptyStringList = emptyList(itemType) :?> string list
If you are calling that quite often, consider caching (reduces execution time by ~1/3):
let emptyList =
let empty =
Assembly.GetAssembly(typeof<_ list>)
.GetType("Microsoft.FSharp.Collections.ListModule")
.GetMethod("Empty", BindingFlags.Static ||| BindingFlags.Public)
fun (t:Type) -> empty.MakeGenericMethod(t).Invoke(null, [||])
Let me add one more alternative answer - although both of the existing methods work, they rely on understanding how F# represents lists. In the first case, you need to know there is Empty method and in the second case, you need to know there is a union case called Empty.
I generally prefer to do this by defining a helper type and using reflection over my custom type:
type ListHelper =
static member Empty<'T>() : list<'T> = []
let makeEmpty =
let empty = typeof<ListHelper>.GetMethod("Empty")
let emptyArr : obj[] = [| |]
fun ty -> empty.MakeGenericMethod([| ty |]).Invoke(null, emptyArr)
This gives you quite simple function that can cache the MethodInfo (you could even use Expression to pre-compile and cache the invocations) and does not rely on clever tricks.
#CaringDev's answer using .NET reflection is fine, but you can also use the F#-specific reflection module to create instances of union cases:
let empty ty =
let uc =
Reflection.FSharpType.GetUnionCases(typedefof<_ list>.MakeGenericType [|ty|])
|> Seq.filter (fun uc -> uc.Name = "Empty")
|> Seq.exactlyOne
Reflection.FSharpValue.MakeUnion(uc, [||])
I'm way late to the party, but I was trying to do the same thing. I found a way to do this with pattern matching:
let emptyListFromExample e =
match [e] with
| [] -> []
| x::xs -> xs
This will give you an empty list of any type, provided you can construct a value of that type to start it off.
How about using Seq.cast to cast the empty generic list?
type Test = {Names : string list}
let genericList = []
let test = {Names = unbox (box genericList ) |> Seq.cast<string> |> Seq.toList}
test.Names //val it : string list = []