/hall/hpack/Conditional/maybe

Copy path to clipboard

Examples

m new C.GE mid "b" "a" ≡ E.Left "b"
m mid C.GE mid "b" "a" ≡ E.Left "b"
m old C.GE mid "b" "a" ≡ cond ">="
m new C.GT mid "b" "a" ≡ E.Left "b"
m mid C.GT mid "b" "a" ≡ cond ">"
m new C.LT mid "b" "a" ≡ E.Left "a"
m mid C.LT mid "b" "a" ≡ E.Left "a"
m old C.LT mid "b" "a" ≡ cond "<"
m new C.LE mid "b" "a" ≡ E.Left "a"
m mid C.LE mid "b" "a" ≡ cond "<="
m new C.EQ mid "b" "a" ≡ E.Left "a"
m mid C.EQ mid "b" "a" ≡ cond "=="

Source

{-| Conditionally generate a `Conditional`. If the `minimumGhcVersion` provided
makes one of the branches impossible, it will return the other branch.
Otherwise, it will return the conditional expression that should be added to
the Cabal file.

__TODO__: We should be able to partition a list of these, so the `Static` can be
folded into a single value while the `Cond` can be provided to `when`
in hpack-dhall.
-}
let Pvp = ../../PVP/package.dhall

let Conditional = ./Type

let Comparator = ./Comparator/package.dhall

let Either = λ(a : Type) → < Left : a | Right : Conditional a >

let maybe =
λ(a : Type) →
λ(minimumGhcVersion : Pvp.Version) →
λ(comparator : Comparator.Type) →
λ(ghcVersion : Pvp.Version) →
λ(body : a) →
λ(alt : a) →
let -- Whether every supported GHC is at least `ghcVersion`.
supportedVersionsAtLeast =
Pvp.lessThanEqual ghcVersion minimumGhcVersion

let -- Whether every supported GHC is newer than `ghcVersion`.
supportedVersionsAbove =
Pvp.lessThanEqual minimumGhcVersion ghcVersion == False

let compareUnless =
λ(settled : Bool) →
λ(single : a) →
if settled
then (Either a).Left single
else (Either a).Right
( ./ifGhcImpl
a
(Comparator.show comparator)
ghcVersion
body
alt
)

in merge
{ EQ = compareUnless supportedVersionsAbove alt
, GE = compareUnless supportedVersionsAtLeast body
, GT = compareUnless supportedVersionsAbove body
, LT = compareUnless supportedVersionsAtLeast alt
, LE = compareUnless supportedVersionsAbove alt
}
comparator

let v = ../../PVP/v

let C = Comparator.Type

let E = Either Text

let old = v [ 8, 0 ]

let mid = v [ 9, 0 ]

let new = v [ 10, 0 ]

let m = maybe Text

let cond =
λ(op : Text) →
E.Right
{ condition = "impl(ghc ${op} 9.0.0)", `then` = "b", `else` = "a" }

let checkGeNewer = assert : m new C.GE mid "b" "a" ≡ E.Left "b"

let checkGeSame = assert : m mid C.GE mid "b" "a" ≡ E.Left "b"

let checkGeOlder = assert : m old C.GE mid "b" "a" ≡ cond ">="

let checkGtNewer = assert : m new C.GT mid "b" "a" ≡ E.Left "b"

let checkGtSame = assert : m mid C.GT mid "b" "a" ≡ cond ">"

let checkLtNewer = assert : m new C.LT mid "b" "a" ≡ E.Left "a"

let checkLtSame = assert : m mid C.LT mid "b" "a" ≡ E.Left "a"

let checkLtOlder = assert : m old C.LT mid "b" "a" ≡ cond "<"

let checkLeNewer = assert : m new C.LE mid "b" "a" ≡ E.Left "a"

let checkLeSame = assert : m mid C.LE mid "b" "a" ≡ cond "<="

let checkEqNewer = assert : m new C.EQ mid "b" "a" ≡ E.Left "a"

let checkEqSame = assert : m mid C.EQ mid "b" "a" ≡ cond "=="

in maybe