type-systems

Can Python implement dependent types?

£可爱£侵袭症+ 提交于 2020-02-19 08:59:57
问题 A simple demo of dependent type in Idris is Vector, whose type depends on it's value. we can define Type Hints in Python. from typing import List def append(a: List[int], b: List[int]) -> List[int]: return a + b print(append([1, 2], [1, 3, 4])) So,can we implement a type Vect which can used as below: def append(a: Vect[m,t], b: Vect[n,t]) -> Vect[(m+n),t]: return a + b m and n are natural numbers, t is of any type. 回答1: Yes , but it's super hacky (and it would be really hard to get everything

Why is the following java code leads to compilation error

﹥>﹥吖頭↗ 提交于 2020-01-24 21:16:13
问题 I am currently working on making my code contain more generics. I encountered a compilation error which looks quite complicated but which I was able to reduce to an equivalent error in the following code: List<List<?>> a = new ArrayList<List<Integer>>(); Why this happens? What can I do to fix it? 回答1: Instances of a generic class with different type parameters are not related, i.e. even though String is a subtype of Object , List<String> is not a subtype of List<Object> , and even though List

Two functions compile with type annotations. Remove one annotation - doesn't compile. Remove two - compiles again. Why?

南楼画角 提交于 2020-01-24 03:08:26
问题 Mind this Reflex program: {-# LANGUAGE ScopedTypeVariables, RecursiveDo #-} import Control.Applicative import Control.Monad import Control.Monad.IO.Class import Prelude hiding (div) import Reflex.Dom import qualified Data.Map as M clickMe :: MonadWidget t m => m (Event t ()) clickMe = do rec (e,_) <- elAttr' "button" M.empty (display c) c :: Dynamic t Int <- count (domEvent Click e) return $ domEvent Click e div :: forall t m a . MonadWidget t m => m a -> m a div = elAttr "div" ("style" =:

Understanding scala's _ vs Any/Nothing

こ雲淡風輕ζ 提交于 2020-01-24 02:07:59
问题 If a class has a convariant type parameter such as Iterable[+A], is there any difference between declaring def foo(bar: Iterable[_]) and def foo(bar: Iterable[Any]) ? If a class has a contravariant type parameter such as Growable[-A], is there any difference between declaring def foo(bar: Growable[_]) and def foo(bar: Growable[Nothing]) ? 回答1: It does make a little difference when generic parameter is bounded. For example, if you had class BoundedIterable[+A <: Something] class

A better way to implement collection of array of different types

∥☆過路亽.° 提交于 2020-01-14 22:52:41
问题 I'm looking for a semi-general purpose data structure in C# to store arrays of different integer and float types. In some cases, the integers are bit fields where each bit is equally important and loss of precision isn't tolerable. I'm finding this difficult and messy because of the C# type system and my lack of C# fluency. The project: Ethercat periodic packets arrive and are converted to a structure (Packet) and accumulated as Packet[] over the course of an experiment. Each field of Packet

java Generics Wildcard

六月ゝ 毕业季﹏ 提交于 2020-01-14 07:40:09
问题 I have a question on the use of wildcards in Java's generic types: what is the basic difference between List<? extends Set> and List<T extends Set> ? When would I use either? 回答1: Two reasons: To avoid unnecessary casts: You have to use the T variant for cases like this: public <T extends Set> T firstOf(List<T> l) { return l.get(0); } With ? this would become: public Set firstOf2(List<? extends Set> l) { return l.get(0); } ...which doesn't give the same amount of information to the caller of

Typed FP: Tuple Arguments and Curriable Arguments

我的未来我决定 提交于 2020-01-12 07:35:10
问题 In statically typed functional programming languages, like Standard ML, F#, OCaml and Haskell, a function will usually be written with the parameters separated from each other and from the function name simply by whitespace: let add a b = a + b The type here being " int -> (int -> int) ", i.e. a function that takes an int and returns a function which its turn takes and int and which finally returns an int. Thus currying becomes possible. It's also possible to define a similar function that

How to manually return a Result<(), Box<dyn Error>>?

こ雲淡風輕ζ 提交于 2020-01-12 03:39:45
问题 I want to return an error from a function in case a condition is true: use std::error::Error; pub fn run() -> Result<(), Box<dyn Error>> { // -- snip --- if condition { // return error } // -- snip -- Ok(()) } fn main() {} I probably don't have the basics of the typesystem down, but everywhere I looked people use the ? operator, so I can't figure out what type to return. Is it possible to just return an error like this? Is there a better way to handle this logic? 回答1: Error is a trait and you

Why `trait T; class C; class X extends (C with T)` can't be compiled?

混江龙づ霸主 提交于 2020-01-01 17:01:10
问题 Scala code: trait T class C type W = C with T class X extends W W is a type alias, but I want define a class to extend it. Why and how to fix it? 回答1: I have difficulty structuring my answer in a nice way, but here is nevertheless an attempt at explaining what's going on: You get a compilation error because the extends clause requires class and traits, not types, and you're giving a type. Classes and traits must not be confused with types. There are certainly better explanations of this out

Using Haskell's types to replace assert statements or if checks in other languages

别等时光非礼了梦想. 提交于 2020-01-01 08:27:09
问题 Sorry if the question is very elementary, I am still very new to Haskell. Lets say I have a function that can only work with two numbers that are in the golden ration (1.618), how do I define the types of myfun x y to take only golden ratio numbers. What happens if I invoke myfun without golden ratio numbers from within my program (a compile error?)? What happens if the call without golden ratio numbers is made at runtime via user input? 回答1: You might want an ADT that can only be constructed