Skip to content

Ord semilattice example #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions examples/ord/Ord.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import * as TL from './Ord'
import * as fc from 'fast-check'

const trafficLightArb: fc.Arbitrary<TL.TrafficLight> = fc.oneof(
fc.constant(TL.red),
fc.constant(TL.yellow),
fc.constant(TL.yellowFlashing),
fc.constant(TL.green)
)

describe('properties', () => {
// the "zero" is always the lowest so does nothing
it('a + zero == a', () => {
fc.assert(
fc.property(
trafficLightArb,
(tl) =>
TL.trafficLightJoin.join(tl, TL.trafficLightJoin.zero) ===
tl
)
)
})

it('a + green == green', () => {
fc.assert(
fc.property(
trafficLightArb,
(tl) => TL.trafficLightJoin.join(tl, TL.green) === TL.green
)
)
})

it('a + b == b + a', () => {
fc.assert(
fc.property(
trafficLightArb,
trafficLightArb,
(tlA, tlB) =>
TL.trafficLightJoin.join(tlA, tlB) ===
TL.trafficLightJoin.join(tlB, tlA)
)
)
})

it('a + (b + c) == (a + b) + c', () => {
fc.assert(
fc.property(
trafficLightArb,
trafficLightArb,
trafficLightArb,
(tlA, tlB, tlC) =>
TL.trafficLightJoin.join(
tlA,
TL.trafficLightJoin.join(tlB, tlC)
) ===
TL.trafficLightJoin.join(
TL.trafficLightJoin.join(tlA, tlB),
tlC
)
)
)
})

it('a + a == a', () => {
fc.assert(
fc.property(
trafficLightArb,
(tlA) => TL.trafficLightJoin.join(tlA, tlA) === tlA
)
)
})

it('a + b + b == a + b', () => {
fc.assert(
fc.property(
trafficLightArb,
trafficLightArb,
(tlA, tlB) =>
TL.trafficLightJoin.join(tlA, tlB) ===
TL.trafficLightJoin.join(
TL.trafficLightJoin.join(tlA, tlB),
tlB
)
)
)
})
})
33 changes: 33 additions & 0 deletions examples/ord/Ord.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import * as BJS from 'fp-ts/BoundedJoinSemilattice'
import * as Ord from 'fp-ts/Ord'
import { Ord as numOrd } from 'fp-ts/number'

const mk = <A>(a: A) => a

export const red = mk<TrafficLight>('red')
export const yellow = mk<TrafficLight>('yellow')
export const yellowFlashing = mk<TrafficLight>('yellow-flashing')
export const green = mk<TrafficLight>('green')

export type TrafficLight =
| 'red'
| 'yellow'
| 'yellow-flashing'
| 'green'

const tlNums: Record<TrafficLight, number> = {
red: 0,
yellow: 1,
'yellow-flashing': 2,
green: 3,
}

const trafficLightOrd: Ord.Ord<TrafficLight> = {
equals: (a, b) => numOrd.equals(tlNums[a], tlNums[b]),
compare: (a, b) => numOrd.compare(tlNums[a], tlNums[b]),
}

export const trafficLightJoin: BJS.BoundedJoinSemilattice<TrafficLight> = {
join: Ord.max(trafficLightOrd),
zero: 'red',
}