diff options
author | Eduardo Julian | 2021-08-07 02:20:09 -0400 |
---|---|---|
committer | Eduardo Julian | 2021-08-07 02:20:09 -0400 |
commit | 17e7566be51df5e428a6b10e6469201a8a9468da (patch) | |
tree | 0d4ed80c9c9d846784b5bf460f6e6f5fc5b96663 /stdlib/source/poly | |
parent | eff4c59794868b89d60fdc411f9b544a270b817e (diff) |
Made the be/de macros for (co)monadic expression extensible.
Diffstat (limited to 'stdlib/source/poly')
-rw-r--r-- | stdlib/source/poly/lux/abstract/equivalence.lux | 14 | ||||
-rw-r--r-- | stdlib/source/poly/lux/abstract/functor.lux | 18 | ||||
-rw-r--r-- | stdlib/source/poly/lux/data/format/json.lux | 85 |
3 files changed, 56 insertions, 61 deletions
diff --git a/stdlib/source/poly/lux/abstract/equivalence.lux b/stdlib/source/poly/lux/abstract/equivalence.lux index b1d4f413f..1c8aa70e1 100644 --- a/stdlib/source/poly/lux/abstract/equivalence.lux +++ b/stdlib/source/poly/lux/abstract/equivalence.lux @@ -43,12 +43,12 @@ (poly: #export equivalence (`` (do {! <>.monad} - [#let [g!_ (code.local_identifier "_____________")] + [.let [g!_ (code.local_identifier "_____________")] *env* <type>.env inputT <type>.peek - #let [@Equivalence (: (-> Type Code) + .let [@Equivalence (: (-> Type Code) (function (_ type) - (` ((~! /.Equivalence) (~ (poly.to_code *env* type))))))]] + (` ((~! /.Equivalence) (~ (poly.code *env* type))))))]] ($_ <>.either ## Basic types (~~ (template [<matcher> <eq>] @@ -108,7 +108,7 @@ ## Variants (do ! [members (<type>.variant (<>.many equivalence)) - #let [last (dec (list.size members)) + .let [last (dec (list.size members)) g!_ (code.local_identifier "_____________") g!left (code.local_identifier "_____________left") g!right (code.local_identifier "_____________right")]] @@ -129,7 +129,7 @@ ## Tuples (do ! [g!eqs (<type>.tuple (<>.many equivalence)) - #let [g!_ (code.local_identifier "_____________") + .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) g!rights (list\map (|>> nat\encode (text\compose "right") code.local_identifier) indices)]] @@ -141,7 +141,7 @@ ## Type recursion (do ! [[g!self bodyC] (<type>.recursive equivalence) - #let [g!_ (code.local_identifier "_____________")]] + .let [g!_ (code.local_identifier "_____________")]] (in (` (: (~ (@Equivalence inputT)) ((~! /.rec) (.function ((~ g!_) (~ g!self)) (~ bodyC))))))) @@ -157,7 +157,7 @@ [[funcC varsC bodyC] (<type>.polymorphic equivalence)] (in (` (: (All [(~+ varsC)] (-> (~+ (list\map (|>> (~) ((~! /.Equivalence)) (`)) varsC)) - ((~! /.Equivalence) ((~ (poly.to_code *env* inputT)) (~+ varsC))))) + ((~! /.Equivalence) ((~ (poly.code *env* inputT)) (~+ varsC))))) (function ((~ funcC) (~+ varsC)) (~ bodyC)))))) <type>.recursive_call diff --git a/stdlib/source/poly/lux/abstract/functor.lux b/stdlib/source/poly/lux/abstract/functor.lux index 089d5119b..ff797b29c 100644 --- a/stdlib/source/poly/lux/abstract/functor.lux +++ b/stdlib/source/poly/lux/abstract/functor.lux @@ -26,34 +26,34 @@ (poly: #export functor (do {! p.monad} - [#let [type_funcC (code.local_identifier "____________type_funcC") + [.let [type_funcC (code.local_identifier "____________type_funcC") funcC (code.local_identifier "____________funcC") inputC (code.local_identifier "____________inputC")] *env* <type>.env inputT <type>.peek [polyC varsC non_functorT] (<type>.local (list inputT) (<type>.polymorphic <type>.any)) - #let [num_vars (list.size varsC)] - #let [@Functor (: (-> Type Code) + .let [num_vars (list.size varsC)] + .let [@Functor (: (-> Type Code) (function (_ unwrappedT) (if (n.= 1 num_vars) - (` ((~! /.Functor) (~ (poly.to_code *env* unwrappedT)))) + (` ((~! /.Functor) (~ (poly.code *env* unwrappedT)))) (let [paramsC (|> num_vars dec list.indices (list\map (|>> %.nat code.local_identifier)))] (` (All [(~+ paramsC)] - ((~! /.Functor) ((~ (poly.to_code *env* unwrappedT)) (~+ paramsC))))))))) + ((~! /.Functor) ((~ (poly.code *env* unwrappedT)) (~+ paramsC))))))))) Arg<?> (: (-> Code (<type>.Parser Code)) (function (Arg<?> valueC) ($_ p.either ## Type-var (do p.monad - [#let [varI (|> num_vars (n.* 2) dec)] + [.let [varI (|> num_vars (n.* 2) dec)] _ (<type>.parameter! varI)] (in (` ((~ funcC) (~ valueC))))) ## Variants (do ! [_ (in []) membersC (<type>.variant (p.many (Arg<?> valueC))) - #let [last (dec (list.size membersC))]] + .let [last (dec (list.size membersC))]] (in (` (case (~ valueC) (~+ (list\join (list\map (function (_ [tag memberC]) (if (n.= last tag) @@ -81,11 +81,11 @@ ## Functions (do ! [_ (in []) - #let [g! (code.local_identifier "____________") + .let [g! (code.local_identifier "____________") outL (code.local_identifier "____________outL")] [inT+ outC] (<type>.function (p.many <type>.any) (Arg<?> outL)) - #let [inC+ (|> (list.size inT+) + .let [inC+ (|> (list.size inT+) list.indices (list\map (|>> %.nat (format "____________inC") code.local_identifier)))]] (in (` (function ((~ g!) (~+ inC+)) diff --git a/stdlib/source/poly/lux/data/format/json.lux b/stdlib/source/poly/lux/data/format/json.lux index d8eecd816..c4cae2881 100644 --- a/stdlib/source/poly/lux/data/format/json.lux +++ b/stdlib/source/poly/lux/data/format/json.lux @@ -4,8 +4,7 @@ [lux #* ["." debug] [abstract - [monad (#+ Monad do)] - [equivalence (#+ Equivalence)] + [monad (#+ do)] ["." codec]] [control ["." try] @@ -13,26 +12,22 @@ ["<.>" type] ["</>" json]]] [data - ["." bit] - maybe - ["." sum] - ["." product] - ["." text ("#\." equivalence) + ["." text ["%" format (#+ format)]] [collection - ["." list ("#\." fold monad)] - ["." row (#+ Row row) ("#\." monad)] - ["d" dictionary]]] + ["." list ("#\." monad)] + ["." row (#+ row)] + ["." dictionary]]] [macro [syntax (#+ syntax:)] ["." code] ["." poly (#+ poly:)]] [math [number - ["." i64] ["n" nat ("#\." decimal)] + ["." i64] ["." int] - ["." frac ("#\." decimal)]]] + ["." frac]]] [time ## ["." instant] ## ["." duration] @@ -104,7 +99,7 @@ (with_expansions [<basic> (template [<matcher> <encoder>] [(do ! - [#let [g!_ (code.local_identifier "_______")] + [.let [g!_ (code.local_identifier "_______")] _ <matcher>] (in (` (: (~ (@JSON\encode inputT)) <encoder>))))] @@ -128,9 +123,9 @@ [month.Month month.codec])] (do {! <>.monad} [*env* <type>.env - #let [@JSON\encode (: (-> Type Code) + .let [@JSON\encode (: (-> Type Code) (function (_ type) - (` (-> (~ (poly.to_code *env* type)) /.JSON))))] + (` (-> (~ (poly.code *env* type)) /.JSON))))] inputT <type>.peek] ($_ <>.either <basic> @@ -141,18 +136,18 @@ (in (` (: (~ (@JSON\encode inputT)) (\ (~! qty_codec) (~' encode)))))) (do ! - [#let [g!_ (code.local_identifier "_______") + [.let [g!_ (code.local_identifier "_______") g!key (code.local_identifier "_______key") g!val (code.local_identifier "_______val")] [_ _ =val=] (<type>.applied ($_ <>.and - (<type>.exactly d.Dictionary) + (<type>.exactly dictionary.Dictionary) (<type>.exactly .Text) encode))] (in (` (: (~ (@JSON\encode inputT)) - (|>> ((~! d.entries)) + (|>> ((~! dictionary.entries)) ((~! list\map) (function ((~ g!_) [(~ g!key) (~ g!val)]) [(~ g!key) ((~ =val=) (~ g!val))])) - ((~! d.of_list) (~! text.hash)) + ((~! dictionary.of_list) (~! text.hash)) #/.Object))))) (do ! [[_ =sub=] (<type>.applied ($_ <>.and @@ -167,27 +162,27 @@ (in (` (: (~ (@JSON\encode inputT)) (|>> ((~! list\map) (~ =sub=)) ((~! row.of_list)) #/.Array))))) (do ! - [#let [g!_ (code.local_identifier "_______") + [.let [g!_ (code.local_identifier "_______") g!input (code.local_identifier "_______input")] members (<type>.variant (<>.many encode)) - #let [last (dec (list.size members))]] + .let [last (dec (list.size members))]] (in (` (: (~ (@JSON\encode inputT)) (function ((~ g!_) (~ g!input)) (case (~ g!input) (~+ (list\join (list\map (function (_ [tag g!encode]) (if (n.= last tag) - (list (` ((~ (code.nat (dec tag))) #1 (~ g!input))) - (` ((~! /.json) [(~ (code.frac (..tag (dec tag)))) - #1 - ((~ g!encode) (~ g!input))]))) - (list (` ((~ (code.nat tag)) #0 (~ g!input))) - (` ((~! /.json) [(~ (code.frac (..tag tag))) - #0 - ((~ g!encode) (~ g!input))]))))) + (.list (` ((~ (code.nat (dec tag))) #1 (~ g!input))) + (` ((~! /.json) [(~ (code.frac (..tag (dec tag)))) + #1 + ((~ g!encode) (~ g!input))]))) + (.list (` ((~ (code.nat tag)) #0 (~ g!input))) + (` ((~! /.json) [(~ (code.frac (..tag tag))) + #0 + ((~ g!encode) (~ g!input))]))))) (list.enumeration members)))))))))) (do ! [g!encoders (<type>.tuple (<>.many encode)) - #let [g!_ (code.local_identifier "_______") + .let [g!_ (code.local_identifier "_______") g!members (|> (list.size g!encoders) list.indices (list\map (|>> n\encode code.local_identifier)))]] @@ -199,7 +194,7 @@ ## Type recursion (do ! [[selfC non_recC] (<type>.recursive encode) - #let [g! (code.local_identifier "____________")]] + .let [g! (code.local_identifier "____________")]] (in (` (: (~ (@JSON\encode inputT)) ((~! ..rec_encode) (.function ((~ g!) (~ selfC)) (~ non_recC))))))) @@ -214,7 +209,7 @@ (in (` (: (All [(~+ varsC)] (-> (~+ (list\map (function (_ varC) (` (-> (~ varC) /.JSON))) varsC)) - (-> ((~ (poly.to_code *env* inputT)) (~+ varsC)) + (-> ((~ (poly.code *env* inputT)) (~+ varsC)) /.JSON))) (function ((~ funcC) (~+ varsC)) (~ bodyC)))))) @@ -251,9 +246,9 @@ [month.Month month.codec])] (do {! <>.monad} [*env* <type>.env - #let [@JSON\decode (: (-> Type Code) + .let [@JSON\decode (: (-> Type Code) (function (_ type) - (` (</>.Parser (~ (poly.to_code *env* type))))))] + (` (</>.Parser (~ (poly.code *env* type))))))] inputT <type>.peek] ($_ <>.either <basic> @@ -265,7 +260,7 @@ ((~! <>.codec) (~! qty_codec) (~! </>.any)))))) (do ! [[_ _ valC] (<type>.applied ($_ <>.and - (<type>.exactly d.Dictionary) + (<type>.exactly dictionary.Dictionary) (<type>.exactly .Text) decode))] (in (` (: (~ (@JSON\decode inputT)) @@ -282,7 +277,7 @@ ((~! </>.array) ((~! <>.some) (~ subC))))))) (do ! [members (<type>.variant (<>.many decode)) - #let [last (dec (list.size members))]] + .let [last (dec (list.size members))]] (in (` (: (~ (@JSON\decode inputT)) ($_ ((~! <>.or)) (~+ (list\map (function (_ [tag memberC]) @@ -303,7 +298,7 @@ ## Type recursion (do ! [[selfC bodyC] (<type>.recursive decode) - #let [g! (code.local_identifier "____________")]] + .let [g! (code.local_identifier "____________")]] (in (` (: (~ (@JSON\decode inputT)) ((~! <>.rec) (.function ((~ g!) (~ selfC)) (~ bodyC))))))) @@ -317,7 +312,7 @@ [[funcC varsC bodyC] (<type>.polymorphic decode)] (in (` (: (All [(~+ varsC)] (-> (~+ (list\map (|>> (~) </>.Parser (`)) varsC)) - (</>.Parser ((~ (poly.to_code *env* inputT)) (~+ varsC))))) + (</>.Parser ((~ (poly.code *env* inputT)) (~+ varsC))))) (function ((~ funcC) (~+ varsC)) (~ bodyC)))))) <type>.parameter @@ -344,10 +339,10 @@ #dictionary (Dictionary Text Frac)}) (derived: (..codec Record)))} - (in (list (` (: (codec.Codec /.JSON (~ inputT)) - (implementation - (def: (~' encode) - ((~! ..encode) (~ inputT))) - (def: (~' decode) - ((~! </>.run) ((~! ..decode) (~ inputT)))) - )))))) + (in (.list (` (: (codec.Codec /.JSON (~ inputT)) + (implementation + (def: (~' encode) + ((~! ..encode) (~ inputT))) + (def: (~' decode) + ((~! </>.run) ((~! ..decode) (~ inputT)))) + )))))) |