From 0f7bfa0d4a4e5a79fffe72adec1dd35992c7dde3 Mon Sep 17 00:00:00 2001
From: Eduardo Julian
Date: Sun, 5 Sep 2021 15:17:20 -0400
Subject: Clearing the local scope during eval analysis to avoid accidentally
 seeing variables outside of eval's scope.

---
 documentation/bookmark/floating_point.md           |  1 +
 lux-bootstrapper/src/lux/analyser/lux.clj          | 23 +++++++++++-----------
 lux-bootstrapper/src/lux/base.clj                  | 10 ++++++++++
 lux-python/project.lux                             | 18 ++++++++---------
 .../lux/tool/compiler/language/lux/analysis.lux    | 15 ++++++++++++++
 .../compiler/language/lux/analysis/evaluation.lux  |  5 +++--
 stdlib/source/library/lux/type.lux                 |  7 +++----
 7 files changed, 53 insertions(+), 26 deletions(-)

diff --git a/documentation/bookmark/floating_point.md b/documentation/bookmark/floating_point.md
index 509990dd1..fe22137dc 100644
--- a/documentation/bookmark/floating_point.md
+++ b/documentation/bookmark/floating_point.md
@@ -12,6 +12,7 @@
 
 # Random generation
 
+1. [Drawing random floating-point numbers from an interval](https://hal.archives-ouvertes.fr/hal-03282794/document)
 1. [Uniform random floats:  How to generate a double-precision floating-point number in [0, 1] uniformly at random given a uniform random source of bits](https://mumble.net/~campbell/2014/04/28/uniform-random-float)
 
 # Correctness
diff --git a/lux-bootstrapper/src/lux/analyser/lux.clj b/lux-bootstrapper/src/lux/analyser/lux.clj
index 7b8019211..f4d76912f 100644
--- a/lux-bootstrapper/src/lux/analyser/lux.clj
+++ b/lux-bootstrapper/src/lux/analyser/lux.clj
@@ -1,4 +1,5 @@
 (ns lux.analyser.lux
+  (:refer-clojure :exclude [eval])
   (:require (clojure [template :refer [do-template]]
                      [set :as set])
             clojure.core.match
@@ -553,6 +554,11 @@
                                 (str "\nThis is an alias for " source-name)))))
       (return &/$End))))
 
+(defn eval [analyse optimize eval! type code]
+  (|do [analysis (&/without-scope
+                  (&&/analyse-1 analyse type code))]
+    (eval! (optimize analysis))))
+
 (defn analyse-def* [analyse optimize eval! compile-def ?name ?value ?annotations exported? type? & [?expected-type]]
   (|do [_ &/ensure-directive
         module-name &/get-module-name
@@ -563,10 +569,8 @@
                     (&/with-expected-type ?expected-type
                       (&&/analyse-1 analyse ?expected-type ?value))
                     (&&/analyse-1+ analyse ?value))))
-        =exported? (&&/analyse-1 analyse &type/Bit exported?)
-        ==exported? (eval! (optimize =exported?))
-        =annotations (&&/analyse-1 analyse &type/Code ?annotations)
-        ==annotations (eval! (optimize =annotations))
+        ==exported? (eval analyse optimize eval! &type/Bit exported?)
+        ==annotations (eval analyse optimize eval! &type/Code ?annotations)
         def-value (compile-def ?name (optimize =value) ==annotations ==exported? type?)
         _ &type/reset-mappings
         :let [def-type (&&/expr-type* =value)
@@ -664,8 +668,7 @@
 
 (defn analyse-module [analyse optimize eval! compile-module ?annotations ?imports]
   (|do [_ &/ensure-directive
-        =anns (&&/analyse-1 analyse &type/Code ?annotations)
-        ==anns (eval! (optimize =anns))
+        ==anns (eval analyse optimize eval! &type/Code ?annotations)
         module-name &/get-module-name
         _ (&&module/set-anns ==anns module-name)
         _imports (&&module/fetch-imports ?imports)
@@ -707,17 +710,15 @@
               _analysis)))
 
 (defn analyse-type-check [analyse optimize eval! exo-type ?type ?value]
-  (|do [=type (&&/analyse-1 analyse &type/Type ?type)
-        ==type (eval! (optimize =type))
+  (|do [==type (eval analyse optimize eval! &type/Type ?type)
         _ (&type/check exo-type ==type)
         =value (&&/analyse-1 analyse ==type ?value)
         _location &/location]
     (return (&/|list (&&/|meta ==type _location
-                               (&&/$ann =value =type))))))
+                               (&&/$ann =value ==type))))))
 
 (defn analyse-type-as [analyse optimize eval! exo-type ?type ?value]
-  (|do [=type (&&/analyse-1 analyse &type/Type ?type)
-        ==type (eval! (optimize =type))
+  (|do [==type (eval analyse optimize eval! &type/Type ?type)
         _ (&type/check exo-type ==type)
         =value (&&/analyse-1+ analyse ?value)]
     (return (&/|list (coerce ==type =value)))))
diff --git a/lux-bootstrapper/src/lux/base.clj b/lux-bootstrapper/src/lux/base.clj
index b19bb4fae..41cf66a0f 100644
--- a/lux-bootstrapper/src/lux/base.clj
+++ b/lux-bootstrapper/src/lux/base.clj
@@ -966,6 +966,16 @@
         _
         output))))
 
