unsafeDestructiveAssign?

Does anybody know if there is some unsafe IO function that would let me do destructive assignment? Something like: a = 5 main = do veryUnsafeAndYouShouldNeverEveryCallThisFunction_DestructiveAssign a 8 print a
8
and yes I am in fact insane... I'm also looking for a way to make actual copies of data. so I could do something like this: a = Node 5 [Node 2 [], Node 5 [a]] main = do b <- makeCopy a veryUnsafeAndYouShouldNeverEveryCallThisFunction_DestructiveAssign b (Node 0 []) -- 'a' is unchanged It would be even more fantastic, if the copy function was lazy. I think the traverse function might actually make a copy, but I would be happier with something more general (doesn't require membership in traversable), and more explicit (was actually designed for making real copies). Any ideas? Thanks, - Job

In both of your examples, a simple let would allow you to rebind the
name (not the same as assigning, but it didn't look like you need that
in those examples at least).
Note that you're already in IO, so you can use an IORef if you want a
true mutable variable, or an STRef in another function if you want to
maintain a pure interface.
But I'd strongly encourage you to look for alternate options here.
Just because it's possible to use mutable variables in Haskell doesn't
mean it's always a good idea to do so. Especially if you're new to the
language, I'd suggest doing your best to avoid approaches from other
languages.
But in general, if something "looks" pure (like your "variables" in
your email), you may be able to secretly mutate it behind the scenes,
but the compiler may not even notice. One advantage of purity is that
the compiler knows for sure that something won't change if you read it
multiple times. If you go changing its value (assuming you find some
hack that even allows it), you'll break many fundamental assumptions.
Hope this helps!
Dan
On Tue, Aug 11, 2009 at 11:48 AM, Job Vranish
Does anybody know if there is some unsafe IO function that would let me do destructive assignment? Something like:
a = 5 main = do veryUnsafeAndYouShouldNeverEveryCallThisFunction_DestructiveAssign a 8 print a
8
and yes I am in fact insane...
I'm also looking for a way to make actual copies of data. so I could do something like this:
a = Node 5 [Node 2 [], Node 5 [a]] main = do b <- makeCopy a veryUnsafeAndYouShouldNeverEveryCallThisFunction_DestructiveAssign b (Node 0 []) -- 'a' is unchanged
It would be even more fantastic, if the copy function was lazy. I think the traverse function might actually make a copy, but I would be happier with something more general (doesn't require membership in traversable), and more explicit (was actually designed for making real copies).
Any ideas?
Thanks,
- Job
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Job Vranish
Does anybody know if there is some unsafe IO function that would let me do destructive assignment? Something like:
a = 5 main = do veryUnsafeAndYouShouldNeverEveryCallThisFunction_DestructiveAssign a 8 print a
8
Aren't StateT or IORefs the exact thing you are looking for?
I'm also looking for a way to make actual copies of data. so I could do something like this:
a = Node 5 [Node 2 [], Node 5 [a]] main = do b <- makeCopy a veryUnsafeAndYouShouldNeverEveryCallThisFunction_DestructiveAssign b (Node 0 []) -- 'a' is unchanged
It would be even more fantastic, if the copy function was lazy. I think the traverse function might actually make a copy, but I would be happier with something more general (doesn't require membership in traversable), and more explicit (was actually designed for making real copies).
Same thing, IORefs could help you. Anyway, I can't imagine any case where veryUnsafeAndYouShouldNeverEveryCallThisFunction_DestructiveAssign could be useful with its imperative semantics as you've described. The point is that Haskell is pure language and I use it because of this feature (not only because of this, to be exact). I don't want to use any library code that brokes pure semantics and launches nuclear bombs behind the IO monad. GHC is smart enough these days to do all optimised destructive assignments, copies and all that imperative stuff and there are plenty of other ways to get a performance boost without unsafeHorribleThings.

unsafeHorribleThings
I think we have a name for this new feature.
I can see how something like this would be useful to some areas. There
are many cases (graphs!) where it's just easier to build up structures
destructively, seal them, and return them to happyPureFunTimesLand.
Even if GHC does do just as good of a job as performing the unsafe
button pushing our selves, it would be an interesting exercise.
On Tue, Aug 11, 2009 at 12:14 PM, Max
Desyatov
Job Vranish
writes: Does anybody know if there is some unsafe IO function that would let me do destructive assignment? Something like:
a = 5 main = do veryUnsafeAndYouShouldNeverEveryCallThisFunction_DestructiveAssign a 8 print a
8
Aren't StateT or IORefs the exact thing you are looking for?
I'm also looking for a way to make actual copies of data. so I could do something like this:
a = Node 5 [Node 2 [], Node 5 [a]] main = do b <- makeCopy a veryUnsafeAndYouShouldNeverEveryCallThisFunction_DestructiveAssign b (Node 0 []) -- 'a' is unchanged
It would be even more fantastic, if the copy function was lazy. I think the traverse function might actually make a copy, but I would be happier with something more general (doesn't require membership in traversable), and more explicit (was actually designed for making real copies).
Same thing, IORefs could help you. Anyway, I can't imagine any case where veryUnsafeAndYouShouldNeverEveryCallThisFunction_DestructiveAssign could be useful with its imperative semantics as you've described. The point is that Haskell is pure language and I use it because of this feature (not only because of this, to be exact). I don't want to use any library code that brokes pure semantics and launches nuclear bombs behind the IO monad. GHC is smart enough these days to do all optimised destructive assignments, copies and all that imperative stuff and there are plenty of other ways to get a performance boost without unsafeHorribleThings. _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Hello John, Tuesday, August 11, 2009, 8:23:26 PM, you wrote:
I can see how something like this would be useful to some areas. There are many cases (graphs!) where it's just easier to build up structures destructively, seal them, and return them to happyPureFunTimesLand.
it's ST monad. btw, many array algos from std library (histogram, for example) are written this way -- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com

Ga! Before to many people start flooding me responses of "This is really
dumb idea don't do it!" I would like to clarify that for the most part
IKnowWhatI'mDoing(TM)
I am well aware of the usual ST/IORefs as the usual solutions to data
mutability in haskell.
I very very much understand purity, and why it is a good thing, and why we
should try to stay away from IO and ST as much as possible.
I am very much away that even if I had such a function that it will probably
break everything.
I am not just trying to make things run faster.
What I am trying to do is hyper unusual and I really do need an
unsafeHorribleThings to do it.
- Job
On Tue, Aug 11, 2009 at 12:14 PM, Max Desyatov
Job Vranish
writes: Does anybody know if there is some unsafe IO function that would let me do destructive assignment? Something like:
a = 5 main = do veryUnsafeAndYouShouldNeverEveryCallThisFunction_DestructiveAssign a 8 print a
8
Aren't StateT or IORefs the exact thing you are looking for?
I'm also looking for a way to make actual copies of data. so I could do something like this:
a = Node 5 [Node 2 [], Node 5 [a]] main = do b <- makeCopy a veryUnsafeAndYouShouldNeverEveryCallThisFunction_DestructiveAssign b (Node 0 []) -- 'a' is unchanged
It would be even more fantastic, if the copy function was lazy. I think the traverse function might actually make a copy, but I would be happier with something more general (doesn't require membership in traversable), and more explicit (was actually designed for making real copies).
Same thing, IORefs could help you. Anyway, I can't imagine any case where veryUnsafeAndYouShouldNeverEveryCallThisFunction_DestructiveAssign could be useful with its imperative semantics as you've described. The point is that Haskell is pure language and I use it because of this feature (not only because of this, to be exact). I don't want to use any library code that brokes pure semantics and launches nuclear bombs behind the IO monad. GHC is smart enough these days to do all optimised destructive assignments, copies and all that imperative stuff and there are plenty of other ways to get a performance boost without unsafeHorribleThings. _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On Tue, Aug 11, 2009 at 12:45:29PM -0400, Job Vranish wrote:
What I am trying to do is hyper unusual and I really do need an unsafeHorribleThings to do it.
I guess these unsafeHorribleThings are impossible, you're trying to subvert the very core of the language. The only thing that comes close to that probably is IORefs + unsafePerformIO, but it seems this isn't what you want. -- Felipe.

Job Vranish
I am well aware of the usual ST/IORefs as the usual solutions to data mutability in haskell. I very very much understand purity, and why it is a good thing, and why we should try to stay away from IO and ST as much as possible. I am very much away that even if I had such a function that it will probably break everything. I am not just trying to make things run faster.
What I am trying to do is hyper unusual and I really do need an unsafeHorribleThings to do it.
Well, is purpose of this a big secret? Maybe if you describe the whole direction of such efforts, somebody could propose better solution that satisfies your needs. I recommend you to describe semantics of unsafeHorribleThings in details, and in what exact cases this could be useful.

Hello Job, Tuesday, August 11, 2009, 7:48:45 PM, you wrote:
I'm also looking for a way to make actual copies of data. so I could do something like this:
i think you are just going non-FP way. haskell is great for declaring
algorithms in pure mathematical way - as a function projecting input
values into results. "updating" is just word from another world
just for example - little function that joins two ordered lists
returning list-in-order. note that it doesn't describe any updates, it
describes how to build result from input values:
merge xs [] = xs
merge [] ys = ys
merge (x:xs) (y:ys) | x

No, there's no way you can change the value of 'a' after the fact.
Since 'a' is a constant the compiler has most likely inlined it
wherever it has been used, the done constant folding etc.
If you need an updateable variable, you need to tell the compiler,
otherwise it will assume your code is pure.
-- Lennart
On Tue, Aug 11, 2009 at 5:48 PM, Job Vranish
Does anybody know if there is some unsafe IO function that would let me do destructive assignment? Something like:
a = 5 main = do veryUnsafeAndYouShouldNeverEveryCallThisFunction_DestructiveAssign a 8 print a
8
and yes I am in fact insane...
I'm also looking for a way to make actual copies of data. so I could do something like this:
a = Node 5 [Node 2 [], Node 5 [a]] main = do b <- makeCopy a veryUnsafeAndYouShouldNeverEveryCallThisFunction_DestructiveAssign b (Node 0 []) -- 'a' is unchanged
It would be even more fantastic, if the copy function was lazy. I think the traverse function might actually make a copy, but I would be happier with something more general (doesn't require membership in traversable), and more explicit (was actually designed for making real copies).
Any ideas?
Thanks,
- Job
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

2009/8/12 Job Vranish
Does anybody know if there is some unsafe IO function that would let me do destructive assignment? Something like:
a = 5 main = do veryUnsafeAndYouShouldNeverEveryCallThisFunction_DestructiveAssign a 8 print a
8
I doubt you will be able to achieve that in Haskell, but there is another language which does support what you want which is very close to Haskell. It is called Disciple, and there is a compiler for it called DDC: http://www.haskell.org/haskellwiki/DDC DDC is rather young, so it depends on what you are doing as to whether it will fulfil all your needs. Cheers, Bernie.

Yeah I'm thinking that even if I had such a function, it might be impossible
to make it play nicely. I think I can do a restricted form of what I want
with STRefs and StableNames (still dangerous) so I think I'll stick with
that at least while experimenting in haskell.
DCC looks extremely interesting! In fact it looks like I can do what I'm
wanting out of the box!
I will definitely have to experiment with it :)
Thanks,
On Wed, Aug 12, 2009 at 12:46 AM, Bernie Pope
2009/8/12 Job Vranish
: Does anybody know if there is some unsafe IO function that would let me do destructive assignment? Something like:
a = 5 main = do veryUnsafeAndYouShouldNeverEveryCallThisFunction_DestructiveAssign a 8 print a
8
I doubt you will be able to achieve that in Haskell, but there is another language which does support what you want which is very close to Haskell. It is called Disciple, and there is a compiler for it called DDC:
http://www.haskell.org/haskellwiki/DDC
DDC is rather young, so it depends on what you are doing as to whether it will fulfil all your needs.
Cheers, Bernie.

Job Vranish wrote:
Does anybody know if there is some unsafe IO function that would let me do destructive assignment? Something like:
a = 5 main = do veryUnsafeAndYouShouldNeverEveryCallThisFunction_DestructiveAssign a 8 print a
8
Untested, just guessing: {-# NOILINE aRef , a #-} aRef = unsafePerformIO (newRef 0) a = unsafePerformIO aRef main = do use a -- 0 if you are lucky writeRef aRef 5 use a -- 5 if you are lucky However, I would not in the least be surprised if your program stops working whenever your cat purrs. And yes, I find the above "code" quite disgusting. Zun.
participants (10)
-
Bernie Pope
-
Bulat Ziganshin
-
Daniel Peebles
-
Felipe Lessa
-
Jake McArthur
-
Job Vranish
-
John Van Enk
-
Lennart Augustsson
-
Max Desyatov
-
Roberto Zunino