Monday, March 26, 2018

Re: Magical F#


​[sending slightly improved version to blog]

open System
open System.Collections.Generic
type Stats = Map<string, obj>

// Lens code based on http://www.fssnip.net/7Pk/title/Polymorphic-lenses by Vesa Karvonen

type Lens<'s,'t,'a,'b> = ('a -> Option<'b>) -> 's -> Option<'t>
module Lens =
let view l s =
let r = ref Unchecked.defaultof<_>
s |> l (fun a -> r := a; None) |> ignore
!r

let over l f =
l (f >> Some) >> function Some t -> t | _ -> failwith "Impossible"
let set l b = over l <| fun _ -> b
let lens get set = fun f s ->
(get s |> f : Option<_>) |> Option.map (fun f -> set f s)

module Props =
let prop<'t> (propName: string) f =
Lens.lens
(fun x -> match Map.tryFind propName x with | Some(:? 't as v:obj) -> (v |> unbox<'t>) | _ -> Unchecked.defaultof<'t>)
(fun v x -> Map.add propName v x)
f
let set prop v stats = stats |> Lens.set prop (box v)
let get prop stats = stats |> Lens.view prop

open Props
let name = prop<string> "Name"
let id = prop<string> "ID"
let iq = prop<int> "IQ"
let me = Map.empty |> set name "Max" |> set id "30777" |> set iq 4
printfn "%s (%s) is %s" (get name me) (get id me) (if get iq me > 100 then "smart" else "not smart")
// prints "Max (30777) is not smart"
// Did you catch that? iq returns an int, not a boxed int, and name statically knows that it's returning a string! Magic!


Magical F#

Check this out!


open System
open System.Collections.Generic
type Stats = Map<string, obj>

// Lens code based on http://www.fssnip.net/7Pk/title/Polymorphic-lenses by Vesa Karvonen

type Lens<'s,'t,'a,'b> = ('a -> Option<'b>) -> 's -> Option<'t>
module Lens =
let view l s =
let r = ref Unchecked.defaultof<_>
s |> l (fun a -> r := a; None) |> ignore
!r

let over l f =
l (f >> Some) >> function Some t -> t | _ -> failwith "Impossible"
let set l b = over l <| fun _ -> b
let lens get set = fun f s ->
(get s |> f : Option<_>) |> Option.map (fun f -> set f s)

module Props =
let prop<'t> (propName: string) f =
Lens.lens
(fun x -> match Map.tryFind propName x with | Some(v) -> (if (box v :? 't) then (v |> unbox<'t>) else Unchecked.defaultof<'t>) | _ -> Unchecked.defaultof<'t>)
(fun v x -> Map.add propName v x)
f
let set prop v stats = stats |> Lens.set prop (box v)
let get prop stats = stats |> Lens.view prop

open Props
let name = prop<string> "Name"
let id = prop<string> "ID"
let iq = prop<int> "IQ"
let me = Map.empty |> set name "Max" |> set id "30777" |> set iq 4
printfn "%s (%s) is %s" (get name me) (get id me) (if get iq me > 100 then "smart" else "not smart")
// prints "Max (30777) is not smart"
// Did you catch that? iq returns an int, not a boxed int, and name statically knows that it's returning a string! Magic!

~B.C.

--
If I esteem mankind to be in error, shall I bear them down? No. I will lift them up, and in their own way too, if I cannot persuade them my way is better; and I will not seek to compel any man to believe as I do, only by the force of reasoning, for truth will cut its own way.

"Thou shalt love thy wife with all thy heart, and shalt cleave unto her and none else."

Tuesday, March 20, 2018

Diversity hiring initiatives

I'm in favor of diversity hiring initiatives as long as they are done carefully, and the reason why is "to fight confirmation bias."

I read a study a few years ago about an orchestra in New York that started doing blind auditions. I don't remember the exact numbers, but my takeaway was twofold: (1) even with blind auditions, there was still a gap between the probability of female and male candidates actually making the cut; (2) the gap was significantly smaller with blind auditions. What that says to me is that even when there is a real difference in ability between two subpopulations, hiring managers can be overbiased by their past experience, more than the difference in actual performance justifies.

Sure, maybe women are better nurses than men, but not necessarily THAT much better, that often.

So the fact that Microsoft is taking modest steps to increase the number and promotion rates of under-represented demographics including women is, to my eye, a reasonable step to take. It would be unreasonable to ever expect all demographics to have equal representation in every profession, but it's reasonable to think that confirmation bias may play some role in hiring and promotion decisions at Microsoft, and to be deliberately looking for opportunities to let under-represented groups achieve great things.

That's my opinion.

~B.C.

--
If I esteem mankind to be in error, shall I bear them down? No. I will lift them up, and in their own way too, if I cannot persuade them my way is better; and I will not seek to compel any man to believe as I do, only by the force of reasoning, for truth will cut its own way.

"Thou shalt love thy wife with all thy heart, and shalt cleave unto her and none else."

Sunday, March 11, 2018

On D&D Alignments

[From a discussion on Lawful Evil vs. Chaotic Neutral, and whether Chaotic Neutral implies foresight, and in response to the specific claim that highly disciplined people "get so wrapped up in the right way that they fail to see possibilities"]

Lawful != organized.

An interesting illustration is Captain America: Civil War. Tony signs the Accords because he figures, hey, I can always break them if I need to (and he does), and besides, this is what I need to do RIGHT NOW to keep what I want. IMO that's a Chaotic (Good) attitude Tony is displaying. Steve Rogers is very reluctant to sign them because he assumes he will be bound by them if he signs them (the idea of "hey, I could always just break them" doesn't seem to ever cross his mind) and worries about the possibility of being someday given orders that he doesn't agree with. IMO that's a clearly Lawful attitude.

What makes this particularly interesting is that the character who has a clear ethical code is the one who winds up an outlaw, because of that code, and the one who doesn't retains his position of power (despite continuing to flout the law even in the last scene of the movie) because he has no qualms about saying one thing and doing another. Now I wouldn't claim that Tony Stark is not capable of foresight (clearly he can invent things that he doesn't yet need), but with respect to the moral dilemma, it is Steve Rogers who is worrying about precedents and structure and things that haven't happened yet, and Tony Stark who is basically winging it.

AFAIC that is Lawful vs. Chaotic in a nutshell. Lawful Good individuals think through the Trolley Problem and the Tragedy of the Commons in their spare time. Chaotic Good individuals shrug it off until it actually happens and then go by their gut feel.

-Max

--
If I esteem mankind to be in error, shall I bear them down? No. I will lift them up, and in their own way too, if I cannot persuade them my way is better; and I will not seek to compel any man to believe as I do, only by the force of reasoning, for truth will cut its own way.

"Thou shalt love thy wife with all thy heart, and shalt cleave unto her and none else."