Haskell Logical Constant Expression

Data structures have mutating and non-mutating operations. For example, inserting a dictionary may change the state of its underlying data structure, but searches are usually not performed.

Some data structures mutate their internal structure - even with logically non-mutating operations - but in a way that does not change the observed state. For example, a splay tree moves items towards the root when searching, and move -to-front list moves items in the search direction. It is logical that the set of keys does not change.

In C ++, this can be conveyed by defining a data structure that has methods constbut mutabledata members. Is there a way to do this in Haskell? The only thing I can come up with is

setContains :: Set k -> k -> (Set k, Bool)

but this is ugly as the underlying data structure changes the interface.

+4
source share
1 answer

This low-level optimization cannot be achieved without using unsafe primitives, therefore, to mutate data structures from pure code.

First of all, note that in the GHC runtime, clean code modifies data structures - evaluating them. For instance.

x = (3+2, 4+5)
main = print (fst x) >>> print (fst x)

GHC print x (5, 4+5), . , print . , x, , "" .

, , . - .

, Data.Array.Diff. (!). , , . , () " ", . , : , . ; , .

MVar , , OP .

, Haskell. , , Haskell.

+6

All Articles