BUGFIX: Can't define '=' in terms of '=' on generic members of type, see Pair.=

This commit is contained in:
Erik Svedäng 2018-06-13 12:29:54 +02:00
parent bcd4e6d298
commit 275947dc35
6 changed files with 23 additions and 15 deletions

View File

@ -4,9 +4,8 @@
(defn init-from-refs [r1 r2]
(Pair.init @r1 @r2))
;; BUG: Can't define this function because = resolves to Pair.= in the body.
;; (defn = [p1 p2]
;; (and (= (Pair.a p1) (Pair.a p2))
;; (= (Pair.b p1) (Pair.b p2))))
(defn = [p1 p2]
(and (= (Pair.a p1) (Pair.a p2))
(= (Pair.b p1) (Pair.b p2))))
)

View File

@ -8,7 +8,6 @@
** 0.3
*** References must keep track of their origin and prevent usage of them if the origin has been given away.
Currently it's possible to set a ref so it points to a value in a more short-lived scope, leads to 'stack-use-after-scope' error in clang-sanitizer.
*** Can't define '=' in terms of '=' on generic members of type, see Pair.=
* Big Language Features
** 1.0
*** Lambdas (anonymous functions) that compile on Windows and don't leak memory!

View File

@ -45,8 +45,12 @@
;; (set! r &xs))
;; (println* r)))
;; Resolves to wrong thing
;; '=' resolves to wrong thing
(deftype (Foo t) [x t])
(defmodule Foo
(defn = [a b]
(= (Foo.x a) (Foo.x b))))
(defn main []
(do (println* (= &(Foo.init 123) &(Foo.init 666)))
(println* (= &(Foo.init 123) &(Foo.init 123)))))

View File

@ -7,8 +7,6 @@
(defn construct-from-ref [r]
(Trivial.init @r))
(deftype (Pair a b) [x a y b])
;; Simplified dictionary:
(deftype (Entry a b) [key a value b])
(deftype (Bucket a b) [entries (Array (Entry a b))])
@ -40,8 +38,8 @@
(IO.println &(str &(Pair.init 10 @"hello")))
(IO.println &(str &(Pair.init true 3.2)))
(IO.println &(str &(Pair.init [1 2 3] [true false true false])))
(IO.println &(str &(Pair.update-x (Pair.init 100 100) Int.inc)))
(IO.println &(str &(Pair.set-y (Pair.init 100 100) 200)))
(IO.println &(str @(Pair.x &(Pair.init 100 100))))
(IO.println &(str &(Pair.update-a (Pair.init 100 100) Int.inc)))
(IO.println &(str &(Pair.set-b (Pair.init 100 100) 200)))
(IO.println &(str @(Pair.a &(Pair.init 100 100))))
(try-dictionary)
))

View File

@ -61,3 +61,8 @@
;; (defn main []
;; (f))
(defn-do main []
(println* "eq? " (= &(Pair.init 5 true) &(Pair.init 6 false)))
(println* "eq? " (= &(Pair.init 5 true) &(Pair.init 5 true)))
)

View File

@ -29,10 +29,13 @@ setFullyQualifiedSymbols typeEnv globalEnv env (XObj (Lst [defn@(XObj Defn _ _),
body])
i t) =
-- For self-recursion, there must be a binding to the function in the inner env.
-- Note: This inner env is ephemeral since it is not stored in a module or global scope.
let functionEnv = Env Map.empty (Just env) Nothing [] InternalEnv
envWithSelf = extendEnv functionEnv functionName sym
envWithArgs = foldl' (\e arg@(XObj (Sym (SymPath _ argSymName) _) _ _) -> extendEnv e argSymName arg) envWithSelf argsArr
-- It is marked as external to not mess up lookup.
-- Inside the recursion env is the function env that contains bindings for the arguments of the function.
-- Note: These inner envs is ephemeral since they are not stored in a module or global scope.
let recursionEnv = Env Map.empty (Just env) Nothing [] ExternalEnv
envWithSelf = extendEnv recursionEnv functionName sym
functionEnv = Env Map.empty (Just recursionEnv) Nothing [] InternalEnv
envWithArgs = foldl' (\e arg@(XObj (Sym (SymPath _ argSymName) _) _ _) -> extendEnv e argSymName arg) functionEnv argsArr
in XObj (Lst [defn, sym, args, setFullyQualifiedSymbols typeEnv globalEnv envWithArgs body]) i t
setFullyQualifiedSymbols typeEnv globalEnv env (XObj (Lst [the@(XObj The _ _), typeXObj, value]) i t) =
let value' = setFullyQualifiedSymbols typeEnv globalEnv env value