diff options
Diffstat (limited to '')
-rw-r--r-- | stdlib/source/lux/data/error/exception.lux | 2 | ||||
-rw-r--r-- | stdlib/source/lux/data/format/json.lux | 70 | ||||
-rw-r--r-- | stdlib/source/lux/data/struct/vector.lux | 15 | ||||
-rw-r--r-- | stdlib/source/lux/lexer.lux | 2 | ||||
-rw-r--r-- | stdlib/source/lux/macro/poly.lux | 44 | ||||
-rw-r--r-- | stdlib/source/lux/macro/syntax/common.lux | 27 | ||||
-rw-r--r-- | stdlib/source/lux/math/random.lux | 10 | ||||
-rw-r--r-- | stdlib/source/lux/type.lux | 97 |
8 files changed, 142 insertions, 125 deletions
diff --git a/stdlib/source/lux/data/error/exception.lux b/stdlib/source/lux/data/error/exception.lux index be9a09327..c3e9143e2 100644 --- a/stdlib/source/lux/data/error/exception.lux +++ b/stdlib/source/lux/data/error/exception.lux @@ -35,7 +35,7 @@ (#;Right (then error)) (#;Left error)))) -(def: #export (else to-do try) +(def: #export (otherwise to-do try) (All [a] (-> (-> Text a) (Error a) a)) (case try diff --git a/stdlib/source/lux/data/format/json.lux b/stdlib/source/lux/data/format/json.lux index c51e4b04c..d48b5b97a 100644 --- a/stdlib/source/lux/data/format/json.lux +++ b/stdlib/source/lux/data/format/json.lux @@ -67,8 +67,8 @@ wrapper (lambda [x] (` (;;json (~ x))))] (case token (^template [<ast-tag> <ctor> <json-tag>] - [_ (<ast-tag> value)] - (wrap (list (` (: JSON (<json-tag> (~ (<ctor> value)))))))) + [_ (<ast-tag> value)] + (wrap (list (` (: JSON (<json-tag> (~ (<ctor> value)))))))) ([#;BoolS ast;bool #Boolean] [#;IntS (|>. int-to-real ast;real) #Number] [#;RealS ast;real #Number] @@ -109,7 +109,7 @@ (def: (show-array show-json elems) (-> (-> JSON Text) (-> Array Text)) (format "[" - (|> elems (Vector/map show-json) vector;vector-to-list (text;join-with ",")) + (|> elems (Vector/map show-json) vector;to-list (text;join-with ",")) "]")) (def: (show-object show-json object) @@ -125,8 +125,8 @@ (-> JSON Text) (case json (^template [<tag> <show>] - (<tag> value) - (<show> value)) + (<tag> value) + (<show> value)) ([#Null show-null] [#Boolean show-boolean] [#Number show-number] @@ -316,7 +316,7 @@ _ (lexer;this-char <close>)] (wrap (<prep> elems))))] - [array~ Array #"[" #"]" (json~ []) vector;list-to-vector] + [array~ Array #"[" #"]" (json~ []) vector;from-list] [object~ Object #"{" #"}" (kv~ json~) (dict;from-list text;Hash<Text>)] ) @@ -485,7 +485,7 @@ (case json (#Array values) (do Monad<Error> - [elems (mapM @ parser (vector;vector-to-list values))] + [elems (mapM @ parser (vector;to-list values))] (wrap elems)) _ @@ -631,8 +631,8 @@ true (^template [<tag> <struct>] - [(<tag> x') (<tag> y')] - (:: <struct> = x' y')) + [(<tag> x') (<tag> y')] + (:: <struct> = x' y')) ([#Boolean bool;Eq<Bool>] [#Number number;Eq<Real>] [#String text;Eq<Text>]) @@ -719,18 +719,18 @@ (All [a b] (-> (-> a b) (List a) (List b))) List/map) -(poly: #export (|Codec@JSON//encode| *env* :x:) +(poly: #export (Codec<JSON,?>//encode *env* :x:) (let [->Codec//encode (: (-> AST AST) (lambda [.type.] (` (-> (~ .type.) JSON))))] (let% [<basic> (do-template [<type> <matcher> <encoder>] [(do @ [_ (<matcher> :x:)] (wrap (` (: (~ (->Codec//encode (` <type>))) <encoder>))))] [Unit poly;unit (lambda [(~ (ast;symbol ["" "0"]))] #Null)] - [Bool poly;bool ;;boolean] - [Int poly;int (|>. int-to-real ;;number)] - [Real poly;real ;;number] - [Char poly;char (|>. char;->Text ;;string)] - [Text poly;text ;;string])] + [Bool poly;bool ;;gen-boolean] + [Int poly;int (|>. ;int-to-real ;;gen-number)] + [Real poly;real ;;gen-number] + [Char poly;char (|>. char;as-text ;;gen-string)] + [Text poly;text ;;gen-string])] ($_ compiler;either <basic> (with-gensyms [g!type-fun g!case g!input g!key g!val] @@ -745,7 +745,7 @@ _ (compiler;fail "")) #let [new-*env* (poly;extend-env g!type-fun g!vars *env*)] - .val. (|Codec@JSON//encode| new-*env* :val:) + .val. (Codec<JSON,?>//encode new-*env* :val:) #let [:x:+ (case g!vars #;Nil (->Codec//encode (type;type-to-ast :x:)) @@ -767,14 +767,14 @@ )) (do @ [:sub: (poly;maybe :x:) - .sub. (|Codec@JSON//encode| *env* :sub:)] + .sub. (Codec<JSON,?>//encode *env* :sub:)] (wrap (` (: (~ (->Codec//encode (type;type-to-ast :x:))) - (;;nullable (~ .sub.)))))) + (;;gen-nullable (~ .sub.)))))) (do @ [:sub: (poly;list :x:) - .sub. (|Codec@JSON//encode| *env* :sub:)] + .sub. (Codec<JSON,?>//encode *env* :sub:)] (wrap (` (: (~ (->Codec//encode (type;type-to-ast :x:))) - (|>. (_map_ (~ .sub.)) vector;list-to-vector ;;array))))) + (|>. (_map_ (~ .sub.)) vector;from-list ;;gen-array))))) (with-gensyms [g!type-fun g!case g!input] (do @ [[g!vars cases] (poly;variant :x:) @@ -783,7 +783,7 @@ (lambda [[name :case:]] (do @ [#let [tag (ast;tag name)] - encoder (|Codec@JSON//encode| new-*env* :case:)] + encoder (Codec<JSON,?>//encode new-*env* :case:)] (wrap (list (` ((~ tag) (~ g!case))) (` (;;json [(~ (ast;text (product;right name))) ((~ encoder) (~ g!case))])))))) @@ -808,7 +808,7 @@ synthesis (mapM @ (lambda [[name :slot:]] (do @ - [encoder (|Codec@JSON//encode| new-*env* :slot:)] + [encoder (Codec<JSON,?>//encode new-*env* :slot:)] (wrap [(` (~ (ast;text (product;right name)))) (` ((~ encoder) (get@ (~ (ast;tag name)) (~ g!input))))]))) slots) @@ -824,7 +824,7 @@ (lambda [(~@ g!vars) (~ g!input)] (;;json (~ (ast;record synthesis)))) ))))) - (with-gensyms [g!type-fun g!case g!input] + (with-gensyms [g!type-fun g!case] (do @ [[g!vars members] (poly;tuple :x:) #let [new-*env* (poly;extend-env g!type-fun g!vars *env*)] @@ -832,7 +832,7 @@ (lambda [:member:] (do @ [g!member (compiler;gensym "g!member") - encoder (|Codec@JSON//encode| new-*env* :member:)] + encoder (Codec<JSON,?>//encode new-*env* :member:)] (wrap [g!member encoder]))) members) #let [:x:+ (case g!vars @@ -845,18 +845,16 @@ (~ (->Codec//encode (` ((~ (type;type-to-ast :x:)) (~@ g!vars)))))))))] #let [.tuple. (` [(~@ (List/map product;left pattern-matching))])]] (wrap (` (: (~ :x:+) - (lambda [(~@ g!vars) (~ g!input)] - (case (~ g!input) - (~ .tuple.) - (;;array (list (~@ (List/map (lambda [[g!member g!encoder]] - (` ((~ g!encoder) (~ g!member)))) - pattern-matching)))))) + (lambda [(~@ g!vars) (~ .tuple.)] + (;;json [(~@ (List/map (lambda [[g!member g!encoder]] + (` ((~ g!encoder) (~ g!member)))) + pattern-matching))])) ))) )) (do @ [[:func: :args:] (poly;apply :x:) - .func. (|Codec@JSON//encode| *env* :func:) - .args. (mapM @ (|Codec@JSON//encode| *env*) :args:)] + .func. (Codec<JSON,?>//encode *env* :func:) + .args. (mapM @ (Codec<JSON,?>//encode *env*) :args:)] (wrap (` (: (~ (->Codec//encode (type;type-to-ast :x:))) ((~ .func.) (~@ .args.)))))) (poly;bound *env* :x:) @@ -869,7 +867,7 @@ (let% [<basic> (do-template [<type> <matcher> <decoder>] [(do @ [_ (<matcher> :x:)] (wrap (` (: (~ (->Codec//decode (` <type>))) <decoder>))))] - [Unit poly;unit ;;null] + [Unit poly;unit ;;unit] [Bool poly;bool ;;bool] [Int poly;int ;;int] [Real poly;real ;;real] @@ -931,8 +929,8 @@ [#let [tag (ast;tag name)] decoder (Codec<JSON,?>//decode new-*env* :case:)] (wrap (list (` (do Monad<Parser> - [(~ g!_) (;;at 0 (;;text! (~ (ast;text (product;right name))))) - (~ g!_) (;;at 1 (~ decoder))] + [(~ g!_) (;;at +0 (;;text! (~ (ast;text (product;right name))))) + (~ g!_) (;;at +1 (~ decoder))] ((~ (' wrap)) ((~ tag) (~ g!_))))))))) cases) #let [:x:+ (case g!vars @@ -1026,6 +1024,6 @@ (syntax: #export (Codec<JSON,?> :x:) (wrap (list (` (: (Codec JSON (~ :x:)) (struct - (def: (~ (' encode)) (|Codec@JSON//encode| (~ :x:))) + (def: (~ (' encode)) (Codec<JSON,?>//encode (~ :x:))) (def: (~ (' decode)) (Codec<JSON,?>//decode (~ :x:))) )))))) diff --git a/stdlib/source/lux/data/struct/vector.lux b/stdlib/source/lux/data/struct/vector.lux index 4b8fb4bf3..9c04bc173 100644 --- a/stdlib/source/lux/data/struct/vector.lux +++ b/stdlib/source/lux/data/struct/vector.lux @@ -365,12 +365,11 @@ (struct: #export (Eq<Vector> Eq<a>) (All [a] (-> (Eq a) (Eq (Vector a)))) (def: (= v1 v2) (and (=+ (get@ #size v1) (get@ #size v2)) - (:: (Eq<Node> Eq<a>) = - (#Base (get@ #tail v1)) - (#Base (get@ #tail v2))) - (:: (Eq<Node> Eq<a>) = - (#Hierarchy (get@ #root v1)) - (#Hierarchy (get@ #root v2)))))) + (let [(^open "Node/") (Eq<Node> Eq<a>)] + (and (Node/= (#Base (get@ #tail v1)) + (#Base (get@ #tail v2))) + (Node/= (#Hierarchy (get@ #root v1)) + (#Hierarchy (get@ #root v2)))))))) (struct: _ (Fold Node) (def: (fold f init xs) @@ -436,9 +435,9 @@ (struct: #export _ (Monad Vector) (def: applicative Applicative<Vector>) - (def: (join ffa) + (def: join (let [(^open) Functor<Vector> (^open) Fold<Vector> (^open) Monoid<Vector>] - (fold append unit ffa))) + (fold append unit))) ) diff --git a/stdlib/source/lux/lexer.lux b/stdlib/source/lux/lexer.lux index 654259d8d..77ce0ce93 100644 --- a/stdlib/source/lux/lexer.lux +++ b/stdlib/source/lux/lexer.lux @@ -4,7 +4,7 @@ ## You can obtain one at http://mozilla.org/MPL/2.0/. (;module: - [lux #- not] + [lux #- not default] (lux (control functor applicative monad diff --git a/stdlib/source/lux/macro/poly.lux b/stdlib/source/lux/macro/poly.lux index ac7043f26..914575cc0 100644 --- a/stdlib/source/lux/macro/poly.lux +++ b/stdlib/source/lux/macro/poly.lux @@ -9,7 +9,7 @@ [eq]) (data [text] text/format - (struct [list "List/" Monad<List>] + (struct [list "List/" Fold<List> Monad<List>] [dict #+ Dict]) [number] [product] @@ -140,7 +140,7 @@ _ (:: compiler;Monad<Lux> wrap [(;list) :type:]))))) -(do-template [<combinator> <sub-comb>] +(do-template [<combinator> <sub-comb> <build>] [(def: #export <combinator> (Matcher [(List AST) (List [Ident Type])]) (lambda [:type:] @@ -148,11 +148,17 @@ [[tags :type:] (tagged :type:) _ (compiler;assert (>+ +0 (list;size tags)) "Records and variants must have tags.") [vars :type:] (polymorphic :type:) - members (<sub-comb> :type:)] - (wrap [vars (list;zip2 tags members)]))))] - - [variant sum+] - [record prod+] + members (<sub-comb> :type:) + #let [num-tags (list;size tags) + [init-tags last-tag] (list;split (dec+ num-tags) tags) + [init-types last-types] (list;split (dec+ num-tags) members)]] + (wrap [vars (list;concat (;list (list;zip2 init-tags init-types) + (;list [(default (undefined) + (list;head last-tag)) + (<build> last-types)])))]))))] + + [variant sum+ type;variant] + [record prod+ type;tuple] ) (def: #export tuple @@ -301,35 +307,21 @@ (def: (common-poly-name? poly-func) (-> Text Bool) - (and (text;starts-with? "|" poly-func) - (text;ends-with? "|" poly-func))) + (text;contains? "?" poly-func)) (def: (derivation-name poly args) (-> Text (List Text) (Maybe Text)) (if (common-poly-name? poly) - (case (text;sub +1 (dec+ (text;size poly)) poly) - (#;Some clean-poly) - (case (list;reverse args) - #;Nil - #;None - - (#;Cons type #;Nil) - (#;Some (format type "/" clean-poly)) - - (#;Cons type args) - (#;Some (format type "/" clean-poly "@" (|> args list;reverse (text;join-with ","))))) - - #;None - #;None) + (#;Some (List/fold (text;replace-once "?") poly args)) #;None)) (syntax: #export (derived: {_ex-lev common;export-level} {?name (s;opt s;local-symbol)} - {[poly-func poly-args] (s;either (s;form (s;seq s;symbol (s;many s;symbol))) - (s;seq s;symbol (:: @ wrap (;list))))} + {[poly-func poly-args] (s;form (s;seq s;symbol (s;many s;symbol)))} {?custom-impl (s;opt s;any)}) (do @ - [name (case ?name + [poly-args (mapM @ compiler;normalize poly-args) + name (case ?name (#;Some name) (wrap name) diff --git a/stdlib/source/lux/macro/syntax/common.lux b/stdlib/source/lux/macro/syntax/common.lux index 743768fe6..6cec7f713 100644 --- a/stdlib/source/lux/macro/syntax/common.lux +++ b/stdlib/source/lux/macro/syntax/common.lux @@ -7,7 +7,9 @@ lux (lux (control monad) (data (struct [list]) - text/format) + text/format + [ident "Ident/" Eq<Ident>] + [product]) [compiler] (macro [ast] ["s" syntax #+ syntax: Syntax]))) @@ -104,18 +106,17 @@ (def: (find-def-args meta-data) (-> (List [Ident AST]) (List Text)) (default (list) - (list;find (lambda [[tag value]] - (case tag - (^=> ["lux" "func-args"] - {(s;run (list value) list-meta^) - (#;Right [_ args])} - {(s;run args (s;some text-meta^)) - (#;Right [_ args])}) - (#;Some args) - - _ - #;None)) - meta-data))) + (case (list;find (|>. product;left (Ident/= ["lux" "func-args"])) meta-data) + (^=> (#;Some [_ value]) + {(s;run (list value) list-meta^) + (#;Right [_ args])} + {(s;run args (s;some text-meta^)) + (#;Right [_ args])}) + (#;Some args) + + _ + #;None) + )) (def: #export (def compiler) (-> Compiler (Syntax Def-Syntax)) diff --git a/stdlib/source/lux/math/random.lux b/stdlib/source/lux/math/random.lux index aee5674ad..41481a284 100644 --- a/stdlib/source/lux/math/random.lux +++ b/stdlib/source/lux/math/random.lux @@ -174,6 +174,16 @@ (wrap sample) (filter pred gen)))) +(def: #export (maybe value-gen) + (All [a] (-> (Random a) (Random (Maybe a)))) + (do Monad<Random> + [some? bool] + (if some? + (do @ + [value value-gen] + (wrap (#;Some value))) + (wrap #;None)))) + (do-template [<name> <type> <zero> <plus>] [(def: #export (<name> size value-gen) (All [a] (-> Nat (Random a) (Random (<type> a)))) diff --git a/stdlib/source/lux/type.lux b/stdlib/source/lux/type.lux index 4a84582c4..fbd269daa 100644 --- a/stdlib/source/lux/type.lux +++ b/stdlib/source/lux/type.lux @@ -22,24 +22,24 @@ (#;HostT name (List/map (beta-reduce env) params)) (^template [<tag>] - (<tag> left right) - (<tag> (beta-reduce env left) (beta-reduce env right))) + (<tag> left right) + (<tag> (beta-reduce env left) (beta-reduce env right))) ([#;SumT] [#;ProdT]) (^template [<tag>] - (<tag> left right) - (<tag> (beta-reduce env left) (beta-reduce env right))) + (<tag> left right) + (<tag> (beta-reduce env left) (beta-reduce env right))) ([#;LambdaT] [#;AppT]) (^template [<tag>] - (<tag> old-env def) - (case old-env - #;Nil - (<tag> env def) + (<tag> old-env def) + (case old-env + #;Nil + (<tag> env def) - _ - type)) + _ + type)) ([#;UnivQ] [#;ExQ]) @@ -65,17 +65,17 @@ (list;zip2 xparams yparams))) (^template [<tag>] - [<tag> <tag>] - true) + [<tag> <tag>] + true) ([#;VoidT] [#;UnitT]) (^template [<tag>] - [(<tag> xid) (<tag> yid)] - (=+ yid xid)) + [(<tag> xid) (<tag> yid)] + (=+ yid xid)) ([#;VarT] [#;ExT] [#;BoundT]) (^or [(#;LambdaT xleft xright) (#;LambdaT yleft yright)] - [(#;AppT xleft xright) (#;AppT yleft yright)]) + [(#;AppT xleft xright) (#;AppT yleft yright)]) (and (= xleft yleft) (= xright yright)) @@ -85,12 +85,12 @@ (= xtype ytype)) (^template [<tag>] - [(<tag> xL xR) (<tag> yL yR)] - (and (= xL yL) (= xR yR))) + [(<tag> xL xR) (<tag> yL yR)] + (and (= xL yL) (= xR yR))) ([#;SumT] [#;ProdT]) (^or [(#;UnivQ xenv xbody) (#;UnivQ yenv ybody)] - [(#;ExQ xenv xbody) (#;ExQ yenv ybody)]) + [(#;ExQ xenv xbody) (#;ExQ yenv ybody)]) (and (=+ (list;size yenv) (list;size xenv)) (= xbody ybody) (List/fold (lambda [[x y] prev] (and prev (= x y))) @@ -140,8 +140,8 @@ (-> Type Type (Maybe Type)) (case type-fun (^template [<tag>] - (<tag> env body) - (#;Some (beta-reduce (list& type-fun param env) body))) + (<tag> env body) + (#;Some (beta-reduce (list& type-fun param env) body))) ([#;UnivQ] [#;ExQ]) (#;AppT F A) @@ -163,24 +163,24 @@ (list (~@ (List/map type-to-ast params))))) (^template [<tag>] - <tag> - (` <tag>)) + <tag> + (` <tag>)) ([#;VoidT] [#;UnitT]) (^template [<tag>] - (<tag> idx) - (` (<tag> (~ (ast;nat idx))))) + (<tag> idx) + (` (<tag> (~ (ast;nat idx))))) ([#;VarT] [#;ExT] [#;BoundT]) (^template [<tag>] - (<tag> left right) - (` (<tag> (~ (type-to-ast left)) - (~ (type-to-ast right))))) + (<tag> left right) + (` (<tag> (~ (type-to-ast left)) + (~ (type-to-ast right))))) ([#;LambdaT] [#;AppT]) (^template [<tag> <macro> <flattener>] - (<tag> left right) - (` (<macro> (~@ (List/map type-to-ast (<flattener> type)))))) + (<tag> left right) + (` (<macro> (~@ (List/map type-to-ast (<flattener> type)))))) ([#;SumT | flatten-sum] [#;ProdT & flatten-prod]) @@ -188,9 +188,9 @@ (ast;symbol name) (^template [<tag>] - (<tag> env body) - (` (<tag> (list (~@ (List/map type-to-ast env))) - (~ (type-to-ast body))))) + (<tag> env body) + (` (<tag> (list (~@ (List/map type-to-ast env))) + (~ (type-to-ast body))))) ([#;UnivQ] [#;ExQ]) )) @@ -212,14 +212,14 @@ "Unit" (^template [<tag> <open> <close> <flatten>] - (<tag> _) - ($_ Text/append <open> - (|> (<flatten> type) - (List/map type-to-text) - list;reverse - (list;interpose " ") - (List/fold Text/append "")) - <close>)) + (<tag> _) + ($_ Text/append <open> + (|> (<flatten> type) + (List/map type-to-text) + list;reverse + (list;interpose " ") + (List/fold Text/append "")) + <close>)) ([#;SumT "(| " ")" flatten-sum] [#;ProdT "[" "]" flatten-prod]) @@ -273,3 +273,20 @@ _ type)) + +(do-template [<name> <base> <ctor>] + [(def: #export (<name> types) + (-> (List Type) Type) + (case types + #;Nil + <base> + + (#;Cons type #;Nil) + type + + (#;Cons type types') + (<ctor> type (<name> types'))))] + + [variant Void #;SumT] + [tuple Unit #;ProdT] + ) |