From 7ac0905fd80dce045d6608c4a3c449c466ae43ab Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Fri, 19 Apr 2019 00:57:04 -0400 Subject: Extracted the type-parsing machinery into its own module. --- stdlib/source/lux/control/parser/type.lux | 348 +++++++++++++++++++++++++ stdlib/source/lux/macro/poly.lux | 376 ++------------------------- stdlib/source/lux/macro/poly/equivalence.lux | 57 ++-- stdlib/source/lux/macro/poly/functor.lux | 49 ++-- stdlib/source/lux/macro/poly/json.lux | 115 ++++---- stdlib/source/test/lux.lux | 7 +- 6 files changed, 487 insertions(+), 465 deletions(-) create mode 100644 stdlib/source/lux/control/parser/type.lux (limited to 'stdlib') diff --git a/stdlib/source/lux/control/parser/type.lux b/stdlib/source/lux/control/parser/type.lux new file mode 100644 index 000000000..56942e5c4 --- /dev/null +++ b/stdlib/source/lux/control/parser/type.lux @@ -0,0 +1,348 @@ +(.module: + [lux (#- function log!) + [abstract + ["." monad (#+ do)]] + [control + ["." exception (#+ exception:)] + ["." function]] + [data + ["." name ("#@." codec)] + ["." error (#+ Error)] + [number + ["." nat ("#@." decimal)]] + ["." text ("#@." monoid) + format] + [collection + ["." list ("#@." functor)] + ["." dictionary (#+ Dictionary)]]] + [macro + ["." code]] + ["." type ("#@." equivalence) + ["." check]]] + ["." // (#+ Parser)]) + +(template [] + [(exception: #export ( {type Type}) + (exception.report + ["Type" (%type type)]))] + + [not-existential] + [not-recursive] + [not-named] + [not-parameter] + [unknown-parameter] + [not-function] + [not-application] + [not-polymorphic] + [not-variant] + [not-tuple] + ) + +(template [] + [(exception: #export ( {expected Type} {actual Type}) + (exception.report + ["Expected" (%type expected)] + ["Actual" (%type actual)]))] + + [types-do-not-match] + [wrong-parameter] + ) + +(exception: #export (unconsumed {remaining (List Type)}) + (exception.report + ["Types" (|> remaining + (list@map (|>> %type (format text.new-line "* "))) + (text.join-with ""))])) + +(type: #export Env + (Dictionary Nat [Type Code])) + +(type: #export (Poly a) + (Parser [Env (List Type)] a)) + +(def: #export fresh Env (dictionary.new nat.hash)) + +(def: (run' env types poly) + (All [a] (-> Env (List Type) (Poly a) (Error a))) + (case (//.run [env types] poly) + (#error.Failure error) + (#error.Failure error) + + (#error.Success [[env' remaining] output]) + (case remaining + #.Nil + (#error.Success output) + + _ + (exception.throw unconsumed remaining)))) + +(def: #export (run type poly) + (All [a] (-> Type (Poly a) (Error a))) + (run' fresh (list type) poly)) + +(def: #export env + (Poly Env) + (.function (_ [env inputs]) + (#error.Success [[env inputs] env]))) + +(def: (with-env temp poly) + (All [a] (-> Env (Poly a) (Poly a))) + (.function (_ [env inputs]) + (case (//.run [temp inputs] poly) + (#error.Failure error) + (#error.Failure error) + + (#error.Success [[_ remaining] output]) + (#error.Success [[env remaining] output])))) + +(def: #export peek + (Poly Type) + (.function (_ [env inputs]) + (case inputs + #.Nil + (#error.Failure "Empty stream of types.") + + (#.Cons headT tail) + (#error.Success [[env inputs] headT])))) + +(def: #export any + (Poly Type) + (.function (_ [env inputs]) + (case inputs + #.Nil + (#error.Failure "Empty stream of types.") + + (#.Cons headT tail) + (#error.Success [[env tail] headT])))) + +(def: #export (local types poly) + (All [a] (-> (List Type) (Poly a) (Poly a))) + (.function (_ [env pass-through]) + (case (run' env types poly) + (#error.Failure error) + (#error.Failure error) + + (#error.Success output) + (#error.Success [[env pass-through] output])))) + +(def: (label idx) + (-> Nat Code) + (code.local-identifier ($_ text@compose "label" text.tab (nat@encode idx)))) + +(def: #export (with-extension type poly) + (All [a] (-> Type (Poly a) (Poly [Code a]))) + (.function (_ [env inputs]) + (let [current-id (dictionary.size env) + g!var (label current-id)] + (case (//.run [(dictionary.put current-id [type g!var] env) + inputs] + poly) + (#error.Failure error) + (#error.Failure error) + + (#error.Success [[_ inputs'] output]) + (#error.Success [[env inputs'] [g!var output]]))))) + +(template [ ] + [(def: #export ( poly) + (All [a] (-> (Poly a) (Poly a))) + (do //.monad + [headT any] + (let [members ( (type.un-name headT))] + (if (n/> 1 (list.size members)) + (local members poly) + (//.fail (exception.construct headT))))))] + + [variant type.flatten-variant #.Sum not-variant] + [tuple type.flatten-tuple #.Product not-tuple] + ) + +(def: polymorphic' + (Poly [Nat Type]) + (do //.monad + [headT any + #let [[num-arg bodyT] (type.flatten-univ-q (type.un-name headT))]] + (if (n/= 0 num-arg) + (//.fail (exception.construct not-polymorphic headT)) + (wrap [num-arg bodyT])))) + +(def: #export (polymorphic poly) + (All [a] (-> (Poly a) (Poly [Code (List Code) a]))) + (do //.monad + [headT any + funcI (:: @ map dictionary.size ..env) + [num-args non-poly] (local (list headT) polymorphic') + env ..env + #let [funcL (label funcI) + [all-varsL env'] (loop [current-arg 0 + env' env + all-varsL (: (List Code) (list))] + (if (n/< num-args current-arg) + (if (n/= 0 current-arg) + (let [varL (label (inc funcI))] + (recur (inc current-arg) + (|> env' + (dictionary.put funcI [headT funcL]) + (dictionary.put (inc funcI) [(#.Parameter (inc funcI)) varL])) + (#.Cons varL all-varsL))) + (let [partialI (|> current-arg (n/* 2) (n/+ funcI)) + partial-varI (inc partialI) + partial-varL (label partial-varI) + partialC (` ((~ funcL) (~+ (|> (list.indices num-args) + (list@map (|>> (n/* 2) inc (n/+ funcI) label)) + list.reverse))))] + (recur (inc current-arg) + (|> env' + (dictionary.put partialI [.Nothing partialC]) + (dictionary.put partial-varI [(#.Parameter partial-varI) partial-varL])) + (#.Cons partial-varL all-varsL)))) + [all-varsL env']))]] + (|> (do @ + [output poly] + (wrap [funcL all-varsL output])) + (local (list non-poly)) + (with-env env')))) + +(def: #export (function in-poly out-poly) + (All [i o] (-> (Poly i) (Poly o) (Poly [i o]))) + (do //.monad + [headT any + #let [[inputsT outputT] (type.flatten-function (type.un-name headT))]] + (if (n/> 0 (list.size inputsT)) + (//.and (local inputsT in-poly) + (local (list outputT) out-poly)) + (//.fail (exception.construct not-function headT))))) + +(def: #export (apply poly) + (All [a] (-> (Poly a) (Poly a))) + (do //.monad + [headT any + #let [[funcT paramsT] (type.flatten-application (type.un-name headT))]] + (if (n/= 0 (list.size paramsT)) + (//.fail (exception.construct not-application headT)) + (local (#.Cons funcT paramsT) poly)))) + +(template [ ] + [(def: #export ( expected) + (-> Type (Poly Any)) + (do //.monad + [actual any] + (if ( expected actual) + (wrap []) + (//.fail (exception.construct types-do-not-match [expected actual])))))] + + [exactly type@=] + [sub check.checks?] + [super (function.flip check.checks?)] + ) + +(def: #export (adjusted-idx env idx) + (-> Env Nat Nat) + (let [env-level (n// 2 (dictionary.size env)) + parameter-level (n// 2 idx) + parameter-idx (n/% 2 idx)] + (|> env-level dec (n/- parameter-level) (n/* 2) (n/+ parameter-idx)))) + +(def: #export parameter + (Poly Code) + (do //.monad + [env ..env + headT any] + (case headT + (#.Parameter idx) + (case (dictionary.get (adjusted-idx env idx) env) + (#.Some [poly-type poly-code]) + (wrap poly-code) + + #.None + (//.fail (exception.construct unknown-parameter headT))) + + _ + (//.fail (exception.construct not-parameter headT))))) + +(def: #export (parameter! id) + (-> Nat (Poly Any)) + (do //.monad + [env ..env + headT any] + (case headT + (#.Parameter idx) + (if (n/= id (adjusted-idx env idx)) + (wrap []) + (//.fail (exception.construct wrong-parameter [(#.Parameter id) headT]))) + + _ + (//.fail (exception.construct not-parameter headT))))) + +(def: #export existential + (Poly Nat) + (do //.monad + [headT any] + (case headT + (#.Ex ex-id) + (wrap ex-id) + + _ + (//.fail (exception.construct not-existential headT))))) + +(def: #export named + (Poly [Name Type]) + (do //.monad + [inputT any] + (case inputT + (#.Named name anonymousT) + (wrap [name anonymousT]) + + _ + (//.fail (exception.construct not-named inputT))))) + +(def: #export (recursive poly) + (All [a] (-> (Poly a) (Poly [Code a]))) + (do //.monad + [headT any] + (case (type.un-name headT) + (#.Apply (#.Named ["lux" "Nothing"] _) (#.UnivQ _ headT')) + (do @ + [[recT _ output] (|> poly + (with-extension .Nothing) + (with-extension headT) + (local (list headT')))] + (wrap [recT output])) + + _ + (//.fail (exception.construct not-recursive headT))))) + +(def: #export recursive-self + (Poly Code) + (do //.monad + [env ..env + headT any] + (case (type.un-name headT) + (^multi (#.Apply (#.Named ["lux" "Nothing"] _) (#.Parameter funcT-idx)) + (n/= 0 (adjusted-idx env funcT-idx)) + [(dictionary.get 0 env) (#.Some [self-type self-call])]) + (wrap self-call) + + _ + (//.fail (exception.construct not-recursive headT))))) + +(def: #export recursive-call + (Poly Code) + (do //.monad + [env ..env + [funcT argsT] (apply (//.and any (//.many any))) + _ (local (list funcT) (..parameter! 0)) + allC (let [allT (list& funcT argsT)] + (|> allT + (monad.map @ (function.constant ..parameter)) + (local allT)))] + (wrap (` ((~+ allC)))))) + +(def: #export log! + (All [a] (Poly a)) + (do //.monad + [current any + #let [_ (.log! ($_ text@compose + "{" (name@encode (name-of ..log)) "} " + (%type current)))]] + (//.fail "LOGGING"))) diff --git a/stdlib/source/lux/macro/poly.lux b/stdlib/source/lux/macro/poly.lux index a3d78177a..695284e0a 100644 --- a/stdlib/source/lux/macro/poly.lux +++ b/stdlib/source/lux/macro/poly.lux @@ -1,356 +1,24 @@ (.module: - [lux (#- function) + [lux #* [abstract - ["." monad (#+ Monad do)] - [equivalence]] + ["." monad (#+ do)]] [control - ["p" parser] - ["ex" exception (#+ exception:)] - ["." function]] + ["p" parser + ["<.>" type (#+ Env)]]] [data ["." product] - ["." bit] ["." maybe] - ["." name ("#;." codec)] - ["." error (#+ Error)] - ["." number (#+ hex) - ["." nat ("#;." decimal)]] - ["." text ("#;." monoid) - format] + ["." text] [collection - ["." list ("#;." fold monad monoid)] - ["dict" dictionary (#+ Dictionary)]]] + ["." list ("#@." fold functor)] + ["." dictionary]]] ["." macro (#+ with-gensyms) ["." code] - ["s" syntax (#+ Syntax syntax:)] - [syntax - ["cs" common + ["s" syntax (#+ syntax:) + [common ["csr" reader] ["csw" writer]]]] - ["." type ("#;." equivalence) - ["." check]]]) - -(template [] - [(exception: #export ( {type Type}) - (%type type))] - - [not-existential] - [not-recursive] - [not-named] - [not-parameter] - [unknown-parameter] - [not-function] - [not-application] - [not-polymorphic] - [not-variant] - [not-tuple] - ) - -(template [] - [(exception: #export ( {expected Type} {actual Type}) - (ex.report ["Expected" (%type expected)] - ["Actual" (%type actual)]))] - - [types-do-not-match] - [wrong-parameter] - ) - -(exception: #export (unconsumed {remaining (List Type)}) - (ex.report ["Types" (|> remaining - (list;map (|>> %type (format text.new-line "* "))) - (text.join-with ""))])) - -(type: #export Env (Dictionary Nat [Type Code])) - -(type: #export (Poly a) - (p.Parser [Env (List Type)] a)) - -(def: #export fresh Env (dict.new nat.hash)) - -(def: (run' env types poly) - (All [a] (-> Env (List Type) (Poly a) (Error a))) - (case (p.run [env types] poly) - (#error.Failure error) - (#error.Failure error) - - (#error.Success [[env' remaining] output]) - (case remaining - #.Nil - (#error.Success output) - - _ - (ex.throw unconsumed remaining)))) - -(def: #export (run type poly) - (All [a] (-> Type (Poly a) (Error a))) - (run' fresh (list type) poly)) - -(def: #export env - (Poly Env) - (.function (_ [env inputs]) - (#error.Success [[env inputs] env]))) - -(def: (with-env temp poly) - (All [a] (-> Env (Poly a) (Poly a))) - (.function (_ [env inputs]) - (case (p.run [temp inputs] poly) - (#error.Failure error) - (#error.Failure error) - - (#error.Success [[_ remaining] output]) - (#error.Success [[env remaining] output])))) - -(def: #export peek - (Poly Type) - (.function (_ [env inputs]) - (case inputs - #.Nil - (#error.Failure "Empty stream of types.") - - (#.Cons headT tail) - (#error.Success [[env inputs] headT])))) - -(def: #export any - (Poly Type) - (.function (_ [env inputs]) - (case inputs - #.Nil - (#error.Failure "Empty stream of types.") - - (#.Cons headT tail) - (#error.Success [[env tail] headT])))) - -(def: #export (local types poly) - (All [a] (-> (List Type) (Poly a) (Poly a))) - (.function (_ [env pass-through]) - (case (run' env types poly) - (#error.Failure error) - (#error.Failure error) - - (#error.Success output) - (#error.Success [[env pass-through] output])))) - -(def: (label idx) - (-> Nat Code) - (code.local-identifier ($_ text;compose "label" text.tab (nat;encode idx)))) - -(def: #export (with-extension type poly) - (All [a] (-> Type (Poly a) (Poly [Code a]))) - (.function (_ [env inputs]) - (let [current-id (dict.size env) - g!var (label current-id)] - (case (p.run [(dict.put current-id [type g!var] env) - inputs] - poly) - (#error.Failure error) - (#error.Failure error) - - (#error.Success [[_ inputs'] output]) - (#error.Success [[env inputs'] [g!var output]]))))) - -(template [ ] - [(def: #export ( poly) - (All [a] (-> (Poly a) (Poly a))) - (do p.monad - [headT any] - (let [members ( (type.un-name headT))] - (if (n/> 1 (list.size members)) - (local members poly) - (p.fail (ex.construct headT))))))] - - [variant type.flatten-variant #.Sum not-variant] - [tuple type.flatten-tuple #.Product not-tuple] - ) - -(def: polymorphic' - (Poly [Nat Type]) - (do p.monad - [headT any - #let [[num-arg bodyT] (type.flatten-univ-q (type.un-name headT))]] - (if (n/= 0 num-arg) - (p.fail (ex.construct not-polymorphic headT)) - (wrap [num-arg bodyT])))) - -(def: #export (polymorphic poly) - (All [a] (-> (Poly a) (Poly [Code (List Code) a]))) - (do p.monad - [headT any - funcI (:: @ map dict.size ..env) - [num-args non-poly] (local (list headT) polymorphic') - env ..env - #let [funcL (label funcI) - [all-varsL env'] (loop [current-arg 0 - env' env - all-varsL (: (List Code) (list))] - (if (n/< num-args current-arg) - (if (n/= 0 current-arg) - (let [varL (label (inc funcI))] - (recur (inc current-arg) - (|> env' - (dict.put funcI [headT funcL]) - (dict.put (inc funcI) [(#.Parameter (inc funcI)) varL])) - (#.Cons varL all-varsL))) - (let [partialI (|> current-arg (n/* 2) (n/+ funcI)) - partial-varI (inc partialI) - partial-varL (label partial-varI) - partialC (` ((~ funcL) (~+ (|> (list.indices num-args) - (list;map (|>> (n/* 2) inc (n/+ funcI) label)) - list.reverse))))] - (recur (inc current-arg) - (|> env' - (dict.put partialI [.Nothing partialC]) - (dict.put partial-varI [(#.Parameter partial-varI) partial-varL])) - (#.Cons partial-varL all-varsL)))) - [all-varsL env']))]] - (|> (do @ - [output poly] - (wrap [funcL all-varsL output])) - (local (list non-poly)) - (with-env env')))) - -(def: #export (function in-poly out-poly) - (All [i o] (-> (Poly i) (Poly o) (Poly [i o]))) - (do p.monad - [headT any - #let [[inputsT outputT] (type.flatten-function (type.un-name headT))]] - (if (n/> 0 (list.size inputsT)) - (p.and (local inputsT in-poly) - (local (list outputT) out-poly)) - (p.fail (ex.construct not-function headT))))) - -(def: #export (apply poly) - (All [a] (-> (Poly a) (Poly a))) - (do p.monad - [headT any - #let [[funcT paramsT] (type.flatten-application (type.un-name headT))]] - (if (n/= 0 (list.size paramsT)) - (p.fail (ex.construct not-application headT)) - (local (#.Cons funcT paramsT) poly)))) - -(template [ ] - [(def: #export ( expected) - (-> Type (Poly Any)) - (do p.monad - [actual any] - (if ( expected actual) - (wrap []) - (p.fail (ex.construct types-do-not-match [expected actual])))))] - - [exactly type;=] - [sub check.checks?] - [super (function.flip check.checks?)] - ) - -(def: (adjusted-idx env idx) - (-> Env Nat Nat) - (let [env-level (n// 2 (dict.size env)) - parameter-level (n// 2 idx) - parameter-idx (n/% 2 idx)] - (|> env-level dec (n/- parameter-level) (n/* 2) (n/+ parameter-idx)))) - -(def: #export parameter - (Poly Code) - (do p.monad - [env ..env - headT any] - (case headT - (#.Parameter idx) - (case (dict.get (adjusted-idx env idx) env) - (#.Some [poly-type poly-code]) - (wrap poly-code) - - #.None - (p.fail (ex.construct unknown-parameter headT))) - - _ - (p.fail (ex.construct not-parameter headT))))) - -(def: #export (parameter! id) - (-> Nat (Poly Any)) - (do p.monad - [env ..env - headT any] - (case headT - (#.Parameter idx) - (if (n/= id (adjusted-idx env idx)) - (wrap []) - (p.fail (ex.construct wrong-parameter [(#.Parameter id) headT]))) - - _ - (p.fail (ex.construct not-parameter headT))))) - -(def: #export existential - (Poly Nat) - (do p.monad - [headT any] - (case headT - (#.Ex ex-id) - (wrap ex-id) - - _ - (p.fail (ex.construct not-existential headT))))) - -(def: #export named - (Poly [Name Type]) - (do p.monad - [inputT any] - (case inputT - (#.Named name anonymousT) - (wrap [name anonymousT]) - - _ - (p.fail (ex.construct not-named inputT))))) - -(def: #export (recursive poly) - (All [a] (-> (Poly a) (Poly [Code a]))) - (do p.monad - [headT any] - (case (type.un-name headT) - (#.Apply (#.Named ["lux" "Nothing"] _) (#.UnivQ _ headT')) - (do @ - [[recT _ output] (|> poly - (with-extension .Nothing) - (with-extension headT) - (local (list headT')))] - (wrap [recT output])) - - _ - (p.fail (ex.construct not-recursive headT))))) - -(def: #export recursive-self - (Poly Code) - (do p.monad - [env ..env - headT any] - (case (type.un-name headT) - (^multi (#.Apply (#.Named ["lux" "Nothing"] _) (#.Parameter funcT-idx)) - (n/= 0 (adjusted-idx env funcT-idx)) - [(dict.get 0 env) (#.Some [self-type self-call])]) - (wrap self-call) - - _ - (p.fail (ex.construct not-recursive headT))))) - -(def: #export recursive-call - (Poly Code) - (do p.monad - [env ..env - [funcT argsT] (apply (p.and any (p.many any))) - _ (local (list funcT) (..parameter! 0)) - allC (let [allT (list& funcT argsT)] - (|> allT - (monad.map @ (function.constant ..parameter)) - (local allT)))] - (wrap (` ((~+ allC)))))) - -(def: #export log - (All [a] (Poly a)) - (do p.monad - [current any - #let [_ (log! ($_ text;compose - "{" (name;encode (name-of ..log)) "} " - (%type current)))]] - (p.fail "LOGGING"))) + ["." type]]) (syntax: #export (poly: {export csr.export} {name s.local-identifier} @@ -361,10 +29,10 @@ (do macro.monad [(~ g!type) (macro.find-type-def (~ g!type))] (case (|> (~ body) - (.function ((~ g!_) (~ g!name))) + (function ((~ g!_) (~ g!name))) p.rec (do p.monad []) - (..run (~ g!type)) + ((~! .run) (~ g!type)) (: (.Either .Text .Code))) (#.Left (~ g!output)) (macro.fail (~ g!output)) @@ -379,7 +47,7 @@ (def: (derivation-name poly args) (-> Text (List Text) (Maybe Text)) (if (common-poly-name? poly) - (#.Some (list;fold (text.replace-once "?") poly args)) + (#.Some (list@fold (text.replace-once "?") poly args)) #.None)) (syntax: #export (derived: {export csr.export} @@ -393,7 +61,7 @@ (wrap name) (^multi #.None - [(derivation-name (product.right poly-func) (list;map product.right poly-args)) + [(derivation-name (product.right poly-func) (list@map product.right poly-args)) (#.Some derived-name)]) (wrap derived-name) @@ -404,7 +72,7 @@ custom-impl #.None - (` ((~ (code.identifier poly-func)) (~+ (list;map code.identifier poly-args)))))]] + (` ((~ (code.identifier poly-func)) (~+ (list@map code.identifier poly-args)))))]] (wrap (.list (` (def: (~+ (csw.export export)) (~ (code.identifier ["" name])) {#.struct? #1} @@ -415,7 +83,7 @@ (case type (#.Primitive name params) (` (#.Primitive (~ (code.text name)) - (list (~+ (list;map (to-code env) params))))) + (list (~+ (list@map (to-code env) params))))) (^template [] ( idx) @@ -423,15 +91,15 @@ ([#.Var] [#.Ex]) (#.Parameter idx) - (let [idx (adjusted-idx env idx)] + (let [idx (.adjusted-idx env idx)] (if (n/= 0 idx) - (|> (dict.get idx env) maybe.assume product.left (to-code env)) + (|> (dictionary.get idx env) maybe.assume product.left (to-code env)) (` (.$ (~ (code.nat (dec idx))))))) (#.Apply (#.Named ["lux" "Nothing"] _) (#.Parameter idx)) - (let [idx (adjusted-idx env idx)] + (let [idx (.adjusted-idx env idx)] (if (n/= 0 idx) - (|> (dict.get idx env) maybe.assume product.left (to-code env)) + (|> (dictionary.get idx env) maybe.assume product.left (to-code env)) (undefined))) (^template [] @@ -442,7 +110,7 @@ (^template [ ] ( left right) - (` ( (~+ (list;map (to-code env) ( type)))))) + (` ( (~+ (list@map (to-code env) ( type)))))) ([#.Sum | type.flatten-variant] [#.Product & type.flatten-tuple]) @@ -451,7 +119,7 @@ (^template [] ( scope body) - (` ( (list (~+ (list;map (to-code env) scope))) + (` ( (list (~+ (list@map (to-code env) scope))) (~ (to-code env body))))) ([#.UnivQ] [#.ExQ]) )) diff --git a/stdlib/source/lux/macro/poly/equivalence.lux b/stdlib/source/lux/macro/poly/equivalence.lux index c39ad9412..50dabcd16 100644 --- a/stdlib/source/lux/macro/poly/equivalence.lux +++ b/stdlib/source/lux/macro/poly/equivalence.lux @@ -4,7 +4,8 @@ [monad (#+ Monad do)] ["/" equivalence]] [control - ["p" parser]] + ["p" parser + ["<.>" type]]] [data ["." product] ["." bit] @@ -42,8 +43,8 @@ (poly: #export equivalence (`` (do @ [#let [g!_ (code.local-identifier "_____________")] - *env* poly.env - inputT poly.peek + *env* .env + inputT .peek #let [@Equivalence (: (-> Type Code) (function (_ type) (` ((~! /.Equivalence) (~ (poly.to-code *env* type))))))]] @@ -55,18 +56,18 @@ (wrap (` (: (~ (@Equivalence inputT)) ))))] - [(poly.exactly Any) (function ((~ g!_) (~ g!_) (~ g!_)) #1)] - [(poly.sub Bit) (~! bit.equivalence)] - [(poly.sub Nat) (~! nat.equivalence)] - [(poly.sub Int) (~! int.equivalence)] - [(poly.sub Rev) (~! rev.equivalence)] - [(poly.sub Frac) (~! frac.equivalence)] - [(poly.sub Text) (~! text.equivalence)])) + [(.exactly Any) (function ((~ g!_) (~ g!_) (~ g!_)) #1)] + [(.sub Bit) (~! bit.equivalence)] + [(.sub Nat) (~! nat.equivalence)] + [(.sub Int) (~! int.equivalence)] + [(.sub Rev) (~! rev.equivalence)] + [(.sub Frac) (~! frac.equivalence)] + [(.sub Text) (~! text.equivalence)])) ## Composite types (~~ (template [ ] [(do @ - [[_ argC] (poly.apply (p.and (poly.exactly ) - equivalence))] + [[_ argC] (.apply (p.and (.exactly ) + equivalence))] (wrap (` (: (~ (@Equivalence inputT)) ( (~ argC))))))] @@ -79,16 +80,16 @@ [rose.Tree (~! rose.equivalence)] )) (do @ - [[_ _ valC] (poly.apply ($_ p.and - (poly.exactly dictionary.Dictionary) - poly.any - equivalence))] + [[_ _ valC] (.apply ($_ p.and + (.exactly dictionary.Dictionary) + .any + equivalence))] (wrap (` (: (~ (@Equivalence inputT)) ((~! dictionary.equivalence) (~ valC)))))) ## Models (~~ (template [ ] [(do @ - [_ (poly.exactly )] + [_ (.exactly )] (wrap (` (: (~ (@Equivalence inputT)) ))))] @@ -99,13 +100,13 @@ [month.Month month.equivalence] )) (do @ - [_ (poly.apply (p.and (poly.exactly unit.Qty) - poly.any))] + [_ (.apply (p.and (.exactly unit.Qty) + .any))] (wrap (` (: (~ (@Equivalence inputT)) unit.equivalence)))) ## Variants (do @ - [members (poly.variant (p.many equivalence)) + [members (.variant (p.many equivalence)) #let [g!_ (code.local-identifier "_____________") g!left (code.local-identifier "_____________left") g!right (code.local-identifier "_____________right")]] @@ -121,7 +122,7 @@ #0)))))) ## Tuples (do @ - [g!eqs (poly.tuple (p.many equivalence)) + [g!eqs (.tuple (p.many equivalence)) #let [g!_ (code.local-identifier "_____________") indices (list.indices (list.size g!eqs)) g!lefts (list@map (|>> nat@encode (text@compose "left") code.local-identifier) indices) @@ -133,29 +134,29 @@ (` ((~ g!eq) (~ g!left) (~ g!right))))))))))))) ## Type recursion (do @ - [[g!self bodyC] (poly.recursive equivalence) + [[g!self bodyC] (.recursive equivalence) #let [g!_ (code.local-identifier "_____________")]] (wrap (` (: (~ (@Equivalence inputT)) ((~! /.rec) (.function ((~ g!_) (~ g!self)) (~ bodyC))))))) - poly.recursive-self + .recursive-self ## Type applications (do @ - [[funcC argsC] (poly.apply (p.and equivalence (p.many equivalence)))] + [[funcC argsC] (.apply (p.and equivalence (p.many equivalence)))] (wrap (` ((~ funcC) (~+ argsC))))) ## Parameters - poly.parameter + .parameter ## Polymorphism (do @ - [[funcC varsC bodyC] (poly.polymorphic equivalence)] + [[funcC varsC bodyC] (.polymorphic equivalence)] (wrap (` (: (All [(~+ varsC)] (-> (~+ (list@map (|>> (~) ((~! /.Equivalence)) (`)) varsC)) ((~! /.Equivalence) ((~ (poly.to-code *env* inputT)) (~+ varsC))))) (function ((~ funcC) (~+ varsC)) (~ bodyC)))))) - poly.recursive-call + .recursive-call ## If all else fails... - (|> poly.any + (|> .any (:: @ map (|>> %type (format "Cannot create Equivalence for: ") p.fail)) (:: @ join)) )))) diff --git a/stdlib/source/lux/macro/poly/functor.lux b/stdlib/source/lux/macro/poly/functor.lux index 947f08ac8..4a2d44e38 100644 --- a/stdlib/source/lux/macro/poly/functor.lux +++ b/stdlib/source/lux/macro/poly/functor.lux @@ -4,7 +4,8 @@ [monad (#+ Monad do)] ["." functor]] [control - ["p" parser]] + ["p" parser + ["<.>" type]]] [data ["." product] ["." text @@ -23,10 +24,10 @@ [#let [type-funcC (code.local-identifier "____________type-funcC") funcC (code.local-identifier "____________funcC") inputC (code.local-identifier "____________inputC")] - *env* poly.env - inputT poly.peek - [polyC varsC non-functorT] (poly.local (list inputT) - (poly.polymorphic poly.any)) + *env* .env + inputT .peek + [polyC varsC non-functorT] (.local (list inputT) + (.polymorphic .any)) #let [num-vars (list.size varsC)] #let [@Functor (: (-> Type Code) (function (_ unwrappedT) @@ -35,18 +36,18 @@ (let [paramsC (|> num-vars dec list.indices (list;map (|>> %n code.local-identifier)))] (` (All [(~+ paramsC)] ((~! functor.Functor) ((~ (poly.to-code *env* unwrappedT)) (~+ paramsC))))))))) - Arg (: (-> Code (poly.Poly Code)) + Arg (: (-> Code (.Poly Code)) (function (Arg valueC) ($_ p.either ## Type-var (do p.monad [#let [varI (|> num-vars (n/* 2) dec)] - _ (poly.parameter! varI)] + _ (.parameter! varI)] (wrap (` ((~ funcC) (~ valueC))))) ## Variants (do @ [_ (wrap []) - membersC (poly.variant (p.many (Arg valueC)))] + membersC (.variant (p.many (Arg valueC)))] (wrap (` (case (~ valueC) (~+ (list;join (list;map (function (_ [tag memberC]) (list (` ((~ (code.nat tag)) (~ valueC))) @@ -54,17 +55,17 @@ (list.enumerate membersC)))))))) ## Tuples (do p.monad - [pairsCC (: (poly.Poly (List [Code Code])) - (poly.tuple (loop [idx 0 - pairsCC (: (List [Code Code]) - (list))] - (p.either (let [slotC (|> idx %n (format "____________slot") code.local-identifier)] - (do @ - [_ (wrap []) - memberC (Arg slotC)] - (recur (inc idx) - (list;compose pairsCC (list [slotC memberC]))))) - (wrap pairsCC)))))] + [pairsCC (: (.Poly (List [Code Code])) + (.tuple (loop [idx 0 + pairsCC (: (List [Code Code]) + (list))] + (p.either (let [slotC (|> idx %n (format "____________slot") code.local-identifier)] + (do @ + [_ (wrap []) + memberC (Arg slotC)] + (recur (inc idx) + (list;compose pairsCC (list [slotC memberC]))))) + (wrap pairsCC)))))] (wrap (` (case (~ valueC) [(~+ (list;map product.left pairsCC))] [(~+ (list;map product.right pairsCC))])))) @@ -73,7 +74,7 @@ [_ (wrap []) #let [g! (code.local-identifier "____________") outL (code.local-identifier "____________outL")] - [inT+ outC] (poly.function (p.many poly.any) + [inT+ outC] (.function (p.many .any) (Arg outL)) #let [inC+ (|> (list.size inT+) list.indices @@ -83,15 +84,15 @@ (~ outC)))))) ## Recursion (do p.monad - [_ poly.recursive-call] + [_ .recursive-call] (wrap (` ((~' map) (~ funcC) (~ valueC))))) ## Parameters (do p.monad - [_ poly.any] + [_ .any] (wrap valueC)) )))] - [_ _ outputC] (: (poly.Poly [Code (List Code) Code]) - (p.either (poly.polymorphic + [_ _ outputC] (: (.Poly [Code (List Code) Code]) + (p.either (.polymorphic (Arg inputC)) (p.fail (format "Cannot create Functor for: " (%type inputT)))))] (wrap (` (: (~ (@Functor inputT)) diff --git a/stdlib/source/lux/macro/poly/json.lux b/stdlib/source/lux/macro/poly/json.lux index f397de2a2..e0e122eb1 100644 --- a/stdlib/source/lux/macro/poly/json.lux +++ b/stdlib/source/lux/macro/poly/json.lux @@ -5,7 +5,8 @@ [equivalence (#+ Equivalence)] ["." codec]] [control - ["p" parser]] + ["p" parser + ["<.>" type]]] [data ["." bit] maybe @@ -97,15 +98,15 @@ (wrap (` (: (~ (@JSON//encode inputT)) ))))] - [(poly.exactly Any) (function ((~ g!_) (~ (code.identifier ["" "0"]))) #/.Null)] - [(poly.sub Bit) (|>> #/.Boolean)] - [(poly.sub Nat) (:: (~! ..nat-codec) (~' encode))] - [(poly.sub Int) (:: (~! ..int-codec) (~' encode))] - [(poly.sub Frac) (|>> #/.Number)] - [(poly.sub Text) (|>> #/.String)]) + [(.exactly Any) (function ((~ g!_) (~ (code.identifier ["" "0"]))) #/.Null)] + [(.sub Bit) (|>> #/.Boolean)] + [(.sub Nat) (:: (~! ..nat-codec) (~' encode))] + [(.sub Int) (:: (~! ..int-codec) (~' encode))] + [(.sub Frac) (|>> #/.Number)] + [(.sub Text) (|>> #/.String)])