Questions tagged [type-constructor]

In type theory and Haskell, an entity that specifies a type in terms of parameters; equivalently, a paramaterized data type

In type theory and in the programming language, a type constructor is an entity that specifies a new , given one or more parameters. For example, in Haskell, the 2- type constructor (,) can be applied to the types Int and String to create the type of (Int, String) tuples.

In Haskell, where basic types like Int have *

Prelude> :kind Int
Int :: *

—type constructors have more complex kinds:

Prelude> :kind (,)
(,) :: * -> * -> *

That means that (,) takes 2 parameters and returns a type:

Prelude> :kind (,) Int String
(,) Int String :: *

A basic type can also be viewed as a zero-argument type constructor (analogous to how Haskell considers constants like True that are members of an to be zero-argument data constructors).

An equivalent way of thinking about type constructors is as paramaterized data types, with a function similar to Vector< > in C++/Java/C#.

66 questions
291
votes
5 answers

What is a higher kinded type in Scala?

You can find the following on the web: Higher kinded type == type constructor? class AClass[T]{...} // For example, class List[T] Some say this is a higher kinded type, because it abstracts over types which would be compliant with the…
Lutz
  • 4,485
  • 4
  • 16
  • 23
17
votes
1 answer

Is there a way to "remove" the parts of a functor that do not store its argument?

Given a functor (or any type constructor) f, we can get a "version" of that functor that does not contain a value of its argument. We just define newtype NoArg f = NoArg (f Void). For example: NoArg [] is just the empty list. NoArg Maybe is just…
PyRulez
  • 9,505
  • 9
  • 37
  • 82
16
votes
2 answers

Difference between F[_] and F[T] In Scala when used in type constructors

This question is about _ as used in type constructor and not when used in defining existential types. So the question is what is the difference when _ is used as type parameter instead of a variable like T. For example difference between F[_] and…
dade
  • 2,600
  • 3
  • 22
  • 40
16
votes
3 answers

How does one statisfy a class constraint in an instance of a class that requires a type constructor rather than a concrete type?

I'm currently in Chapter 8 of Learn you a Haskell, and I've reached the section on the Functor typeclass. In said section the author gives examples of how different types could be made instances of the class (e.g Maybe, a custom Tree type, etc.)…
Miguel
  • 1,930
  • 2
  • 16
  • 31
15
votes
3 answers

Why is there a value constructor in addition to the type constructor in Haskell?

I'm a newcomer to Haskell and am currently going through Real World Haskell. The book says the type constructor is used only in the type signature while the value constructor is used in actual code. It also gives an example of a declaration to show…
Atmaram Shetye
  • 973
  • 6
  • 15
10
votes
1 answer

Mere presence of implicit conversion makes the program compile despite never being applied

Consider method f which is parameterised by a type constructor F[_] and a proper type A def f[F[_], A](v: F[A]) = v Lets try to apply it to new Bar scala> class Bar class Bar scala> def f[F[_], A](v: F[A]) = v def f[F[_], A](v: F[A]): F[A] scala>…
10
votes
3 answers

What is the difference between Either a and Either Int? What is Either a ? A polymorphic type constructor? What is its purpose?

Introduction: I understand the difference between Maybe a and Maybe Int, I also understand the difference between Either a b and Either Int Int. I also understand that Either Int is the same kind of animal as Maybe, they both take a type as an…
jhegedus
  • 18,516
  • 11
  • 84
  • 147
8
votes
3 answers

How does the Haskell compiler "know" that IO cannot be unwrapped?

Obviously, the following function is impossible, because it is impossible to unwrap an IO value permanently (ignoring unsafePerformIO or similar): unwrapIO :: IO String -> String unwrapIO (IO str) = str However, similar functions such as the…
markasoftware
  • 10,714
  • 7
  • 36
  • 62
8
votes
2 answers

Why does foo[F[_], A](ff: F[A]) accept foo(1)?

Why does the call to foo(1) work in my Scala 2.11.7 repl as described below? scala> def foo[F[_], A](fa: F[A]) = null foo: [F[_], A](fa: F[A])Null scala> foo(List(1)) res0: Null = null scala> foo(1) res1: Null = null The parameter in my call to…
rodoherty1
  • 527
  • 3
  • 12
8
votes
2 answers

Type Constructor as Return Type

In Scala, I can define an Algebraic Data Type: scala> sealed trait Maybe[A] defined trait Maybe scala> case class Just[A](x: A) extends Maybe[A] defined class Just scala> case object NothingHere extends Maybe[Nothing] defined object…
Kevin Meredith
  • 38,251
  • 58
  • 190
  • 340
8
votes
3 answers

What does '((->) a)' mean?

I've seen this type before without knowing what it means. Does it mean something and/or does it have a name? Prelude> :m Data.Functor Prelude Data.Functor> :t flip . (flip (<$>)) flip . (flip (<$>)) :: Functor ((->) b) => (b -> a) -> b -> (a -> c)…
Carlos López-Camey
  • 2,394
  • 1
  • 16
  • 19
7
votes
1 answer

Underscores in type bounds on type constructors

Can someone explain why the following doesn't compile? I want that BB[A] is also a List[A]. The method body only enforces this view. scala> def x[A, BB[_] <: List[_]](p: BB[A]) {p: List[A]} :8: error: type mismatch; found : BB[A] …
ziggystar
  • 26,526
  • 9
  • 63
  • 117
7
votes
2 answers

Scala recursive type and type constructor implementation

I have a situation where I need a method that can take in types: Array[Int] Array[Array[Int]] Array[Array[Array[Int]]] Array[Array[Array[Array[Int]]]] etc... let's call this type RAI for "recursive array of ints" def make(rai: RAI): ArrayPrinter =…
7
votes
1 answer

Type Deconstruction

My data types will always have at least two parameters, and the last two parameters are always 'q' and 'm', respectively: {-# LANGUAGE TypeFamilies, FlexibleContexts, UndecidableInstances, TypeOperators, DataKinds, ConstraintKinds, FlexibleInstances…
crockeea
  • 21,467
  • 10
  • 44
  • 93
6
votes
2 answers

Functor Instance for Type Constructor with Two Parameters in Scala

I have a class Foo with two parameters, and I am trying to write a Functor instance for Foo with the first parameter fixed, as follows: object Scratchpad { trait Functor[F[_]] { def fmap[A, B](f: A => B): F[A] => F[B] } case class Foo[X,…
1
2 3 4 5