Questions tagged [polymorphic-functions]

28 questions
19
votes
5 answers

Can I print in Haskell the type of a polymorphic function as it would become if I passed to it an entity of a concrete type?

Here's a function polymorphic in 3 types: :t (.) (.) :: (b -> c) -> (a -> b) -> a -> c and here a non polymorphic function: :t Data.Char.digitToInt Data.Char.digitToInt :: Char -> Int If we apply the former to the latter, we get a function…
13
votes
3 answers

Are polymorphic functions "restrictive" in Scala?

In the book Functional Programming in Scala MEAP v10, the author mentions Polymorphic functions are often so constrained by their type that they only have one implementation! and gives the example def partial1[A,B,C](a: A, f: (A,B) => C): B => C =…
Somasundaram Sekar
  • 5,244
  • 6
  • 43
  • 85
8
votes
1 answer

How do I write higher-order functions that take polymorphic functions as arguments in Typed Racket?

For example, how can I write a version of map that will work with polymorphic functions in Typed Racket? I use a simple id function defined as: (: id : (All (A) A -> A)) (define (id x) x) When I try to map it over a list i get an error: > (map id…
7
votes
1 answer

Dynamically parametrize Poly1 function in shapeless

I have this situation (stripped down to the essential parts) class Foo[L <: HList](columns: L) { class toRecord(row: Row) extends Poly1 { implicit def caseColumn[T] = at[Column[T]] { /* map to a record field */ } } def asRecord = { …
Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235
4
votes
2 answers

Function parameter anyelement, PostgreSQL bug?

I do not see the bug in this implementation: CREATE FUNCTION foo(anyelement) RETURNS SETOF int AS $f$ SELECT id FROM unnest(array[1,2,3]) t(id) WHERE CASE WHEN (pg_typeof($1)::text)='integer' THEN $1::int>2 ELSE true END $f$ LANGUAGE SQL…
Peter Krauss
  • 13,174
  • 24
  • 167
  • 304
4
votes
3 answers

OCaml Explicit polymorphic type annotations

I would enjoy to receive some helpful comments concerning an example given on: http://caml.inria.fr/pub/docs/manual-ocaml-400/manual021.html#toc79 7.12 Explicit polymorphic type annotations type 'a t = Leaf of 'a | Node of ('a * 'a) t let rec…
4
votes
2 answers

How do polymorphic inline caches work with mutable types?

A polymorphic inline cache(PIC) works by caching the actual method by the type of the object, in order to avoid the expensive lookup procedures (usually a hashtable lookup). How does one handle the type comparison if the type objects are mutable…
3
votes
2 answers

In Scala, how to summon a polymorphic function applicable to an input type, without knowing the output type or full type arguments?

Since Scala 2.12 (or is it 2.13, can't be sure), the compiler can infer latent type arguments across multiple methods: def commutative[ A, B ]: ((A, B) => (B, A)) = {???} // implementing omitted val a = (1 -> "a") val b =…
tribbloid
  • 4,026
  • 14
  • 64
  • 103
3
votes
1 answer

Mapping over generic tuples with polymorphic functions

Scala 3 provides polymorphic functions and Tuples similar to shapeless HList: scala> 1 *: "foo" *: Tuple() val res0: (Int, String) = (1,foo) scala> val f: ([T] => T => Option[T]) = [T] => (v: T) => Some(v) val f: PolyFunction{apply: [T](x$1: T):…
Mario Galic
  • 47,285
  • 6
  • 56
  • 98
3
votes
1 answer

port Oracle decode() using variadic, anyarray, and anyelement

I need to port from Oracle a stored procedure that uses decode() extensively. That is, I can't use series of CASE WHEN expr THEN expr [...] ELSE as the guide suggests. I wanted to create a variadic function, but here's the problem: in Oracle the…
basin
  • 3,949
  • 2
  • 27
  • 63
3
votes
2 answers

Pattern match on type of polymorphic parameter - alternatives

Let's say I need different output depending on the type of the polymorphic parameter of a function. My initial attempt fails with some cryptic error message: choice :: a -> Int choice (_ :: Int) = 0 choice (_ :: String) = 1 choice _ = 2 However, we…
mucaho
  • 2,119
  • 20
  • 35
3
votes
0 answers

Diverging implicit expansion for shapeless LeftFolder

I'm trying to solve this problem and want to fold over an HList using a Poly2 function. Here's a self-contained MWE (you can copy-paste this inside a REPL with shapeless 2.0 in order to reproduce the issue) import shapeless._; import…
Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235
1
vote
1 answer

Context bounds for polymorphic function type in Scala3?

The set up: I'm somewhat new to Scala (specifically, Scala 3) - so I understand that the following question might be dumb/naïve. I know that a polymorphic method type below works. def zeroMethod[A:Numeric] = Numeric[A].zero Here Numeric is the…
1
vote
1 answer

Defining a constructor for a typeclass that takes a method with a type parameter?

I have a situation where none of the solutions that I'm aware of feel like good ones. I am trying to define a typeclass, like the example below, where it has an abstract type S that must implement another typeclass (not shown) Valid[A]. It feels…
Chris J Harris
  • 1,597
  • 2
  • 14
  • 26
1
vote
1 answer

Type lambda with higher kind

In Dotty given the following: object Domain { final case class Create(name: String) extends BaseCreate[Create] { override type Model = Domain override def service[F[_]](client: KeystoneClient[F]): CrudService[F, Domain, Create] =…
Simão Martins
  • 1,210
  • 11
  • 22
1
2