How are 'bind' and 'in' different? - rebol2

Take the following code which creates a context:
c: context [a: 2]
You can see it creates a context c, and that a is not bound in the global context:
>> ?? c
c: make object! [
a: 2
]
>> a
** Script Error: a has no value
** Near: a
Now if you use bind 'a c, it returns the value of the word in the context it was bound:
>> get bind 'a c
== 2
Which is also the same as in c 'a:
>> (get bind 'a c) = (get in c 'a)
== true
Looks like in is a version of bind with flipped arguments
So, how is in different?
There are some obvious feature additions in bind, like having a refinement /copy for efficiency, and also accepting a block! instead of a single word for its words argument.
In which case, the question becomes, why in?
Note
This was initially motivated by comments in this question, when I didn't quite understood what bind does, and a discussion on gitter prompted me to post this

The big difference is that BIND can propagate the context of a word:
>> q: func [/local a b][a: 1 b: 2 return 'a]
>> get bind 'b q
== 2
>> get in q 'b
** Script Error: in expected object argument of type: object port
** Near: get in q 'b
Rebol 2 only of course.

Related

Constructor argument mismatch

I've created a type for lazy binary tree:
type 'a lBT = LEmpty | LNode of 'a * (unit -> 'a lBT) * (unit -> 'a lBT);;
and wanted to create an instance of it:
let exlBST = LNode(3, function() -> LEmpty, function() -> LEmpty);;
but I received this error:
Error: The constructor LNode expects 3 argument(s),
but is applied here to 2 argument(s)
Can you tell me what's going on? Isn't unit considered as an argument?
The argument to LNode is not parsed as you expect, a triple with two functions, but as a pair where the the second item is a function returning another pair. Use parentheses around at least the middle function, but preferably both for consistency, to make your intention explicit:
let exlBST = LNode (3, (function () -> LEmpty), (function () -> LEmpty));;
There are unfortunately a few of these weird parsing edge cases/ambiguities with OCaml's syntax. You'll learn to deal with it in time, but a good rule of thumb is: If in doubt, use parentheses (or begin/end when appropriate)

Binding vs Assignment

I've read a number of articles on the difference between assignment and binding, but it hasn't clicked yet (specifically in the context of an imperative language vs one without mutation).
I asked in IRC, and someone mentioned these 2 examples illustrate the difference, but then I had to go and I didn't see the full explanation.
Can someone please explain how/why this works in a detailed way, to help illustrate the difference?
Ruby
x = 1; f = lambda { x }; x = 2; f.call
#=> 2
Elixir
x = 1; f = fn -> x end; x = 2; f.()
#=> 1
I've heard this explanation before and it seems pretty good:
You can think of binding as a label on a suitcase, and assignment as a
suitcase.
In other languages, where you have assignment, it is more like putting a value in a suitcase. You actually change value that is in the suitcase and put in a different value.
If you have a suitcase with a value in it, in Elixir, you put a label on it. You can change the label, but the value in the suitcase is still the same.
So, for example with:
iex(1)> x = 1
iex(2)> f = fn -> x end
iex(3)> x = 2
iex(4)> f.()
1
You have a suitcase with 1 in it and you label it x.
Then you say, "Here, Mr. Function, I want you to tell me what is in this suitcase when I call you."
Then, you take the label off of the suitcase with 1 in it and put it on another suitcase with 2 in it.
Then you say "Hey, Mr. Function, what is in that suitcase?"
He will say "1", because the suitcase hasn't changed. Although, you have taken your label off of it and put it on a different suitcase.
After a while, I came up with the answer that is probably the best explanation of the difference between “binding” and “assignment”; it has nothing in common with what I have written in another answer, hence it’s posted as a separate answer.
In any functional language, where everything is immutable, there is no meaningful difference between terms “binding” and “assignment.” One might call it either way; the common pattern is to use the word “binding,“ explicitly denoting that it’s a value bound to a variable. In Erlang, for instance, one can not rebound a variable. In Elixir this is possible (why, for God’s sake, José, what for?)
Consider the following example in Elixir:
iex> x = 1
iex> 1 = x
The above is perfectly valid Elixir code. It is evident, one cannot assign anything to one. It is neither assignment nor binding. It is matching. That is how = is treated in Elixir (and in Erlang): a = b fails if both are bound to different values; it returns RHO if they match; it binds LHO to RHO if LHO is not bound yet.
In Ruby it differs. There is a significant difference between assignment (copying the content,) and binding (making a reference.)
Elixir vs Ruby might not be the best contrast for this. In Elixir, we can readily "re-assign" the value of a previously assigned named variable. The two anonymous-function examples you provided demonstrate the difference in how the two languages assign local variables in them. In Ruby, the variable, meaning the memory reference, is assigned, which is why when we change it, the anonymous function returns the current value stored in that memory-reference. While in Elixir, the value of the variable at the time the anonymous function is defined (rather than the memory reference) is copied and stored as the local variable.
In Erlang, Elixir's "parent" language, however, variables as a rule are "bound." Once you've declared the value for the variable named X, you are not allowed to alter it for the remainder of the program and any needed alterations would need to be stored in new named variables. (There is a way to reassign a named variable in Erlang but it is not the custom.)
Binding refers to particular concept used in expression-based languages that may seem foreign if you're used to statement-based languages. I'll use an ML-style example to demonstrate:
let x = 3 in
let y = 5 in
x + y
val it : int = 8
The let... in syntax used here demonstrates that the binding let x = 3 is scoped only to the expression following the in. Likewise, the binding let y = 5 is only scoped to the expression x + y, such that, if we consider another example:
let x = 3 in
let f () =
x + 5
let x = 4 in
f()
val it : int = 8
The result is still 8, even though we have the binding let x = 4 above the call to f(). This is because f itself was bound in the scope of the binding let x = 3.
Assignment in statement-based languages is different, because the variables being assigned are not scoped to a particular expression, they are effectively 'global' for whatever block of code they're in, so reassigning the value of a variable changes the result of an evaluation that uses the same variable.
The easiest way to understand the difference, would be to compare the AST that is used by the language interpreter/compiler to produce machine-/byte-code.
Let’s start with ruby. Ruby does not provide the AST viewer out of the box, so I will use RubyParser gem for that:
> require 'ruby_parser'
> RubyParser.new.parse("x = 1; f = -> {x}; x = 2; f.()").inspect
#=> "s(:block, s(:lasgn, :x, s(:lit, 1)),
# s(:lasgn, :f, s(:iter, s(:call, nil, :lambda), 0, s(:lvar, :x))),
# s(:lasgn, :x, s(:lit, 2)), s(:call, s(:lvar, :f), :call))"
The thing we are looking for is the latest node in the second line: there is x variable inside the proc. In other words, ruby expects the bound variable there, named x. At the time the proc is evaluated, x has a value of 2. Hence the the proc returns 2.
Let’s now check Elixir.
iex|1 ▶ quote do
...|1 ▶ x = 1
...|1 ▶ f = fn -> x end
...|1 ▶ x = 2
...|1 ▶ f.()
...|1 ▶ end
#⇒ {:__block__, [],
# [
# {:=, [], [{:x, [], Elixir}, 1]},
# {:=, [], [{:f, [], Elixir}, {:fn, [], [{:->, [], [[], {:x, [], Elixir}]}]}]},
# {:=, [], [{:x, [], Elixir}, 2]},
# {{:., [], [{:f, [], Elixir}]}, [], []}
# ]}
Last node in the second line is ours. It still contains x, but during a compilation stage this x will be evaluated to it’s currently assigned value. That said, fn -> not_x end will result in compilation error, while in ruby there could be literally anything inside a proc body, since it’ll be evaluated when called.
In other words, Ruby uses a current caller’s context to evaluate proc, while Elixir uses a closure. It grabs the context it encountered the function definition and uses it to resolve all the local variables.

Smalltalk : best to avoid non-local returns? Algorithm rewrite

This method (from SOM benchmarks) relies on Smalltalk non local returns. Is there a way to produce the same results without them?
placeQueenNonLocalReturn: c
1 to: 8 do: [ :r |
(self row: r column: c)
ifTrue: [
queenRows at: r put: c.
self row: r column: c put: false.
(c = 8) ifTrue: [ ^true ].
(self placeQueen: c + 1) ifTrue: [ ^true ].
self row: r column: c put: true ] ].
^false
!
Note (another question but related): Is it possible to change this code without knowing anything about callers and callees? I guess it should give a better understanding of purpose, but should not the method be self contained regarding my question?
Yes, it is possible.
You are using the non-local return only to get a quick exit from the 1 to: 8 do: loop.
You can easily write the loop with other syntax, like:
exit := false.
row := 1.
[row < 9 andNot: [exit]] whileTrue: [ (self row: r column: c)
ifTrue: [
queenRows at: r put: c.
self row: r column: c put: false.
c = 8 ifTrue: [ exit := true ]
ifFalse: [ (self placeQueen: c + 1)
ifTrue: [ exit := true ]
ifFalse: [self row: r column: c put: true ] ] ].
^exit
Note that the call to placeQueen: in the loop seems to be recursive, may be the selector of your method is wrong.
I don´t understand what do you mean with "knowing about callers and callees"... I did a mostly syntactic rewrite on the code.
Same about the self-containment.
This method is not self-contained, it seems to be part of the 8 Queen problem solution, but relies on (self row: r column: c) to check the validity of a place for a new queen.
Let's correct a misunderstanding. The code as shown does not contain any non-local returns, so it cannot be relying on this feature.
The returns shown are are all just normal returns - they return a result back to the caller.
A non-local return refers to the case where a value is returned to somewhere other than the calling method.
So there is no reason to avoid the returns shown,

Is it possible to pass parameters by reference in Rebol?

Here, I have attempted to set the value of a global variable from inside a function, but the value has not changed:
setGlobalScope: func [theVar1] [
theVar1: 10
]
theVar: 1
setGlobalScope theVar
print theVar
"This prints 1 instead of 10. The value of theVar has not changed."
Is it possible to modify the value of a function's parameter from inside the function itself, so that the value is modified within the global scope instead of the function's scope?
You passed an integer value, not a word. Within the function, the word theVar1 is assigned the value of that integer. Reassigning it doesn't change it, because values like integers and dates and decimal numbers aren't "pointers" under the hood.
Hence, the answer from #sqlab where you can get around this by various ways of getting the word itself. The difference between function ['x] [code] and function [:x] [code] may interest you as an aside...
Why doesn't Rebol 3 honor quoted function parameters that are parenthesized?
But note that series values in Rebol do have modifying functions that affect the target, vs. just reassignment of where the word points. Consider:
setGlobalScope: func [theVar1 [string!]] [
clear theVar1
insert theVar1 "Modification"
]
theVar: "Original"
setGlobalScope theVar
print theVar
That prints Modification.
If you need to pass non-series values by reference, you need to put them in a series and use series modification operations instead of assignment. Because an assignment would just overwrite the "pointer" you have to the block or whatever. Worst case scenario you can wrap a single value in a block--if you must. But Rebol has a lot of "wait, look at it this other way..." where dialecting comes to the rescue in creating a better interface than that thing you were trying to clone from another less cool language. :-)
Mitigating the complexity of passing by reference is Rebol's simplicity at handling multiple return results:
foo: function [value1 value2] [
return reduce [
value1 + 7
value2 + 16
]
]
set [a b] foo 03 04
print a
print b
That outputs 10 and 20.
By using a combination of lit-word and get-word there is more than one way.
e.g
>> setGlobalScope: func ['theVar1] [set :theVar1 10]
>> theVar: 1
== 1
>>
>> setGlobalScope theVar
== 10
>>
>> print theVar
10
and
>> setGlobalScope: func [theVar1] [set :theVar1 10]
>> theVar: 1
== 1
>> setGlobalScope 'theVar
== 10
>> print theVar
10
I think you can just modify your variable theVar directly in your setGlobalScope funct.

Clojure : Documentation syntax, regarding when-let and if-let

I've been looking at the if-let and when-let macros, Im having trouble determining what exactly it is that they "do". In particular, the documentation sais :
clojure.core/when-let
([bindings & body])
Macro
bindings => binding-form test
When test is true, evaluates body with binding-form bound to the value of test
I thus am somewhat confused about the way macros are documented.
1) What does the "=>" symbol mean ?
2) What does "test" refer to ?
Direct answer to your questions:
=> means "expands to", as in BNF notation. In this case it means that you need two forms: binding-form and the test.
"test" means anything that can be evaluated as bool.
By the way, I think that the docs are unclear or even maybe erroneous here. It is hard (or impossible) to deduce that the two forms constituting the bindings need to be enclosed in a vector. IMHO it should be either when-let ([[bindings] & body]) (the vector shown in the args) or bindings => [binding-form test] (the vector shown in the BNF-like expansion.)
It's often helpful when dealing with a macro to call macroexpand and see what the generated code is.
(macroexpand
'(if-let [x (myfunc)]
(foo x)
(bar))
; expands to
(let* [temp__3695__auto__ (myfunc)]
(if temp__3695__auto__ (clojure.core/let [x temp__3695__auto__]
(foo x))
(bar)))
; the generated symbols are necessary to avoid symbol
; capture but can confuse. The above is equivalent to:
(let* [ t (myfunc)]
(if t
(let [x t]
(foo x))
(bar))
So you can see that if-let is a shorthand for "bind local variable to the result of a function call and if that variable is truthy call the first form, else call the other form. The value returned by your function is only available in the 'truthy' branch."
wrt documentation convention
bindings => binding-form test
=> reads something like 'is equivalent to'
test is some form that returns a value
For most of these functions, clojuredocs is your friend, example usage often clarify things. If the clojuredocs example doesn't cut it for you, you can add your own
Consider the following code:
(if-let [x (a-function)]
(do-something-with x) ;; (a-function) returned a truthy result
(do-something-else) ;; (a-function) returned nil or false
This is like let, in that x will be bound to the return value of (a-function). This function could return nil or false. In that case, the implicit test fails and (do-something-else) will be evaluated. If x is not nil and not false, (do-something-with x) will be evaluated.
A scenario where this could be useful:
(if-let [user (find-logged-in-user)]
(do something with logged in user) ;; a user was found
(redirect to login page) ;; no user was found
I sometimes use something like the following, to conditionally add keys to a map of options:
(apply merge {:username "joe"
:email "joe#example.com"}
(when-let [name (find-full-name)] {:name name})
(when-let [dob (find-date-of-birth)] {:dob dob}))
This results in a map with :username and :email keys, and a :name key if the users' full name was found, plus a :dob key if a date of birth was found.
I hope that makes the use of if-let and when-let clearer.

Resources