From e37e3713e080606930a5f8442f03dabc4c26a7f9 Mon Sep 17 00:00:00 2001
From: Eduardo Julian
Date: Tue, 21 Nov 2017 16:09:07 -0400
Subject: - Fixed some bugs. - Some small refactoring.
---
stdlib/source/lux.lux | 609 +++++++++++++++++++++++++-------------------------
1 file changed, 305 insertions(+), 304 deletions(-)
(limited to 'stdlib/source/lux.lux')
diff --git a/stdlib/source/lux.lux b/stdlib/source/lux.lux
index 407e895a3..0dcc335a0 100644
--- a/stdlib/source/lux.lux
+++ b/stdlib/source/lux.lux
@@ -1189,7 +1189,7 @@
(#Function Nat Code)
(form$ (#Cons (tag$ ["lux" "Bound"]) (#Cons (nat$ idx) #Nil))))
-(def:'' (fold f init xs)
+(def:'' (list/fold f init xs)
#;Nil
## (All [a b] (-> (-> b a a) a (List b) a))
(#UnivQ #Nil (#UnivQ #Nil (#Function (#Function (#Bound +1)
@@ -1203,13 +1203,13 @@
init
(#Cons x xs')
- (fold f (f x init) xs')}))
+ (list/fold f (f x init) xs')}))
(def:'' (length list)
#;Nil
(#UnivQ #Nil
(#Function ($' List (#Bound +1)) Nat))
- (fold (function'' [_ acc] ("lux nat +" +1 acc)) +0 list))
+ (list/fold (function'' [_ acc] ("lux nat +" +1 acc)) +0 list))
(macro:' #export (All tokens)
(#Cons [(tag$ ["lux" "doc"])
@@ -1232,14 +1232,14 @@
{(#Cons [_ (#Tuple args)] (#Cons body #Nil))
(parse-quantified-args args
(function'' [names]
- (let'' body' (fold ("lux check" (#Function Text (#Function Code Code))
- (function'' [name' body']
- (form$ (#Cons (tag$ ["lux" "UnivQ"])
- (#Cons (tag$ ["lux" "Nil"])
- (#Cons (replace-syntax (#Cons [name' (make-bound +1)] #Nil)
- (update-bounds body')) #Nil))))))
- body
- names)
+ (let'' body' (list/fold ("lux check" (#Function Text (#Function Code Code))
+ (function'' [name' body']
+ (form$ (#Cons (tag$ ["lux" "UnivQ"])
+ (#Cons (tag$ ["lux" "Nil"])
+ (#Cons (replace-syntax (#Cons [name' (make-bound +1)] #Nil)
+ (update-bounds body')) #Nil))))))
+ body
+ names)
(return (#Cons ("lux case" [(text/= "" self-name) names]
{[true _]
body'
@@ -1283,14 +1283,14 @@
{(#Cons [_ (#Tuple args)] (#Cons body #Nil))
(parse-quantified-args args
(function'' [names]
- (let'' body' (fold ("lux check" (#Function Text (#Function Code Code))
- (function'' [name' body']
- (form$ (#Cons (tag$ ["lux" "ExQ"])
- (#Cons (tag$ ["lux" "Nil"])
- (#Cons (replace-syntax (#Cons [name' (make-bound +1)] #Nil)
- (update-bounds body')) #Nil))))))
- body
- names)
+ (let'' body' (list/fold ("lux check" (#Function Text (#Function Code Code))
+ (function'' [name' body']
+ (form$ (#Cons (tag$ ["lux" "ExQ"])
+ (#Cons (tag$ ["lux" "Nil"])
+ (#Cons (replace-syntax (#Cons [name' (make-bound +1)] #Nil)
+ (update-bounds body')) #Nil))))))
+ body
+ names)
(return (#Cons ("lux case" [(text/= "" self-name) names]
{[true _]
body'
@@ -1314,10 +1314,10 @@
(def:'' (reverse list)
#;Nil
(All [a] (#Function ($' List a) ($' List a)))
- (fold ("lux check" (All [a] (#Function a (#Function ($' List a) ($' List a))))
- (function'' [head tail] (#Cons head tail)))
- #Nil
- list))
+ (list/fold ("lux check" (All [a] (#Function a (#Function ($' List a) ($' List a))))
+ (function'' [head tail] (#Cons head tail)))
+ #Nil
+ list))
(macro:' #export (-> tokens)
(#Cons [(tag$ ["lux" "doc"])
@@ -1328,10 +1328,10 @@
#;Nil)
("lux case" (reverse tokens)
{(#Cons output inputs)
- (return (#Cons (fold ("lux check" (#Function Code (#Function Code Code))
- (function'' [i o] (form$ (#Cons (tag$ ["lux" "Function"]) (#Cons i (#Cons o #Nil))))))
- output
- inputs)
+ (return (#Cons (list/fold ("lux check" (#Function Code (#Function Code Code))
+ (function'' [i o] (form$ (#Cons (tag$ ["lux" "Function"]) (#Cons i (#Cons o #Nil))))))
+ output
+ inputs)
#Nil))
_
@@ -1342,12 +1342,12 @@
(text$ "## List-construction macro.
(list 1 2 3)")]
#;Nil)
- (return (#Cons (fold (function'' [head tail]
- (form$ (#Cons (tag$ ["lux" "Cons"])
- (#Cons (tuple$ (#Cons [head (#Cons [tail #Nil])]))
- #Nil))))
- (tag$ ["lux" "Nil"])
- (reverse xs))
+ (return (#Cons (list/fold (function'' [head tail]
+ (form$ (#Cons (tag$ ["lux" "Cons"])
+ (#Cons (tuple$ (#Cons [head (#Cons [tail #Nil])]))
+ #Nil))))
+ (tag$ ["lux" "Nil"])
+ (reverse xs))
#Nil)))
(macro:' #export (list& xs)
@@ -1358,11 +1358,11 @@
#;Nil)
("lux case" (reverse xs)
{(#Cons last init)
- (return (list (fold (function'' [head tail]
- (form$ (list (tag$ ["lux" "Cons"])
- (tuple$ (list head tail)))))
- last
- init)))
+ (return (list (list/fold (function'' [head tail]
+ (form$ (list (tag$ ["lux" "Cons"])
+ (tuple$ (list head tail)))))
+ last
+ init)))
_
(fail "Wrong syntax for list&")}))
@@ -1380,9 +1380,9 @@
(return (list (tag$ ["lux" "Unit"])))
(#Cons last prevs)
- (return (list (fold (function'' [left right] (form$ (list (tag$ ["lux" "Product"]) left right)))
- last
- prevs)))}
+ (return (list (list/fold (function'' [left right] (form$ (list (tag$ ["lux" "Product"]) left right)))
+ last
+ prevs)))}
))
(macro:' #export (| tokens)
@@ -1398,9 +1398,9 @@
(return (list (tag$ ["lux" "Void"])))
(#Cons last prevs)
- (return (list (fold (function'' [left right] (form$ (list (tag$ ["lux" "Sum"]) left right)))
- last
- prevs)))}
+ (return (list (list/fold (function'' [left right] (form$ (list (tag$ ["lux" "Sum"]) left right)))
+ last
+ prevs)))}
))
(macro:' (function' tokens)
@@ -1420,13 +1420,13 @@
(return (list (form$ (list (text$ "lux function")
(symbol$ ["" name])
harg
- (fold (function'' [arg body']
- (form$ (list (text$ "lux function")
- (symbol$ ["" ""])
- arg
- body')))
- body
- (reverse targs))))))})
+ (list/fold (function'' [arg body']
+ (form$ (list (text$ "lux function")
+ (symbol$ ["" ""])
+ arg
+ body')))
+ body
+ (reverse targs))))))})
_
(fail "Wrong syntax for function'")})))
@@ -1497,14 +1497,14 @@
(macro:' (let' tokens)
("lux case" tokens
{(#Cons [[_ (#Tuple bindings)] (#Cons [body #Nil])])
- (return (list (fold ("lux check" (-> (& Code Code) Code
- Code)
- (function' [binding body]
- ("lux case" binding
- {[label value]
- (form$ (list (text$ "lux case") value (record$ (list [label body]))))})))
- body
- (reverse (as-pairs bindings)))))
+ (return (list (list/fold ("lux check" (-> (& Code Code) Code
+ Code)
+ (function' [binding body]
+ ("lux case" binding
+ {[label value]
+ (form$ (list (text$ "lux case") value (record$ (list [label body]))))})))
+ body
+ (reverse (as-pairs bindings)))))
_
(fail "Wrong syntax for let'")}))
@@ -1522,16 +1522,6 @@
{true true
false (any? p xs')})}))
-(def:''' (spliced? token)
- #;Nil
- (-> Code Bool)
- ("lux case" token
- {[_ (#Form (#Cons [[_ (#Symbol ["" "~@"])] (#Cons [_ #Nil])]))]
- true
-
- _
- false}))
-
(def:''' (wrap-meta content)
#;Nil
(-> Code Code)
@@ -1592,7 +1582,7 @@
{(#Cons op tokens')
("lux case" tokens'
{(#Cons first nexts)
- (return (list (fold (_$_joiner op) first nexts)))
+ (return (list (list/fold (_$_joiner op) first nexts)))
_
(fail "Wrong syntax for _$")})
@@ -1612,7 +1602,7 @@
{(#Cons op tokens')
("lux case" (reverse tokens')
{(#Cons last prevs)
- (return (list (fold (_$_joiner op) last prevs)))
+ (return (list (list/fold (_$_joiner op) last prevs)))
_
(fail "Wrong syntax for $_")})
@@ -1672,19 +1662,19 @@
{(#Cons monad (#Cons [_ (#Tuple bindings)] (#Cons body #Nil)))
(let' [g!wrap (symbol$ ["" "wrap"])
g!bind (symbol$ ["" " bind "])
- body' (fold ("lux check" (-> (& Code Code) Code Code)
- (function' [binding body']
- (let' [[var value] binding]
- ("lux case" var
- {[_ (#Tag "" "let")]
- (form$ (list (symbol$ ["lux" "let'"]) value body'))
-
- _
- (form$ (list g!bind
- (form$ (list (text$ "lux function") (symbol$ ["" ""]) var body'))
- value))}))))
- body
- (reverse (as-pairs bindings)))]
+ body' (list/fold ("lux check" (-> (& Code Code) Code Code)
+ (function' [binding body']
+ (let' [[var value] binding]
+ ("lux case" var
+ {[_ (#Tag "" "let")]
+ (form$ (list (symbol$ ["lux" "let'"]) value body'))
+
+ _
+ (form$ (list g!bind
+ (form$ (list (text$ "lux function") (symbol$ ["" ""]) var body'))
+ value))}))))
+ body
+ (reverse (as-pairs bindings)))]
(return (list (form$ (list (text$ "lux case")
monad
(record$ (list [(record$ (list [(tag$ ["lux" "wrap"]) g!wrap] [(tag$ ["lux" "bind"]) g!bind]))
@@ -1714,6 +1704,27 @@
(wrap (#Cons y ys)))
})))
+(def:''' (monad/fold m f y xs)
+ #Nil
+ ## (All [m a b]
+ ## (-> (Monad m) (-> a b (m b)) b (List a) (m b)))
+ (All [m a b]
+ (-> ($' Monad m)
+ (-> a b ($' m b))
+ b
+ ($' List a)
+ ($' m b)))
+ (let' [{#;wrap wrap #;bind _} m]
+ ("lux case" xs
+ {#Nil
+ (wrap y)
+
+ (#Cons x xs')
+ (do m
+ [y' (f x y)]
+ (monad/fold m f y' xs'))
+ })))
+
(macro:' #export (if tokens)
(list [(tag$ ["lux" "doc"])
(text$ "Picks which expression to evaluate based on a boolean test value.
@@ -1831,42 +1842,43 @@
#None
(#Left ($_ text/compose "Unknown module: " module " @ " (ident/encode ident)))})))
-(def:''' (splice replace? untemplate tag elems)
+(def:''' (splice replace? untemplate elems)
#Nil
- (-> Bool (-> Code ($' Meta Code)) Code ($' List Code) ($' Meta Code))
+ (-> Bool (-> Code ($' Meta Code)) ($' List Code) ($' Meta Code))
("lux case" replace?
{true
- ("lux case" (any? spliced? elems)
- {true
+ ("lux case" (reverse elems)
+ {#Nil
+ (return (tag$ ["lux" "Nil"]))
+
+ (#Cons lastI inits)
(do Monad
- [elems' ("lux check" ($' Meta ($' List Code))
- (monad/map Monad
- ("lux check" (-> Code ($' Meta Code))
- (function' [elem]
- ("lux case" elem
- {[_ (#Form (#Cons [[_ (#Symbol ["" "~@"])] (#Cons [spliced #Nil])]))]
- (wrap spliced)
-
- _
- (do Monad
- [=elem (untemplate elem)]
- (wrap (form$ (list (text$ "lux check")
- (form$ (list (tag$ ["lux" "Apply"]) (tuple$ (list (symbol$ ["lux" "Code"]) (symbol$ ["lux" "List"])))))
- (form$ (list (tag$ ["lux" "Cons"]) (tuple$ (list =elem (tag$ ["lux" "Nil"])))))))))})))
- elems))]
- (wrap (wrap-meta (form$ (list tag
- (form$ (list& (symbol$ ["lux" "$_"])
- (symbol$ ["lux" "splice-helper"])
- elems')))))))
+ [lastO ("lux case" lastI
+ {[_ (#Form (#Cons [[_ (#Symbol ["" "~@"])] (#Cons [spliced #Nil])]))]
+ (wrap spliced)
+
+ _
+ (do Monad
+ [lastO (untemplate lastI)]
+ (wrap (form$ (list (tag$ ["lux" "Cons"]) (tuple$ (list lastO (tag$ ["lux" "Nil"])))))))})]
+ (monad/fold Monad
+ (function' [leftI rightO]
+ ("lux case" leftI
+ {[_ (#Form (#Cons [[_ (#Symbol ["" "~@"])] (#Cons [spliced #Nil])]))]
+ (wrap (form$ (list (symbol$ ["lux" "splice-helper"])
+ spliced
+ rightO)))
- false
- (do Monad
- [=elems (monad/map Monad untemplate elems)]
- (wrap (wrap-meta (form$ (list tag (untemplate-list =elems))))))})
+ _
+ (do Monad
+ [leftO (untemplate leftI)]
+ (wrap (form$ (list (tag$ ["lux" "Cons"]) (tuple$ (list leftO rightO))))))}))
+ lastO
+ inits))})
false
(do Monad
[=elems (monad/map Monad untemplate elems)]
- (wrap (wrap-meta (form$ (list tag (untemplate-list =elems))))))}))
+ (wrap (untemplate-list =elems)))}))
(def:''' (untemplate replace? subst token)
#Nil
@@ -1918,9 +1930,6 @@
[false [_ (#Symbol [module name])]]
(return (wrap-meta (form$ (list (tag$ ["lux" "Symbol"]) (tuple$ (list (text$ module) (text$ name)))))))
- [_ [_ (#Tuple elems)]]
- (splice replace? (untemplate replace? subst) (tag$ ["lux" "Tuple"]) elems)
-
[true [_ (#Form (#Cons [[_ (#Symbol ["" "~"])] (#Cons [unquoted #Nil])]))]]
(return unquoted)
@@ -1929,9 +1938,15 @@
[_ [meta (#Form elems)]]
(do Monad
- [output (splice replace? (untemplate replace? subst) (tag$ ["lux" "Form"]) elems)
- #let [[_ form'] output]]
- (return [meta form']))
+ [output (splice replace? (untemplate replace? subst) elems)
+ #let [[_ output'] (wrap-meta (form$ (list (tag$ ["lux" "Form"]) output)))]]
+ (wrap [meta output']))
+
+ [_ [meta (#Tuple elems)]]
+ (do Monad
+ [output (splice replace? (untemplate replace? subst) elems)
+ #let [[_ output'] (wrap-meta (form$ (list (tag$ ["lux" "Tuple"]) output)))]]
+ (wrap [meta output']))
[_ [_ (#Record fields)]]
(do Monad
@@ -2041,19 +2056,19 @@
(map int/encode elems)))")])
("lux case" tokens
{(#Cons [init apps])
- (return (list (fold ("lux check" (-> Code Code Code)
- (function' [app acc]
- ("lux case" app
- {[_ (#Tuple parts)]
- (tuple$ (list/compose parts (list acc)))
+ (return (list (list/fold ("lux check" (-> Code Code Code)
+ (function' [app acc]
+ ("lux case" app
+ {[_ (#Tuple parts)]
+ (tuple$ (list/compose parts (list acc)))
- [_ (#Form parts)]
- (form$ (list/compose parts (list acc)))
+ [_ (#Form parts)]
+ (form$ (list/compose parts (list acc)))
- _
- (` ((~ app) (~ acc)))})))
- init
- apps)))
+ _
+ (` ((~ app) (~ acc)))})))
+ init
+ apps)))
_
(fail "Wrong syntax for |>")}))
@@ -2069,19 +2084,19 @@
(map int/encode elems)))")])
("lux case" (reverse tokens)
{(#Cons [init apps])
- (return (list (fold ("lux check" (-> Code Code Code)
- (function' [app acc]
- ("lux case" app
- {[_ (#Tuple parts)]
- (tuple$ (list/compose parts (list acc)))
+ (return (list (list/fold ("lux check" (-> Code Code Code)
+ (function' [app acc]
+ ("lux case" app
+ {[_ (#Tuple parts)]
+ (tuple$ (list/compose parts (list acc)))
- [_ (#Form parts)]
- (form$ (list/compose parts (list acc)))
+ [_ (#Form parts)]
+ (form$ (list/compose parts (list acc)))
- _
- (` ((~ app) (~ acc)))})))
- init
- apps)))
+ _
+ (` ((~ app) (~ acc)))})))
+ init
+ apps)))
_
(fail "Wrong syntax for <|")}))
@@ -2176,7 +2191,7 @@
#Nil
(All [a]
(-> (-> a Bool) ($' List a) Bool))
- (fold (function' [_2 _1] (if _1 (p _2) false)) true xs))
+ (list/fold (function' [_2 _1] (if _1 (p _2) false)) true xs))
(macro:' #export (do-template tokens)
(list [(tag$ ["lux" "doc"])
@@ -2341,11 +2356,11 @@
_
(let' [loop ("lux check" (-> Nat Text Text)
(function' recur [input output]
- (if ("lux nat =" input +0)
- ("lux text concat" "+" output)
- (recur ("lux nat /" input +10)
- ("lux text concat" (digit-to-text ("lux nat %" input +10))
- output)))))]
+ (if (n.= +0 input)
+ (text/compose "+" output)
+ (recur (n./ +10 input)
+ (text/compose (|> input (n.% +10) digit-to-text)
+ output)))))]
(loop value ""))}))
(def:''' (int/abs value)
@@ -2366,10 +2381,10 @@
(("lux check" (-> Int Text Text)
(function' recur [input output]
(if (i.= 0 input)
- ("lux text concat" sign output)
+ (text/compose sign output)
(recur (i./ 10 input)
- ("lux text concat" (|> input (i.% 10) ("lux coerce" Nat) digit-to-text)
- output)))))
+ (text/compose (|> input (i.% 10) ("lux coerce" Nat) digit-to-text)
+ output)))))
(|> value (i./ 10) int/abs)
(|> value (i.% 10) int/abs ("lux coerce" Nat) digit-to-text)))))
@@ -2465,7 +2480,7 @@
#Nil
(All [a]
(-> ($' List ($' List a)) ($' List a)))
- (fold list/compose #Nil (reverse xs)))
+ (list/fold list/compose #Nil (reverse xs)))
(def:''' (interpose sep xs)
#Nil
@@ -2580,10 +2595,10 @@
(` (& (~@ (map walk-type members))))
[_ (#Form (#Cons type-fn args))]
- (fold ("lux check" (-> Code Code Code)
- (function' [arg type-fn] (` (#;Apply (~ arg) (~ type-fn)))))
- (walk-type type-fn)
- (map walk-type args))
+ (list/fold ("lux check" (-> Code Code Code)
+ (function' [arg type-fn] (` (#;Apply (~ arg) (~ type-fn)))))
+ (walk-type type-fn)
+ (map walk-type args))
_
type}))
@@ -2740,10 +2755,10 @@
("lux case" (reverse tokens)
{(#Cons value actions)
(let' [dummy (symbol$ ["" ""])]
- (return (list (fold ("lux check" (-> Code Code Code)
- (function' [pre post] (` ("lux case" (~ pre) {(~ dummy) (~ post)}))))
- value
- actions))))
+ (return (list (list/fold ("lux check" (-> Code Code Code)
+ (function' [pre post] (` ("lux case" (~ pre) {(~ dummy) (~ post)}))))
+ value
+ actions))))
_
(fail "Wrong syntax for exec")}))
@@ -2835,21 +2850,21 @@
(map code-to-text)
(interpose " ")
reverse
- (fold text/compose "")) ")")
+ (list/fold text/compose "")) ")")
[_ (#Tuple xs)]
($_ text/compose "[" (|> xs
(map code-to-text)
(interpose " ")
reverse
- (fold text/compose "")) "]")
+ (list/fold text/compose "")) "]")
[_ (#Record kvs)]
($_ text/compose "{" (|> kvs
(map (function' [kv] ("lux case" kv {[k v] ($_ text/compose (code-to-text k) " " (code-to-text v))})))
(interpose " ")
reverse
- (fold text/compose "")) "}")}
+ (list/fold text/compose "")) "}")}
))
(def:' (expander branches)
@@ -2883,7 +2898,7 @@
(map code-to-text)
(interpose " ")
reverse
- (fold text/compose ""))))}))
+ (list/fold text/compose ""))))}))
(macro:' #export (case tokens)
(list [(tag$ ["lux" "doc"])
@@ -2983,13 +2998,13 @@
(^ (list [_ (#Tuple bindings)] body))
(if (multiple? +2 (length bindings))
(|> bindings as-pairs reverse
- (fold (: (-> [Code Code] Code Code)
- (function' [lr body']
- (let' [[l r] lr]
- (if (symbol? l)
- (` ("lux case" (~ r) {(~ l) (~ body')}))
- (` (case (~ r) (~ l) (~ body')))))))
- body)
+ (list/fold (: (-> [Code Code] Code Code)
+ (function' [lr body']
+ (let' [[l r] lr]
+ (if (symbol? l)
+ (` ("lux case" (~ r) {(~ l) (~ body')}))
+ (` (case (~ r) (~ l) (~ body')))))))
+ body)
list
return)
(fail "let requires an even number of parts"))
@@ -3019,14 +3034,14 @@
(#Some ident head tail body)
(let [g!blank (symbol$ ["" ""])
g!name (symbol$ ident)
- body+ (fold (: (-> Code Code Code)
- (function' [arg body']
- (if (symbol? arg)
- (` ("lux function" (~ g!blank) (~ arg) (~ body')))
- (` ("lux function" (~ g!blank) (~ g!blank)
- (case (~ g!blank) (~ arg) (~ body')))))))
- body
- (reverse tail))]
+ body+ (list/fold (: (-> Code Code Code)
+ (function' [arg body']
+ (if (symbol? arg)
+ (` ("lux function" (~ g!blank) (~ arg) (~ body')))
+ (` ("lux function" (~ g!blank) (~ g!blank)
+ (case (~ g!blank) (~ arg) (~ body')))))))
+ body
+ (reverse tail))]
(return (list (if (symbol? head)
(` ("lux function" (~ g!name) (~ head) (~ body+)))
(` ("lux function" (~ g!name) (~ g!blank) (case (~ g!blank) (~ head) (~ body+))))))))
@@ -3222,7 +3237,7 @@
(-> Code Code Code)
(case addition
[cursor (#;Record pairs)]
- (fold meta-code-add base pairs)
+ (list/fold meta-code-add base pairs)
_
base))
@@ -3360,10 +3375,10 @@
{#;doc }
(case (reverse tokens)
(^ (list& last init))
- (return (list (fold (: (-> Code Code Code)
- (function [pre post] (`