(I'm still banging on with units of measure in F#)
I'm having a problem making 'generic' functions which take 'typed' floats.
The following mockup class is intended to keep tabs on a cumulative error in position, based on a factor 'c'. The compiler doesn't like me saying 0.<'a> in the body of the type ("Unexpected type parameter in unit-of-measure literal").
///Corrects cumulative error in position based on s and c
type Corrector(s_init:float<'a>) =
let deltaS ds c = sin (ds / c) //incremental error function
//mutable values
let mutable nominal_s = s_init
let mutable error_s = 0.<'a> //<-- COMPILER NO LIKE
///Set new start pos and reset error to zero
member sc.Reset(s) =
nominal_s <- s
error_s <- 0.<'a> //<-- COMPILER NO LIKE
///Pass in new pos and c to corrector, returns corrected s and current error
member sc.Next(s:float<'a>, c:float<'a>) =
let ds = s - nominal_s //distance since last request
nominal_s <- s //update nominal s
error_s <- error_s + (deltaS ds c) //calculate cumulative error
(nominal_s + error_s, error_s) //pass back tuple
Another related question, I believe, still to do with 'generic' functions.
In the following code, what I am trying to do is make a function which will take a #seq of any type of floats and apply it to a function which only accepts 'vanilla' floats. The third line gives a 'Value Restriction' error, and I can't see any way out. (Removing the # solves the problem, but I'd like to avoid having to write the same thing for lists, seqs, arrays etc.)
[<Measure>] type km //define a unit of measure
let someFloatFn x = x + 1.2 //this is a function which takes 'vanilla' floats
let MapSeqToNonUnitFunction (x:#seq<float<'a>>) = Seq.map (float >> someFloatFn) x
let testList = [ 1 .. 4 ] |> List.map float |> List.map ((*) 1.0<km>)
MapSeqToNonUnitFunction testList
You can change the first 'compiler no like' to
let mutable error_s : float<'a> = 0.0<_>
and the compiler seems to like that.
As for the second question, I am not seeing the same error as you, and this
[<Measure>] type km
//define a unit of measure
let someFloatFn x = x + 1.2 //this is a function which takes 'vanilla' floats
let MapSeqToNonUnitFunction (x:seq<float<_>>) = Seq.map (float >> someFloatFn) x
let testList = [ 1 .. 4 ] |> List.map float |> List.map ((*) 1.0<km>)
let testList2 = testList :> seq<_>
let result = MapSeqToNonUnitFunction testList2
printfn "%A" result
compiles for me (though the upcast to seq<_> is a little annoying, I am not sure if there is an easy way to get rid of it or not).
Aside, I think convention is to name units parameters 'u, 'v, ... rather than 'a, 'b, ...
Units of measure cannot be used as type parameters. This is because the are erased by the compiler during compilation. This question is quite similar:
F# Units of measure - 'lifting' values to float<something>
Related
In F#, I receive an error when I write the following code:
let records = {1..100}
let middleElement= records |> Seq.length / 2
The type 'int' does not match the type ''a -> int'
I know this error is basic.
But I'm new to F# and really don't know what I need to do to resolve this error.
You need to add parentheses:
let middleElement = (records |> Seq.length) / 2
In your version, the compiler reads your code as
let middleElement = records |> (Seq.length / 2)
... and it gets confused, because it thinks you are trying to divide the length function by 2!
I endorse Tomas Petricek's answer, but you could also write it this way. :-)
let records = {1..100}
let middleElement = records |> Seq.length |> (/) <| 2
This says, take the records, feed them to the length function. Feed the result as the first argument to the divide (/) function. This will result in a function which takes an int and returns an int (int -> int). We then feed 2 to that function to get 50.
[Edit] I just realized that this might be even clearer
let records = {1..100}
let middleElement = let length = records |> Seq.length in length / 2;;
I want to generalize my standard deviation function to allow for calculations of multiples of deviations, but still use it in the context of piping. It appears that I am setting up my function incorrectly.
let variance (x:seq<float>) =
let mean = x |> Seq.average
x |> Seq.map(fun x -> (x - mean) ** 2.0)
|> Seq.average
let stdDeviation (deviations:float, x:seq<float>) =
sqrt (x |> variance) * deviations
Example usage would be
let sTester = seq{1.0 .. 20.0}
let stdDev = sTester |> stdDeviation 1.0
I keep getting the error: The expression was expecting to have the type: seq -> a' but here has type float
Help is greatly appreciated.
Thanks,
~David
If you change your stdDeviation so that it takes two parameters, rather than a tuple then it works:
let stdDeviation (deviations:float) (x:seq<float>) =
sqrt (x |> variance) * deviations
let stdDev = sTester |> stdDeviation 1.0
The idea is that when you write let stdDeviation (deviations, x:seq<float>) then you are defining a function that takes a single parameter that is actually a tuple.
The way the |> operator works is that it supplies one parameter to the function on the right. So if you have just one parameter (which is a tuple), then the pipe isn't all that useful.
But if you say let stdDeviation deviations (x:seq<float>) then you are defining a function with two parameters. When you write input |> stdDeviations 1.0 you are then providing the first parameter on the right hand side and the input (second parameter) on the left via the pipe.
I'm new on F#, and can't see how extract the int value from:
let autoInc = FsCheck.Gen.choose(1,999)
The compiler say the type is Gen<int>, but can't get the int from it!. I need to convert it to decimal, and both types are not compatible.
From a consumer's point of view, you can use the Gen.sample combinator which, given a generator (e.g. Gen.choose), gives you back some example values.
The signature of Gen.sample is:
val sample : size:int -> n:int -> gn:Gen<'a> -> 'a list
(* `size` is the size of generated test data
`n` is the number of samples to be returned
`gn` is the generator (e.g. `Gen.choose` in this case) *)
You can ignore size because Gen.choose ignores it, as its distribution is uniform, and do something like:
let result = Gen.choose(1,999) |> Gen.sample 0 1 |> Seq.exactlyOne |> decimal
(* 0 is the `size` (gets ignored by Gen.choose)
1 is the number of samples to be returned *)
The result should be a value in the closed interval [1, 999], e.g. 897.
Hi to add to what Nikos already told you, this is how you can get an decimal between 1 and 999:
#r "FsCheck.dll"
open FsCheck
let decimalBetween1and999 : Gen<decimal> =
Arb.generate |> Gen.suchThat (fun d -> d >= 1.0m && d <= 999.0m)
let sample () =
decimalBetween1and999
|> Gen.sample 0 1
|> List.head
you can now just use sample () to get a random decimal back.
In case you just want integers between 1 and 999 but have those converted to decimal you can just do:
let decimalIntBetween1and999 : Gen<decimal> =
Gen.choose (1,999)
|> Gen.map decimal
let sampleInt () =
decimalIntBetween1and999
|> Gen.sample 0 1
|> List.head
what you probably really want to do instead
Is use this to write you some nice types and check properties like this (here using Xunit as a test-framework and the FsCheck.Xunit package:
open FsCheck
open FsCheck.Xunit
type DecTo999 = DecTo999 of decimal
type Generators =
static member DecTo999 =
{ new Arbitrary<DecTo999>() with
override __.Generator =
Arb.generate
|> Gen.suchThat (fun d -> d >= 1.0m && d <= 999.0m)
|> Gen.map DecTo999
}
[<Arbitrary(typeof<Generators>)>]
module Tests =
type Marker = class end
[<Property>]
let ``example property`` (DecTo999 d) =
d > 1.0m
Gen<'a> is a type that essentially abstracts a function int -> 'a (the actual type is a bit more complex, but let's ignore for now). This function is pure, i.e. when given the same int, you'll get the same instance of 'a back every time. The idea is that FsCheck generates a bunch of random ints, feeds them to the Gen function, out come random instances of the type 'a you're interested in, and feeds those to a test.
So you can't really get out the int. You have in your hands a function that given an int, generates another int.
Gen.sample as described in another answer essentially just feeds a sequence of random ints to the function and applies it to each, returning the results.
The fact that this function is pure is important because it guarantees reproducibility: if FsCheck finds a value for which a test fails, you can record the original int that was fed into the Gen function - rerunning the test with that seed is guaranteed to generate the same values, i.e. reproduce the bug.
I have the following program:
open System
open MathNet.Numerics
open MathNet.Numerics.LinearAlgebra
//entropy
let entropy v =
let pct = v / v.Sum()
let l1 = pct.Map (fun x -> System.Math.Log(x, 2.0))
let p = Vector.map2 (fun x y -> x * y) pct l1
let e = - p.Sum()
e
[<EntryPoint>]
let main argv =
let v1 = vector [ 1.0 ; 3.0 ; 5.0 ]
let e1 = entropy v1
0 // return an integer exit code
I need to provide a type annotation for the varable v in the entropy function. As you can see, the parameter I am passing to the function (v1) is defined as MathNet.Numerics.LinearAlgebra.vector. I have tried lots of options for the type annotation without success.
What should it be? Bonus points if you can help me understand how you came up with your answer.
This vector type is generic; the generic argument indicates the type of each component of the vector. A type annotation must at least indicate the number of generic arguments, e.g. Vector<_> for any such vector, or Vector<float> for the exact type used in the question.
In other words, Vector<_> and Vector are unrelated types to the compiler. The annotation is supposed to denote the type Vector with one, not zero generic arguments.
I would expect the "rough" annotation (v : Vector<_>) to suffice; the compiler would then infer the generic argument from the use of a float -- the value 2.0 -- later in the function. I don't use the library though, so I didn't test this.
I suspect that I am missing something very obvious here but this doesn't work:
let t = Array2D.create 1 1 1.0
for x in t do printfn "%f" x;;
It fails with
error FS0001: The type 'obj' is not compatible with any of the types float,float32,decimal, arising from the use of a printf-style format string
Interestingly using printf "%A" or "%O" prints the expected values which suggests to me that the problem is with the type inference
The corresponding code for a 1D array works fine
let t = Array.create 1 1.0
for x in t do printfn "%f" x;;
For reference this is on version 2.0 (both interactive and compiler) running on the latest mono
In .NET, a 1D array implicitly implements IList, which means it also implements (by inheritance) IEnumerable<T>. So, when you run:
let t = Array.create 1 1.0
for x in t do printfn "%f" x;;
the F# compiler emits code which gets an implementation of IEnumerable<T> (seq<T> in F#) from t, then iterates over it. Since it's able to get an IEnumerable<T> from the array, x will have type T.
On the other hand, multi-dimensional arrays (2d, 3d, etc.) only implement IEnumerable (not IEnumerable<T>) so the F# compiler infers the type of x as System.Object (or obj, in F#).
There are two solutions for what you want:
Cast each individual value within the loop, before printing it:
for x in t do printfn "%f" (x :?> float);;
Or, use Seq.cast to create and iterate over a strongly-typed enumerator:
for x in (Seq.cast<float> t) do printfn "%f" x;;
As Jack pointed out, this is a problem. One easy solution is:
let t = Array2D.create 2 2 1.0
t |> Array2D.iter (printfn "%f");;
And if you really like the for .. in .. do syntax:
type Array2DForLoopBuilder() =
member __.Zero() = ()
member __.For(a, f) = Array2D.iter f a
member __.Run e = e
let a2dfor = Array2DForLoopBuilder()
let t = Array2D.init 2 2 (fun a b -> float a + float b)
a2dfor { for x in t do printfn "%f" x }