16 | %hide Builtin.infixr.(#)
17 | %hide Data.Vect.Quantifiers.All.index
19 | {-------------------------------------------------------------------------------
20 | {-------------------------------------------------------------------------------
21 | Various utilities necessary for TensorType, but that don't fit anywhere else
22 | Does not depend on any other file within this project
24 | Some of these feel like they should be in the Idris standard library
26 | -------------------------------------------------------------------------------}
27 | -------------------------------------------------------------------------------}
30 | ||| The proof that a decidable property leads to a contradiction
31 | ||| `IsNo` is a type Idris can automatically synthesise, unlike `Not`
32 | ||| See example below
39 | failing
60 | ||| Proof of inequality yields IsNo
100 | ||| If an element `i` is not in the singleton list `[j]`, then `j` is not in
101 | ||| the singleton list `[i]`
106 | ||| If an element `i` is in the singleton list `[j]`, then `j` is in the
107 | ||| singleton list `[i]`
115 | ||| Definition of liftA2 in terms of (<*>)
120 | ||| Tensorial strength
125 | ||| Pointwise Num structure for Applicative functors
134 | ||| Implementation of Foldable for Vect that is denotationally equivalent to
135 | ||| one in Data.Vect, but which does not use `foldrImpl` and therefore
136 | ||| reduces in the typechecker
142 | ||| toList with a different foldable implementation
152 | ||| Duplicate of utilities for Data.Vect in their Naperian form
158 | -- Because of the way foldr for Vect is implemented in Idris
159 | -- we have to use this approach below, otherwise allSuccThenProdSucc breaks
163 | -- prod [] = fromInteger 1
164 | -- prod (x :: xs) = x * prod xs
186 | ||| Dual to concat from Data.Vect
193 | ||| Trim a specified trailing value
198 | ||| Combination of `cons` and `snoc`: adds an element in front, and at the end
203 | ||| Pad a vector with a specified element to exactly `targetSize`
212 | ||| Drop the first i elements of a vector
213 | ||| Analogous to Data.Vect.drop, except the index is Fin n instead of Nat
220 | ||| Drop all the elements up and until the element `x` from a vector
244 | ||| Map each element along with its zero-based position in the list.
248 | where
253 | ||| Split a list into consecutive chunks of size `n` (clamped to at least 1).
254 | ||| The final chunk may be shorter than `n`, this is why the length of the
255 | ||| list is needed as upper bound.
259 | where
279 | ||| Trim a specified trailing value
284 | ||| Combination of `cons` and `snoc`: adds an element in front, and at the end
289 | ||| Pad a list with a specified element to at least `targetSize`
296 | ||| Drop all the elements after the element `x` from a list
303 | ||| Analogue of `(::)`
309 | -- dcons : x -> ((i : Fin k) -> i' i) -> ((i : Fin (S k)) -> i' (cons x i'))
319 | ||| All but the last element
324 | ||| Analogus to `Data.Vect.take`
351 | ||| Like weakenN from Data.Fin, but where n is on the other side of +
356 | ||| Like weakenN, but with mutliplication
357 | ||| Like shiftMul, but without changing the value of the index
373 | ||| Variant of `shift` from Data.Fin, but with multiplication
374 | ||| Given an index i : Fin n, it recasts it as one where steps are stride sized
375 | ||| That is, returns stride * i : Fin (stride * n)
376 | ||| Implemented by recursing on i, adding stride each time
387 | ||| Analogous to strengthen from Data.Fin
388 | ||| Attempts to strengthen the bound on Fin (m + n) to Fin m
389 | ||| If it doesn't succeed, then returns the remainder in Fin n
397 | -- strengthenN {m = 0} x = Nothing
398 | -- strengthenN {m = (S k)} FZ = Just FZ
399 | -- strengthenN {m = (S k)} (FS x) with (strengthenN x)
400 | -- _ | Nothing = Nothing
401 | -- _ | (Just p) = Just $ FS p
402 | --= let t = strengthenN x
403 | -- in ?strengthenN_rhs_3
405 | -- strengthenN {n = 0} x = Just x
406 | -- strengthenN {m = 0} {n = (S k)} FZ = Nothing
407 | -- strengthenN {m = (S j)} {n = (S k)} FZ = Just FZ
408 | -- strengthenN {m} {n = (S k)} (FS x)
409 | -- = let t = strengthenN x
410 | -- v = Fin.FS
411 | -- in ?what -- strengthenN x
414 | -- restCount = indexCount is -- fpn = 13 : Fin (20)
415 | -- iCTest1 : indexCount {shape = [3, 4, 5]} [1, 2, 3] = 33
416 | -- iCTest1 = ?iCTest_rhs
418 | ||| Like finS, but without wrapping
419 | ||| finS' last = last
425 | --finS' {n = S _} x = case strengthen x of
426 | -- Nothing => x
427 | -- Just y => FS y
430 | ||| Adds two Fin n, and bounds the result
431 | ||| Meaning (93:Fin 5) + (4 : Fin 5) = 4
443 | ||| Divides a Fin by 2, rounding down
450 | ||| Computes the midway index between two bounds
458 | ||| Given a non-empty sorted vector `xs`, finds the "right bin", i.e. the index
459 | ||| of the smallest element between the bounds that `x` is not bigger than.
460 | ||| If `x` is bigger than the highest element, returns `Nothing`
461 | ||| `findBinBetween [2,7,10] 1 0 2 = Just 0`
462 | ||| `findBinBetween [2,7,10] 3 0 2 = Just 1`
463 | ||| `findBinBetween [2,7,10] 9 0 2 = Just 2`
464 | ||| `findBinBetween [2,7,10] 7 0 2 = Just 2`
465 | ||| `findBinBetween [1,2,3,4,5] 6 0 4 = Nothing`
482 | ||| Todo can this eventually be generalised to non-cubical tensors?
483 | ||| Given a non-empty sorted vector `xs`, finds the "right bin", i.e. the index
484 | ||| of the smallest element between the bounds that `x` is not bigger than.
485 | ||| If `x` is bigger than the highest element, returns `Nothing`
486 | ||| `findBin [2,7,10] 1 = Just 0`
487 | ||| `findBin [2,7,10] 3 = Just 1`
488 | ||| `findBin [2,4,6,8] 7 = Just 3`
498 | -- t : Double -> Type
499 | -- t 4 = Double
500 | -- t _ = String
501 | --
502 | -- th : (x : Double ** t x)
503 | -- th = (4 ** 5)
504 | --
505 | -- thh : (x : Double) -> Show (t x)
506 | -- thh x = ?thh_rhs
516 | -- public export
517 | -- Num Unit where
518 | -- fromInteger _ = ()
519 | -- () * () = ()
520 | -- () + () = ()
536 | -- Probably there's a faster way to do this
537 | -- public export
538 | -- {n : Nat} -> Random a => Random (Vect n a) where
539 | -- randomIO = sequence $ replicate n randomIO
540 | -- randomRIO (lo, hi) = sequence $ zipWith (\l, h => randomRIO (l, h)) lo hi
555 | -- for reshaping a tensor
560 | -- -> Data.Fin.Arith.(*) (Fin (S x)) (Fin (S y))
563 | ||| There is a similar function in Data.Fin.Arith, which has the smallest
564 | ||| possible bound. This one does not, but has a simpler type signature.
570 | ||| Splits xs at each occurence of delimeter (general version for lists)
585 | where
586 | -- Check if list starts with delimiter
592 | -- Check if delimiter occurs anywhere in the list
598 | -- Break list at first occurrence of delimiter
601 | where
610 | ||| Splits xs at each occurence of delimeter (string version)
617 | ||| Simple string replacement function
625 | where
678 | ||| Cnvert an all to a vector if it's made out of replicated things
691 | ||| Dependent parametric traverse
723 | ||| Graph of a dependent function
730 | ||| Version of `map` for dependent function
731 | ||| Note that here `x : a` is identity in some sense, it comes from `f a`
739 | -- ||| Duplicate of `index` from Data.Vect.Quantifiers.All, but with an
740 | -- ||| additional `public` export modifier
747 | {-
749 | interface Comult (f : Type -> Type) a where
750 | comult : f a -> f (f a)
752 | {shape : Vect n Nat} -> Num a => Comult (TensorA shape) a where
753 | comult t = ?eir
755 | gg : TensorA [3] Double -> TensorA [3, 3] Double
756 | gg (TS xs) = TS $ map ?fn ?gg_rhs_0
758 | -- [1, 2, 3]
759 | -- can we even do outer product?
760 | -- we wouldn't need reduce, but something like multiply?
761 | outer : {f : Type -> Type} -> {a : Type}
762 | -> (Num a, Applicative f, Algebra f a)
763 | => f a -> f a -> f (f a)
764 | outer xs ys = let t = liftA2 xs ys
765 | in ?outer_rhs
767 | -}
769 | |||| filter' works without `with`?
775 | ||| filter'' implemented with `with`
781 | {-
782 | Prelude.absurd : Uninhabited t => t -> a
783 | believe_me : a -> b
785 | -}
795 | -- Should this be detected as `using` the variable `n`?
796 | -- in pattern matching, we'd have to unify type of `xs` which has in itself `len`
797 | -- and `n` which in this case is computed to be `S len`?
798 | -- this step of `ll2` is decomposing `n` only one level down, but the entire recursion ends up using the entire `n`
817 | -- public export
818 | -- filter : (elem -> Bool) -> Vect len elem -> (p ** Vect p elem)
819 | -- filter p [] = ( _ ** [] )
820 | -- filter p (x::xs) =
821 | -- let (_ ** tail) = filter p xs
822 | -- in if p x then
823 | -- (_ ** x::tail)
824 | -- else
825 | -- (_ ** tail)