From 4bd2f378011bf28449ed907d637a7867524e3b4b Mon Sep 17 00:00:00 2001
From: Eduardo Julian
Date: Thu, 2 Jul 2020 22:39:02 -0400
Subject: Now using the new syntax for variants (even though they still work
the old way... for now)
---
luxc/src/lux/analyser.clj | 5 +-
luxc/src/lux/analyser/case.clj | 19 +-
luxc/src/lux/analyser/lux.clj | 8 +-
stdlib/source/lux.lux | 266 ++++++++++-----------
stdlib/source/lux/abstract/enum.lux | 14 +-
stdlib/source/lux/control/concatenative.lux | 4 +-
stdlib/source/lux/control/parser.lux | 4 +-
stdlib/source/lux/data/collection/list.lux | 15 +-
stdlib/source/lux/data/number/rev.lux | 14 +-
stdlib/source/lux/data/sum.lux | 37 +--
stdlib/source/lux/data/text/regex.lux | 4 +-
stdlib/source/lux/math/random.lux | 4 +-
.../lux/tool/compiler/language/lux/analysis.lux | 18 ++
.../tool/compiler/language/lux/phase/analysis.lux | 28 ++-
.../compiler/language/lux/phase/analysis/case.lux | 30 ++-
.../language/lux/phase/analysis/inference.lux | 20 +-
.../language/lux/phase/analysis/structure.lux | 167 ++++++-------
stdlib/source/poly/lux/abstract/equivalence.lux | 13 +-
stdlib/source/poly/lux/abstract/functor.lux | 10 +-
stdlib/source/poly/lux/data/format/json.lux | 30 ++-
stdlib/source/spec/compositor/analysis/type.lux | 12 +-
stdlib/source/test/lux/data/sum.lux | 14 +-
stdlib/source/test/lux/data/text/regex.lux | 10 +-
stdlib/source/test/lux/macro.lux | 6 +-
stdlib/source/test/lux/type/implicit.lux | 37 +--
25 files changed, 417 insertions(+), 372 deletions(-)
diff --git a/luxc/src/lux/analyser.clj b/luxc/src/lux/analyser.clj
index 4522b9aea..abdd0acd7 100644
--- a/luxc/src/lux/analyser.clj
+++ b/luxc/src/lux/analyser.clj
@@ -182,8 +182,9 @@
(&&common/analyse-proc analyse exo-type ?procedure parameters))))
(&/$Nat idx)
- (&/with-analysis-meta cursor exo-type
- (&&lux/analyse-variant analyse (&/$Right exo-type) idx nil parameters))
+ (|let [(&/$Cons [_ (&/$Bit ?right)] parameters*) parameters]
+ (&/with-analysis-meta cursor exo-type
+ (&&lux/analyse-variant analyse (&/$Right exo-type) (if ?right (inc idx) idx) ?right parameters*)))
(&/$Tag ?ident)
(&/with-analysis-meta cursor exo-type
diff --git a/luxc/src/lux/analyser/case.clj b/luxc/src/lux/analyser/case.clj
index 4d7b4ccca..d059ce189 100644
--- a/luxc/src/lux/analyser/case.clj
+++ b/luxc/src/lux/analyser/case.clj
@@ -360,15 +360,16 @@
[=test =kont] (analyse-pattern &/$None case-type unit-tuple kont)]
(return (&/T [($VariantTestAC (&/T [idx (&/|length group) =test])) =kont])))
- (&/$Form (&/$Cons [_ (&/$Nat idx)] ?values))
- (|do [value-type* (adjust-type value-type)
- case-type (&type/sum-at idx value-type*)
- [=test =kont] (case (int (&/|length ?values))
- 0 (analyse-pattern &/$None case-type unit-tuple kont)
- 1 (analyse-pattern &/$None case-type (&/|head ?values) kont)
- ;; 1+
- (analyse-pattern &/$None case-type (&/T [(&/T ["" -1 -1]) (&/$Tuple ?values)]) kont))]
- (return (&/T [($VariantTestAC (&/T [idx (&/|length (&type/flatten-sum value-type*)) =test])) =kont])))
+ (&/$Form (&/$Cons [_ (&/$Nat idx)] (&/$Cons [_ (&/$Bit right?)] ?values)))
+ (let [idx (if right? (inc idx) idx)]
+ (|do [value-type* (adjust-type value-type)
+ case-type (&type/sum-at idx value-type*)
+ [=test =kont] (case (int (&/|length ?values))
+ 0 (analyse-pattern &/$None case-type unit-tuple kont)
+ 1 (analyse-pattern &/$None case-type (&/|head ?values) kont)
+ ;; 1+
+ (analyse-pattern &/$None case-type (&/T [(&/T ["" -1 -1]) (&/$Tuple ?values)]) kont))]
+ (return (&/T [($VariantTestAC (&/T [idx (&/|length (&type/flatten-sum value-type*)) =test])) =kont]))))
(&/$Form (&/$Cons [_ (&/$Tag ?ident)] ?values))
(|do [[=module =name] (&&/resolved-ident ?ident)
diff --git a/luxc/src/lux/analyser/lux.clj b/luxc/src/lux/analyser/lux.clj
index fee56b624..eb47ac039 100644
--- a/luxc/src/lux/analyser/lux.clj
+++ b/luxc/src/lux/analyser/lux.clj
@@ -213,15 +213,11 @@
(|case exo-type*
(&/$Sum _)
(|do [vtype (&type/sum-at idx exo-type*)
- :let [num-variant-types (&/|length (&type/flatten-sum exo-type*))
- is-last?* (if (nil? is-last?)
- (= idx (dec num-variant-types))
- is-last?)]
=value (analyse-variant-body analyse vtype ?values)
_cursor &/cursor]
- (if (= 1 num-variant-types)
+ (if (= 1 (&/|length (&type/flatten-sum exo-type*)))
(return (&/|list =value))
- (return (&/|list (&&/|meta exo-type _cursor (&&/$variant idx is-last?* =value))))
+ (return (&/|list (&&/|meta exo-type _cursor (&&/$variant idx is-last? =value))))
))
(&/$UnivQ _)
diff --git a/stdlib/source/lux.lux b/stdlib/source/lux.lux
index e375c7ed5..d6fa1c40a 100644
--- a/stdlib/source/lux.lux
+++ b/stdlib/source/lux.lux
@@ -1,172 +1,172 @@
("lux def" dummy-cursor
["" 0 0]
- [["" 0 0] (10 (0))]
+ [["" 0 0] (9 #1 (0 #0))]
#1)
("lux def" double-quote
("lux i64 char" +34)
- [dummy-cursor (10 (0))]
+ [dummy-cursor (9 #1 (0 #0))]
#0)
("lux def" new-line
("lux i64 char" +10)
- [dummy-cursor (10 (0))]
+ [dummy-cursor (9 #1 (0 #0))]
#0)
("lux def" __paragraph
("lux text concat" new-line new-line)
- [dummy-cursor (10 (0))]
+ [dummy-cursor (9 #1 (0 #0))]
#0)
## (type: Any
## (Ex [a] a))
("lux def" Any
("lux check type"
- (10 ["lux" "Any"]
- (8 (0) (4 1))))
+ (9 #1 ["lux" "Any"]
+ (8 #0 (0 #0) (4 #0 1))))
[dummy-cursor
- (10 (1 [[dummy-cursor (7 ["lux" "doc"])]
- [dummy-cursor (5 ("lux text concat"
- ("lux text concat" "The type of things whose type is irrelevant." __paragraph)
- "It can be used to write functions or data-structures that can take, or return, anything."))]]
- (0)))]
+ (9 #1 (0 #1 [[dummy-cursor (7 #0 ["lux" "doc"])]
+ [dummy-cursor (5 #0 ("lux text concat"
+ ("lux text concat" "The type of things whose type is irrelevant." __paragraph)
+ "It can be used to write functions or data-structures that can take, or return, anything."))]]
+ (0 #0)))]
#1)
## (type: Nothing
## (All [a] a))
("lux def" Nothing
("lux check type"
- (10 ["lux" "Nothing"]
- (7 (0) (4 1))))
+ (9 #1 ["lux" "Nothing"]
+ (7 #0 (0 #0) (4 #0 1))))
[dummy-cursor
- (10 (1 [[dummy-cursor (7 ["lux" "doc"])]
- [dummy-cursor (5 ("lux text concat"
- ("lux text concat" "The type of things whose type is undefined." __paragraph)
- "Useful for expressions that cause errors or other 'extraordinary' conditions."))]]
- (0)))]
+ (9 #1 (0 #1 [[dummy-cursor (7 #0 ["lux" "doc"])]
+ [dummy-cursor (5 #0 ("lux text concat"
+ ("lux text concat" "The type of things whose type is undefined." __paragraph)
+ "Useful for expressions that cause errors or other 'extraordinary' conditions."))]]
+ (0 #0)))]
#1)
## (type: (List a)
## #Nil
## (#Cons a (List a)))
("lux def type tagged" List
- (10 ["lux" "List"]
- (7 (0)
- (1 ## "lux.Nil"
- Any
- ## "lux.Cons"
- (2 (4 1)
- (9 (4 1) (4 0))))))
+ (9 #1 ["lux" "List"]
+ (7 #0 (0 #0)
+ (1 #0 ## "lux.Nil"
+ Any
+ ## "lux.Cons"
+ (2 #0 (4 #0 1)
+ (9 #0 (4 #0 1) (4 #0 0))))))
[dummy-cursor
- (10 (1 [[dummy-cursor (7 ["lux" "type-args"])]
- [dummy-cursor (9 (1 [dummy-cursor (5 "a")] (0)))]]
- (1 [[dummy-cursor (7 ["lux" "doc"])]
- [dummy-cursor (5 "A potentially empty list of values.")]]
- (0))))]
+ (9 #1 (0 #1 [[dummy-cursor (7 #0 ["lux" "type-args"])]
+ [dummy-cursor (9 #0 (0 #1 [dummy-cursor (5 #0 "a")] (0 #0)))]]
+ (0 #1 [[dummy-cursor (7 #0 ["lux" "doc"])]
+ [dummy-cursor (5 #0 "A potentially empty list of values.")]]
+ (0 #0))))]
["Nil" "Cons"]
#1)
("lux def" Bit
("lux check type"
- (10 ["lux" "Bit"]
- (0 "#Bit" #Nil)))
+ (9 #1 ["lux" "Bit"]
+ (0 #0 "#Bit" #Nil)))
[dummy-cursor
- (10 (#Cons [[dummy-cursor (7 ["lux" "doc"])]
- [dummy-cursor (5 "Your standard, run-of-the-mill boolean values (as bits).")]]
- #Nil))]
+ (9 #1 (#Cons [[dummy-cursor (7 #0 ["lux" "doc"])]
+ [dummy-cursor (5 #0 "Your standard, run-of-the-mill boolean values (as bits).")]]
+ #Nil))]
#1)
("lux def" I64
("lux check type"
- (10 ["lux" "I64"]
- (7 (0)
- (0 "#I64" (#Cons (4 1) #Nil)))))
+ (9 #1 ["lux" "I64"]
+ (7 #0 (0 #0)
+ (0 #0 "#I64" (#Cons (4 #0 1) #Nil)))))
[dummy-cursor
- (10 (#Cons [[dummy-cursor (7 ["lux" "doc"])]
- [dummy-cursor (5 "64-bit integers without any semantics.")]]
- #Nil))]
+ (9 #1 (#Cons [[dummy-cursor (7 #0 ["lux" "doc"])]
+ [dummy-cursor (5 #0 "64-bit integers without any semantics.")]]
+ #Nil))]
#1)
("lux def" Nat
("lux check type"
- (10 ["lux" "Nat"]
- (0 "#I64" (#Cons (0 "#Nat" #Nil) #Nil))))
+ (9 #1 ["lux" "Nat"]
+ (0 #0 "#I64" (#Cons (0 #0 "#Nat" #Nil) #Nil))))
[dummy-cursor
- (10 (#Cons [[dummy-cursor (7 ["lux" "doc"])]
- [dummy-cursor (5 ("lux text concat"
- ("lux text concat" "Natural numbers (unsigned integers)." __paragraph)
- "They start at zero (0) and extend in the positive direction."))]]
- #Nil))]
+ (9 #1 (#Cons [[dummy-cursor (7 #0 ["lux" "doc"])]
+ [dummy-cursor (5 #0 ("lux text concat"
+ ("lux text concat" "Natural numbers (unsigned integers)." __paragraph)
+ "They start at zero (0) and extend in the positive direction."))]]
+ #Nil))]
#1)
("lux def" Int
("lux check type"
- (10 ["lux" "Int"]
- (0 "#I64" (#Cons (0 "#Int" #Nil) #Nil))))
+ (9 #1 ["lux" "Int"]
+ (0 #0 "#I64" (#Cons (0 #0 "#Int" #Nil) #Nil))))
[dummy-cursor
- (10 (#Cons [[dummy-cursor (7 ["lux" "doc"])]
- [dummy-cursor (5 "Your standard, run-of-the-mill integer numbers.")]]
- #Nil))]
+ (9 #1 (#Cons [[dummy-cursor (7 #0 ["lux" "doc"])]
+ [dummy-cursor (5 #0 "Your standard, run-of-the-mill integer numbers.")]]
+ #Nil))]
#1)
("lux def" Rev
("lux check type"
- (10 ["lux" "Rev"]
- (0 "#I64" (#Cons (0 "#Rev" #Nil) #Nil))))
+ (9 #1 ["lux" "Rev"]
+ (0 #0 "#I64" (#Cons (0 #0 "#Rev" #Nil) #Nil))))
[dummy-cursor
- (10 (#Cons [[dummy-cursor (7 ["lux" "doc"])]
- [dummy-cursor (5 ("lux text concat"
- ("lux text concat" "Fractional numbers that live in the interval [0,1)." __paragraph)
- "Useful for probability, and other domains that work within that interval."))]]
- #Nil))]
+ (9 #1 (#Cons [[dummy-cursor (7 #0 ["lux" "doc"])]
+ [dummy-cursor (5 #0 ("lux text concat"
+ ("lux text concat" "Fractional numbers that live in the interval [0,1)." __paragraph)
+ "Useful for probability, and other domains that work within that interval."))]]
+ #Nil))]
#1)
("lux def" Frac
("lux check type"
- (10 ["lux" "Frac"]
- (0 "#Frac" #Nil)))
+ (9 #1 ["lux" "Frac"]
+ (0 #0 "#Frac" #Nil)))
[dummy-cursor
- (10 (#Cons [[dummy-cursor (7 ["lux" "doc"])]
- [dummy-cursor (5 "Your standard, run-of-the-mill floating-point (fractional) numbers.")]]
- #Nil))]
+ (9 #1 (#Cons [[dummy-cursor (7 #0 ["lux" "doc"])]
+ [dummy-cursor (5 #0 "Your standard, run-of-the-mill floating-point (fractional) numbers.")]]
+ #Nil))]
#1)
("lux def" Text
("lux check type"
- (10 ["lux" "Text"]
- (0 "#Text" #Nil)))
+ (9 #1 ["lux" "Text"]
+ (0 #0 "#Text" #Nil)))
[dummy-cursor
- (10 (#Cons [[dummy-cursor (7 ["lux" "doc"])]
- [dummy-cursor (5 "Your standard, run-of-the-mill string values.")]]
- #Nil))]
+ (9 #1 (#Cons [[dummy-cursor (7 #0 ["lux" "doc"])]
+ [dummy-cursor (5 #0 "Your standard, run-of-the-mill string values.")]]
+ #Nil))]
#1)
("lux def" Name
("lux check type"
- (10 ["lux" "Name"]
- (2 Text Text)))
+ (9 #1 ["lux" "Name"]
+ (2 #0 Text Text)))
[dummy-cursor
- (10 (#Cons [[dummy-cursor (7 ["lux" "doc"])]
- [dummy-cursor (5 "A name. It is used as part of Lux syntax to represent identifiers and tags.")]]
- #Nil))]
+ (9 #1 (#Cons [[dummy-cursor (7 #0 ["lux" "doc"])]
+ [dummy-cursor (5 #0 "A name. It is used as part of Lux syntax to represent identifiers and tags.")]]
+ #Nil))]
#1)
## (type: (Maybe a)
## #None
## (#Some a))
("lux def type tagged" Maybe
- (10 ["lux" "Maybe"]
- (7 #Nil
- (1 ## "lux.None"
- Any
- ## "lux.Some"
- (4 1))))
+ (9 #1 ["lux" "Maybe"]
+ (7 #0 #Nil
+ (1 #0 ## "lux.None"
+ Any
+ ## "lux.Some"
+ (4 #0 1))))
[dummy-cursor
- (10 (#Cons [[dummy-cursor (7 ["lux" "type-args"])]
- [dummy-cursor (9 (#Cons [dummy-cursor (5 "a")] #Nil))]]
- (#Cons [[dummy-cursor (7 ["lux" "doc"])]
- [dummy-cursor (5 "A potentially missing value.")]]
- #Nil)))]
+ (9 #1 (#Cons [[dummy-cursor (7 #0 ["lux" "type-args"])]
+ [dummy-cursor (9 #0 (#Cons [dummy-cursor (5 #0 "a")] #Nil))]]
+ (#Cons [[dummy-cursor (7 #0 ["lux" "doc"])]
+ [dummy-cursor (5 #0 "A potentially missing value.")]]
+ #Nil)))]
["None" "Some"]
#1)
@@ -184,43 +184,43 @@
## (#Named Name Type)
## )
("lux def type tagged" Type
- (10 ["lux" "Type"]
- ({Type
- ({Type-List
- ({Type-Pair
- (9 Nothing
- (7 #Nil
- (1 ## "lux.Primitive"
- (2 Text Type-List)
- (1 ## "lux.Sum"
- Type-Pair
- (1 ## "lux.Product"
- Type-Pair
- (1 ## "lux.Function"
- Type-Pair
- (1 ## "lux.Parameter"
- Nat
- (1 ## "lux.Var"
- Nat
- (1 ## "lux.Ex"
- Nat
- (1 ## "lux.UnivQ"
- (2 Type-List Type)
- (1 ## "lux.ExQ"
- (2 Type-List Type)
- (1 ## "lux.Apply"
- Type-Pair
- ## "lux.Named"
- (2 Name Type)))))))))))))}
- ("lux check type" (2 Type Type)))}
- ("lux check type" (9 Type List)))}
- ("lux check type" (9 (4 1) (4 0)))))
+ (9 #1 ["lux" "Type"]
+ ({Type
+ ({Type-List
+ ({Type-Pair
+ (9 #0 Nothing
+ (7 #0 #Nil
+ (1 #0 ## "lux.Primitive"
+ (2 #0 Text Type-List)
+ (1 #0 ## "lux.Sum"
+ Type-Pair
+ (1 #0 ## "lux.Product"
+ Type-Pair
+ (1 #0 ## "lux.Function"
+ Type-Pair
+ (1 #0 ## "lux.Parameter"
+ Nat
+ (1 #0 ## "lux.Var"
+ Nat
+ (1 #0 ## "lux.Ex"
+ Nat
+ (1 #0 ## "lux.UnivQ"
+ (2 #0 Type-List Type)
+ (1 #0 ## "lux.ExQ"
+ (2 #0 Type-List Type)
+ (1 #0 ## "lux.Apply"
+ Type-Pair
+ ## "lux.Named"
+ (2 #0 Name Type)))))))))))))}
+ ("lux check type" (2 #0 Type Type)))}
+ ("lux check type" (9 #0 Type List)))}
+ ("lux check type" (9 #0 (4 #0 1) (4 #0 0)))))
[dummy-cursor
- (10 (#Cons [[dummy-cursor (7 ["lux" "doc"])]
- [dummy-cursor (5 "This type represents the data-structures that are used to specify types themselves.")]]
- (#Cons [[dummy-cursor (7 ["lux" "type-rec?"])]
- [dummy-cursor (0 #1)]]
- #Nil)))]
+ (9 #1 (#Cons [[dummy-cursor (7 #0 ["lux" "doc"])]
+ [dummy-cursor (5 #0 "This type represents the data-structures that are used to specify types themselves.")]]
+ (#Cons [[dummy-cursor (7 #0 ["lux" "type-rec?"])]
+ [dummy-cursor (0 #0 #1)]]
+ #Nil)))]
["Primitive" "Sum" "Product" "Function" "Parameter" "Var" "Ex" "UnivQ" "ExQ" "Apply" "Named"]
#1)
@@ -232,9 +232,9 @@
(#Named ["lux" "Cursor"]
(#Product Text (#Product Nat Nat)))
[dummy-cursor
- (10 (#Cons [[dummy-cursor (7 ["lux" "doc"])]
- [dummy-cursor (5 "Cursors are for specifying the location of Code nodes in Lux files during compilation.")]]
- #Nil))]
+ (9 #1 (#Cons [[dummy-cursor (7 #0 ["lux" "doc"])]
+ [dummy-cursor (5 #0 "Cursors are for specifying the location of Code nodes in Lux files during compilation.")]]
+ #Nil))]
["module" "line" "column"]
#1)
@@ -248,11 +248,11 @@
(#Product (#Parameter 3)
(#Parameter 1)))))
[dummy-cursor
- (10 (#Cons [[dummy-cursor (7 ["lux" "doc"])]
- [dummy-cursor (5 "The type of things that can be annotated with meta-data of arbitrary types.")]]
- (#Cons [[dummy-cursor (7 ["lux" "type-args"])]
- [dummy-cursor (9 (#Cons [dummy-cursor (5 "m")] (#Cons [dummy-cursor (5 "v")] #Nil)))]]
- #Nil)))]
+ (9 #1 (#Cons [[dummy-cursor (7 #0 ["lux" "doc"])]
+ [dummy-cursor (5 #0 "The type of things that can be annotated with meta-data of arbitrary types.")]]
+ (#Cons [[dummy-cursor (7 #0 ["lux" "type-args"])]
+ [dummy-cursor (9 #0 (#Cons [dummy-cursor (5 #0 "m")] (#Cons [dummy-cursor (5 #0 "v")] #Nil)))]]
+ #Nil)))]
["meta" "datum"]
#1)
@@ -302,9 +302,9 @@
(#Parameter 0))
(#Parameter 1)))))
[dummy-cursor
- (10 (#Cons [[dummy-cursor (7 ["lux" "type-args"])]
- [dummy-cursor (9 (#Cons [dummy-cursor (5 "w")] #Nil))]]
- #Nil))]
+ (9 #1 (#Cons [[dummy-cursor (7 #0 ["lux" "type-args"])]
+ [dummy-cursor (9 #0 (#Cons [dummy-cursor (5 #0 "w")] #Nil))]]
+ #Nil))]
["Bit" "Nat" "Int" "Rev" "Frac" "Text" "Identifier" "Tag" "Form" "Tuple" "Record"]
#1)
diff --git a/stdlib/source/lux/abstract/enum.lux b/stdlib/source/lux/abstract/enum.lux
index 9f074f7c5..9470cd142 100644
--- a/stdlib/source/lux/abstract/enum.lux
+++ b/stdlib/source/lux/abstract/enum.lux
@@ -12,10 +12,14 @@
(def: #export (range enum from to)
{#.doc "An inclusive [from, to] range of values."}
(All [a] (-> (Enum a) a a (List a)))
- (let [(^open "/@.") enum
- <= (order.<= /@&order)]
+ (let [(^open "/@.") enum]
(loop [end to
output #.Nil]
- (if (<= end from)
- (recur (/@pred end) (#.Cons end output))
- output))))
+ (cond (/@< end from)
+ (recur (/@pred end) (#.Cons end output))
+
+ (/@= end from)
+ (#.Cons end output)
+
+ ## else
+ output))))
diff --git a/stdlib/source/lux/control/concatenative.lux b/stdlib/source/lux/control/concatenative.lux
index 48c7cf2eb..1ba47f1af 100644
--- a/stdlib/source/lux/control/concatenative.lux
+++ b/stdlib/source/lux/control/concatenative.lux
@@ -176,12 +176,12 @@
(def: #export ||L
(All [a b] (=> [a] [(| a b)]))
(function (_ [stack l])
- [stack (0 l)]))
+ [stack (0 #0 l)]))
(def: #export ||R
(All [a b] (=> [b] [(| a b)]))
(function (_ [stack r])
- [stack (1 r)]))
+ [stack (0 #1 r)]))
(template [