aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/poly
diff options
context:
space:
mode:
Diffstat (limited to 'stdlib/source/poly')
-rw-r--r--stdlib/source/poly/lux/abstract/equivalence.lux13
-rw-r--r--stdlib/source/poly/lux/abstract/functor.lux10
-rw-r--r--stdlib/source/poly/lux/data/format/json.lux30
3 files changed, 38 insertions, 15 deletions
diff --git a/stdlib/source/poly/lux/abstract/equivalence.lux b/stdlib/source/poly/lux/abstract/equivalence.lux
index 3ee6fb5c5..bb037c7cc 100644
--- a/stdlib/source/poly/lux/abstract/equivalence.lux
+++ b/stdlib/source/poly/lux/abstract/equivalence.lux
@@ -108,16 +108,21 @@
## Variants
(do @
[members (<type>.variant (p.many equivalence))
- #let [g!_ (code.local-identifier "_____________")
+ #let [last (dec (list.size members))
+ g!_ (code.local-identifier "_____________")
g!left (code.local-identifier "_____________left")
g!right (code.local-identifier "_____________right")]]
(wrap (` (: (~ (@Equivalence inputT))
(function ((~ g!_) (~ g!left) (~ g!right))
(case [(~ g!left) (~ g!right)]
(~+ (list@join (list@map (function (_ [tag g!eq])
- (list (` [((~ (code.nat tag)) (~ g!left))
- ((~ (code.nat tag)) (~ g!right))])
- (` ((~ g!eq) (~ g!left) (~ g!right)))))
+ (if (nat.= last tag)
+ (list (` [((~ (code.nat (dec tag))) #1 (~ g!left))
+ ((~ (code.nat (dec tag))) #1 (~ g!right))])
+ (` ((~ g!eq) (~ g!left) (~ g!right))))
+ (list (` [((~ (code.nat tag)) #0 (~ g!left))
+ ((~ (code.nat tag)) #0 (~ g!right))])
+ (` ((~ g!eq) (~ g!left) (~ g!right))))))
(list.enumerate members))))
(~ g!_)
#0))))))
diff --git a/stdlib/source/poly/lux/abstract/functor.lux b/stdlib/source/poly/lux/abstract/functor.lux
index 4fdff71ef..7381cc0b8 100644
--- a/stdlib/source/poly/lux/abstract/functor.lux
+++ b/stdlib/source/poly/lux/abstract/functor.lux
@@ -51,11 +51,15 @@
## Variants
(do @
[_ (wrap [])
- membersC (<type>.variant (p.many (Arg<?> valueC)))]
+ membersC (<type>.variant (p.many (Arg<?> valueC)))
+ #let [last (dec (list.size membersC))]]
(wrap (` (case (~ valueC)
(~+ (list;join (list;map (function (_ [tag memberC])
- (list (` ((~ (code.nat tag)) (~ valueC)))
- (` ((~ (code.nat tag)) (~ memberC)))))
+ (if (n.= last tag)
+ (list (` ((~ (code.nat (dec tag))) #1 (~ valueC)))
+ (` ((~ (code.nat (dec tag))) #1 (~ memberC))))
+ (list (` ((~ (code.nat tag)) #0 (~ valueC)))
+ (` ((~ (code.nat tag)) #0 (~ memberC))))))
(list.enumerate membersC))))))))
## Tuples
(do p.monad
diff --git a/stdlib/source/poly/lux/data/format/json.lux b/stdlib/source/poly/lux/data/format/json.lux
index d3a32b27a..b324790fb 100644
--- a/stdlib/source/poly/lux/data/format/json.lux
+++ b/stdlib/source/poly/lux/data/format/json.lux
@@ -165,14 +165,21 @@
(do @
[#let [g!_ (code.local-identifier "_______")
g!input (code.local-identifier "_______input")]
- members (<type>.variant (p.many codec//encode))]
+ members (<type>.variant (p.many codec//encode))
+ #let [last (dec (list.size members))]]
(wrap (` (: (~ (@JSON//encode inputT))
(function ((~ g!_) (~ g!input))
(case (~ g!input)
(~+ (list@join (list@map (function (_ [tag g!encode])
- (list (` ((~ (code.nat tag)) (~ g!input)))
- (` ((~! /.json) [(~ (code.frac (..tag tag)))
- ((~ g!encode) (~ g!input))]))))
+ (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.enumerate members))))))))))
(do @
[g!encoders (<type>.tuple (p.many codec//encode))
@@ -270,13 +277,20 @@
(wrap (` (: (~ (@JSON//decode inputT))
((~! </>.array) ((~! p.some) (~ subC)))))))
(do @
- [members (<type>.variant (p.many codec//decode))]
+ [members (<type>.variant (p.many codec//decode))
+ #let [last (dec (list.size members))]]
(wrap (` (: (~ (@JSON//decode inputT))
($_ ((~! p.or))
(~+ (list@map (function (_ [tag memberC])
- (` (|> (~ memberC)
- ((~! p.after) ((~! </>.number!) (~ (code.frac (..tag tag)))))
- ((~! </>.array)))))
+ (if (n.= last tag)
+ (` (|> (~ memberC)
+ ((~! p.after) ((~! </>.number!) (~ (code.frac (..tag (dec tag))))))
+ ((~! p.after) ((~! </>.boolean!) (~ (code.bit #1))))
+ ((~! </>.array))))
+ (` (|> (~ memberC)
+ ((~! p.after) ((~! </>.number!) (~ (code.frac (..tag tag)))))
+ ((~! p.after) ((~! </>.boolean!) (~ (code.bit #0))))
+ ((~! </>.array))))))
(list.enumerate members))))))))
(do @
[g!decoders (<type>.tuple (p.many codec//decode))]