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 6075741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:31:41+00:00 2026-05-23T10:31:41+00:00

Is it possible to emulate a function with your own data type with some

  • 0

Is it possible to emulate a function with your own data type with some GHC extension? What I want to do is e.g.

(imaginary syntax)

data MyFunc = MyFunc String (Int->Int)

instance (Int->Int) MyFunc where
    ($) (MyFunc _ f) i = f i

inc = MyFunc "increment" (1+)

test = inc 1

I.e. data that carries some meta-information with it and can be pattern matched, but which can still be called like a regular function. Now, I know that I could define my own infix operator like $$ and call inc $$ 1, but being able to use the regular function call syntax would be very useful in embedded DSLs.

  • 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-23T10:31:42+00:00Added an answer on May 23, 2026 at 10:31 am

    Yes, it can be done to a limited extent.

    But first we’ll need

    {-# LANGUAGE Rank2Types #-}
    

    Let’s define

    data M a b = M { name :: Int -> String -> String, eval :: a -> b }
    

    I’m adding more structure to your names so I can get nicer show support. 😉

    Then lets define a class:

    class Magic m where
        magic :: M a b -> m a b
    
    instance Magic M where
        magic = id
    
    instance Magic (->) where
        magic (M _ f) = f
    

    Now, consider the type:

    type MyFunc a b = forall m. Magic m => m a b
    

    The result type of magic is either (a -> b) or a M a b.

    So it can be used as a member of MyFunc. Now, this type is somewhat unsatisfying, because you can’t make instances dispatch on it, but it does mean that

    inc :: MyFunc Int Int
    inc = magic (M (const (showString "inc")) (+1))
    
    test :: Int
    test = inc 1
    

    works just fine.

    We can even make a rather nice way to show them. Even though we can’t use show on MyFunc, we can define it for M.

    instance Show (M a b) where
        showsPrec d (M s _) = s d
    

    Then we can make a function we can apply to M a b (and by extension any MyFunc) to get out an M a b.

    m :: M a b -> M a b
    m = id
    

    and we can define a special combinator to show MyFuncs:

    showM :: MyFunc a b -> String
    showM f = show (m f)
    

    Then we can play. We can define compositions of MyFuncs.

    infixr 9 .#
    (.#) :: MyFunc b c -> MyFunc a b -> MyFunc a c
    f .# g = magic (M 
        (\d -> showParen (d > 9) $ showsPrec 10 (m f) . 
                                   showString " . " . 
                                   showsPrec 9 (m g)) 
        (f . g))
    
    inc2 :: MyFunc Int Int
    inc2 = inc .# inc
    
    test2 :: Int
    test2 = inc2 1
    
    bar, baz :: String
    bar = showM inc
    baz = showM inc2
    

    And because I gave enough structure to the names, we even get correct parenthesization for more complicated compositions, without needless parentheses.

    *Main> showM $ inc2 .# inc
    "(inc . inc) . inc"
    
    *Main> showM $ inc .# inc2
    "inc . inc . inc"
    

    But remember, you won’t be able to define any instances for MyFunc, since it can only be a type, and not a newtype. In order to define instances you’ll have to define them on M, and then use m to convert to that type so that implicit dispatch has a type to grab onto.

    Because of the rank 2 type, if you use these heavily in local contexts, you may also want to turn on NoMonoLocalBinds and/or NoMonomorphismRestriction.

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

Sidebar

Related Questions

Possible Duplicate: Emulate Samsung Galaxy Tab I want to check my application on a
I want to know if it is possible to emulate work: Mobile Phone (sms,
Is it possible to emulate a device sleeping and waking using the Microsoft-supplied device
Possible Duplicate: JavaScript: var functionName = function() {} vs function functionName() {} What's the
I was wondering if it's possible to emulate a big-endian behavior, for testing purpose?
How does UIGestureRecognizer work internally? Is it possible to emulate it in iOS <
I'm going to be as specific and verbose as possible and include some of
It is possible to emulate vim modes, macros and bindings in textmate?
Is it possible to emulate the behavior of 'git stash' when using fossil/bzr? Basically
I am aware that it's possible to emulate the maxlength property from input elements

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.