+(defn without-scope [body]
+  (fn [state]
+    (|case (body (set$ $scopes (|list (env "WITHOUT-SCOPE" "")) state))
+      ($Right state* datum)
+      (return* (set$ $scopes (get$ $scopes state) state*)
+               datum)
+      
+      output
+      output)))
+
 (defn run-state [monad state]
   (monad state))
 
diff --git a/lux-python/project.lux b/lux-python/project.lux
index 118bc95b2..2f6627b83 100644
--- a/lux-python/project.lux
+++ b/lux-python/project.lux
@@ -1,14 +1,14 @@
 [""
- [#identity ["com.github.luxlang" "lux-python" "0.6.0-SNAPSHOT"]
+ ["identity" ["com.github.luxlang" "lux-python" "0.6.0-SNAPSHOT"]
   
-  #deploy_repositories ["snapshots" "https://oss.sonatype.org/content/repositories/snapshots/"
-                        "releases" "https://oss.sonatype.org/service/local/staging/deploy/maven2/"]
+  "deploy_repositories" ["snapshots" "https://oss.sonatype.org/content/repositories/snapshots/"
+                         "releases" "https://oss.sonatype.org/service/local/staging/deploy/maven2/"]
 
-  #repositories ["https://oss.sonatype.org/content/repositories/snapshots/"
-                 "https://oss.sonatype.org/service/local/staging/deploy/maven2/"]
+  "repositories" ["https://oss.sonatype.org/content/repositories/snapshots/"
+                  "https://oss.sonatype.org/service/local/staging/deploy/maven2/"]
   
-  #compiler ["com.github.luxlang" "lux-jvm" "0.6.0-SNAPSHOT" "jar"]
-  #dependencies [["com.github.luxlang" "stdlib" "0.6.0-SNAPSHOT" "tar"]
-                 ["org.python" "jython-standalone" "2.7.2" "jar"]]
+  "compiler" ["com.github.luxlang" "lux-jvm" "0.6.0-SNAPSHOT" "jar"]
+  "dependencies" [["com.github.luxlang" "stdlib" "0.6.0-SNAPSHOT" "tar"]
+                  ["org.python" "jython-standalone" "2.7.2" "jar"]]
   
-  #program "program"]]
+  "program" "program"]]
diff --git a/stdlib/source/library/lux/tool/compiler/language/lux/analysis.lux b/stdlib/source/library/lux/tool/compiler/language/lux/analysis.lux
index 772b0b04d..5522749e0 100644
--- a/stdlib/source/library/lux/tool/compiler/language/lux/analysis.lux
+++ b/stdlib/source/library/lux/tool/compiler/language/lux/analysis.lux
@@ -447,6 +447,21 @@
       (#try.Failure error)
       (#try.Failure error))))
 
+(def: scope_reset
+  (List Scope)
+  (list fresh_scope))
+
+(def: .public (without_scopes action)
+  (All (_ a) (-> (Operation a) (Operation a)))
+  (function (_ [bundle state])
+    (case (action [bundle (with@ #.scopes ..scope_reset state)])
+      (#try.Success [[bundle' state'] output])
+      (#try.Success [[bundle' (with@ #.scopes (value@ #.scopes state) state')]
+                     output])
+
+      (#try.Failure error)
+      (#try.Failure error))))
+
 (def: .public (with_current_module name)
   (All (_ a) (-> Text (Operation a) (Operation a)))
   (extension.localized (value@ #.current_module)
diff --git a/stdlib/source/library/lux/tool/compiler/language/lux/analysis/evaluation.lux b/stdlib/source/library/lux/tool/compiler/language/lux/analysis/evaluation.lux
index a770d7798..94305f30a 100644
--- a/stdlib/source/library/lux/tool/compiler/language/lux/analysis/evaluation.lux
+++ b/stdlib/source/library/lux/tool/compiler/language/lux/analysis/evaluation.lux
@@ -9,7 +9,7 @@
     [math
      [number
       ["n" nat]]]]]
-  [// {"+" [Operation]}
+  ["." // {"+" [Operation]}
    [macro {"+" [Expander]}]
    [//
     [phase
@@ -45,7 +45,8 @@
     (function (eval archive type exprC)
       (do phase.monad
         [exprA (type.with_type type
-                 (analyze archive exprC))
+                 (//.without_scopes
+                  (analyze archive exprC)))
          module (extensionP.lifted
                  meta.current_module_name)
          count (extensionP.lifted
diff --git a/stdlib/source/library/lux/type.lux b/stdlib/source/library/lux/type.lux
index c6f57f52f..c60be0b73 100644
--- a/stdlib/source/library/lux/type.lux
+++ b/stdlib/source/library/lux/type.lux
@@ -445,10 +445,9 @@
                             exemplar ..typed
                             computation ..typed])
   (macro.with_identifiers [g!_]
-    (let [typeC (("lux in-module" "library/lux" .quantified)
-                 (` (All ((~ g!_) (~+ (list\each code.local_identifier type_vars)))
-                      (-> (~ (value@ #type exemplar))
-                          (~ (value@ #type computation))))))
+    (let [typeC (` (All ((~ g!_) (~+ (list\each code.local_identifier type_vars)))
+                     (-> (~ (value@ #type exemplar))
+                         (~ (value@ #type computation)))))
           shareC (` (: (~ typeC)
                        (.function ((~ g!_) (~ g!_))
                          (~ (value@ #expression computation)))))]
-- 
cgit v1.2.3