{-# LANGUAGE OverlappingInstances
, FlexibleInstances
, UndecidableInstances
#-}{-# OPTIONS_GHC -Wall -fwarn-tabs #-}------------------------------------------------------------------ ~ 2009.01.29-- |-- Module : Data.Number.PartialOrd-- Copyright : Copyright (c) 2007--2009 wren ng thornton-- License : BSD3-- Maintainer : wren@community.haskell.org-- Stability : experimental-- Portability : semi-portable (overlapping instances, etc)-- -- The Prelude's 'Ord' class for dealing with ordered types is often-- onerous to use because it requires 'Eq' as well as a total-- ordering. While such total orderings are common, partial orderings-- are moreso. This module presents a class for partially ordered-- types.----------------------------------------------------------------moduleData.Number.PartialOrd(-- * Partial OrderingPartialOrd(..)-- * Functions,comparingPO)where-- Bugfix for Hugs (September 2006), see note below.importPreludehiding(isNaN)importHugs.RealFloat(isNaN)------------------------------------------------------------------ | This class defines a partially ordered type. The method names-- were chosen so as not to conflict with 'Ord' and 'Eq'. We use-- 'Maybe' instead of defining new types @PartialOrdering@ and-- @FuzzyBool@ because this way should make the class easier to-- use.---- Minimum complete definition: 'cmp'classPartialOrdawhere-- | like 'compare'cmp::a->a->MaybeOrdering-- | like ('>')gt::a->a->MaybeBoolgtxy=casex`cmp`yofJustGT->JustTrueJust_->JustFalseNothing->Nothing-- | like ('>=')ge::a->a->MaybeBoolgexy=casex`cmp`yofJustLT->JustFalseJust_->JustTrueNothing->Nothing-- | like ('==')eq::a->a->MaybeBooleqxy=casex`cmp`yofJustEQ->JustTrueJust_->JustFalseNothing->Nothing-- | like ('/=')ne::a->a->MaybeBoolnexy=casex`cmp`yofJustEQ->JustFalseJust_->JustTrueNothing->Nothing-- | like ('<=')le::a->a->MaybeBoollexy=casex`cmp`yofJustGT->JustFalseJust_->JustTrueNothing->Nothing-- | like ('<')lt::a->a->MaybeBoolltxy=casex`cmp`yofJustLT->JustTrueJust_->JustFalseNothing->Nothing-- | like 'max'. The default instance returns the left argument-- when they're equal.maxPO::a->a->MaybeamaxPOxy=doo<-x`cmp`ycaseoofGT->JustxEQ->JustxLT->Justy-- | like 'min'. The default instance returns the left argument-- when they're equal.minPO::a->a->MaybeaminPOxy=doo<-x`cmp`ycaseoofGT->JustyEQ->JustxLT->Justxinfix4`gt`,`ge`,`eq`,`ne`,`le`,`lt`,`maxPO`,`minPO`instance(Orda)=>PartialOrdawherecmpxy=Just$!x`compare`ygtxy=Just$!x>ygexy=Just$!x>=yeqxy=Just$!x==ynexy=Just$!x/=ylexy=Just$!x<=yltxy=Just$!x<ymaxPOxy=Just$!x`max`yminPOxy=Just$!x`min`y-- N.B. Hugs (Sept 2006) has a buggy definition for 'isNaN' which-- always returns @False@. We use a fixed version, provided the CPP-- was run with the right arguments. See "Hugs.RealFloat". If 'cmp'-- returns @Just Eq@ for @notANumber@ then CPP was run wrongly.---- The instances inherited from Ord are wrong. So we'll fix them.instancePartialOrdFloatwherecmpxy|isNaNx||isNaNy=Nothing|otherwise=Just$!x`compare`yinstancePartialOrdDoublewherecmpxy|isNaNx||isNaNy=Nothing|otherwise=Just$!x`compare`y------------------------------------------------------------------ TODO? add maximumPO\/minimumPO via left or right fold?-- BUG: Haddock doesn't link the `comparing`---- | Like @Data.Ord.comparing@. Helpful in conjunction with the-- @xxxBy@ family of functions from "Data.List"comparingPO::(PartialOrdb)=>(a->b)->a->a->MaybeOrderingcomparingPOpxy=px`cmp`py--------------------------------------------------------------------------------------------------------------------------- fin.