Skip to content

Commit 30dd8b8

Browse files
committed
Yet yet more work on symbology draft
1 parent 4b11652 commit 30dd8b8

File tree

1 file changed

+60
-5
lines changed

1 file changed

+60
-5
lines changed

_drafts/2025-02-14-symbolic-of-what.md

+60-5
Original file line numberDiff line numberDiff line change
@@ -236,11 +236,9 @@ Let us contemplate the following situation.
236236

237237
(defn g [z] (inc z))
238238
(defn h [x] (g x))
239-
```
240239

241240
(ns ns1
242-
(:use [big.deal.namespace :as ns2 :only [g]])
243-
(:import [System String])) ; unnecessary in real life because this import is done automatically
241+
(:require [big.deal.namespace :as ns2])) ; unnecessary in real life because this import is done automatically
244242

245243
(defn f [x y z] [z y x])
246244
```
@@ -250,8 +248,8 @@ Now consider the situation where we have read the following code and are evaluat
250248
```Clojure
251249
(fn* [x]
252250
(let* [y 7]
253-
(f (ns2/g ^System.Int64 y)
254-
(String/ToUpper ^String x)
251+
(f (ns2/g Int64/MaxValue y)
252+
(String/.ToUpper x)
255253
(big.deal.namespace/h 7))))
256254
```
257255
(Note: the parser would see `fn*` instead of `fn` -- the latter is macro that expands to the former. Similarly for `let*.)
@@ -265,5 +263,62 @@ We need to interpret each symbol in that form:
265263
f x y ns2/g big.deal.namespace/h System.Int64 String String/ToUpper
266264
```
267265

266+
`x` and `y` are easy. They do not have a namespace, so the first step is look to see if they are currently bound in the lexical scope.
267+
They are, so they resolve to local bindings expressions.
268+
269+
`f` also does not have namespace. However, it not bound in the current lexical scope.
270+
It does not have a namespace, so it does not refer to directly or indirectly (via an alias) to a namespace.
271+
The remaining option is that it has a mapping in the current namespace. It does, to a `Var` and that is what we use.
272+
273+
`ns2/g` is a bit more complicated. It has a namespace, so it won't be a local. We need to determine what namespace `ns2` stands for.
274+
The `NamespaceFor` method is used:
275+
276+
```F#
277+
static member NamespaceFor(inns: Namespace, sym: Symbol) : Namespace =
278+
//note, presumes non-nil sym.ns
279+
// first check against currentNS' aliases...
280+
let nsSym = Symbol.intern (sym.Namespace)
281+
282+
match inns.lookupAlias (nsSym) with
283+
| null -> Namespace.find (nsSym)
284+
| _ as ns -> ns
285+
```
286+
287+
In this context, the current namespace is `ns1` and `ns1` is an alias for `big.deal.namespace`. So we look up `g` in `big.deal.namspace`, finding a `Var`.
288+
289+
Next up is `Int64/MaxValue`. It does have a namespace, so it won't be a local.
290+
When we call `NamespaceFor` on `Int64`, no namespace is found. (`null` is returned.)
291+
If you were to try to find a type `Int64`, you would fail. There is a type named `System.Int64`.
292+
Fortunately, all namespaces are set up with a mappings from unqualified names of types to their types.
293+
Thus, there is a mapping from `Int64` to `System.Int64` in the `ns1` namespace.
294+
When we have something of the form `Type/Member`, we look for the `Member` in as a property or field in that type.
295+
In this case, there is a property `MaxValue` in `System.Int64`, so we turn this into a static method call node.
268296

297+
`String/.ToUpper` is similar. In this case, because this symbol appears in the functional position of function invocation,
298+
given that `String` maps to `System.String`, we look for methods also. Beacause the name starts with a period, we look for an instance method, and find one.
269299

300+
301+
302+
303+
304+
305+
306+
```Clojure
307+
Fn ns1$fn__1
308+
invoke [ x ]
309+
Let: [
310+
y
311+
Literal: PrimNumeric = 7
312+
]
313+
Invoke:
314+
Var: #'ns1/f
315+
Invoke:
316+
Var: #'big.deal.namespace/g
317+
InteropCall: FieldOrProperty: System.Int64.MaxValue Static
318+
<y>
319+
InteropCall: Method: System.String.ToUpper Instance
320+
<x>
321+
Invoke:
322+
Var: #'big.deal.namespace/h
323+
Literal: PrimNumeric = 7
324+
```

0 commit comments

Comments
 (0)