dependent-type

How or is that possible to prove or falsify `forall (P Q : Prop), (P -> Q) -> (Q -> P) -> P = Q.` in Coq?

眉间皱痕 提交于 2019-12-28 18:04:06
问题 I want to prove or falsify forall (P Q : Prop), (P -> Q) -> (Q -> P) -> P = Q. in Coq. Here is my approach. Inductive True2 : Prop := | One : True2 | Two : True2. Lemma True_has_one : forall (t0 t1 : True), t0 = t1. Proof. intros. destruct t0. destruct t1. reflexivity. Qed. Lemma not_True2_has_one : (forall (t0 t1 : True2), t0 = t1) -> False. Proof. intros. specialize (H One Two). inversion H. But, inversion H does nothing. I think maybe it's because the coq's proof independence (I'm not a

How to make a type with restrictions

戏子无情 提交于 2019-12-28 02:51:31
问题 For example I want to make a type MyType of integer triples. But not just Cartesian product of three Integer, I want the type to represent all (x, y, z) such that x + y + z = 5 How do I do that? Except of using just (x, y) since z = 5 - x - y And the same question if I have three constructors A, B, C and the type should be all (A x, B y, C z) such that x + y + z = 5 回答1: I think the trick here is that you don't enforce it on the type-level, you use "smart constructors": i.e. only allow

From set inclusion to set equality in lean

Deadly 提交于 2019-12-24 09:39:38
问题 Given a proof of set inclusion and its converse I'd like to be able to show that two sets are equal. For example, I know how to prove the following statement, and its converse: open set universe u variable elem_type : Type u variable A : set elem_type variable B : set elem_type def set_deMorgan_incl : A ∩ B ⊆ set.compl ((set.compl A) ∪ (set.compl B)) := sorry Given these two inclusion proofs, how do I prove set equality, i.e. def set_deMorgan_eq : A ∩ B = set.compl ((set.compl A) ∪ (set.compl

Using a monad to implicitly check refinement type well-formedness

拜拜、爱过 提交于 2019-12-24 06:48:20
问题 While implementing a refinement type system, I need to put in checks to make sure the types are well-formed. For example, a type like Num[100,0] shouldn't happen, where Num[lb,ub] is the type of numbers larger than lb and smaller than ub . I then wrote: -- FORMATION RULES class RefTy t where tyOK :: t -> Bool instance RefTy Ty where tyOK (NumTy (n1, n2)) = n1 <= n2 tyOK (CatTy cs) = isSet cs {- data WellFormed t = Valid t | Invalid instance Monad WellFormed where (>>=) :: RefTy a =>

Infer constraints for both if and else of type equality

守給你的承諾、 提交于 2019-12-24 05:29:35
问题 I am trying to fill the hole in the following snippet import Data.Proxy import GHC.TypeLits import Data.Type.Equality import Data.Type.Bool import Unsafe.Coerce ifThenElse :: forall (a :: Nat) (b :: Nat) x l r. (KnownNat a, KnownNat b, x ~ If (a==b) l r) => Proxy a -> Proxy b -> Either (x :~: l) (x :~: r) ifThenElse pa pb = case sameNat pa pb of Just Refl -> Left Refl Nothing -> Right $ unsafeCoerce Refl -- This was the hole Is it possible? Edit: Checked the source of sameNat and it turns out

Idris - map function on custom dependent data type fails

放肆的年华 提交于 2019-12-24 01:47:07
问题 I am relatively new to idris and dependent-types and I encountered the following problem - I created a custom data type similar to vectors: infixr 1 ::: data TupleVect : Nat -> Nat -> Type -> Type where Empty : TupleVect Z Z a (:::) : (Vect o a, Vect p a) -> TupleVect n m a -> TupleVect (n+o) (m+p) a exampleTupleVect : TupleVect 5 4 Int exampleTupleVect = ([1,2], [1]) ::: ([3,4],[2,3]) ::: ([5], [4]) ::: Empty It is inductively constructed by adding tuples of vectors and indexed by the sum of

ghc-7.6 class instances for dependent types

自闭症网瘾萝莉.ら 提交于 2019-12-23 09:29:45
问题 Heterogeneous lists are one of the examples given for the new dependent type facility of ghc 7.6: data HList :: [*] -> * where HNil :: HList '[] HCons:: a -> HList t -> HList (a ': t) The example list "li" compiles fine: li = HCons "Int: " (HCons 234 (HCons "Integer: " (HCons 129877645 HNil))) Obviously we would like HList to be in the Show class, but I can only come up with the following working class instantiation that uses mutually recursive constraints (superclasses): instance Show (HList

Type safe lookup on heterogeneous lists in Haskell

混江龙づ霸主 提交于 2019-12-23 07:59:20
问题 I want to develop a type safe lookup function for the following data type: data Attr (xs :: [(Symbol,*)]) where Nil :: Attr '[] (:*) :: KnownSymbol s => (Proxy s, t) -> Attr xs -> Attr ('(s , t) ': xs) The obvious lookup function would be like: lookupAttr :: (KnownSymbol s, Lookup s env ~ 'Just t) => Proxy s -> Attr env -> t lookupAttr s ((s',t) :* env') = case sameSymbol s s' of Just Refl -> t Nothing -> lookupAttr s env' where Lookup type family is defined in singletons library. This

is it possible to have different behaviors for the same constructor?

我们两清 提交于 2019-12-22 22:24:47
问题 I'm writing an SQL interpreter. I need to distinguish between ill-formed expressions at compile time, and run-time errors. I'll give you an example of something that should be well-formed, but possibly fails at run-time. SELECT $ [ColumnName "first_name" `AS` "name"] `FROM` TABLE "people.csv" `WHERE` (ColumnName "age" `Gte` LiteralInt 40) I'd like to focus in on the expression: (ColumnName "age" `Gte` LiteralInt 40) This should pass the type checker. But, say "age" did not contain something

Haskell: Specifying equal-length constraints of lists in the type system

主宰稳场 提交于 2019-12-21 09:20:58
问题 In Haskell, I often have a function like f , which accepts a list and returns a list of equal length: f :: [a] -> [a] -- length f(xs) == length xs Similarly, I might have a function like g , which accepts two lists that should be of equal length: g :: [a] -> [a] -> ... If f and g are typed as above, then run-time errors may result if their length-related constraints are not satisfied. I would therefore like to encode these constraints in the type system. How might I do this? Please note that