Skip to content

Section 5.4: Wrong simplification for checkCompany #265

@h-bex

Description

@h-bex

Please quote the text that is incorrect:

One more simplification is possible. For every Applicative, pure f <*> E is equivalent to f <$> E. In other words, using seq to apply a function that was placed into the Applicative type using pure is overkill, and the function could have just been applied using Functor.map. This simplification yields:
def checkCompany (input : RawInput) :
Validate (Field × String) LegacyCheckedInput :=
checkThat (input.birthYear == "FIRM")
"birth year" "FIRM if a company" *>
.company <$> checkName input.name

In what way is this incorrect?

This simplification is technically incorrect, because:

The second definition of checkCompany yields to the following expansion:

Seq.seq
  (SeqRight.seqRight (checkThat (BEq.beq myInput.birthYear "FIRM") "birth year" "FIRM is a company") fun x ↦
    pure LegacyCheckedInput.company)
  fun x ↦ checkName myInput.name : Validate (Prod Field String) LegacyCheckedInput

and the third one using map yields to:

SeqRight.seqRight (checkThat (BEq.beq myInput.birthYear "FIRM") "birth year" "FIRM is a company") fun x ↦
  Functor.map LegacyCheckedInput.company (checkName myInput.name) : Validate (Prod Field String) LegacyCheckedInput

Proposed Correction

Add necessary parenthesis to the second checkCompany implementation:

def checkCompany (input : RawInput) : Validate (Field × String) LegacyCheckedInput :=
    checkThat (input.birthYear == "FIRM") "birth year" "FIRM is a company" *>
    (pure .company <*> checkName input.name)

this yields to the following expansion:

SeqRight.seqRight (checkThat (BEq.beq myInput.birthYear "FIRM") "birth year" "FIRM is a company") fun x ↦
  Seq.seq (pure LegacyCheckedInput.company) fun x ↦
    checkName myInput.name : Validate (Prod Field String) LegacyCheckedInput

which is consistent tho the third implementation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions