putting symbols into Environment

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

putting symbols into Environment

Sonny To
I'm trying to use the TelnetRepl in an android Service but cannot
figure out how to put bindings into the environment


import android.app.Service
import android.content.Intent
import android.os.Binder
import android.os.IBinder
import android.util.Log
import gnu.expr.Language
import gnu.mapping.Environment
import gnu.mapping.Symbol
import kawa.TelnetRepl

/**
 * Created by sto on 9/12/17.
 */

class ReplService : Service() {
    val TAG = "ReplService"
    override fun onCreate() {
        Thread(Runnable {
            Log.e(TAG, "repl")
            try {
                val ssocket = java.net.ServerSocket(9999)
                val port = ssocket.localPort
                val lang = Language.getInstance(null as String?)
                Language.setDefaults(lang)
                Log.e(TAG, "repl listening on port " + port)

                while (true) {
                    Log.e(TAG, "waiting ... ")
                    val client = ssocket.accept()
                    Log.e(TAG, "got connection from "
                            + client.inetAddress
                            + " port:" + client.port)
                    TelnetRepl.serve(lang, client)
                    val env = Environment.getCurrent()
                    env.put(Symbol.makeUninterned("context", null),
applicationContext)
                }
            } catch (ex: java.io.IOException) {
                Log.e(TAG, ex.toString())
                throw Error(ex.toString())
            }
        }).start()
    }

    override fun onBind(i: Intent): IBinder {
        return Binder();
    }
}

I can telnet to it but the "context" symbol is not available

#|kawa:4|# context
/dev/stdin:4:1: unbound location: context
at gnu.expr.ReferenceExp.apply(ReferenceExp.java:163)
at gnu.expr.ModuleExp.evalModule2(ModuleExp.java:281)
at gnu.expr.ModuleExp.evalModule(ModuleExp.java:211)
at kawa.Shell.run(Shell.java:283)
at kawa.Shell.run(Shell.java:196)
at kawa.Shell.run(Shell.java:183)
at kawa.TelnetRepl.apply0(TelnetRepl.java:25)
at gnu.mapping.RunnableClosure.run(RunnableClosure.java:75)
at java.lang.Thread.run(Thread.java:764)

Do I have the wrong environment? if so how to get the correct environment?

thanks,
Sonny
Reply | Threaded
Open this post in threaded view
|

Re: putting symbols into Environment

Per Bothner
On 09/13/2017 05:05 PM, Sonny To wrote:
> I'm trying to use the TelnetRepl in an android Service but cannot
> figure out how to put bindings into the environment

>                      env.put(Symbol.makeUninterned("context", null),
> applicationContext)

> I can telnet to it but the "context" symbol is not available
>
> #|kawa:4|# context
> /dev/stdin:4:1: unbound location: context

That would be expected if you use Symbol.makeUninterned.
An uninterned symbol is a unique object that you can *not*
"lookup".  For example (string->symbol "context") is the
same as 'symbol - but (Symbol:makeUninterned "context") is a completely
different object.

Instead, try Symbol.valueOf("context").

> Do I have the wrong environment? if so how to get the correct environment?

There might be other problems, but fix the above-mentioned problem first.
--
        --Per Bothner
[hidden email]   http://per.bothner.com/
Reply | Threaded
Open this post in threaded view
|

Re: putting symbols into Environment

Sonny To
How close does kawa stick to standard scheme? I'm trying these
functions described here
https://www.gnu.org/software/mit-scheme/documentation/mit-scheme-ref/Environment-Operations.html
and none of work

#|kawa:5|# (define e (environment))
#|kawa:6|# (environment-bindings e)
/dev/stdin:6:2: unbound location: environment-bindings
at gnu.expr.ReferenceExp.apply(ReferenceExp.java:163)
at gnu.expr.Expression.applyMethodExpression(Expression.java:41)
at java.lang.reflect.Method.invoke(Native Method)
at gnu.mapping.CallContext$ReflectMethodHandle.invokeExact(CallContext.java:726)
at gnu.mapping.CallContext.runUntilDone(CallContext.java:586)
at gnu.mapping.CallContext.getFromContext(CallContext.java:616)
at gnu.expr.Expression.eval(Expression.java:52)
at gnu.expr.ApplyExp.apply(ApplyExp.java:161)
at gnu.expr.ModuleExp.evalModule2(ModuleExp.java:281)
at gnu.expr.ModuleExp.evalModule(ModuleExp.java:211)
at kawa.Shell.run(Shell.java:283)
at kawa.Shell.run(Shell.java:196)
at kawa.Shell.run(Shell.java:183)
at kawa.TelnetRepl.apply0(TelnetRepl.java:25)
at gnu.mapping.RunnableClosure.run(RunnableClosure.java:75)
at java.lang.Thread.run(Thread.java:764)
#|kawa:7|# (environment-bound-names e)
/dev/stdin:7:2: unbound location: environment-bound-names
at gnu.expr.ReferenceExp.apply(ReferenceExp.java:163)
at gnu.expr.Expression.applyMethodExpression(Expression.java:41)
at java.lang.reflect.Method.invoke(Native Method)
at gnu.mapping.CallContext$ReflectMethodHandle.invokeExact(CallContext.java:726)
at gnu.mapping.CallContext.runUntilDone(CallContext.java:586)
at gnu.mapping.CallContext.getFromContext(CallContext.java:616)
at gnu.expr.Expression.eval(Expression.java:52)
at gnu.expr.ApplyExp.apply(ApplyExp.java:161)
at gnu.expr.ModuleExp.evalModule2(ModuleExp.java:281)
at gnu.expr.ModuleExp.evalModule(ModuleExp.java:211)
at kawa.Shell.run(Shell.java:283)
at kawa.Shell.run(Shell.java:196)
at kawa.Shell.run(Shell.java:183)
at kawa.TelnetRepl.apply0(TelnetRepl.java:25)
at gnu.mapping.RunnableClosure.run(RunnableClosure.java:75)
at java.lang.Thread.run(Thread.java:764)

On Fri, Sep 15, 2017 at 4:48 AM, Sonny To <[hidden email]> wrote:

> Thanks Per. I was looking for a Symbol.makeIntern but didnt find one
> and used Symbol.makeUninterned. I was not expecting a Symbol.valueOf
> to make an interned symbol. perhaps either rename or add an alias
> Symbol.makeIntern?
>
> In anycase, using Symbol.valueOf still does not make the "context"
> symbol available in my repl
>
> sto@obi:~$ telnet localhost 9999
> Trying 127.0.0.1...
> Connected to localhost.
> Escape character is '^]'.
> #|kawa:1|# context
> /dev/stdin:1:1: unbound location: context
> at gnu.expr.ReferenceExp.apply(ReferenceExp.java:163)
> at gnu.expr.ModuleExp.evalModule2(ModuleExp.java:281)
> at gnu.expr.ModuleExp.evalModule(ModuleExp.java:211)
> at kawa.Shell.run(Shell.java:283)
> at kawa.Shell.run(Shell.java:196)
> at kawa.Shell.run(Shell.java:183)
> at kawa.TelnetRepl.apply0(TelnetRepl.java:25)
> at gnu.mapping.RunnableClosure.run(RunnableClosure.java:75)
> at java.lang.Thread.run(Thread.java:764)
>
> any other ideas? I suspect its because I have the wrong Environment
>
> On Wed, Sep 13, 2017 at 5:28 PM, Per Bothner <[hidden email]> wrote:
>> On 09/13/2017 05:05 PM, Sonny To wrote:
>>>
>>> I'm trying to use the TelnetRepl in an android Service but cannot
>>> figure out how to put bindings into the environment
>>
>>
>>>                      env.put(Symbol.makeUninterned("context", null),
>>> applicationContext)
>>
>>
>>> I can telnet to it but the "context" symbol is not available
>>>
>>> #|kawa:4|# context
>>> /dev/stdin:4:1: unbound location: context
>>
>>
>> That would be expected if you use Symbol.makeUninterned.
>> An uninterned symbol is a unique object that you can *not*
>> "lookup".  For example (string->symbol "context") is the
>> same as 'symbol - but (Symbol:makeUninterned "context") is a completely
>> different object.
>>
>> Instead, try Symbol.valueOf("context").
>>
>>> Do I have the wrong environment? if so how to get the correct environment?
>>
>>
>> There might be other problems, but fix the above-mentioned problem first.
>> --
>>         --Per Bothner
>> [hidden email]   http://per.bothner.com/
Reply | Threaded
Open this post in threaded view
|

Re: putting symbols into Environment

Sonny To
In reply to this post by Per Bothner
oddly the fix is to first put into the environment before creating the repl

val env = Environment.getCurrent()
env.put(Symbol.valueOf("context"), applicationContext)
TelnetRepl.serve(lang, client)

the symbol "context" is now available in the repl

On Fri, Sep 15, 2017 at 4:48 AM, Sonny To <[hidden email]> wrote:

> Thanks Per. I was looking for a Symbol.makeIntern but didnt find one
> and used Symbol.makeUninterned. I was not expecting a Symbol.valueOf
> to make an interned symbol. perhaps either rename or add an alias
> Symbol.makeIntern?
>
> In anycase, using Symbol.valueOf still does not make the "context"
> symbol available in my repl
>
> sto@obi:~$ telnet localhost 9999
> Trying 127.0.0.1...
> Connected to localhost.
> Escape character is '^]'.
> #|kawa:1|# context
> /dev/stdin:1:1: unbound location: context
> at gnu.expr.ReferenceExp.apply(ReferenceExp.java:163)
> at gnu.expr.ModuleExp.evalModule2(ModuleExp.java:281)
> at gnu.expr.ModuleExp.evalModule(ModuleExp.java:211)
> at kawa.Shell.run(Shell.java:283)
> at kawa.Shell.run(Shell.java:196)
> at kawa.Shell.run(Shell.java:183)
> at kawa.TelnetRepl.apply0(TelnetRepl.java:25)
> at gnu.mapping.RunnableClosure.run(RunnableClosure.java:75)
> at java.lang.Thread.run(Thread.java:764)
>
> any other ideas? I suspect its because I have the wrong Environment
>
> On Wed, Sep 13, 2017 at 5:28 PM, Per Bothner <[hidden email]> wrote:
>> On 09/13/2017 05:05 PM, Sonny To wrote:
>>>
>>> I'm trying to use the TelnetRepl in an android Service but cannot
>>> figure out how to put bindings into the environment
>>
>>
>>>                      env.put(Symbol.makeUninterned("context", null),
>>> applicationContext)
>>
>>
>>> I can telnet to it but the "context" symbol is not available
>>>
>>> #|kawa:4|# context
>>> /dev/stdin:4:1: unbound location: context
>>
>>
>> That would be expected if you use Symbol.makeUninterned.
>> An uninterned symbol is a unique object that you can *not*
>> "lookup".  For example (string->symbol "context") is the
>> same as 'symbol - but (Symbol:makeUninterned "context") is a completely
>> different object.
>>
>> Instead, try Symbol.valueOf("context").
>>
>>> Do I have the wrong environment? if so how to get the correct environment?
>>
>>
>> There might be other problems, but fix the above-mentioned problem first.
>> --
>>         --Per Bothner
>> [hidden email]   http://per.bothner.com/
Reply | Threaded
Open this post in threaded view
|

Re: putting symbols into Environment

Per Bothner
In reply to this post by Sonny To
On 09/15/2017 05:48 AM, Sonny To wrote:
> How close does kawa stick to standard scheme? I'm trying these
> functions described here
> https://www.gnu.org/software/mit-scheme/documentation/mit-scheme-ref/Environment-Operations.html
> and none of work

That page is for MIT Scheme - a specific Scheme implementation/dialect,
not "standard Scheme".

> #|kawa:5|# (define e (environment))
> #|kawa:6|# (environment-bindings e)
> /dev/stdin:6:2: unbound location: environment-bindings

The Git version of Kawa (and soon Kawa 3.0) includes environment-fold

-- Procedure: (environment-fold environment proc init)
      Call PROC for each key in the ENVIRONMENT, which may be any
      argument to ‘eval’, such as ‘(interaction-environment)’ or a call
      to the ‘environment’ procedure.  The PROC is called with two
      arguments: The binding’s key, and an accumulator value.  The INIT
      is the initial accumulator value; the result returned by PROC is
      used for subsequent accumulator values.  The value returned by
      ‘environment-fold’ is the final acumulator value.

      A key is normally a symbol, but can also be a ‘KeyPair’ object (a
      pair of a symbol and a property symbol used to implement Common
      Lisp-style property lists).
           (environment-fold (environment '(scheme write)) cons '())
             ⇒ (display write-shared write write-simple)
      To get all the predefined bindings use
          (environment '(kawa base))


--
        --Per Bothner
[hidden email]   http://per.bothner.com/