on master help sign in
Total version of the [a..b] and [a,b..c] syntax.
171b8f9David Christiansen
lib/Prelude.idr+61−11
@@ -17,6 +17,7 @@ import Prelude.Strings
17import Prelude.Chars
17import Prelude.Chars
18import Prelude.Traversable
18import Prelude.Traversable
19import Prelude.Bits
19import Prelude.Bits
20+import Prelude.Stream
20
21
21%access public
22%access public
22%default total
23%default total
@@ -343,24 +344,73 @@ ceiling x = prim__floatCeil x
343
344
344---- Ranges
345---- Ranges
345
346
346−partial abstract
347−count : (Ord a, Num a) => a -> a -> a -> List a
348−count a inc b = if a <= b then a :: count (a + inc) inc b
349− else []
350
347
351−partial abstract
348+natRange : Nat -> List Nat
352−countFrom : (Ord a, Num a) => a -> a -> List a
349+natRange n = List.reverse (go n)
353−countFrom a inc = a :: lazy (countFrom (a + inc) inc)
350+ where go Z = []
351+ go (S n) = n :: go n
352+
353+class Enum a where
354+ total pred : a -> a
355+ total succ : a -> a
356+ total toNat : a -> Nat
357+ total fromNat : Nat -> a
358+ total enumFrom : a -> Stream a
359+ enumFrom n = n :: enumFrom (succ n)
360+ total enumFromThen : a -> a -> Stream a
361+ total enumFromTo : a -> a -> List a
362+ total enumFromThenTo : a -> a -> a -> List a
363+
364+
365+instance Enum Nat where
366+ pred = Nat.pred
367+ succ = S
368+ toNat = id
369+ fromNat = id
370+ enumFromThen n inc = n :: enumFromThen (fromNat (plus (toNat inc) (toNat n))) inc
371+ enumFromThenTo n inc m = map (plus n . (* inc)) (natRange (S (( m - n) `div` inc)))
372+ enumFromTo n m = map (plus n) (natRange (S (m - n)))
373+
374+
375+instance Enum Integer where
376+ pred n = n - 1
377+ succ n = n + 1
378+ toNat n = cast n
379+ fromNat n = cast n
380+ enumFromThen n inc = n :: enumFromThen (inc + n) inc
381+ enumFromTo n m = go (natRange (S (cast {to = Nat} (m - n))))
382+ where go : List Nat -> List Integer
383+ go [] = []
384+ go (x :: xs) = n + cast x :: go xs
385+ enumFromThenTo n inc m = go (natRange (S (fromInteger (abs (m - n)) `div` fromInteger (abs inc))))
386+ where go : List Nat -> List Integer
387+ go [] = []
388+ go (x :: xs) = n + (cast x * inc) :: go xs
389+
390+instance Enum Int where
391+ pred n = n - 1
392+ succ n = n + 1
393+ toNat n = cast n
394+ fromNat n = cast n
395+ enumFromThen n inc = n :: enumFromThen (inc + n) inc
396+ enumFromTo n m = go (natRange (S (cast {to = Nat} (m - n))))
397+ where go : List Nat -> List Int
398+ go [] = []
399+ go (x :: xs) = n + cast x :: go xs
400+ enumFromThenTo n inc m = go (natRange (S (cast {to=Nat} (abs (m - n)) `div` cast {to=Nat} (abs inc))))
401+ where go : List Nat -> List Int
402+ go [] = []
403+ go (x :: xs) = n + (cast x * inc) :: go xs
354
404
355syntax "[" [start] ".." [end] "]"
405syntax "[" [start] ".." [end] "]"
356− = count start 1 end
406+ = enumFromTo start end
357syntax "[" [start] "," [next] ".." [end] "]"
407syntax "[" [start] "," [next] ".." [end] "]"
358− = count start (next - start) end
408+ = enumFromThenTo start (next - start) end
359
409
360syntax "[" [start] "..]"
410syntax "[" [start] "..]"
361− = countFrom start 1
411+ = enumFrom start 1
362syntax "[" [start] "," [next] "..]"
412syntax "[" [start] "," [next] "..]"
363− = countFrom start (next - start)
413+ = enumFromThen start (next - start)
364
414
365---- More utilities
415---- More utilities
366
416
lib/Prelude/Nat.idr+9
@@ -190,6 +190,15 @@ instance Lattice Nat where { }
190instance BoundedJoinSemilattice Nat where
190instance BoundedJoinSemilattice Nat where
191 bottom = Z
191 bottom = Z
192
192
193+
194+instance Cast Int Nat where
195+ cast i = fromInteger (cast i)
196+
197+instance Cast Nat Int where
198+ cast Z = 0
199+ cast (S n) = 1 + cast {to=Int} n
200+
201+
193--------------------------------------------------------------------------------
202--------------------------------------------------------------------------------
194-- Auxilliary notions
203-- Auxilliary notions
195--------------------------------------------------------------------------------
204--------------------------------------------------------------------------------
lib/base.ipkg+1−1
@@ -7,7 +7,7 @@ modules = Builtins, Prelude, IO, System,
7 Prelude.List, Prelude.Maybe, Prelude.Monad, Prelude.Applicative,
7 Prelude.List, Prelude.Maybe, Prelude.Monad, Prelude.Applicative,
8 Prelude.Either, Prelude.Vect, Prelude.Strings, Prelude.Chars,
8 Prelude.Either, Prelude.Vect, Prelude.Strings, Prelude.Chars,
9 Prelude.Heap, Prelude.Complex, Prelude.Functor, Prelude.Foldable,
9 Prelude.Heap, Prelude.Complex, Prelude.Functor, Prelude.Foldable,
10− Prelude.Traversable, Prelude.Bits,
10+ Prelude.Traversable, Prelude.Bits, Prelude.Stream,
11
11
12 Network.Cgi,
12 Network.Cgi,
13 Debug.Trace,
13 Debug.Trace,