6||| Numbers strictly less than some bound. The name comes from "finite sets".
8||| It's probably not a good idea to use `Fin` for arithmetic, and they will be
9||| exceedingly inefficient at run time.
10||| @ n the upper bound
11data Fin : (n : Nat) -> Type where
13 FS : Fin k -> Fin (S k)
15implementation Uninhabited (Fin Z) where
16 uninhabited FZ impossible
17 uninhabited (FS f) impossible
19FSInjective : (m : Fin k) -> (n : Fin k) -> FS m = FS n -> m = n
20FSInjective left _ Refl = Refl
22implementation Eq (Fin n) where
24 (==) (FS k) (FS k') = k == k'
27||| There are no elements of `Fin Z`
28FinZAbsurd : Fin Z -> Void
29FinZAbsurd FZ impossible
32FinZElim x = void (FinZAbsurd x)
34||| Convert a Fin to a Nat
35finToNat : Fin n -> Nat
37finToNat (FS k) = S (finToNat k)
39||| `finToNat` is injective
40finToNatInjective : (fm : Fin k) -> (fn : Fin k) -> (finToNat fm) = (finToNat fn) -> fm = fn
41finToNatInjective FZ FZ Refl = Refl
42finToNatInjective (FS m) FZ Refl impossible
43finToNatInjective FZ (FS n) Refl impossible
44finToNatInjective (FS m) (FS n) prf =
45 cong (finToNatInjective m n (succInjective (finToNat m) (finToNat n) prf))
47implementation Cast (Fin n) Nat where
50||| Convert a Fin to an Integer
51finToInteger : Fin n -> Integer
53finToInteger (FS k) = 1 + finToInteger k
55implementation Cast (Fin n) Integer where
56 cast x = finToInteger x
58||| Weaken the bound on a Fin by 1
59weaken : Fin n -> Fin (S n)
61weaken (FS k) = FS (weaken k)
63||| Weaken the bound on a Fin by some amount
64weakenN : (n : Nat) -> Fin m -> Fin (m + n)
66weakenN n (FS f) = FS (weakenN n f)
68||| Attempt to tighten the bound on a Fin.
69||| Return `Left` if the bound could not be tightened, or `Right` if it could.
70strengthen : Fin (S n) -> Either (Fin (S n)) (Fin n)
71strengthen {n = S k} FZ = Right FZ
72strengthen {n = S k} (FS i) with (strengthen i)
73 strengthen (FS k) | Left x = Left (FS x)
74 strengthen (FS k) | Right x = Right (FS x)
77||| Add some natural number to a Fin, extending the bound accordingly
78||| @ n the previous bound
79||| @ m the number to increase the Fin by
80shift : (m : Nat) -> Fin n -> Fin (m + n)
82shift {n=n} (S m) f = FS {k = (m + n)} (shift m f)
84||| The largest element of some Fin type
89total FSinjective : {f : Fin n} -> {f' : Fin n} -> (FS f = FS f') -> f = f'
90FSinjective Refl = Refl
92implementation Ord (Fin n) where
94 compare FZ (FS _) = LT
95 compare (FS _) FZ = GT
96 compare (FS x) (FS y) = compare x y
98implementation MinBound (Fin (S n)) where
101implementation MaxBound (Fin (S n)) where
105-- Construct a Fin from an integer literal which must fit in the given Fin
107natToFin : Nat -> (n : Nat) -> Maybe (Fin n)
108natToFin Z (S j) = Just FZ
109natToFin (S k) (S j) with (natToFin k j)
110 | Just k' = Just (FS k')
112natToFin _ _ = Nothing
114||| Convert an `Integer` to a `Fin`, provided the integer is within bounds.
115||| @n The upper bound of the Fin
116integerToFin : Integer -> (n : Nat) -> Maybe (Fin n)
117integerToFin x Z = Nothing -- make sure 'n' is concrete, to save reduction!
118integerToFin x n = if x >= 0 then natToFin (cast x) n else Nothing
120||| Allow overloading of Integer literals for Fin.
121||| @ x the Integer that the user typed
122||| @ prf an automatically-constructed proof that `x` is in bounds
123fromInteger : (x : Integer) ->
125 prf : (IsJust (integerToFin x n))} ->
127fromInteger {n} x {prf} with (integerToFin x n)
128 fromInteger {n} x {prf = ItIsJust} | Just y = y
130||| Convert an Integer to a Fin in the required bounds/
131||| This is essentially a composition of `mod` and `fromInteger`
133restrict : (n : Nat) -> Integer -> Fin (S n)
134restrict n val = let val' = assert_total (abs (mod val (cast (S n)))) in
135 -- reasoning about primitives, so we need the
136 -- 'believe_me'. It's fine because val' must be
137 -- in the right range
139 {prf = believe_me {a=IsJust (Just val')} ItIsJust}
141%language ErrorReflection
143||| Attempt to convert a reflected (fromInteger n) to a Nat
145getNat' : TT -> Maybe TT
146getNat' `(fromInteger ~(TConst (BI c)) : Nat) = Just $ quote (the Nat (fromInteger c))
149||| Attempt to convert a reflected (fromInteger n) to a Nat. Return
150||| the original term on failure.
153getNat tm = maybe tm id $ getNat' tm
156mkFinIntegerErr : TT -> TT -> List ErrorReportPart -> Maybe (List ErrorReportPart)
157mkFinIntegerErr lit finSize subErr
158 = Just [ TextPart "When using", TermPart lit
159 , TextPart "as a literal for a"
160 , TermPart `(Fin ~(getNat finSize))
164finFromIntegerErrors : Err -> Maybe (List ErrorReportPart)
165finFromIntegerErrors (CantUnify x tm `(IsJust (integerToFin ~(TConst c) ~m)) err xs y)
166 = mkFinIntegerErr (TConst c) m
167 [ TermPart (TConst c)
168 , TextPart "is not strictly less than"
169 , TermPart (getNat m)
171finFromIntegerErrors (CantUnify x tm `(IsJust (integerToFin ~(P Bound n t) ~m)) err xs y)
172 = mkFinIntegerErr (P Bound n t) m
173 [ TextPart "Could not show that", TermPart (P Bound n t)
174 , TextPart "is less than", TermPart (getNat m)
175 , TextPart "because", TermPart (P Bound n t)
176 , TextPart "is a bound variable instead of a constant"
177 , TermPart `(Integer : Type)
179finFromIntegerErrors (CantUnify x tm `(IsJust (integerToFin ~n ~m)) err xs y)
180 = mkFinIntegerErr n m
181 [ TextPart "Could not show that" , TermPart n
182 , TextPart "is less than" , TermPart (getNat m)
184finFromIntegerErrors _ = Nothing
186%error_handlers Data.Fin.fromInteger prf finFromIntegerErrors
188implementation Enum (Fin (S n)) where
190 pred (FS n) = weaken n
191 succ n = case strengthen (FS n) of
195 fromNat {n=n} m = case natToFin m (S n) of
199--------------------------------------------------------------------------------
201--------------------------------------------------------------------------------
203total FZNotFS : {f : Fin n} -> FZ {k = n} = FS f -> Void
204FZNotFS Refl impossible
206implementation DecEq (Fin n) where
207 decEq FZ FZ = Yes Refl
208 decEq FZ (FS f) = No FZNotFS
209 decEq (FS f) FZ = No $ negEqSym FZNotFS
210 decEq (FS f) (FS f') with (decEq f f')
211 | Yes p = Yes $ cong p
212 | No p = No $ \h => p $ FSinjective {f = f} {f' = f'} h
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list