I just don't understand what the compiler is trying to tell me here:
let someCard = getFirstCard hand
Error:
This expression was expected to have type
Card * Card
but here has type
(Card * Card) option
I then tried this:
let getFirstCard (hand:Card*Card) = function
| card1, card2 -> Some(card1)
But I get the same error.
I just don't get the Options template.
I continue to struggle with this.
My code is below:
type Suit = | Spades| Clubs | Diamonds | Hearts
type Face = | Two | Three | Four | Five
| Six | Seven | Eight | Nine | Ten
| Jack | Queen | King | Ace
type Card = {Face:Face; Suit:Suit}
type Deal = | Hand of Card * Card
| Hit of Card
let private suits = [Spades; Clubs; Diamonds ; Hearts]
let private faces = [Two; Three; Four; Five; Six; Seven; Eight; Nine; Ten;
Jack; Queen; King; Ace]
let deck = [for suit in suits do
for face in faces do
yield {Face=face; Suit=suit}]
let hitPlayer (deck:Card list) =
(deck.Head, deck.Tail)
let shuffle xs =
let swap i j (array : _[]) =
let tmp = array.[i]
array.[i] <- array.[j]
array.[j] <- tmp
let rnd = System.Random()
let xArray = Seq.toArray xs
let n = Array.length xArray
for i in [0..(n-2)] do
let j = rnd.Next(i, n-1)
swap i j xArray
xArray |> Seq.toList
let deal = function
| card1::card2::remaining -> Some(card1, card2), remaining
| _ -> None, [];;
let getFirstCard (hand:Card*Card) = function
| card1, card2 -> card1
let hand, deckRemaining = deal (shuffle deck);;
let someCard = getFirstCard hand
The getFirstCard function has the type Card * Card -> 'a * 'b -> 'a (which, BTW, looks strange, to say the least). It does, however, expect its first parameter to be of the type Card * Card.
hand, on the other hand (pun intended), has the type (Card * Card) option. This means that it may contain a Card * Card value, but it also may not. You'll need to tell your program how to deal with both cases.
As we've discussed before, the Option type is an 'elevated world', and the most appropriate strategy is to stay in that elevated world until you can deal with all cases. Here's one way to do it:
let someCard = hand |> Option.map getFirstCard
This still doesn't work because of the weird implementation of getFirstCard.
Looking at the implementation of getFirstCard, though, I get the impression that what you attempt to do is to get the first card out of a tuple of cards. This, however, can be written as:
let getFirstCard (card1, card2) = card1
That function is already built into F#, although there's nothing wrong with giving it a domain-specific name. In that case, though, I'd suggest aliasing it instead:
let getFirstCard = fst
Given one of the two above definitions of getFirstCard, the expression
let someCard = hand |> Option.map getFirstCard
gives you a someCard value of the type Card option. You'll notice that we've stayed in the 'elevated' Option world.
Specifically, though, someCard has this value:
> someCard;;
val it : Card option = Some {Face = King;
Suit = Clubs;}
You'll note that it isn't just King of Clubs, but Some King of Clubs.
Try this :
let getFirstCard (hand) =
match hand with
| Some(x) -> Some (fst x)
|_ -> None
let hand, deckRemaining = deal (shuffle deck);;
let someCard = getFirstCard hand
It compiles & runs for me.
Actually, function getFirstCard could be renamed as a generic fun on tuples :
let getFirstOfTupleOption tup =
match tup with
| Some (x) -> Some (fst x)
| _ -> None
Alternatively, you may want explicit type restriction on your domain with :
let getFirstCard (hand:(Card*Card) option) =
match hand with
| Some(x) -> Some (fst x)
|_ -> None
Related
I have the following Discriminated Union (DU) declaration:
type Book =
| Dictionary of string[]
| Novel of int[]
| Comics of bool[]
An example:
let x = Dictionary [|"a"; "b"|]
How can I extract the length of the array inside without doing pattern matching and without caring about the data type of the array (in this case: string, int, bool). Note: I have no control over the DU declaration; as a result, I can't write new member method within Book, like getArrayLength()
Of course, we can do it in some way as followed:
match x with
| Dictionary (x: _[]) -> x |> Array.length
| Novel (x: _[]) -> x |> Array.length
| Comics (x: _[]) -> x |> Array.length
But typing x |> Array.length a lot is incovenient. This is a simple example, but we can think of a general problem:
type Animal =
| Dog of DogClass
| Cat of CatClass
| Cow of CowClass
...
... and DogClass, CatClass, etc. may share something. We want to get that shared thing. E.g. those classes inherit from AnimalClass, within which there is countLegs() method. Suppsed there are many animals, pattern matching for all of them while the code block after -> is almost the same. I love the principle DRY (Don't Repeat Yourself).
Is there any convenient way to tackle such problem?
==
EDITED 21.10.2019
I was also looking for some syntax like:
let numEles =
match x with
| _ (arr: _[]) -> x |> Array.Length
| _ -> failwith "No identifiers with fields as Array."
let numLegs =
match anAnimall with
| _ (animal: ?> Animal) -> animal.countLegs()
| _ -> failwith "Can't count legs because of not being an animal."
I think this still follows the spirit of matching, but seem like this approach is not supported.
Realistically, there's no getting around pattern matching here. DUs were, in a way, built for it. Since you don't control the type, you can always add a type extension:
type Book with
member this.Length =
match this with
| Dictionary d -> d.Length
| Novel n -> n.Length
| Comics c -> c.Length
let x = Dictionary [|"a"; "b"|]
printfn "%d" x.Length // Prints 2
Though it's also equally valid to define a Book module with a length function on it if you prefer that:
module Book =
let length b =
match b with
| Dictionary d -> d.Length
| Novel n -> n.Length
| Comics c -> c.Length
let x = Dictionary [|"a"; "b"|]
printfn "%d" (x |> Book.length) // prints 2
But you'll need to write a pattern match expression on the Book type at least once. The fact that every case is made up of data that all has the same property doesn't really help the fact that you need to still identify every case individually.
How can I refactor such that scoring above a specific number of points is unrepresentable?
For example, how can I take the following code and make the compiler reject any shot that would exceed an overall score of 11 points?
let results = (player1, player2) |> makeFieldBasket TwoPointer
|> makeFoulShots ThreeFoulShots
|> makeFieldBasket TwoPointer
|> makeFoulShots TwoFoulShots
|> makeFieldBasket TwoPointer
The output for the above code is the following:
val results : FoulShooter * FieldShooter =
(FoulShooter {Score = 11;}, FieldShooter {Score = 0;})
Now I want to structure my code such that making an additional shot cannot be compiled.
For example, I want the compiler to reject an additional foul shot that would exceed 11 points:
let results = (player1, player2) |> makeFieldBasket TwoPointer
|> makeFoulShots ThreeFoulShots
|> makeFieldBasket TwoPointer
|> makeFoulShots TwoFoulShots
|> makeFieldBasket TwoPointer
|> makeFoulShots FoulShot
Currently, the code above is legal.
The entire code is below:
(*Types*)
type Player = { Score:int }
type FieldShot = TwoPointer| ThreePointer
type FoulShots = FoulShot | TwoFoulShots | ThreeFoulShots
type FoulShooter = FoulShooter of Player
type FieldShooter = FieldShooter of Player
(*Functions*)
let shoot lastShot player =
(player.Score + lastShot)
let fieldShot (fieldShooter, shot) =
let player = match fieldShooter with
| FieldShooter player -> player
match player.Score with
| score when score >= 11 -> score
| _ -> match (fieldShooter, shot) with
| FieldShooter player, shot -> match shot with
| TwoPointer -> player |> shoot 2
| ThreePointer -> player |> shoot 3
let foulShot (foulShooter, shot) =
let player = match foulShooter with
| FoulShooter player -> player
match player.Score with
| score when score >= 11 -> score
| _ -> match (foulShooter, shot) with
| FoulShooter player, shot -> match shot with
| FoulShot -> player |> shoot 1
| TwoFoulShots -> player |> shoot 2
| ThreeFoulShots -> player |> shoot 3
let makeFoulShots foulShots (shooter, defender) =
FieldShooter { Score= foulShot (shooter, foulShots) }, defender
let makeFieldBasket fieldBasket (shooter, defender) =
FoulShooter { Score= fieldShot (shooter, fieldBasket) }, defender
let turnover (shooter, defender) = (defender, shooter)
(*Client*)
let player1, player2 = FieldShooter { Score=0 } ,
FieldShooter { Score=0 }
let results = (player1, player2) |> makeFieldBasket TwoPointer
|> makeFoulShots ThreeFoulShots
|> makeFieldBasket TwoPointer
|> makeFoulShots TwoFoulShots
|> makeFieldBasket TwoPointer
|> makeFoulShots FoulShot
The feature you are wishing for (and you're not the only one!) is called dependent typing (Wikipedia, and a quick introduction). Your particular example, more precisely, would be called a refinement type, because the dependence of type Score on the value n is expressed by a predicate, in this case n <= 11.
Supporting dependent types is no easy feat. It requires the compiler to run a full theorem prover program in order to properly check all possible execution routes in your code and ensure that no, this function with an 'a -> Integer<11> signature is never going to return an output greater than 11.
Dependent types are currently not implemented in mainstream programming languages such as F#, Haskell, Erlang, or Clojure. However, they are implemented in a few academic and/or research languages, usually in a mathematical context; the Wikipedia article above probably has the complete list.
If you need to do serious work with dependent types, Coq is the most mature and well established among them, while Agda probably comes next and is significantly more modern.
Otherwise, if you're just working on a personal project, you may want to check out F*, which is a dependently-typed language in active development that is based on, and compiles to, F#, and should be the easiest one for you to pick up.
Now, assuming that we'll be stuck with "only" F# for the current decade or so, the traditional solution to your problem is to store the possibly-invalid value in a discriminated union.
A simple DU with a custom + operator will prevent you from accidentally adding up to an invalid score:
type Score = InvalidScore | ValidScore of int<pts>
let (+) s1 s2 = match (s1, s2) with
| ValidScore a, ValidScore b when (a + b) <= 11<pts> -> ValidScore (a + b)
| _ -> InvalidScore
If you also want to prevent yourself from creating an invalid score in the first place, then we need a slightly more complex implementation based on accessor modifiers.
That is, we can put the whole thing into a module, make the two subclasses of the DU private to that module, and only expose safe methods / properties, like this:
[<AutoOpen>]
module Score =
type Score = private InvalidScore | ValidScore of int<pts> with
static member Create n =
if n > 11<pts> then InvalidScore else ValidScore n
member this.GetPoints =
match this with
| InvalidScore -> None
| ValidScore x -> Some x
let (+) s1 s2 =
match (s1, s2) with
| ValidScore a, ValidScore b when (a + b) <= 11<pts> -> ValidScore (a + b)
| _ -> InvalidScore
let x = ValidScore 12<pts> // won't compile
let y = Score.Create 12<pts> // compiles, but if you call y.GetPoints you get None
To the best of my knowledge, there is no way of achieving that at compile time. A custom numeric type can help you keep operations within the valid range, and represent (or fail on) overflow - but it will not help at compile time. (Ab)using units of measure will have the same problem. I think you would have to go to the level of running a theorem prover like Z3 on your code to establish that repeated addition of points will lead you outside the valid range.
I m trying to filter a mixed data type for a specific type, say float (ideally this would be dynamic though)
here my example:
let testobj = [8.0 , 1.0, "bla" ; 8.0 , 1.0, "bla"]
let testfun data = data |> List.filter (fun a ->
match a.GetType() with
| float -> a
| _ -> 0.0)
now this should return [8.0 , 1.0, 0.0 ; 8.0 , 1.0, 0.0] for testobj but I m gettting an error that the function is of type bool
This isn't what you want to do.
Seriously.
F# wants lists to be homogeneous and your list is not homogeneous. float and string don't share a common base class so you're not going to get a list from it.
What F# wants you to do is to use a discriminated union for this. So if you have this type:
type Composite =
| Num of float
| Str of string
you can define your list like this:
let data = [ Num(8.0); Num(1.0); Str("bla"); Num(8.0); Num(1.0); Str("bla") ]
and from there you can pattern match on the types and your function looks like this:
let testfun d = d |> List.map (fun a ->
match a with
| Num x -> a
| _ -> Num(0.0) )
data|> testfun |> printfn "%A"
And the output will be:
[Num 8.0; Num 1.0; Num 0.0; Num 8.0 ; Num 1.0 ; Num 0.0;]
If you want floats in the end and not Composites, do this:
let testfun1 d = d |> List.map (fun a ->
match a with
| Num x -> x
| _ -> 0.0 )
which sheds the composite type. And everything (and I mean everything) in that code is type strong and type-safe.
From a real-world maintenance point of view, I would eschew the _ case in the matches and instead use all my types, reasoning that if I extend Composite to include another type I would want the compiler to scream at me and look at each function that uses it rather than silently assuming that 0.0 or Num(0.0) is really what I wanted.
For example, if I added integers to that type, this would do exactly the wrong thing if I wanted to sum the contents of a list of composites.
Given that you're stuck/hell-bent on a weakly-typed data set, then you want something like this:
let testfun2 d = d |> Array.map (fun (a:Object) ->
match a with
| :? float as x -> x
| _ -> 0.0
)
let data:Object[] = [|8.0; 1.0; "bla"; 8.0; 1.0; "bla"|]
data |> testfun2 |> printfn "%A"
which will print what you expect. Note that I'm using proper Array syntax and not list syntax.
However this is feeling really wonky for F#. See how I have to adorn a and d with types? In my previous code, the language can figure it all out. If I don't adorn either, I get compiler errors because we're really going against the grain of the type system.
If I were you, I would be inclined to do something like this first:
let recast d = d |> Array.map (fun (a:Object) ->
match a with
| :? float as x -> Num x
| :? string as x -> Str x
| _ -> raise (ArgumentException("that was unexpected: " + a.GetType().Name))
)
which turns this into an Array of Composite which is now type strong. If you tack on |> Array.toList after the Array.map, you get a list (if you want that).
I have a discriminated union, such as
type Dish =
| Eggs
| Spam of Dish
This is basically a linked list, without any content, e.g. Spam(Spam(Spam(Eggs))). I want to strictly perform a computation on this structure, such as counting the length, and memorize the result. In a normal type, I'd use class-local let bindings, but those aren't available in discriminated unions.
One way to do this would be,
type Count = int
type Dish =
| Eggs
| Spam of Dish * Count
But this is really messy, when the data I need is easily computable, but I still hope there is a better way (without using external mutable constructs).
One option is making the union cases private to hide the cached length.
//the 'guts' of Dish -- entirely hidden
type private DishImpl =
| Eggs
| Spam of DishImpl
// Dish wrapper type -- implementation hidden
type Dish =
private
| Dish of DishImpl * int
with
// O(1), just get the 'length' field
member x.Length = let (Dish(_, len)) = x in len
static member Eggs() = Dish(Eggs, 1)
static member Spam(Dish(dish, len)) = Dish(Spam dish, len + 1)
let eggs = Dish.Eggs()
let spam = Dish.Spam(eggs)
printfn "%d" eggs.Length //outputs: 1
printfn "%d" spam.Length //outputs: 2
To do it up right, create an accompanying module with let-bound functions and active patterns for destructuring.
If you tolerate a bit internal mutable state, here is a memoize function which creates a dictionary per function:
let memoize f =
let dict = Dictionary()
fun n ->
match dict.TryGetValue(n) with
| (true, v) -> v
| _ ->
let res = f(n)
dict.Add(n, res)
res
// This function results in a warning though
let rec length = memoize (function Eggs -> 0 | Spam d -> 1 + length d)
The approach isn't that bad since the mutable dictionary is hidden.
A purely functional approach could be using Map to hold values and a kind of State computation expression to hide Map values passing around. Please refer to this snippet to see how a memoize computation expression looks like.
There is also Memo Functions, Polytypically! by Ralph Hinze (2000). Adapting to F#:
type Dish =
| Eggs
| Spam of Dish
type DishTable<'T> =
{
Eggs : Lazy<'T>
Spam : Lazy<DishTable<'T>>
}
let rec tabulate (f: Dish -> 'T) : DishTable<'T> =
{
Eggs = lazy f Eggs
Spam = lazy tabulate (f << Spam)
}
let rec lookup (table: DishTable<'T>) (dish: Dish) : 'T =
match dish with
| Eggs -> table.Eggs.Value
| Spam x -> lookup table.Spam.Value x
let memo (f: Dish -> 'T) : (Dish -> 'T) =
lookup (tabulate f)
let rec len x =
match x with
| Eggs -> 0
| Spam x -> 1 + len x
let l2 = memo len
This is what I came up with. It's not true memoization because it counts eagerly when you call mem, but might work for your needs.
type Dish =
| Eggs
| Spam of Dish
| Memo of Dish * int
with
member d.length =
match d with
| Eggs -> 1
| Spam d -> 1 + d.length
| Memo (d, i) -> i
member d.mem =
match d with
| Eggs -> Memo(d, d.length)
| Spam d2 -> Memo(d, d.length)
| Memo(d2, i) -> d // no need to memo it again
let x = Spam (Spam(Spam Eggs))
let m = x.mem
x.length // val it : int = 4
m.length // val it : int = 4
Note that in your case, literally the only interesting property of a value of your type is its length, so you might as well just use integers as your representation instead:
let Eggs = 0
let Spam n = 1 + n
let (|Eggs|Spam|) = function
| 0 -> Eggs
| n -> Spam(n-1)
let length = id
// example usage
let dish = Spam(Spam(Eggs))
let l = length dish
let kind =
match dish with
| Eggs -> "Eggs"
| Spam(Eggs) -> "One Spam"
| Spam(Spam _) -> "At least two Spams"
If your real question is how to do this for a more interesting type, then one approach would be to create mutually recursive types, one of which is annotated:
type 'a AnnotatedDish = { dish : 'a Dish; value : 'a }
and 'a Dish =
| Eggs
| Spam of 'a AnnotatedDish
// "smart" constructors, given that you want to annotate with length
let eggs = { dish = Eggs; value = 0 }
let spam d = { dish = Spam d; value = 1 + d.value }
let length { value = l } : int = l
// active patterns
let (|Eggs|Spam|) = function
| { dish = Eggs } -> Eggs
| { dish = Spam d } -> Spam d
// example usage
let dish = spam(spam(eggs))
let l = length dish
let kind =
match dish with
| Eggs -> "Eggs"
| Spam(Eggs) -> "One Spam"
| Spam(Spam _) -> "At least two Spams"
After reviewing the answers, I've decided to go with a model that seems the least obtrusive to me. I've used a modified object to demonstrate how it would work in a slightly more complex scenario.
type StackDef<'a>(v : 'a, s : Stack<'a>) =
member val Length = s.Length + 1
member val Inner = v, s
and Stack<'a> =
| Empty
| Stack of StackDef<'a>
member this.Length =
match this with
| Empty -> 0
| Stack(def) -> def.Length
let Stack (v, s) = Stack(StackDef(v, s))
let (|Stack|Empty|) = function | Empty -> Empty | Stack(sd) -> Stack(sd.Inner)
//...
let example = Stack(1, Stack(2, Stack(3, Empty))).Length
It doesn't contain any external mutable state.
The discriminated union Dish (or in the example, Stack) continues to exist.
The field length doesn't appear in the union definition at all, nor is it provided by any constructor, just as it should be.
The memoized data is associated with the instance, as it should be.
However, having thought about it, by using a static weaver such as Afterthought it might be possible to replace any method such as:
Stack<'a> =
| Empty
| Stack of 'a * Stack<'a>
[<Lazy>] //custom attribute that would work with a static weaver
member this.Length =
match this with
| Empty -> 0
| Stack(_, s) -> s.Length + 1
With a private readonly Lazy<int> __length initialized in the constructor with a delegate that executes the above code, and change the actual content of the method to simply invoking __length.Value.
While F# doesn't allow union types to contain fields, possibly for very valid reasons, I highly doubt the IL would have such restrictions.
In fact, it would be possible to do a lot of things using some IL manipulation. Maybe it's something to think about.
Say I have a list of shapes:
type shape =
| Circle of float
| Rectangle of float * float
let a = [ Circle 5.0; Rectangle (4.0, 6.0)]
How can I then test e.g. a Circle exists in a? I could create a function for each shape
let isCircle s =
match s with
| Circle -> true
| _ -> false
List.exists isCircle a
but I feel there must be a more elegant way in F#, other than having to define such a function for each shape type. Is there?
Related question is how to group a list of shapes, based on shape types:
a |> seq.groupBy( <shapetype? >)
If you're interested in the different categories of shapes, then it makes sense to define another type that exactly captures them:
type shapeCategory = Circular | Rectangular
let categorize = function
| Circle _ -> Circular
| Rectangle _ -> Rectangular
List.exists ((=) Circular) (List.map categorize a)
a |> Seq.groupBy(categorize)
Edit - as suggested by Brian, you can alternatively use active patterns instead of a new type. It works out pretty similarly for your examples, but would extend better to more complicated patterns, while the approach above may be better if you're code often works with the categories, and you want a nice union type for them instead of a Choice type.
let (|Circular|Rectangular|) = function
| Circle _ -> Circular
| Rectangle _ -> Rectangular
List.exists (function Circular -> true | _ -> false) a
let categorize : shape -> Choice<unit, unit> = (|Circular|Rectangular|)
a |> Seq.groupBy(categorize)
you can combine F# reflection with quotations to get generic solution
open Microsoft.FSharp.Quotations
open Microsoft.FSharp.Quotations.Patterns
type Shape =
| Circle of float
| Rectangle of float * float
let isUnionCase (c : Expr<_ -> 'T>) =
match c with
| Lambda (_, NewUnionCase(uci, _)) ->
let tagReader = Microsoft.FSharp.Reflection.FSharpValue.PreComputeUnionTagReader(uci.DeclaringType)
fun (v : 'T) -> (tagReader v) = uci.Tag
| _ -> failwith "Invalid expression"
let a =
[ Circle 5.0; Rectangle (4.0, 6.0)]
|> List.filter (isUnionCase <# Rectangle #>)
printf "%A" a
You can use the F# reflection library to get a value's tag:
let getTag (a:'a) =
let (uc,_) = Microsoft.FSharp.Reflection.FSharpValue.GetUnionFields(a, typeof<'a>)
uc.Name
a |> Seq.groupBy getTag
I want to add another solution that works with quotations for every union case, based on the one desco provided. Here it goes:
open Microsoft.FSharp.Quotations.Patterns
open Microsoft.FSharp.Reflection
let rec isUnionCase = function
| Lambda (_, expr) | Let (_, _, expr) -> isUnionCase expr
| NewTuple exprs ->
let iucs = List.map isUnionCase exprs
fun value -> List.exists ((|>) value) iucs
| NewUnionCase (uci, _) ->
let utr = FSharpValue.PreComputeUnionTagReader uci.DeclaringType
box >> utr >> (=) uci.Tag
| _ -> failwith "Expression is no union case."
Defined this way, isUnionCase works like desco has shown, but even on union cases that are empty or have more than one value. You can also enter a tuple of comma-separated union cases. Consider this:
type SomeType =
| SomeCase1
| SomeCase2 of int
| SomeCase3 of int * int
| SomeCase4 of int * int * int
| SomeCase5 of int * int * int * int
let list =
[
SomeCase1
SomeCase2 1
SomeCase3 (2, 3)
SomeCase4 (4, 5, 6)
SomeCase5 (7, 8, 9, 10)
]
list
|> List.filter (isUnionCase <# SomeCase4 #>)
|> printfn "Matching SomeCase4: %A"
list
|> List.filter (isUnionCase <# SomeCase3, SomeCase4 #>)
|> printfn "Matching SomeCase3 & SomeCase4: %A"
The first isUnionCase I provided only worked for single case checks. I later added the expression check for NewTuple and thought you might like it. Just make sure that if you alter the code the precomputations still work, this is why iucs is defined outside of the returned anonymous function.
A more elegant solution could be the following:
let shapeExistsInList shapeType list =
List.exists (fun e -> e.GetType() = shapeType) list
let circleExists = shapeExistsInList ((Circle 2.0).GetType()) a
However, I'm not very satisfied with this myself since you have to create an instance of the discriminated union for it to work.
Grouping by shape type could work in a similar fashion.