purescript / purescript-st

The ST effect, for safe local mutation
BSD 3-Clause "New" or "Revised" License
25 stars 25 forks source link

Global region #7

Closed rightfold closed 6 years ago

rightfold commented 7 years ago

If this library adds a named region:

foreign import data Global :: *

Then you can use st :: ST Global as the effect of global mutations. For example:

main :: forall eff. Eff (st :: ST Global | eff) Unit
main = ...

Which would eliminate the need for REF. You could use existing local ST computations (forall r e. Eff (st :: ST r | e)) without runST if you already have an st :: ST Global effect, so I think there would be no issue with duplicate labels.

hdgarrood commented 7 years ago

Most of the time, I think you want to use runST though, in order to have the compiler verify that mutation can't happen outside some particular scope.

rightfold commented 7 years ago

Right, as long as you don't do both global and local mutations in a computation, you can still use runST to cast away the local ST region, and then use row polymorphism to add the global ST region.

hdgarrood commented 7 years ago

Ok, good point. Maybe it's useful that STRef and Ref are different types, though, because then you can signal whether you expect to restrict the mutation to some local scope or not? That might not be clear from the rows, since you could universally quantify over the parameter to the ST effect. Although maybe that's a good thing, because then you could write code that works in both situations.

rightfold commented 7 years ago

@hdgarrood Is this still an issue now psc supports duplicate labels? Can you have multiple st effects that work as expected?

rightfold commented 6 years ago

We no longer use effect rows, so I'll close this issue as the situation is now different.