parametric-polymorphism

Induction principle for `le`

我怕爱的太早我们不能终老 提交于 2019-12-25 02:24:06
问题 For the inductive type nat , the generated induction principle uses the constructors O and S in its statement: Inductive nat : Set := O : nat | S : nat -> nat nat_ind : forall P : nat -> Prop, P 0 -> (forall n : nat, P n -> P (S n)) -> forall n : nat, P n But for le , the generated statement does not uses the constructors le_n and le_S : Inductive le (n : nat) : nat -> Prop := le_n : n <= n | le_S : forall m : nat, n <= m -> n <= S m le_ind : forall (n : nat) (P : nat -> Prop), P n -> (forall

Why is this implementation invalid?

末鹿安然 提交于 2019-12-23 10:37:10
问题 Let's say I have the following type signature: someFunction :: (Eq a, Eq b) => a -> b With implementation: someFunction x = (2 :: Int) (Don't look in to it too far, it's just an example). My understanding of the signature is that " someFunction takes an argument that is an instance of the Eq typeclass, and returns a value (that can be of a different type) that is an instance of the Eq typeclass". Int is an instance of Eq , so why does GHC get upset about this implementation? The error makes

Universal quantification in generic function type

白昼怎懂夜的黑 提交于 2019-12-21 21:18:11
问题 Reading the paper on Types and Polymorphism in programming languages, i wondered is it possible to express the similar universal quantification on type members with Scala. Example from the paper: type GenericID = ∀A.A ↦ A Which is a type for generic identity function and the following example in their paper language Fun was correct: value inst = fun(f: ∀a.a ↦ a) (f[Int], f[Bool]) value intId = fst(inst(id)) // return a function Int ↦ Int Is there some way to express the similar thing in Scala

Type signatures that never make sense

╄→尐↘猪︶ㄣ 提交于 2019-12-21 11:05:30
问题 Consider (a->a) -> [a] -> Bool Is there any meaningful definition for this signature? That is, a definition that not simply ignores the argument? x -> [a] -> Bool It seems there are many such signatures that can be ruled out immediately. 回答1: Carsten König suggested in a comment to use the free theorem. Let's try that. Prime the cannon We start by generating the free theorem corresponding to the type (a->a) -> [a] -> Bool . This is a property that every function with that type must satisfy,

How to cast generic types that I know to be integers?

柔情痞子 提交于 2019-12-17 14:04:33
问题 I want to check return codes of C APIs in a generic way and the result must be free from C types such as libc::c_int . Are there any ways to write a function like fn check<S: PartialOrd + std::num::Zero, T> (x: S) -> Option<T> { if std::num::zero::<S>() <= x { Some(x as T) } else { None } } when I'm sure that S and T are integral types for all usages of check() ? The compiler rejects my code complaining error: non-scalar cast: `S` as `T` 回答1: Updated for Rust 1.x It is impossible to cast

How to cast generic types that I know to be integers?

烈酒焚心 提交于 2019-12-17 14:04:12
问题 I want to check return codes of C APIs in a generic way and the result must be free from C types such as libc::c_int . Are there any ways to write a function like fn check<S: PartialOrd + std::num::Zero, T> (x: S) -> Option<T> { if std::num::zero::<S>() <= x { Some(x as T) } else { None } } when I'm sure that S and T are integral types for all usages of check() ? The compiler rejects my code complaining error: non-scalar cast: `S` as `T` 回答1: Updated for Rust 1.x It is impossible to cast

Abstract Data Types vs. Parametric Polymorphism in Haskell

不打扰是莪最后的温柔 提交于 2019-12-14 04:18:21
问题 I am trying to get a grip on the relationship between these two concepts. First consider an example of an Abstract Data Type : data Tree a = Nil | Node { left :: Tree a, value :: a, right :: Tree a } According to Haskell wiki: This type is abstract because it leaves some aspects of its structure undefined, to be provided by the user of the data type. This is a weak form of abstract data type. Source Now consider the notion of parametric polymorphism : Parametric polymorphism refers to when

How to express mixed ad-hoc and parametric polymorphic in typescript?

我的未来我决定 提交于 2019-12-13 03:20:10
问题 I'm not sure if I'm describing the questing currently in the title. What I'm trying to ask comes from the following requirement. I'm trying to make an abstract for states of finite state machines and comes up with the following definition (in typescript) interface IState { send<T, E>(message: T, callback?:(event: E)=>void): IState; } I'm trying to express that a state of the finite state machine should be able to accept messages and return new state, with an optional callback to handle event

In Idris, can I prove free theorems, e.g. the only (total) function of type `forall t. t -> t` is `id`?

和自甴很熟 提交于 2019-12-12 10:30:05
问题 For sufficiently polymorphic types, parametricity can uniquely determine the function itself (see Wadler's Theorems for free! for details). For example, the only total function with type forall t. t -> t is the identity function id . Is it possible to state and prove this in Idris? (And if it can't be proven inside Idris, is it true anyway?) The following is my attempt (I know that function equality is not a primitive concept in Idris, so I assert that any function of generic type t -> t

Scala: understanding parametric polymorphism

我只是一个虾纸丫 提交于 2019-12-12 08:14:34
问题 What is the difference between def drop1[A](l: List[A]) = l.tail and def drop1(l: List[Int]) = l.tail provided the usage looks something like drop1(List(1,2,3)) ? When should one or the other be used and why? Whereas I can understand the second example, I don't really understand the purpose of the first one. 回答1: It's very simple really. Your first example refers to the concept of generics . Generics have a simple goal, to make certain methods generic , e.g non-type dependant. Lets look at