From eb770f4473a904285ea559279331a93cdb5b7ded Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Thu, 19 Oct 2017 13:40:34 -0400 Subject: - Implemented an more comfortable alternative to "with-expansions". --- stdlib/source/lux.lux | 65 +++++++- stdlib/source/lux/data/coll/ordered/dict.lux | 34 ++-- stdlib/source/lux/meta/poly/eq.lux | 228 +++++++++++++-------------- 3 files changed, 189 insertions(+), 138 deletions(-) (limited to 'stdlib/source') diff --git a/stdlib/source/lux.lux b/stdlib/source/lux.lux index 205f1a543..fd8948164 100644 --- a/stdlib/source/lux.lux +++ b/stdlib/source/lux.lux @@ -4556,8 +4556,8 @@ ## => (function [] (fold text/compose \"\" - (interpose \" \" - (map int/encode ))))"} + (interpose \" \" + (map int/encode ))))"} (do Monad [g!arg (gensym "arg")] (return (list (` (function [(~ g!arg)] (|> (~ g!arg) (~@ tokens)))))))) @@ -4568,8 +4568,8 @@ ## => (function [] (fold text/compose \"\" - (interpose \" \" - (map int/encode ))))"} + (interpose \" \" + (map int/encode ))))"} (do Monad [g!arg (gensym "arg")] (return (list (` (function [(~ g!arg)] (<| (~@ tokens) (~ g!arg)))))))) @@ -5906,3 +5906,60 @@ _ (fail "Wrong syntax for 'for'")))) + +(do-template [ ] + [(def: ( xy) + (All [a b] (-> [a b] )) + (let [[x y] xy] + ))] + + [left a x] + [right b y]) + +(def: (label-code code) + (-> Code (Meta [(List [Code Code]) Code])) + (case code + (^ [ann (#Form (list [_ (#Symbol ["" "~~"])] expansion))]) + (do Monad + [g!expansion (gensym "g!expansion")] + (wrap [(list [g!expansion expansion]) g!expansion])) + + (^template [] + [ann ( parts)] + (do Monad + [=parts (mapM Monad label-code parts)] + (wrap [(fold list/compose (list) (map left =parts)) + [ann ( (map right =parts))]]))) + ([#Form] [#Tuple]) + + [ann (#Record kvs)] + (do Monad + [=kvs (mapM Monad + (function [[key val]] + (do Monad + [=key (label-code key) + =val (label-code val) + #let [[key-labels key-labelled] =key + [val-labels val-labelled] =val]] + (wrap [(list/compose key-labels val-labels) [key-labelled val-labelled]]))) + kvs)] + (wrap [(fold list/compose (list) (map left =kvs)) + [ann (#Record (map right =kvs))]])) + + _ + (:: Monad wrap [(list) code]))) + +(macro: #export (`` tokens) + (case tokens + (^ (list raw)) + (do Monad + [=raw (label-code raw) + #let [[labels labelled] =raw]] + (wrap (list (` (with-expansions [(~@ (|> labels + (map (function [[label expansion]] (list label expansion))) + list/join))] + (~ labelled)))))) + + _ + (fail "Wrong syntax for ``") + )) diff --git a/stdlib/source/lux/data/coll/ordered/dict.lux b/stdlib/source/lux/data/coll/ordered/dict.lux index 21cd20eb8..1151a018b 100644 --- a/stdlib/source/lux/data/coll/ordered/dict.lux +++ b/stdlib/source/lux/data/coll/ordered/dict.lux @@ -251,24 +251,22 @@ (#;Some root) (let [reference (get@ #key root)] - (with-expansions - [ (do-template [ ] - [( reference key) - (let [side-root (get@ root) - outcome (recur side-root)] - (if (is side-root outcome) - ?root - (#;Some ( (maybe;assume outcome) - root))))] - - [T/< #left add-left] - [T/> #right add-right] - )] - (cond - - ## (T/= reference key) - ?root - ))) + (`` (cond (~~ (do-template [ ] + [( reference key) + (let [side-root (get@ root) + outcome (recur side-root)] + (if (is side-root outcome) + ?root + (#;Some ( (maybe;assume outcome) + root))))] + + [T/< #left add-left] + [T/> #right add-right] + )) + + ## (T/= reference key) + ?root + ))) ))] (set@ #root root' dict))) diff --git a/stdlib/source/lux/meta/poly/eq.lux b/stdlib/source/lux/meta/poly/eq.lux index 86a373a4c..c2ecd5988 100644 --- a/stdlib/source/lux/meta/poly/eq.lux +++ b/stdlib/source/lux/meta/poly/eq.lux @@ -30,122 +30,118 @@ ## [Derivers] (poly: #export Eq - (with-expansions - [ (do-template [ ] - [(do @ - [_ ] - (wrap (` (: (~ (@Eq inputT)) - ))))] + (`` (do @ + [#let [g!_ (code;local-symbol "\u0000_")] + *env* poly;env + inputT poly;peek + #let [@Eq (: (-> Type Code) + (function [type] + (` (eq;Eq (~ (poly;to-ast *env* type))))))]] + ($_ p;either + ## Primitive types + (~~ (do-template [ ] + [(do @ + [_ ] + (wrap (` (: (~ (@Eq inputT)) + ))))] - [poly;unit (function [(~ g!_) (~ g!_)] true)] - [poly;bool bool;Eq] - [poly;nat number;Eq] - [poly;int number;Eq] - [poly;deg number;Eq] - [poly;frac number;Eq] - [poly;text text;Eq]) -