Îõ³h$Ø‘7      !"#$%&'()*+,-./0123456 Safe-Inferred #$5>?ÀÁÂÑÙáH binary-search2The type of function that returns a value between (lo, hi) as long as one is available. binary-searchìThe (possibly infinite) lists of candidates for lower and upper bounds from which the search may be started. binary-searchThe Range k lo k' hi# represents the search result that  pred x == k for  lo <= x <= hi. The Ä type also holds the evidences for the lower and the upper boundary. binary-searchThe  datatype is similar to 7 , but differes in that all  , values are equal to each other, and all - values are also equal to each other. The à type is used to binary-searching for some predicate and meanwhile returning evidences for that.In other words,  is a 8' with additional information why it is 9 or :.Start binary search from between the given pair of boundaries. binary-search-Exponentially search for lower boundary from [-1, -2, -4, -8, ...], upper boundary from [0, 1, 2, 4, 8, ...]/. Move on to the binary search once the first (lo, hi) is found such that pred lo /= pred hi. binary-search,Lower boundary is 1, upper boundary is from [2, 4, 8, 16, ...]. binary-search3Lower boundary is 0, search upper boundary is from [1, 2, 4, 8, ...]. binary-searchLower boundary is [0.5, 0.25, 0.125, ...], upper boundary is from [1, 2, 4, 8, ...]. binary-searchLower boundary is from [-2, -4, -8, -16, ...], upper boundary is -1. binary-searchLower boundary is from [-1, -2, -4, -8, ...], upper boundary is -0. binary-searchLower boundary is [-1, -2, -4, -8, ...], upper boundary is from [-0.5, -0.25, -0.125, ...]. binary-searchÀPerform split forever, until we cannot find a mid-value because  hi-lo < 2Ç. This splitter assumes that the arguments are Integral, and uses the > funtion.ÄNote that our dividing algorithm always find the mid value for any  hi-lo >= 2.ðprove $ \x y -> (y .>= x+2 &&& x+2 .> x) ==> let z = (x+1) `sDiv` 2 + y `sDiv` 2 in x .< z &&& z .< (y::SInt32)Q.E.D. binary-searchPerform splitting until hi - lo <= eps. binary-searcháPerform split forever, until we cannot find a mid-value due to machine precision. This one uses ? operator. binary-searchPerform splitting until hi - lo <= eps. binary-searchÄPerform search over pure predicates. The monadic version of this is . binary-search Mother of all search variations.Å keeps track of the predicates found, so that it works well with the  type. binary-searchLook up for the first  with the given predicate. binary-searchPick up the smallest a that satisfies  pred a == b. binary-searchPick up the largest a that satisfies  pred a == b.  binary-search1Get the content of the evidence for the smallest a which satisfies pred a.! binary-search0Get the content of the evidence for the largest a which satisfies pred a." binary-search8Get the content of the counterEvidence for the smallest a which does not satisfy pred a.# binary-search7Get the content of the counterEvidence for the largest a which does not satisfy pred a.$  !"#$   !"#(c) Ross Paterson 2008 BSD-styleross@soi.city.ac.uk experimentalportable Safe-Inferreds/ binary-search O(log(abs n)). Search the integers.If p is an upward-closed predicate, search p returns the least n satisfying p. If no such n. exists, either because no integer satisfies p or all do, search p does not terminate.ÊFor example, the following function computes discrete logarithms (base 2): ÊdiscreteLog :: Integer -> Integer discreteLog n = search (\ k -> 2^k <= n)0 binary-search O(log(n-l))0. Search the integers from a given lower bound.If p! is an upward-closed predicate, searchFrom p l = / (\ i -> i >= l && p i). If no such n& exists (because no integer satisfies p),  searchFrom p does not terminate.1 binary-search O(log(h-n))1. Search the integers up to a given upper bound.If p is an upward-closed predicate, searchTo p h == @ n if and only if n is the least number n <= h satisfying p.2 binary-search O(m log(n/m))>. Two-dimensional search, using an algorithm due described inRichard Bird, /Saddleback search: a lesson in algorithm design, in #Mathematics of Program Construction+ 2006, Springer LNCS vol. 4014, pp82-89.If p? is closed upwards in each argument on non-negative integers,  search2 p3 returns the minimal non-negative pairs satisfying p., listed in order of increasing x-coordinate.m and nÖ refer to the smaller and larger dimensions of the rectangle containing the boundary. For example, Ásearch2 (\ x y -> x^2 + y^2 >= 25) == [(0,5),(3,4),(4,3),(5,0)]/012/012(c) Ross Paterson 2008 BSD-styleross@soi.city.ac.uk experimentalportable Safe-Inferred&3 binary-search O(log(h-l))3. Search a bounded interval of some integral type.If p is an upward-closed predicate, searchFromTo p l h == Just n if and only if n is the least number  l <= n <= h satisfying p.õFor example, the following function determines the first index (if any) at which a value occurs in an ordered array: ¾searchArray :: Ord a => a -> Array Int a -> Maybe Int searchArray x array = do let (lo, hi) = bounds array k <- searchFromTo (\ i -> array!i >= x) lo hi guard (array!k == x) return k33(c) Ross Paterson 2008 BSD-styleross@soi.city.ac.uk experimentalportable Safe-Inferred‡4 binary-search O(log(abs n))". Search a bounded integral type.If p is an upward-closed predicate, search p returns Just n if and only if n is the least such satisfying p.5 binary-search O(log(abs n))Á. Search the part of a bounded integral type from a given point.If p is an upward-closed predicate, searchFrom p l returns Just n if and only if n is the least n >= l satisfying p.6 binary-search O(log(abs n))Â. Search the part of a bounded integral type up to a given point.If p is an upward-closed predicate,  searchTo p h returns Just n if and only if n is the least n <= h satisfying p.456456Á       !"#$%&'()*+,-./012345236789:;9:<9:=6>?6@A6@B6CD6CE6FGÈ*binary-search-2.0.0-7b69nVdXGLdGaJFsdiwM7eNumeric.SearchNumeric.Search.IntegerNumeric.Search.RangeNumeric.Search.BoundedSplitter SearchRangeRangeloKeyloValhiKeyhiValEvidenceCounterEvidenceevidencecounterEvidenceinitializeSearchMminToMaxfromTo exponentialpositiveExponentialnonNegativeExponentialpositiveFractionalExponentialnegativeExponentialnonPositiveExponentialnegativeFractionalExponential divForeverdivTill divideForever divideTillsearchsearchM lookupRangessmallestlargestevidenceForSmallestevidenceForLargestcounterEvidenceForSmallestcounterEvidenceForLargest$fMonadEvidence$fApplicativeEvidence $fOrdEvidence $fEqEvidence $fShowRange $fReadRange $fEqRange $fOrdRange$fShowEvidence$fReadEvidence$fFunctorEvidence searchFromsearchTosearch2 searchFromTobase Data.EitherEitherghc-prim GHC.TypesBoolTrueFalseGHC.Err undefinedGHC.EnumminBoundmaxBoundGHC.Realdiv/ GHC.MaybeJust