question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Defining Ord in terms of lt vs lte

See original GitHub issue

Almost immediately after 3.2 was cut I thought about how libraries (Sanctuary in particular) would implement it. I realized that either there were going to be extra equality checks (not performant) or an indirect definition in terms of gt (unnecessarily convoluted).

// with Ord defined by lte
// note double equality checks
// only define 'fantasy-land/lte' and 'fantasy-land/equals'
const lte = a => b => a.lte(b);
const lt = a => b => a.lte(b) && !a.equals(b);
const gt = a => b => !a.lte(b);
const gte = a => b => !a.lte(b) || a.equals(b)

// with Ord defined by lte and lte defined in terms of gt
// solves extra equality checks
// requires implementing gt, 'fantasy-land/equals', and 'fantasy-land/lte' in terms of gt
const gt = a => b => a.gt(b);
const lte = a => b => !a.gt(b);
const lt = a => b => !a.gt(b) && !a.equals(b);
const gte = a => b => a.gt(b) || a.equals(b);

An alternative would be to define Ord in terms of lt and equals.

// no extra equality checks
// only define 'fantasy-land/lt` and 'fantasy-land/equals'
const lt = a => b => a.lt(b);
const lte = a => b => a.lt(b) || a.equals(b);
const gt = a => b => !a.lt(b) && !a.equals(b);
const gte = a => b => !a.lt(b);

laws:

  1. If a.lt(b) then b.lt(a) === false (irreflexivity)
  2. a.lt(b) or b.lt(a) or a.equals(b) (trichotomy)
  3. If a.lt(b) and b.lt(c), then a.lt(c) (transitivity)

An added bonus is that we could refer to lt as precedes which has no connotation of quantity.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:32 (29 by maintainers)

github_iconTop GitHub Comments

3reactions
xgbuilscommented, Apr 22, 2017

I don’t really understand the root of the performance problem. I can take any Ord operator(lt, lte, gt, gte) and define the rest using only negation.

// based on .lte
const lte = a => b => a.lte(b);
const lt = a => b => !b.lte(a);
const gt = a => b => !a.lte(b);
const gte = a => b => b.lte(a);

// based on .gt
const lte = a => b => !a.gt(b);
const lt = a => b => b.gt(a); // edit: before const lt = a => b => b.lte(a);
const gt = a => b => a.gt(b);
const gte = a => b => !b.gt(a); // edit: before const gte = a => b => !b.lte(a);

1reaction
i-am-tomcommented, Apr 9, 2017

@davidchambers … Today, you have legitimately and significantly improved my life.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Fantas, Eel, and Specification 3.5: Ord - Tom Harding
Using only lte and equals (because every Ord is a Setoid ), we can derive all ... lt :: Ord a => a...
Read more >
Ord.md | Minnesota Extensible Language Tools Group
(Undocumented.) instance Ord Float . lt. Contained in grammar [silver:core] . Defined at silver/core/Ord.sv line 94. (Undocumented.) instance Ord Float . lte.
Read more >
Data.Nat - Idris
LT is defined in terms of LTE which makes it annoying to use. This convenient view of allows us to avoid having to...
Read more >
LTE Acronyms - lteencyclopedia - Google Sites
The term used to describe the pattern of subdivisions of radio frames in the time domain. In LTE, the frame structure can be...
Read more >
TS 136 212 - V10.0.0 - LTE - ETSI
in ETSI SR 000 314: "Intellectual Property Rights (IPRs); Essential, or ... For the purposes of the present document, the terms and definitions...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found