Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do option types collapse?

Tags:

optional

f#

Here is an option type in F#:

type Option<'a> =
   | Some of 'a
   | None

Suppose I have an Option of an Option:

type Option2<'a> =
   | O of Option<'a>
   | None

Does it get collapsed down to just one Option?

like image 984
sdgfsdh Avatar asked Dec 13 '16 14:12

sdgfsdh


People also ask

Do options lose value over time?

As the time to expiration approaches, the chances of a large enough swing in the underlying's price to bring the contract in-the-money diminishes, along with the premium. This is known as time-decay, whereby all else equal, an option's price will decline over time.

Do options lose value every day?

With each day that passes, time decay will cause the value of an option to decrease. The dropping of the option's value will typically accelerate as the expiration date draws nearer. The daily decrease in the option's price will be higher the week before expiration than the month before expiration.

Why am I losing so much on options?

Traders lose money because they try to hold the option too close to expiry. Normally, you will find that the loss of time value becomes very rapid when the date of expiry is approaching. Hence if you are getting a good price, it is better to exit at a profit when there is still time value left in the option.

Why do options fluctuate so much?

Like most other financial assets, Options prices are influenced by current interest rates and are impacted by interest rate fluctuations. Interest rate changes have an inverse effect on call and put option premiums: calls benefit from rising rates, while puts lose value.


2 Answers

No, F# has a nominal type system. Structurally equivalent types are not compatible. So even if you had Option and Option2 exactly the same (except the name), these types would be different. Your case is different, similar to asking whether a list of ints and a list of lists of ints are the same.

let hasOptionType (_ : Option<_>) = ()
let hasOption2Type (_ : Option2<_>) = ()

let o = Option.None
let o2 = Option2.None

hasOptionType o
//hasOption2Type o // does not compile
//hasOptionType o2 // does not compile
hasOption2Type o2

You can have type aliases though that work both ways:

type IntOption = Option<int>

let isOptionOfInt (_ : Option<int>) = ()
let isIntOption (_ : IntOption) = ()

let i = IntOption.None
let i2 = Option<int>.None

isOptionOfInt i
isOptionOfInt i2
isIntOption i
isIntOption i2
like image 118
CaringDev Avatar answered Oct 06 '22 19:10

CaringDev


Your question is a little confusing because you talk about an Option of an Option, but then show a type which is your own Option2 type containing an Option.

I'm going to assume your question is really this: Does Some (Some x) collapse down to Some x?

The answer to that is no. That collapsing would implicitly change the type and you would lose some of the type safety that Option provides. And the distinction between the collapsed and non-collapsed versions may be important. Take this example.

match List.tryHead [Some 1; None; Some 2] with
| Some (Some x) -> sprintf "The first item exists with a value of %i" x
| Some None     -> "The first item exists but it has no value"
| None          -> "The list was empty"

The List.tryHead function returns the first element of the list, or None if the list is empty. We are passing it a list of Option<int> so it returns an Option<Option<int>>

We are able to match on the return values to cover all the different possible cases of that return type. This may be useful if you want to handle those cases differently.

But we still have the ability to treat Some None and None as equivalent:

match List.tryHead [Some 1; None; Some 2] with
| Some (Some x) -> sprintf "The first item exists with a value of %i" x
| _             -> "No value found"
like image 33
TheQuickBrownFox Avatar answered Oct 06 '22 21:10

TheQuickBrownFox