# Questions tagged [type-constructor]

29 questions

1

votes

0

answer

84

Views

### Define function independent of specific value constructor (templates?)

Is it possible to write a function for generic types (in this case applicatives) without making assumptions about the name of type constructors?
I can write the following:
f :: Maybe a -> Maybe a
f (Just a) = (Just a)
That one works on Maybes and only instances with the Just constructor.
But say I h...

1

votes

1

answer

79

Views

### Data type with type constraint in Wadler - Essence of functional programming paper,

In Phil Wadler's paper : The essence of functional programming, Wadler describes the application of Monads using a simple interpreter program. The program is as shown below:
A term is either a variable, a constant, a sum, a lambda expression,
or an application. The following will serve as test data....

1

votes

1

answer

60

Views

### Scala case class: generating Instances for type constructor cases?

I want to use a generic function that returns the 'default value' for a given type. I have implemented this with the help of the following case class construct:
case class DefaultOp[T](op: () => T)
implicit val defaultString = DefaultOp[String](() => '')
implicit val defaultInt = DefaultOp[Int](() =...

1

votes

1

answer

555

Views

### flow optional type parameter

How can I use an optional type parameter in a type constructor? In the following, I want the Fruit type constructor to return different types based on whether C is passed or not:
type Color =
| 'yellow'
| 'orange'
| 'red';
type Fruit = {
// ^ I wanna make this optional
t: T,
color: C, //

5

votes

2

answer

500

Views

### Is it possible to provide a custom pattern decomposition in Haskell?

In Haskell, I have a module with a partial order type:
data PartialOrder a = PartialOrder [a] [(a, a)]
I don't export the value constructor because that's not how I want the type to be used, but I still want to be able to pattern match the PartialOrder type outside the module; is this possible? In p...

2

votes

2

answer

553

Views

### Elm: How to Json decode a Union Type with a single TypeConstructor?

As seen in following code, it is easy to decode UserAlias but the moment I try to decode UserType i.e. Replace D.map2 UserAlias with D.map2 UserType Compiler cries out loud. How do I fix this compiler error?
import Json.Decode as D
import Html exposing (..)
import Result as R
type UserType = UserTy...

4

votes

4

answer

204

Views

### F# assign (custom) type to a function

For all the progress I've made in F#, I still get lost in various of the constructor and deconstructor syntax.
I'm running a recursive simulation. One of the parameters is a function for the stopping condition. I have various possible stopping conditions to choose from. I make them all have the same...

3

votes

1

answer

200

Views

### How do I use a Haskell type constructor as an enumeration?

I am writing a program in Haskell that makes use of a lookup table.
eg.
type Table = [(Object, FilePath)]
data Object = Player { pName :: String }
I want to construct this in such a way that Player can be a lookup key:
[(Player, 'data/players'), ...]
If I added another Object type Monster, my table...

2

votes

2

answer

531

Views

### Anonymous Type Functions

This is a followup to a previous question: Type-level Map with DataKinds, starting with from the two answers it received.
My goal is to take a HList of arbitrary types and turn it into a list of related/derived types
type family TypeMap (a :: * -> *) (xs :: [*]) :: [*]
type instance TypeMap t '[] =...

2

votes

1

answer

327

Views

### Applying type constructors to generated type parameters with Scala macros

I am trying to materialize an instance of the (simplified) trait
trait TC[F[_]] {
def apply[A](fa: F[A]): F[A]
}
using Scala macros. The signature of the macro therefore is
def materialize[F[_]](c: Context)(
implicit fT: c.WeakTypeTag[F[_]]): c.Expr[TC[F]]
Type constructor F[_] now needs to be appli...

3

votes

3

answer

119

Views

### Does the Either type constructor contain a phantom type each for the left/right case?

AFAIK, only types are inhabited by values in Haskell, not type constructors. Either is a binary type constructor of kind * -> * -> *. Left and Right both apply this type constructor to a single type, which is provided by the passed value. Doesn't that mean that in both cases Either is merely partial...

3

votes

2

answer

190

Views

### Is there are way to create a generic class with a trait or mixin that is a subtype of the type parameter

I am trying to attach extra data to other types, and have a trait similar to:
trait ExtraData {
def getExtraData() : Array[Byte]
}
And I'm currently using it like this:
class ExternalType1WithExtraData(superType:ExternalType1, bytes:Array[Byte]) extends ExternalType1(superType.a,superType.b, ...) wi...

3

votes

1

answer

860

Views

### Julia: Making empty/initialized multidimensional arrays of self defined types

I am making a type of my own called KeyVal defined as below:
type KeyVal
first::Int
second::Float64
end
And I am trying to make an empty/or initialized at zero matrix that its elements are of type KeyVal.
Normally with other types I do
myMat = zeros(KeyVal, (10,3))
But this will not work, because t...

7

votes

1

answer

652

Views

### 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 #-}
data D1 q m = D1 q
data D2 t q m = D2 q
class...

2

votes

2

answer

86

Views

### Haskell construct a type contains class

I want to construct a haskell type:
type SinglePP = (String,GLattice)
Where the GLattice is defined as:
class GLattice l where
join :: l->l->l
....
Is there a way for me to do that?

2

votes

1

answer

212

Views

### Implementing algebraic data types to my compiler

I've been trying to write a small compiler for the past few weeks, while reading Stephen Diehl's great tutorial 'Write You A Haskell'. I'm currently writing an interpreter before writing the compiler.
I have troubles to represent my values evaluated from an algebraic data type constructor applicatio...

13

votes

3

answer

2k

Views

### 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 that the names for both are independent of each oth...

2

votes

1

answer

36

Views

### Scala - how to create a single implicit that can be used for a type constructor

I'm trying to write a method which uses the isEmpty method on types String, Option and List. These classes don't share a common base trait with that method, so I've tried to pass an implicit EmptyChecker in with them:
trait EmptyChecker[Field] {
def isEmpty(data: Field): Boolean
}
implicit val Strin...

7

votes

1

answer

90

Views

### 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 foo(1) is not a Type Constructor so why does the Scala...

3

votes

2

answer

125

Views

### How to make a Type-constructor part of the Functor typeclass if it has more than one Type-parameter?

I am trying to understand the way the Functor-Typeclass works in Haskell. If you have a function f :: a -> b -> c and you want to partially apply it to the argB to get a function that takes one argument, you can simply do:
f' :: a -> c
f' x = f x argB
and use that. Is it possible to get such behavi...

3

votes

1

answer

2.5k

Views

### ocaml type constructor arguments

I defined an AVL tree as such, with 'a -> 'a -> int being the comparison function
type 'a t = Empty of ('a -> 'a -> int)
| Node of 'a * 'a t * 'a t * ('a -> 'a -> int)
I'm trying to use this AVL module to implement a priority queue in a separate module.
type 'a t = Queue of (Avl.t * int)
But when I...

1

votes

1

answer

55

Views

### How to enforce a type constructor parameter for GHCI

Hello i have the following problem:
I am constructing a parametric newtype over a method and i do not know how to explictly tell GHCI : I want you to instiantiate this newtype using this type parameter
newtype M a = M {fu::a->Int}
var = M (\s-> length (s:'asa')) #tell him i want the type parameter...

1

votes

1

answer

90

Views

### Haskell restricting integer values via enumeration

I want to do something like this:
data Bit = 0 | 1
But since the right hand side has to be a valid data constructor (?) I would have to use something like
data Bit = Zero | One
This isn't particularly good since I want to use the actual values 0 and 1. What's the best solution to my conundrum?

4

votes

1

answer

204

Views

### Modeling a binary relationship between two types

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[_],TargetedTyp...

2

votes

1

answer

1.4k

Views

### OCaml - Unbound type constructor

I am new to OCaml and confused about the Module system in Ocaml
module type Queue =
sig
type element
type queue
val enq: queue * element -> queue
end
module StringQ : Queue with type element = string =...

1

votes

1

answer

55

Views

### What does the parameters without arrows mean in type constructor?

In the expression
data Frank a b = Frank {frankField :: b a} deriving (Show)
What does {frankField :: b a} means?
Is {frankField :: b a} a type constructor? If so, should the parameters look like b->a instead b a?

5

votes

2

answer

128

Views

### 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 following are possible:
unwrapJust :: Maybe String -> Stri...

8

votes

3

answer

329

Views

### 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) -> c

6

votes

2

answer

174

Views

### 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 = { ArrayPrinter(rai) }
Where ArrayPrinter is a class...