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

前端 未结 3 2166
野的像风
野的像风 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:46

    It's fairly impossible for a fairly technical reasons. Type classes are open so, the polymorphic constant can't at compile time necessarily "see" how many types satisfy the constraint so it can't allocate that many monomorphic thunks. On the other side, a type class certainly can't see all the possible constants that it might generate, so the monomorphic thunks cannot be allocated in the type class dictionary.

    You will have to explicitly mention any types at which you want a monomorphic thunk allocated.

提交回复
热议问题