From f7097aee6854d255849c61b1f29fc62988a790da Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Thu, 1 Dec 2016 23:40:02 -0400 Subject: - Bug fixes, refactorings and minor expansions. - Added tests for lux/data/error/exception. - Update tests for lux/data/format/json. --- stdlib/source/lux/data/error/exception.lux | 2 +- stdlib/source/lux/data/format/json.lux | 70 +++++++++++---------- stdlib/source/lux/data/struct/vector.lux | 15 +++-- stdlib/source/lux/lexer.lux | 2 +- stdlib/source/lux/macro/poly.lux | 44 ++++++-------- stdlib/source/lux/macro/syntax/common.lux | 27 +++++---- stdlib/source/lux/math/random.lux | 10 +++ stdlib/source/lux/type.lux | 97 ++++++++++++++++++------------ 8 files changed, 142 insertions(+), 125 deletions(-) (limited to 'stdlib/source') 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 [ ] - [_ ( value)] - (wrap (list (` (: JSON ( (~ ( value)))))))) + [_ ( value)] + (wrap (list (` (: JSON ( (~ ( 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 [ ] - ( value) - ( value)) + ( value) + ( value)) ([#Null show-null] [#Boolean show-boolean] [#Number show-number] @@ -316,7 +316,7 @@ _ (lexer;this-char )] (wrap ( elems))))] - [array~ Array #"[" #"]" (json~ []) vector;list-to-vector] + [array~ Array #"[" #"]" (json~ []) vector;from-list] [object~ Object #"{" #"}" (kv~ json~) (dict;from-list text;Hash)] ) @@ -485,7 +485,7 @@ (case json (#Array values) (do Monad - [elems (mapM @ parser (vector;vector-to-list values))] + [elems (mapM @ parser (vector;to-list values))] (wrap elems)) _ @@ -631,8 +631,8 @@ true (^template [ ] - [( x') ( y')] - (:: = x' y')) + [( x') ( y')] + (:: = x' y')) ([#Boolean bool;Eq] [#Number number;Eq] [#String text;Eq]) @@ -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//encode *env* :x:) (let [->Codec//encode (: (-> AST AST) (lambda [.type.] (` (-> (~ .type.) JSON))))] (let% [ (do-template [ ] [(do @ [_ ( :x:)] (wrap (` (: (~ (->Codec//encode (` ))) ))))] [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 (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//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//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//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//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//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//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//encode *env* :func:) + .args. (mapM @ (Codec//encode *env*) :args:)] (wrap (` (: (~ (->Codec//encode (type;type-to-ast :x:))) ((~ .func.) (~@ .args.)))))) (poly;bound *env* :x:) @@ -869,7 +867,7 @@ (let% [ (do-template [ ] [(do @ [_ ( :x:)] (wrap (` (: (~ (->Codec//decode (` ))) ))))] - [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//decode new-*env* :case:)] (wrap (list (` (do Monad - [(~ 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 :x:) (wrap (list (` (: (Codec JSON (~ :x:)) (struct - (def: (~ (' encode)) (|Codec@JSON//encode| (~ :x:))) + (def: (~ (' encode)) (Codec//encode (~ :x:))) (def: (~ (' decode)) (Codec//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 Eq) (All [a] (-> (Eq a) (Eq (Vector a)))) (def: (= v1 v2) (and (=+ (get@ #size v1) (get@ #size v2)) - (:: (Eq Eq) = - (#Base (get@ #tail v1)) - (#Base (get@ #tail v2))) - (:: (Eq Eq) = - (#Hierarchy (get@ #root v1)) - (#Hierarchy (get@ #root v2)))))) + (let [(^open "Node/") (Eq Eq)] + (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) - (def: (join ffa) + (def: join (let [(^open) Functor (^open) Fold (^open) Monoid] - (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] + (struct [list "List/" Fold Monad] [dict #+ Dict]) [number] [product] @@ -140,7 +140,7 @@ _ (:: compiler;Monad wrap [(;list) :type:]))))) -(do-template [ ] +(do-template [ ] [(def: #export (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 ( :type:)] - (wrap [vars (list;zip2 tags members)]))))] - - [variant sum+] - [record prod+] + members ( :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)) + ( 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] + [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 + [some? bool] + (if some? + (do @ + [value value-gen] + (wrap (#;Some value))) + (wrap #;None)))) + (do-template [ ] [(def: #export ( size value-gen) (All [a] (-> Nat (Random a) (Random ( 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 [] - ( left right) - ( (beta-reduce env left) (beta-reduce env right))) + ( left right) + ( (beta-reduce env left) (beta-reduce env right))) ([#;SumT] [#;ProdT]) (^template [] - ( left right) - ( (beta-reduce env left) (beta-reduce env right))) + ( left right) + ( (beta-reduce env left) (beta-reduce env right))) ([#;LambdaT] [#;AppT]) (^template [] - ( old-env def) - (case old-env - #;Nil - ( env def) + ( old-env def) + (case old-env + #;Nil + ( env def) - _ - type)) + _ + type)) ([#;UnivQ] [#;ExQ]) @@ -65,17 +65,17 @@ (list;zip2 xparams yparams))) (^template [] - [ ] - true) + [ ] + true) ([#;VoidT] [#;UnitT]) (^template [] - [( xid) ( yid)] - (=+ yid xid)) + [( xid) ( 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 [] - [( xL xR) ( yL yR)] - (and (= xL yL) (= xR yR))) + [( xL xR) ( 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 [] - ( env body) - (#;Some (beta-reduce (list& type-fun param env) body))) + ( 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 [] - - (` )) + + (` )) ([#;VoidT] [#;UnitT]) (^template [] - ( idx) - (` ( (~ (ast;nat idx))))) + ( idx) + (` ( (~ (ast;nat idx))))) ([#;VarT] [#;ExT] [#;BoundT]) (^template [] - ( left right) - (` ( (~ (type-to-ast left)) - (~ (type-to-ast right))))) + ( left right) + (` ( (~ (type-to-ast left)) + (~ (type-to-ast right))))) ([#;LambdaT] [#;AppT]) (^template [ ] - ( left right) - (` ( (~@ (List/map type-to-ast ( type)))))) + ( left right) + (` ( (~@ (List/map type-to-ast ( type)))))) ([#;SumT | flatten-sum] [#;ProdT & flatten-prod]) @@ -188,9 +188,9 @@ (ast;symbol name) (^template [] - ( env body) - (` ( (list (~@ (List/map type-to-ast env))) - (~ (type-to-ast body))))) + ( env body) + (` ( (list (~@ (List/map type-to-ast env))) + (~ (type-to-ast body))))) ([#;UnivQ] [#;ExQ]) )) @@ -212,14 +212,14 @@ "Unit" (^template [ ] - ( _) - ($_ Text/append - (|> ( type) - (List/map type-to-text) - list;reverse - (list;interpose " ") - (List/fold Text/append "")) - )) + ( _) + ($_ Text/append + (|> ( type) + (List/map type-to-text) + list;reverse + (list;interpose " ") + (List/fold Text/append "")) + )) ([#;SumT "(| " ")" flatten-sum] [#;ProdT "[" "]" flatten-prod]) @@ -273,3 +273,20 @@ _ type)) + +(do-template [ ] + [(def: #export ( types) + (-> (List Type) Type) + (case types + #;Nil + + + (#;Cons type #;Nil) + type + + (#;Cons type types') + ( type ( types'))))] + + [variant Void #;SumT] + [tuple Unit #;ProdT] + ) -- cgit v1.2.3