Cannot create apply function with static language?

前端 未结 12 872
梦谈多话
梦谈多话 2021-02-04 02:09

I have read that with a statically typed language like Scala or Haskell there is no way to create or provide a Lisp apply function:

(apply #\'+ (lis         


        
12条回答
  •  囚心锁ツ
    2021-02-04 02:36

    In Haskell, there is no datatype for multi-types lists, although I believe, that you can hack something like this together whith the mysterious Typeable typeclass. As I see, you're looking for a function, which takes a function, a which contains exactly the same amount of values as needed by the function and returns the result.

    For me, this looks very familiar to haskells uncurryfunction, just that it takes a tuple instead of a list. The difference is, that a tuple has always the same count of elements (so (1,2) and (1,2,3) are of different types (!)) and there contents can be arbitrary typed.

    The uncurry function has this definition:

    uncurry :: (a -> b -> c) -> (a,b) -> c
    uncurry f (a,b) = f a b
    

    What you need is some kind of uncurry which is overloaded in a way to provide an arbitrary number of params. I think of something like this:

    {-# LANGUAGE MultiParamTypeClasses #-}
    {-# LANGUAGE FlexibleInstances #-}
    {-# LANGUAGE UndecidableInstances #-}
    
    class MyApply f t r where
      myApply :: f -> t -> r
    
    instance MyApply (a -> b -> c) (a,b) c where
      myApply f (a,b) = f a b
    
    instance MyApply (a -> b -> c -> d) (a,b,c) d where
      myApply f (a,b,c) = f a b c
    
    -- and so on
    

    But this only works, if ALL types involved are known to the compiler. Sadly, adding a fundep causes the compiler to refuse compilation. As I'm not a haskell guru, maybe domeone else knows, howto fix this. Sadly, I don't know how to archieve this easier.

    Résumee: apply is not very easy in Haskell, although possible. I guess, you'll never need it.

    Edit I have a better idea now, give me ten minutes and I present you something whithout these problems.

提交回复
热议问题