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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T14:22:31+00:00 2026-05-21T14:22:31+00:00

Generic programming time! If I have a function: f :: a1 -> a2 ->

  • 0

Generic programming time!

If I have a function:

f :: a1 -> a2 -> a3 -> ... -> an

and a value

v :: aX   -- where 1 <= x < n

Without knowing at compile time which of the arguments of f the value v is the right type for (if any), can I partially apply f to v? (using Typeable, Data, TH, or any other trick)

Slightly more solidly, can I construct the function g (below) at run-time? It doesn’t actually have to be polymorphic, all my types will be monomorphic!

g :: (a1 -> a2 -> a3 -> a4 -> a5) -> a3 -> (a1 -> a2 -> a4 -> a5)
g f v = \x y z -> f x y v z

I know that, using Typeable (typeRepArgs specifically), v is the 3rd argument of f, but that doesn’t mean I have a way to partially apply f.

My code would probably look like:

import Data.Typeable

data Box = forall a. Box (TyRep, a)

mkBox :: Typeable a => a -> Box
mkBox = (typeOf a, a)

g :: Box -> Box -> [Box]
g (Box (ft,f)) (Box (vt,v)) = 
    let argNums = [n | n <- [1..nrArgs], isNthArg n vt ft]
    in map (mkBox . magicApplyFunction f v) argNums

isNthArg :: Int -> TyRep -> TyRep -> Bool
isNthArg n arg func = Just arg == lookup n (zip [1..] (typeRepArgs func))

nrArgs :: TyRep -> Int
nrArgs = (\x -> x - 1) . length . typeRepArgs

Is there anything that can implement the magicApplyFunction?

EDIT: I finally got back to playing with this. The magic apply function is:

buildFunc :: f -> x -> Int -> g
buildFunc f x 0 = unsafeCoerce f x
buildFunc f x i =
        let !res = \y -> (buildFunc (unsafeCoerce f y) x (i-1))
        in unsafeCoerce res
  • 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-21T14:22:32+00:00Added an answer on May 21, 2026 at 2:22 pm

    I’m not going to write the whole solution here for now, but I’m sure this can be done purely with Data.Dynamic and Typeable. The source for dynApply and funResultTy should provide the key elements:

    dynApply :: Dynamic -> Dynamic -> Maybe Dynamic
    dynApply (Dynamic t1 f) (Dynamic t2 x) =
      case funResultTy t1 t2 of
        Just t3 -> Just (Dynamic t3 ((unsafeCoerce f) x))
        Nothing -> Nothing
    
    
    funResultTy :: TypeRep -> TypeRep -> Maybe TypeRep
    funResultTy trFun trArg
      = case splitTyConApp trFun of
          (tc, [t1,t2]) | tc == funTc && t1 == trArg -> Just t2
          _ -> Nothing
    

    To keep things simple, I’d have type Box = (Dynamic, [Either TypeRep Dynamic]). The latter starts out as a list of typereps of arguments. magicApply would look for the first matching TypeRep in the box and substitute the Dynamic of the value. Then you could have an extract that given a Box to which all arguments have been magicapplied, actually performs the dynApply calls to produce the resulting dynamic result.

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

Sidebar

Related Questions

I have a generic class that should allow any type, primitive or otherwise. The
From Wikipedia : Generic programming is a style of computer programming in which algorithms
I have a generic class that I'm trying to implement implicit type casting for.
Coming from C++, I find generic programming indispensable. I wonder how people approach that
any generic way to trace/log values of all local variables when an exception occurs
I have a generic Repository<T> class I want to use with an ObjectDataSource. Repository<T>
I've got a generic<> function that takes a linq query ('items') and enumerates through
Is there a generic way, without creating and managing your own CLR host, to
Java has generics and C++ provides a very strong programming model with template s.
There must be a generic way to transform some hierachical XML such as: <element1

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.