parametric-polymorphism

Parametric Polymorphism vs Subtype polymorphism F#

笑着哭i 提交于 2019-12-06 06:11:53
What is the difference (if any) between these two F# type signatures? UseTheStream<'a when 'a :> Stream> : 'a -> unit and UseTheStream : (stream : Stream) -> unit Do they mean the same thing in this case? msdn says the following about the (:>) Type Constraint type-parameter :> type -- The provided type must be equal to or derived from the type specified, or, if the type is an interface, the provided type must implement the interface. This would indicate that the two signatures are saying the same thing. So Functionally, how are they different? Daniel They are different. Most importantly, the

Modeling a binary relationship between two types

梦想的初衷 提交于 2019-12-06 04:39:24
问题 There are businesses and people. Users can either like or post a comment about a business but the same can not happen with a person. When a user posts something about a business or likes it, that business is called the target of that like or post: trait TargetingRelation[TargetingType[_],TargetedType] class Business class Person class Post[Target | TargetingRelation[Business,Post] ] { def target:Target } class Like[Target | TargetingRelation[Business,Like] ] { def target:Target } Here I'm

Why does `peek` with a polymorphic Ptr return GHC.Prim.Any when used with a bind?

假装没事ソ 提交于 2019-12-06 02:34:55
Using the low-level GNU Science Library bindings Bindings.Gsl.RandomNumberGeneration , I'm running into this odd type behavior in GHCi where binding changes return type from a peek into GHC.Prim.Any . I'm trying to understand why since I can't use the c'rng_alloc unless I retain the type of pointer to an rng . For eample: λ> :t c'gsl_rng_alloc c'gsl_rng_alloc :: Ptr C'gsl_rng_type -> IO (Ptr C'gsl_rng) λ> :t p'gsl_rng_mt19937 p'gsl_rng_mt19937 :: Ptr (Ptr gsl_rng_type) λ> :t peek p'gsl_rng_mt19937 peek p'gsl_rng_mt19937 :: IO (Ptr gsl_rng_type) λ> x <- peek p'gsl_rng_mt19937 λ> :t x x :: Ptr

Monadic substitution under binders

喜夏-厌秋 提交于 2019-12-06 01:36:16
In the following Agda code, I have a term language based on de Bruijn indices. I can define substitution over terms in the usual de Bruijn indices way, using renaming to allow the substitution to proceed under a binder. module Temp where data Type : Set where unit : Type _⇾_ : Type → Type → Type -- A context is a snoc-list of types. data Cxt : Set where ε : Cxt _∷_ : Cxt → Type → Cxt -- Context membership. data _∈_ (τ : Type) : Cxt → Set where here : ∀ {Γ} → τ ∈ Γ ∷ τ there : ∀ {Γ τ′} → τ ∈ Γ → τ ∈ Γ ∷ τ′ infix 3 _∈_ data Term (Γ : Cxt) : Type → Set where var : ∀ {τ} → τ ∈ Γ → Term Γ τ 〈〉 :

How to use a generic class without the type argument in Swift?

二次信任 提交于 2019-12-05 04:24:19
I want to encapsulate a generic object in another class without setting the generic type argument. I created a base Animal<T> class and defined other subclasses from it. Example: public class Animal<T: YummyObject> { // Code } public class Dog: Animal<Bark> { // Code } public class Cat: Animal<Meow> { // Code } and defined an Animal property, without the type argument, in the UITableView extension bellow: extension UITableView { private static var animal: Animal! func addAnimal(animal: Animal) { UITableView.animal = animal } } but I get the following compile error when doing so: Reference to

difference between polymorphism and overloading

好久不见. 提交于 2019-12-05 02:30:59
问题 I found there has many definition about polymorphism and overloading. Some people said that overloading is one type of polymorphism. While some people said they are not the same. Because only one function will be allocate in overloading. While the polymorphism need allocate the memory for each redefined member function. I really feel confusion about this, any one could explain this for me? Further, whether overloading happens at compile time while the polymorphism happens at running time? 回答1

Is there an automatic way to memoise global polymorphic values in Haskell?

主宰稳场 提交于 2019-12-04 18:21:10
问题 Polymorphic "constants", like 5 :: Num a => a , aren't really constants but functions of a dictionary argument. Hence, if you define primes :: Num n => [n] primes = ... Bad example of course, there's no good reason here to have it polymorphic... what I'm really interested is if you try to globally memoise a nontrivial polymorphic function, with e.g. memo-trie s. then this sequence won't be shared between calls from different sites, which isn't nice in terms of performance. (Isn't this the

Why is C++ said not to support parametric polymorphism?

半城伤御伤魂 提交于 2019-12-04 17:42:14
问题 According to the wikipedia page for Parametric Polymorphism: Some implementations of type polymorphism are superficially similar to parametric polymorphism while also introducing ad hoc aspects. One example is C++ template specialization. Question: Why is C++ said to only implement something superficially similar to paramaterized polymorphism? In particular, aren't templates an example of full on parametric polymorphism? 回答1: Why is C++ said to only implement something superficially similar

Universal quantification in generic function type

瘦欲@ 提交于 2019-12-04 16:08:04
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? This is not the same as type constructor type GenericId[A] = A => A , cause it's a type operation

Modeling a binary relationship between two types

蓝咒 提交于 2019-12-04 09:26:31
There are businesses and people. Users can either like or post a comment about a business but the same can not happen with a person. When a user posts something about a business or likes it, that business is called the target of that like or post: trait TargetingRelation[TargetingType[_],TargetedType] class Business class Person class Post[Target | TargetingRelation[Business,Post] ] { def target:Target } class Like[Target | TargetingRelation[Business,Like] ] { def target:Target } Here I'm inventing a T | P[T] notation meaning type parameter T such that it satisfies some property P[T] (or T :|: