idris

Implementing isLast with Idris

☆樱花仙子☆ 提交于 2019-12-10 17:27:27
问题 Looking at exercise 9.2 from Type-Driven Development with Idris: data Last : List a -> a -> Type where LastOne : Last [value] value LastCons : (prf : Last xs value) -> Last (x :: xs) value Uninhabited (Last [] value) where uninhabited LastOne impossible uninhabited (LastCons _) impossible notLast : Not (x = value) -> Last [x] value -> Void notLast prf LastOne impossible notLast prf (LastCons _) impossible isLast : DecEq a => (xs : List a) -> (value : a) -> Dec (Last xs value) isLast [] value

What is the preferred alternative to Fin from Idris in Haskell

久未见 提交于 2019-12-09 15:01:16
问题 I would like to have a type which can contain values 0 to n, where n lives on the type level. I was trying something like: import GHC.TypeLits import Data.Proxy newtype FiniteNat n = FiniteNat { toInteger :: Integer } smartConstructFiniteNat :: (KnownNat n) => Proxy n -> Integer -> Maybe (FiniteNat (Proxy n)) smartConstructFiniteNat pn i | 0 <= i && i < n = Just (FiniteNat i) | otherwise = Nothing where n = natVal pn which works basically, but it's not really satisfying somehow. Is there a

Implementing vector addition in Coq

最后都变了- 提交于 2019-12-07 07:56:44
问题 Implementing vector addition in some of the dependently typed languages (such as Idris) is fairly straightforward. As per the example on Wikipedia: import Data.Vect %default total pairAdd : Num a => Vect n a -> Vect n a -> Vect n a pairAdd Nil Nil = Nil pairAdd (x :: xs) (y :: ys) = x + y :: pairAdd xs ys (Note how Idris' totality checker automatically infers that addition of Nil and non- Nil vectors is a logical impossibility.) I am trying to implement the equivalent functionality in Coq,

natToFin when there is evidence that the conversion will work

*爱你&永不变心* 提交于 2019-12-07 05:45:20
问题 The natToFin function from the standard library has the following signature: natToFin : Nat -> (n : Nat) -> Maybe (Fin n) natToFin 4 5 returns Just (FS (FS (FS (FS FZ)))) : Maybe (Fin 5) , while natToFin 5 5 returns Nothing . I would like a function with the following signature: myNatToFin : (m : Nat) -> (n : Nat) -> { auto p : n `GT` m } -> Fin n It behaves the same as the standard lib function but doesn't need to return a Maybe because it is always possible to generate a Fin n from m given

Can Idris infer indices in types of top-level constants?

自作多情 提交于 2019-12-07 01:15:17
问题 For example, Agda allows me to write this: open import Data.Vec open import Data.Nat myVec : Vec ℕ _ myVec = 0 ∷ 1 ∷ 2 ∷ 3 ∷ [] and myVec will have type Vec ℕ 4 as expected. But if I try the same in Idris: import Data.Vect myVec : Vect _ Nat myVec = [0, 1, 2, 3] I get an error message from the typechecker: When checking right hand side of myVec with expected type Vect len Nat Type mismatch between Vect 4 Nat (Type of [0, 1, 2, 3]) and Vect len Nat (Expected type) Specifically: Type mismatch

Generating run time proofs with type predicates in Idris

狂风中的少年 提交于 2019-12-06 23:05:22
问题 I am using this type to reason about strings on which decidable parsing can be performed: data Every : (a -> Type) -> List a -> Type where Nil : {P : a -> Type} -> Every P [] (::) : {P : a -> Type} -> P x -> Every P xs -> Every P (x::xs) For example, defining the digits [0-9] like this: data Digit : Char -> Type where Zero : Digit '0' One : Digit '1' Two : Digit '2' Three : Digit '3' Four : Digit '4' Five : Digit '5' Six : Digit '6' Seven : Digit '7' Eight : Digit '8' Nine : Digit '9'

Idris eager evaluation

假装没事ソ 提交于 2019-12-06 16:36:31
问题 In Haskell , I might implement if like this: if' True x y = x if' False x y = y spin 0 = () spin n = spin (n - 1) This behaves how I expect : haskell> if' True (spin 1000000) () -- takes a moment haskell> if' False (spin 1000000) () -- immediate In Racket , I could implement a flawed if like this: (define (if2 cond x y) (if cond x y)) (define (spin n) (if (= n 0) (void) (spin (- n 1)))) This behaves how I expect : racket> (if2 #t (spin 100000000) (void)) -- takes a moment racket> (if2 #f

Constraining a function argument in an interface

爱⌒轻易说出口 提交于 2019-12-06 09:00:40
问题 What is the syntax to constrain a function argument in an interface which takes a function? I tried: interface Num a => Color (f : a -> Type) where defs... But it says the Name a is not bound in interface... 回答1: Your interface actually has two parameters: a and f . But f should be enough to pick an implementation : interface Num a => Color (a : Type) (f : a -> Type) | f where f here is called a determining parameter. Here's a nonsensical full example: import Data.Fin interface Num a => Color

Plus vs S in a function type

荒凉一梦 提交于 2019-12-05 23:59:33
The following declaration of vector cons cons : a -> Vect n a -> Vect (n + 1) a cons x xs = x :: xs fails with the error Type mismatch between S n and plus n 1 while the following vector append compiles and works append : Vect n a -> Vect m a -> Vect (n + m) a append xs ys = xs ++ ys Why type-level plus is accepted for the second case but not for the first. What's the difference? Why does x :: xs : Vect (n + 1) a lead to a type error? (+) is defined by induction on its first argument so n + 1 is stuck (because n is a stuck expression, a variable in this case). (::) is defined with the type a -

How to extract the second element of Sigma on the Calculus of Constructions?

老子叫甜甜 提交于 2019-12-05 20:48:36
问题 I'm attempting to do that as follows: λ (A : *) -> λ (B : (A -> *)) -> λ (t : (∀ (r : *) -> (∀ (x : a) -> (B x) -> r)) -> r) -> (t (B (t A (λ (x : A) -> λ (y : (B x)) -> x))) (λ (x : A) -> λ (y : (B x)) -> y)) Notice that, since the value returned by that function depends on a value inside the sigma itself, I need to extract that value. This code doesn't check, because, I believe, it fails to unify the type extracted from Sigma with the type inside it. Is there any workaround? 来源: https:/