[Haskell-cafe] evaluate arbitrary code + mutable variables

Mike Ledger eleventynine at gmail.com
Thu May 14 05:57:28 UTC 2015


It's maybe a bit of a hack, but you can just shadow names in GHCi. (You
might want to :set -fno-warn-name-shadowing so you aren't harassed with
warnings).
e.g.
>>> let x = 423
>>> let y = x + 123
>>> let x = y + 54

but this breaks down if you try something like
>>> let x = x + 1
expecting it to increment x (it'll just recurse infinitely)

For actual variables, probably just use an IORef, or venture into state
monads (and use :{ and :} to enter multi-line expressions in ghci)

Cheers,
Mike

On Wed, May 13, 2015 at 10:02 AM, Jeffrey Brown <jeffbrown.the at gmail.com>
wrote:

> Half solved!
>
> David Gladstein pointed out that GHCI's "it" variable can at least
> sometimes solve the problem:
>
>   Prelude System.IO> let x = 3
>   Prelude System.IO> x
>   3
>   Prelude System.IO> let x = it + 1
>   Prelude System.IO> x
>   4
>
> Then I discovered that in GHCI one can bind monadically:
>
>   Prelude> x <- return 3
>   Prelude> x <- return $ x + 1
>   Prelude> x
>   4
>
>   Prelude> x <- getLine
>   This line was user input, not computer output.
>   Prelude> x
>   "This line was user input, not computer output."
>   Prelude>
>
> I don't remember seeing anyone demonstrate it; perhaps it is deprecated.
>
> I would still very much like to know whether and if so how it is possible
> to let the user evaluate arbitrary haskell when running compiled code.
>
> On Tue, May 12, 2015 at 1:19 PM, Jeffrey Brown <jeffbrown.the at gmail.com>
> wrote:
>
>> I wrote a graph-like data type, with some functions for adding statements
>> and relationships between statements. (It's different from a graph in that
>> there's no edge/vertex distinction; there are only statements, but a
>> statement might refer to other statements.)
>>
>> Currently I'm using them in GHCI. Because it does not allow one to change
>> a variable, I keep having to do things like this:
>>   let g0 = emptyDocument
>>   let s1 = newStatement ...
>>   let g1 = addStatement s1 g0
>>   let s2 = newStatement ...
>>   let g2 = addStatement s2 g1
>>   ...
>> If I wrote a standalone application, I could use mutable variables, so I
>> would not have to define a new object every time I want to modify an
>> existing one. However I like being able to type in arbitrary code into GHCI.
>>
>> Can one have both of those at once? That is, could I either (1) use MVars
>> from within GHCI, or (2) write a standalone app that lets the user evaluate
>> arbitrary Haskell?
>>
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20150514/d3beba12/attachment.html>


More information about the Haskell-Cafe mailing list