Is there an automatic way to memoise global polymorphic values in Haskell?

前端 未结 3 2157
野的像风
野的像风 2021-02-14 15:51

Polymorphic \"constants\", like 5 :: Num a => a, aren\'t really constants but functions of a dictionary argument. Hence, if you define

primes ::          


        
3条回答
  •  死守一世寂寞
    2021-02-14 16:28

    One could add Typeable constraint to n and use a different memoization table for every ground type n. You probably would need to exploit Dynamic and cast a lot for this, which is suboptimal. It also feels a bit hackish, too.

    In a dependently typed language, of course, one can model a map (n : Num) -> [n] which would not require the castss from Dynamic. Maybe something like that can be simulated exploiting GADTs and some kind of reification machinery.

提交回复
热议问题