Is it ever possible to detect sharing in Haskell?

后端 未结 3 1974
说谎
说谎 2020-12-03 17:56

In Scheme, the primitive eq? tests whether its arguments are the same object. For example, in the following list

(define lst
  (let (x (list \'a         


        
相关标签:
3条回答
  • 2020-12-03 18:10

    There is reallyUnsafePtrEquality#. Also see here

    0 讨论(0)
  • 2020-12-03 18:28

    There's lots of approaches.

    1. Generate unique IDs and stick everything in a finite map (e.g. IntMap).
    2. The refined version of the last choice is to make an explicit graph, e.g. using fgl.
    3. Use stable names.
    4. Use IORefs (see also), which have both Eq and Ord instances regardless of the contained type.
    5. There are libraries for observable sharing.
    6. As mentioned above, there is reallyUnsafePtrEquality# but you should understand what's really unsafe about it before you use it!

    See also this answer about avoiding equality checks altogether.

    0 讨论(0)
  • 2020-12-03 18:33

    It is not possible in Haskell, the pure language.

    But in its implementation in GHC, there are loopholes, such as

    • the use of reallyUnsafePtrEquality# or
    • introspection libraries like ghc-heap-view.

    In any case, using this in regular code would be very unidiomatic; at most I could imagine that building a highly specialized library for something (memoizatoin, hash tables, whatever) that then provides a sane, pure API, might be acceptable.

    0 讨论(0)
提交回复
热议问题