Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7075921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:14:09+00:00 2026-05-28T06:14:09+00:00

I’m having a terrible time trying to hook up functions at runtime (which I

  • 0

I’m having a terrible time trying to hook up functions at runtime (which I have to do) which might involve type class constraints in the inputs and outputs of the function.

In a language like Java, this would be trivial. f1 :: Int -> Num, f2 :: Num -> Num We can now call f2 . f1. If Num was a Java-style interface, that would work no problem. But type classes don’t behave like interfaces.

One thing a type class allows you to do is avoid converting between data types. We could just convert back and forth all over and get rid of type classes. But the concern is then we’re creating objects all over for no reason. Let’s try to avoid that.

I started out trying to create a tuple of (a, Api a) where Api is a record of functions that will operate on a. My guess is that’s probably very similar to how type classes work? But then you run into concrete type issue and I thought existentials. But then I realized that (a, Api a) should be able to hide the a completely since noone cares, and then the Api turns into a plain record of data types, not functions.

And so I’m left to wonder… is it laziness that solves this?

module Main where

--data Api a = Api { f1 :: a -> Int, f2 :: a -> String }
data Api = Api { f1 :: Int, f2 :: String }

data MyData = MyData Int String

myf1 (MyData x y) = x
myf2 (MyData x y) = y
myApi x = Api (myf1 x) (myf2 x)

from :: Int -> Api
from x = myApi $ MyData x "string"

to :: Api -> String
to api = f2 api

main = print $ to . from $ 5

So, is it smart enough (or could be) to realize that it doesn’t need to create an Api value at all since all we need is the call into myf2 on the MyData value?

So the “equivalent” of a Java interface is not a type class as someone once told me, but rather it’s a record or data type? And that laziness provides the “lightweightness” of the interface?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T06:14:10+00:00Added an answer on May 28, 2026 at 6:14 am

    But then I realized that (a, Api a) should be able to hide the a completely since noone cares, and then the Api turns into a plain record of data types, not functions.

    Exactly! Using an existential like that when a plain data-type would do is called the existential typeclass antipattern (see also the FAQ). It’s not directly related to laziness, however; you could just as easily represent every field as () -> Result in a strict language. Of course, it wouldn’t be nearly as nice to use.

    The advantage of a type-class is in the implicit, type-directed resolution: you can just use operations directly, as if they were monomorphic on the specific types you’re using, and it works fine, without having to come up with a separate name for every type you want to implement an operation for. For an example of why type-classes are valuable, imagine if Num was implemented as a record; you’d have to pass around the implementation record for each type everywhere. Worse, there’s no place to put fromInteger :: (Num a) => Integer -> a, since it’s polymorphic in the result, and doesn’t take any value of type a at all!

    Another example is something like Ord. You can’t represent Ord as a record, because any instance of Ord has to operate on the very values that are eliminated by this type-class-to-record transformation. Having Ord as a type-class also lets us write code that is generic on any type of value that can be ordered, which is very valuable indeed, and in this respect the use of type-classes certainly resembles OOP interfaces to some degree.

    However, when there’s no real relevant values to speak of, or they all come from “outside” just to act as internal state (like an existential), a type-class just adds unnecessary boilerplate and is much less “first-class”. Functions and data-types are the real units of abstraction in Haskell; type-classes are just convenience.

    So, is it smart enough (or could be) to realize that it doesn’t need to create an Api value at all since all we need is the call into myf2 on the MyData value?

    GHC probably will create the intermediate data type, but you shouldn’t worry too much; an existential also carries around the type-class instance dictionary, which is just like your Api type. The advantage of this record interface isn’t really performance, but simplicity, clarity and composability (it’s easy to transform all the fields of a record to produce a “modified” implementation — but you can’t have a function that transforms a type-class instance).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I have a text area in my form which accepts all possible characters from
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to loop through a bunch of documents I have to put
I have just tried to save a simple *.rtf file with some websites and
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.