DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • MuleSoft: Tactical and Strategical Role of an Application Template
  • A Better Web3 Experience: Account Abstraction From Flow (Part 1)
  • Common Problems in React Native Apps on iOS Platform: Solutions and Tips
  • How To Verify Database Connection From a Spring Boot Application

Trending

  • Five Free AI Tools for Programmers to 10X Their Productivity
  • Streamlined Infrastructure Deployment: Harnessing the Power of Terraform and Feature Toggles
  • Beyond the Prompt: Unmasking Prompt Injections in Large Language Models
  • [DZone Research] Join Us for Our 5th Annual Kubernetes Survey!

Partial Application of Type Constructors with Type Lambdas

Learn about the application of type lambda in solving specific problems for the Scala programming language

Adil Akhter user avatar by
Adil Akhter
·
Jun. 03, 15 · Interview
Like (0)
Save
Tweet
Share
3.44K Views

Join the DZone community and get the full member experience.

Join For Free

Type Lambda, a must-have when utilizing higher-kinded types with Scala programming language, is the topic of this post. In this post, we discuss the application of type lambda in solving specific problems.

Background

In this section, we enumerate several concepts that are imperative to our today’s discussion.

Type Members

Scala allows defining type members in a trait or class as follows.

trait HList{
  type Hd
  ....
}

We can define an abstract type member Hd and we can also give a concrete meaning to Hd from its enclosing context, as shown next.

class IntList extends HList {
  type Hd = Int
  ....
}

As noted in #1, the above definition of Hd can also be used as an alias for type Int. For instance, we can define a type alias for Map[Int, V] asIntMap`—

  type IntMap[V] = Map[Int, V]

Note that, in the above code, V is a parametric type parameter and can vary based on the context of IntMap.

Type Projection

We can access the type members of a class or trait with the # operator (similar to . operator in case of value members), e.g., IntList#Hd. For instance, the following line asserts that IntList#Hd is indeed an Int type.

implicitly[Int =:= IntList#Hd]

Type members of a class or a trait can be reused into different context and the relevant type checks are performed statically —

val x: IntList#Hd = 10

Design Principle

From the design perspective, type members are particularly interesting when they are evolved along with their enclosing type to match the behavior accordingly. As noted in #2, it is regarded as family polymorphism or covariant specialization.

Application Type Lambda

Type Lambda is often regarded as Type-Level Lambda. As its name suggests, it is similar to anonymous functions, but for types. In fact, type lambdas are used to define an anonymous/inner types for a given context. It is particularly useful when a type constructor has fewer type parameters compared to the parameterized type that we want to apply in that context.

Next, we explain the concept of type lambda with an example.

Problem Description

Consider the following excellent example of a Functor from #2:

trait Functor[A, +M[_]]{
  def map[B] (f: A => B): M[B]
}

Notice that M[_] type constructor only accepts one type parameter. So, it is quite straightforward if we want extend the Functor defined above with a Seq types.

case class SeqFunctor[A](seq: Seq[A])
  extends Functor[A, Seq]{

  override def map[B](f: (A) => B): Seq[B] =
    seq.map(f)
  }
}

And we can use SeqFunctor as follows:

> val lst = List(1,2,3)
> ListFunctor(lst).map(_ * 10)
// prints List(10, 20, 30)

However, in case of Map[K, V], it would be tricky to extend, since Map[K,V] has two type parameters while M[_] type constructor only accepts one.

Solution

To solve this, we apply type lambda, which handles the additional type parameter as shown below.

case class MapFunctor[K,V](mapKV: Map[K,V])
  extends Functor[V, ({type L[a] = Map[K,a]})#L]{

  override def map[V2](f: V => V2): Map[K, V2] =
    mapKV map{
      case (k,v) => (k, f(v))
    }
}

> MapFunctor(Map(1->1, 2->2, 3->3)).map(_ * 10)
// Result: Map(1 -> 10, 2 -> 20, 3 -> 30)

Here, we simply apply f on the values of mapKV. Thus, type variable are indeedV and V2.

Informally, we extend Functor for Map with a type lambda as follows—

Functor[A, +M[_]] ==>
Functor[V,
  (
    {type L[a] = Map[K, a]} // structural type definition
  )
  #L // type projection
]

Here {type L[a] = Map[K, a]} denotes a structural type. It essentially specifies an inner/annonymous type alias L[a], which is then matched against the (outer) #L. Type parameter K is partially applied and has been resolved from the context in this case. By applying type projection, #L, we get the type member out of the structural type, and thus define an alias for Map[K,_] in effect.

As noted in #3, an empty block in the type position (as in M[_]) essentially creates an anonymous structural type and the type projection allows us to get the type member from it.

Additional Tricks

Type lambda seems a bit intimidating; and handling multiple of them leads to somewhat difficult-to-comprehend code. To avoid that, Dan Rosen proposed a trick.

To demonstrate that, lets consider the previous example. Revisiting the type lambda from MapFunctor, we note that the only varying type is V and V2. We can define a Functor for Map by avoiding type lambda and use type member Map[K] as shown below.

case class ReadableMapFunctor[K,V](mapKV: Map[K,V]){
  def mapFunctor[V2] = {
    type `Map[K]`[V2] = Map[K, V2]
    new Functor[V, `Map[K]`] {
      override def map[V2](f: (V) => V2): `Map[K]`[V2] = mapKV map{
        case (k,v) => (k, f(v))
      }
    }
  }
}

> ReadableMapFunctor(Map(1->1, 2->2, 3->3)).mapFunctor.map(_*10)
//Map(1 -> 10, 2 -> 20, 3 -> 30)

This trick handles the extra type parameter of Map[K, V]. Also note the backticks; they permit the use of [] in the name of an identifier #4.

Following gist outlines code examples used in this post.

Outlook

Overall, it seems to be an excellent feature with specific use-case, yet quite verbose. Sometimes, it is quite difficult to head around it. But surely, it has its applications and is a nice-to-have tool for Scala developers.

Let me know if you have any question. Thanks for visiting this post and going through this rambling.

References

  1. Programming in Scala by Odersky et al.
  2. Programming Scala by Dean Wampler and Alex Payne.
  3. Stackoverflow answer by Kris Nuttycombe regaring the benefits of Type Lambda in Scala.
  4. A More Readable Type Lambda Trick by Dan Rosen
application

Opinions expressed by DZone contributors are their own.

Related

  • MuleSoft: Tactical and Strategical Role of an Application Template
  • A Better Web3 Experience: Account Abstraction From Flow (Part 1)
  • Common Problems in React Native Apps on iOS Platform: Solutions and Tips
  • How To Verify Database Connection From a Spring Boot Application

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: