Questions tagged [adhoc-polymorphism]

ad hoc polymorphism is a kind of polymorphism in which polymorphic functions can be applied to arguments of different types.

ad hoc polymorphism is a kind of polymorphism in which polymorphic functions can be applied to arguments of different types, because a polymorphic function can denote a number of distinct and potentially heterogeneous implementations depending on the type of argument(s) to which it is applied. It is also known as function overloading or operator overloading. The term "ad hoc" in this context is not intended to be pejorative; it refers simply to the fact that this type of polymorphism is not a fundamental feature of the type system. This is in contrast to parametric polymorphism, in which polymorphic functions are written without mention of any specific type, and can thus apply a single abstract implementation to any number of types in a transparent way.

33 questions
18
votes
7 answers

Type inference interferes with referential transparency

What is the precise promise/guarantee the Haskell language provides with respect to referential transparency? At least the Haskell report does not mention this notion. Consider the expression (7^7^7`mod`5`mod`2) And I want to know whether or not…
false
  • 10,264
  • 13
  • 101
  • 209
10
votes
2 answers

good way to convert between ad-hoc polymorphic functions and parametric polymorphic ones

I'm wondering if there are general ways to convert between ad-hoc polymorphic functions and parametric polymorphic ones. In other words, given an ad-hoc polymorphic function, how to implement its parametric counterpart? and what about the other way…
Javran
  • 3,394
  • 2
  • 23
  • 40
6
votes
1 answer

Can type classes at the type level be simulated with higher-rank types?

I have a pluggable runtime type checker that supports parametric but no ad-hoc polymorphism, because there is no compiling step and type information are erased as soon as the type checker is deactivated. Now I recently came up with the idea to reify…
user6445533
5
votes
3 answers

Polymorphism in OCaml - ad hoc,parametric, inclusion/subtyping

I am having a problem understanding the different types of polymorphism, specifically in regards to OCaml. I understand that polymorphism allows for multiple types in OCaml denoted as 'a, but I do not understand what the different types of…
4
votes
2 answers

Are there viable and type safe alternatives to the 1:1 type/type-class-instance relation?

This question originally arose during my work on a dynamic JS type validator that relies on dictionary passing style as a rather simple type class mechanism, but I think it also applies to Haskell or other languages having a type class mechanism. At…
4
votes
1 answer

Is this understanding correct: trait and function overloading both achieved ad hoc polymorphism but in different direction

I am learning some polymorphism. The wiki page for rust addresses trait is a method to achieve ad hoc polymorphism, and the page for ad hoc polymorphism says function overloading is an example of ad hoc polymorphism. Based on my current level of…
Ailrk
  • 103
  • 1
  • 8
4
votes
1 answer

Does interface belong to ad hoc polymorphism (i.e. overloading) or subtype polymorphism?

https://wiki.haskell.org/Polymorphism says Ad-hoc polymorphism refers to when a value is able to adopt any one of several types because it, or a value it uses, has been given a separate definition for each of those types. For example, the +…
Tim
  • 1
  • 141
  • 372
  • 590
3
votes
2 answers

How to implement `Constraint` reflection? [Ad-Hoc polymorphism based on available Constraints]

For example, with types, you can implement AdHoc polymorphism using Typeable to define a different behavior depending on what the input type is: foo :: forall a. Typeable a => a -> a foo | Just HRefl <- typeRep @a `eqTypeRep` typeRep @Bool = not …
Mathias Sven
  • 349
  • 3
  • 7
3
votes
1 answer

What concept in category theory can be used to represent a typeclass?

In Haskell programming language, according to https://en.wikibooks.org/wiki/Haskell/Category_theory#Translating_categorical_concepts_into_Haskell 59.2.2 Translating categorical concepts into Haskell We work in the category Hask and its…
3
votes
3 answers

When shall I define polymorphic functions by type classes or by some other ways?

I am trying to figure out the purpose of type class, and what else is there if not using type class. Is type class a way to define polymorphic functions? Is type class the only way to define polymorphic functions? For example: class Eq a where …
Tim
  • 1
  • 141
  • 372
  • 590
3
votes
1 answer

Ways to make operator `<` work on custom types

I have type 'a edge = {from: 'a; destination: 'a; weight: int} and I want to have Printf.printf "%b\n" ( {from= 0; destination= 8; weight= 7} < {from= 100; destination= 33; weight= -1} ) print true so I tried this let ( < ) {weight= wa}…
Shuumatsu
  • 592
  • 2
  • 5
  • 12
3
votes
1 answer

Difference between Ad-hoc polymorphism and Parametric polymorphism in Scala

So, I have been searching documentation about the main difference between parametric polymorphism and adhoc-polymorphism, but I still have some doubts. For instance, methods like head in Collections, are clearly parametric polymorphism, since the…
3
votes
4 answers

Why can a Num act like a Fractional?

As expected, this works fine: foo :: Fractional a => a foo = undefined -- datum bar :: Num a => a -> a bar a = undefined -- function baz :: Fractional a => a baz = bar foo -- application This works as…
3
votes
2 answers

Declaring a "subclass" in Haskell

I have troubles with the following simple code in Haskell: import Prelude hiding (cycle). class ICycle a where cycle :: a -> a instance ICycle [a] where cycle [] = [] cycle (x:xs) = xs ++ [x] instance ICycle Bool where cycle True…
Sergey
  • 31
  • 2
3
votes
1 answer

Ad-hoc polymorphism - type classes

I had a look at the scalaz tutorial. From this link, I understand the following code: scala> def sum[A](xs: List[A])(implicit m: Monoid[A]): A = xs.foldLeft(m.mzero)(m.mappend) sum: [A](xs: List[A])(implicit m: Monoid[A])A scala> implicit val…
ccheneson
  • 49,072
  • 8
  • 63
  • 68
1
2 3