Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What kind of type is this?

Tags:

f#

suave

I just started working through Tamizhvendan S' book F# Applied and came across this code snippet:

type WebPart = Context -> Async<Context option>

I've been looking through Microsoft's F# docs as well as my favorite F# site, Scott Wlaschin's F# for Fun and Profit but have been unable to find any reference to this kind of type. It doesn't seem like a record type. It almost seems like a plain old function. So what is it?

Thanks for any help gals and guys.

like image 315
Kurt Mueller Avatar asked Aug 14 '16 01:08

Kurt Mueller


People also ask

What kind or what type?

One of the main differences between kind and type is that type is used to show sub-division or category while kind is used in the sense of 'sort'.

What is the example of kinds?

In formal English, “kind of” means “one category of,” or “one example of” a given category. It's the most formal, “correct” use of the expression. For example, you can say: The bald eagle is a kind of bird.

What is the difference between the words type and kind?

The word 'Type' is used to show the category or sub-division within a bigger or complete thing. The use of preposition 'of' after the word 'kind' tells us that the sentence have complete meaning. Type refers to clearly distinct and essential traits shared by members of a group. Its root meaning is “impression”.

What kind of word is kinds?

Kind is a countable noun. After words like all and many, you use kinds, not 'kind'.


2 Answers

The WebPart type you're looking at comes from Suave. You can read more about it at https://suave.io/async.html, but I'll summarize. You're correct: it is a type for a function. Specifically, it is a function that takes a Context (which in Suave is a combination of an HTTP request record and a response record), and returns a Context option asynchronously. That is, since some requests can fail, the function has the option of returning None instead of a value. And since some requests can take a long time, all requests are treated as returning asynchronously, hence the Async.

To chain two requests together, Suave provides the binding operator >=> so that you don't have to go through the boilerplate of typing async { match result1 with None -> None | Some x -> return! request2 x all the time; instead, you can just type request1 >=> request2 for the same effect.

If you need more help and reading through the Suave documentation hasn't provided the help you need, let us know and we'll try to explain further.

like image 85
rmunn Avatar answered Sep 18 '22 19:09

rmunn


This is too long to be a comment. And I have never used Suave. However, I think that I can guess from the monadic properties of the types involved that >=> is not a bind operator, but the Kleisli compositional operator. Without observing monad laws or trying to understand category theory, just by presuming the signatures to be:

val ( >>= ) :
  Async<'a option> -> ('a -> Async<'b option>) -> Async<'b option>
val ( >=> ) :
  ('a -> Async<'b option>) -> ('b -> Async<'c option>) -> 'a -> Async<'c option>

The bind operator takes an Async<'a option> and transforms it by way of a binder function 'a -> Async<'b option> into an Async<'b option>. The Kleisli operator composes two binder functions into a third.

These operators are working on monadic types and functions that are more generic than the concrete types described by a type abbreviation like type WebPart = Context -> Async<Context option>. An implementation of the operators follows almost naturally.

let (>>=) request1 binder = async { 
    let! result1 = request1
    match result1 with 
    | None -> return None 
    | Some x -> return! binder x }

let (>=>) f g a = f a >>= g
like image 24
kaefer Avatar answered Sep 20 '22 19:09

kaefer