aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEduardo Julian2017-12-10 15:26:36 -0400
committerEduardo Julian2017-12-10 15:26:36 -0400
commit859c7485cd0e9ebe8d456ed58238bdec849bd6e1 (patch)
treeb854f14eb921d8567e4c06f38d1149a0659ff0a4
parent6352437a8403b09fa0c83843984323ce1e67e980 (diff)
- Some minor refactoring.
- Eliminated the dependency of STM upon FRP.
Diffstat (limited to '')
-rw-r--r--lux-mode/lux-mode.el1
-rw-r--r--stdlib/source/lux/concurrency/actor.lux14
-rw-r--r--stdlib/source/lux/concurrency/promise.lux98
-rw-r--r--stdlib/source/lux/concurrency/stm.lux192
-rw-r--r--stdlib/source/lux/data/color.lux12
-rw-r--r--stdlib/source/lux/data/lazy.lux18
-rw-r--r--stdlib/source/lux/data/tainted.lux4
-rw-r--r--stdlib/source/lux/time/duration.lux16
-rw-r--r--stdlib/source/lux/time/instant.lux18
-rw-r--r--stdlib/source/lux/type/abstract.lux14
-rw-r--r--stdlib/source/lux/type/unit.lux4
-rw-r--r--stdlib/source/lux/world/net/tcp.jvm.lux12
-rw-r--r--stdlib/source/lux/world/net/udp.jvm.lux10
13 files changed, 218 insertions, 195 deletions
diff --git a/lux-mode/lux-mode.el b/lux-mode/lux-mode.el
index 0a6e73f11..15c24490d 100644
--- a/lux-mode/lux-mode.el
+++ b/lux-mode/lux-mode.el
@@ -231,6 +231,7 @@ Called by `imenu--generic-function'."
"list" "list&" "io" "sequence" "tree"
"get@" "set@" "update@" "|>" "|>>" "<|" "<<|" "_$" "$_" "~" "~+" "~!" "~'" "::" ":::"
"|" "&" "->" "All" "Ex" "Rec" "primitive" "$" "type"
+ "@abstraction" "@representation" "^@representation"
"^" "^or" "^slots" "^multi" "^~" "^@" "^template" "^open" "^|>" "^code" "^stream&" "^regex"
"bin" "oct" "hex"
"pre" "post"
diff --git a/stdlib/source/lux/concurrency/actor.lux b/stdlib/source/lux/concurrency/actor.lux
index 3a032e00f..4c98d10e4 100644
--- a/stdlib/source/lux/concurrency/actor.lux
+++ b/stdlib/source/lux/concurrency/actor.lux
@@ -45,8 +45,8 @@
## TODO: Delete after new-luxc becomes the new standard compiler.
(def: (actor mailbox obituary)
(All [s] (-> (Atom <Mailbox>) (Promise <Obituary>) (Actor s)))
- (@abstract {#mailbox mailbox
- #obituary obituary}))
+ (@abstraction {#mailbox mailbox
+ #obituary obituary}))
(type: #export (Message s)
<Message>)
@@ -66,7 +66,7 @@
self (actor (atom (promise #.None))
(promise #.None))
process (loop [state init
- |mailbox| (io.run (atom.read (get@ #mailbox (@repr self))))]
+ |mailbox| (io.run (atom.read (get@ #mailbox (@representation self))))]
(do promise.Monad<Promise>
[[head tail] |mailbox|
?state' (handle head state self)]
@@ -75,7 +75,7 @@
(do @
[_ (end error state)]
(exec (io.run (promise.resolve [error state (#.Cons head (obituary tail))]
- (get@ #obituary (@repr self))))
+ (get@ #obituary (@representation self))))
(wrap [])))
(#e.Success state')
@@ -84,7 +84,7 @@
(def: #export (alive? actor)
(All [s] (-> (Actor s) Bool))
- (case (promise.poll (get@ #obituary (@repr actor)))
+ (case (promise.poll (get@ #obituary (@representation actor)))
#.None
true
@@ -97,7 +97,7 @@
(if (alive? actor)
(let [entry [message (promise #.None)]]
(do Monad<IO>
- [|mailbox| (atom.read (get@ #mailbox (@repr actor)))]
+ [|mailbox| (atom.read (get@ #mailbox (@representation actor)))]
(loop [|mailbox| |mailbox|]
(case (promise.poll |mailbox|)
#.None
@@ -105,7 +105,7 @@
[resolved? (promise.resolve entry |mailbox|)]
(if resolved?
(do @
- [_ (atom.write (product.right entry) (get@ #mailbox (@repr actor)))]
+ [_ (atom.write (product.right entry) (get@ #mailbox (@representation actor)))]
(wrap true))
(recur |mailbox|)))
diff --git a/stdlib/source/lux/concurrency/promise.lux b/stdlib/source/lux/concurrency/promise.lux
index 429a11931..e1084d9a9 100644
--- a/stdlib/source/lux/concurrency/promise.lux
+++ b/stdlib/source/lux/concurrency/promise.lux
@@ -6,26 +6,61 @@
[applicative #+ Applicative]
[monad #+ do Monad])
(data [product])
- (concurrency [atom #+ Atom atom])))
+ (concurrency [atom #+ Atom atom])
+ (type abstract)))
(def: #export concurrency-level
Nat
("lux process concurrency-level"))
-(type: #export (Promise a)
+(abstract: #export (Promise a)
{#.doc "Represents values produced by asynchronous computations (unlike IO, which is synchronous)."}
- (Atom [(Maybe a) (List (-> a (IO Top)))]))
-
-(def: #export (promise ?value)
- (All [a] (-> (Maybe a) (Promise a)))
- (atom [?value (list)]))
-
-(def: #export (poll promise)
- {#.doc "Polls a promise's value."}
- (All [a] (-> (Promise a) (Maybe a)))
- (|> (atom.read promise)
- io.run
- product.left))
+ (Atom [(Maybe a) (List (-> a (IO Top)))])
+
+ (def: #export (promise ?value)
+ (All [a] (-> (Maybe a) (Promise a)))
+ (@abstraction (atom [?value (list)])))
+
+ (def: #export (poll (^@representation promise))
+ {#.doc "Polls a promise's value."}
+ (All [a] (-> (Promise a) (Maybe a)))
+ (|> (atom.read promise)
+ io.run
+ product.left))
+
+ (def: #export (resolve value (^@representation promise))
+ {#.doc "Sets an promise's value if it has not been done yet."}
+ (All [a] (-> a (Promise a) (IO Bool)))
+ (do io.Monad<IO>
+ [(^@ old [_value _observers]) (atom.read promise)]
+ (case _value
+ (#.Some _)
+ (wrap false)
+
+ #.None
+ (do @
+ [#let [new [(#.Some value) #.None]]
+ succeeded? (atom.compare-and-swap old new promise)]
+ (if succeeded?
+ (do @
+ [_ (monad.map @ (function [f] (f value))
+ _observers)]
+ (wrap true))
+ (resolve value (@abstraction promise)))))))
+
+ (def: #export (await f (^@representation promise))
+ (All [a] (-> (-> a (IO Top)) (Promise a) Top))
+ (let [(^@ old [_value _observers]) (io.run (atom.read promise))]
+ (case _value
+ (#.Some value)
+ (io.run (f value))
+
+ #.None
+ (let [new [_value (#.Cons f _observers)]]
+ (if (io.run (atom.compare-and-swap old new promise))
+ []
+ (await f (@abstraction promise)))))))
+ )
(def: #export (resolved? promise)
{#.doc "Checks whether a promise's value has already been resolved."}
@@ -37,39 +72,6 @@
(#.Some _)
true))
-(def: #export (resolve value promise)
- {#.doc "Sets an promise's value if it has not been done yet."}
- (All [a] (-> a (Promise a) (IO Bool)))
- (do io.Monad<IO>
- [(^@ old [_value _observers]) (atom.read promise)]
- (case _value
- (#.Some _)
- (wrap false)
-
- #.None
- (do @
- [#let [new [(#.Some value) _observers]]
- succeeded? (atom.compare-and-swap old new promise)]
- (if succeeded?
- (do @
- [_ (monad.map @ (function [f] (f value))
- _observers)]
- (wrap true))
- (resolve value promise))))))
-
-(def: #export (await f promise)
- (All [a] (-> (-> a (IO Top)) (Promise a) Top))
- (let [(^@ old [_value _observers]) (io.run (atom.read promise))]
- (case _value
- (#.Some value)
- (io.run (f value))
-
- #.None
- (let [new [_value (#.Cons f _observers)]]
- (if (io.run (atom.compare-and-swap old new promise))
- []
- (await f promise))))))
-
(struct: #export _ (Functor Promise)
(def: (map f fa)
(let [fb (promise #.None)]
@@ -81,7 +83,7 @@
(def: functor Functor<Promise>)
(def: (wrap a)
- (atom [(#.Some a) (list)]))
+ (promise (#.Some a)))
(def: (apply ff fa)
(let [fb (promise #.None)]
diff --git a/stdlib/source/lux/concurrency/stm.lux b/stdlib/source/lux/concurrency/stm.lux
index 09daa5e2d..9b1cabe01 100644
--- a/stdlib/source/lux/concurrency/stm.lux
+++ b/stdlib/source/lux/concurrency/stm.lux
@@ -3,7 +3,7 @@
(lux (control [functor #+ Functor]
[applicative #+ Applicative]
[monad #+ do Monad])
- [io #- run]
+ [io #+ IO io]
(data [product]
[maybe]
[number "nat/" Codec<Text,Nat>]
@@ -11,13 +11,75 @@
(coll [list "list/" Functor<List> Fold<List>]
[dict #+ Dict]))
(concurrency [atom #+ Atom atom]
- ["P" promise]
+ [promise #+ Promise promise]
[frp "frp/" Functor<Channel>])
- ))
+ (type abstract)))
-(type: #export (Var a)
+(abstract: #export (Var a)
{#.doc "A mutable cell containing a value, and observers that will be alerted of any change to it."}
- (Atom [a (Dict Text (-> a (IO Unit)))]))
+ (Atom [a (Dict Text (-> a (IO Unit)))])
+
+ (def: #export (var value)
+ {#.doc "Creates a new STM var, with a default value."}
+ (All [a] (-> a (Var a)))
+ (@abstraction (atom.atom [value (dict.new text.Hash<Text>)])))
+
+ (def: read!!
+ (All [a] (-> (Var a) a))
+ (|>> @representation atom.read io.run product.left))
+
+ (def: #export (read! (^@representation var))
+ {#.doc "Reads var immediately, without going through a transaction."}
+ (All [a] (-> (Var a) (IO a)))
+ (|> var
+ atom.read
+ (:: io.Functor<IO> map product.left)))
+
+ (def: (write! new-value (^@representation var))
+ (All [a] (-> a (Var a) (IO Unit)))
+ (do io.Monad<IO>
+ [(^@ old [_value _observers]) (atom.read var)
+ succeeded? (atom.compare-and-swap old [new-value _observers] var)]
+ (if succeeded?
+ (do @
+ [_ (|> _observers
+ dict.values
+ (monad.map @ (function [f] (f new-value))))]
+ (wrap []))
+ (write! new-value (@abstraction var)))))
+
+ (def: #export (follow (^@representation target))
+ {#.doc "Creates a channel that will receive all changes to the value of the given var."}
+ (All [a] (-> (Var a) (IO (frp.Channel a))))
+ (let [head (: (frp.Channel ($ +0)) (frp.channel))
+ ## head (frp.channel)
+ channel-var (var head)
+ observer (function [label value]
+ (case (io.run (|> channel-var read!! (frp.write value)))
+ #.None
+ ## By closing the output Channel, the
+ ## observer becomes obsolete.
+ (atom.update (function [[value observers]]
+ [value (dict.remove label observers)])
+ target)
+
+ (#.Some tail')
+ (write! tail' channel-var)))]
+ (do io.Monad<IO>
+ [_ (atom.update (function [[value observers]]
+ (let [label (nat/encode (list/fold (function [key base]
+ (case (nat/decode key)
+ (#.Left _)
+ base
+
+ (#.Right key-num)
+ (n/max key-num base)))
+ +0
+ (dict.keys observers)))]
+ [value (dict.put label (observer label) observers)]))
+ target)]
+ (wrap head))))
+ )
(type: (Tx-Frame a)
{#var (Var a)
@@ -31,15 +93,6 @@
{#.doc "A computation which updates a transaction and produces a value."}
(-> Tx [Tx a]))
-(def: #export (var value)
- {#.doc "Creates a new STM var, with a default value."}
- (All [a] (-> a (Var a)))
- (atom.atom [value (dict.new text.Hash<Text>)]))
-
-(def: raw-read
- (All [a] (-> (Var a) a))
- (|>> atom.read io.run product.left))
-
(def: (find-var-value var tx)
(All [a] (-> (Var a) Tx (Maybe a)))
(|> tx
@@ -59,17 +112,10 @@
[tx value]
#.None
- (let [value (raw-read var)]
+ (let [value (read!! var)]
[(#.Cons [var value value] tx)
value]))))
-(def: #export (read! var)
- {#.doc "Reads var immediately, without going through a transaction."}
- (All [a] (-> (Var a) (IO a)))
- (|> var
- atom.read
- (:: Functor<IO> map product.left)))
-
(def: (update-tx-value var value tx)
(All [a] (-> (Var a) a Tx Tx))
(case tx
@@ -99,54 +145,9 @@
[]]
#.None
- [(#.Cons [var (raw-read var) value] tx)
+ [(#.Cons [var (read!! var) value] tx)
[]])))
-(def: (write! new-value var)
- (All [a] (-> a (Var a) (IO Unit)))
- (do Monad<IO>
- [(^@ old [_value _observers]) (atom.read var)
- succeeded? (atom.compare-and-swap old [new-value _observers] var)]
- (if succeeded?
- (do @
- [_ (|> _observers
- dict.values
- (monad.map @ (function [f] (f new-value))))]
- (wrap []))
- (write! new-value var))))
-
-(def: #export (follow target)
- {#.doc "Creates a channel that will receive all changes to the value of the given var."}
- (All [a] (-> (Var a) (IO (frp.Channel a))))
- (let [head (: (frp.Channel ($ +0)) (frp.channel))
- ## head (frp.channel)
- channel-var (var head)
- observer (function [label value]
- (case (io.run (|> channel-var raw-read (frp.write value)))
- #.None
- ## By closing the output Channel, the
- ## observer becomes obsolete.
- (atom.update (function [[value observers]]
- [value (dict.remove label observers)])
- target)
-
- (#.Some tail')
- (write! tail' channel-var)))]
- (do Monad<IO>
- [_ (atom.update (function [[value observers]]
- (let [label (nat/encode (list/fold (function [key base]
- (case (nat/decode key)
- (#.Left _)
- base
-
- (#.Right key-num)
- (n/max key-num base)))
- +0
- (dict.keys observers)))]
- [value (dict.put label (observer label) observers)]))
- target)]
- (wrap head))))
-
(struct: #export _ (Functor STM)
(def: (map f fa)
(function [tx]
@@ -185,7 +186,7 @@
(def: (can-commit? tx)
(-> Tx Bool)
(list.every? (function [[_var _original _current]]
- (is _original (raw-read _var)))
+ (is _original (read!! _var)))
tx))
(def: (commit-var! [_var _original _current])
@@ -196,44 +197,53 @@
(def: fresh-tx Tx (list))
+(type: Commit (Ex [a] [(STM a) (Promise a)]))
+
(def: pending-commits
- (Var (Ex [a] [(STM a) (P.Promise a)]))
- (var (:!! [])))
+ (Atom (Rec Commits (Promise [Commit Commits])))
+ (atom (promise #.None)))
(def: commit-processor-flag
(Atom Bool)
(atom false))
+(def: (issue-commit commit)
+ (-> Commit (IO Unit))
+ (let [entry [commit (promise #.None)]]
+ (loop [|commits| (io.run (atom.read pending-commits))]
+ (case (promise.poll |commits|)
+ #.None
+ (do io.Monad<IO>
+ [resolved? (promise.resolve entry |commits|)]
+ (if resolved?
+ (atom.write (product.right entry) pending-commits)
+ (recur |commits|)))
+
+ (#.Some [head tail])
+ (recur tail)))))
+
(def: (process-commit [stm-proc output])
- (-> [(STM Unit) (P.Promise Unit)] Top)
+ (-> [(STM Unit) (Promise Unit)] Top)
(let [[finished-tx value] (stm-proc fresh-tx)]
(io.run (if (can-commit? finished-tx)
(exec (list/map commit-var! finished-tx)
- (P.resolve value output))
- (write! [stm-proc output] pending-commits)))))
+ (promise.resolve value output))
+ (issue-commit [stm-proc output])))))
(def: init-processor!
(IO Unit)
- (do Monad<IO>
+ (do io.Monad<IO>
[flag (atom.read commit-processor-flag)]
(if flag
(wrap [])
(do @
[was-first? (atom.compare-and-swap flag true commit-processor-flag)]
(if was-first?
- (do Monad<IO>
- [inputs (follow pending-commits)]
- (exec (|> inputs
- (:! (frp.Channel [(STM Unit) (P.Promise Unit)]))
- (P.await (function recur [?inputs]
- (io (case ?inputs
- #.None
- []
-
- (#.Some [head tail])
- (exec (process-commit head)
- (P.await recur tail)))))))
- (wrap [])))
+ (exec (|> (io.run (atom.read pending-commits))
+ (promise.await (function recur [[head tail]]
+ (io (exec (process-commit (:! [(STM Unit) (Promise Unit)] head))
+ (promise.await recur tail))))))
+ (wrap []))
(wrap [])))
)))
@@ -243,8 +253,8 @@
Note that a transaction may be re-run an indeterminate number of times if other transactions involving the same variables successfully commit first.
For this reason, it's important to note that transactions must be free from side-effects, such as I/O."}
- (All [a] (-> (STM a) (P.Promise a)))
- (let [output (P.promise #.None)]
+ (All [a] (-> (STM a) (Promise a)))
+ (let [output (promise #.None)]
(exec (io.run init-processor!)
- (io.run (write! [stm-proc output] pending-commits))
+ (io.run (issue-commit [stm-proc output]))
output)))
diff --git a/stdlib/source/lux/data/color.lux b/stdlib/source/lux/data/color.lux
index 2a23c5406..899531a79 100644
--- a/stdlib/source/lux/data/color.lux
+++ b/stdlib/source/lux/data/color.lux
@@ -28,18 +28,18 @@
(def: #export (color [red green blue])
(-> [Nat Nat Nat] Color)
- (@abstract [(n/% rgb red)
- (n/% rgb green)
- (n/% rgb blue)]))
+ (@abstraction [(n/% rgb red)
+ (n/% rgb green)
+ (n/% rgb blue)]))
(def: #export unpack
(-> Color [Nat Nat Nat])
- (|>> @repr))
+ (|>> @representation))
(struct: #export _ (eq.Eq Color)
(def: (= reference sample)
- (let [[rr rg rb] (@repr reference)
- [sr sg sb] (@repr sample)]
+ (let [[rr rg rb] (@representation reference)
+ [sr sg sb] (@representation sample)]
(and (n/= rr sr)
(n/= rg sg)
(n/= rb sb)))))
diff --git a/stdlib/source/lux/data/lazy.lux b/stdlib/source/lux/data/lazy.lux
index 154a3018b..7d5fbb417 100644
--- a/stdlib/source/lux/data/lazy.lux
+++ b/stdlib/source/lux/data/lazy.lux
@@ -15,19 +15,19 @@
(def: (freeze' generator)
(All [a] (-> (-> [] a) (Lazy a)))
(let [cache (atom.atom #.None)]
- (@abstract (function [_]
- (case (io.run (atom.read cache))
- (#.Some value)
- value
+ (@abstraction (function [_]
+ (case (io.run (atom.read cache))
+ (#.Some value)
+ value
- _
- (let [value (generator [])]
- (exec (io.run (atom.compare-and-swap _ (#.Some value) cache))
- value)))))))
+ _
+ (let [value (generator [])]
+ (exec (io.run (atom.compare-and-swap _ (#.Some value) cache))
+ value)))))))
(def: #export (thaw l-value)
(All [a] (-> (Lazy a) a))
- ((@repr l-value) [])))
+ ((@representation l-value) [])))
(syntax: #export (freeze expr)
(with-gensyms [g!_]
diff --git a/stdlib/source/lux/data/tainted.lux b/stdlib/source/lux/data/tainted.lux
index 2e15ba974..d1ecbd213 100644
--- a/stdlib/source/lux/data/tainted.lux
+++ b/stdlib/source/lux/data/tainted.lux
@@ -8,11 +8,11 @@
(def: #export taint
(All [a] (-> a (Tainted a)))
- (|>> @abstract))
+ (|>> @abstraction))
(def: #export trust
(All [a] (-> (Tainted a) a))
- (|>> @repr)))
+ (|>> @representation)))
(def: #export (validate pred tainted)
(All [a] (-> (-> a Bool) (Tainted a) (Maybe a)))
diff --git a/stdlib/source/lux/time/duration.lux b/stdlib/source/lux/time/duration.lux
index f2eb63863..c17c5693f 100644
--- a/stdlib/source/lux/time/duration.lux
+++ b/stdlib/source/lux/time/duration.lux
@@ -17,16 +17,16 @@
(def: #export from-millis
(-> Int Duration)
- (|>> @abstract))
+ (|>> @abstraction))
(def: #export to-millis
(-> Duration Int)
- (|>> @repr))
+ (|>> @representation))
(do-template [<name> <op>]
[(def: #export (<name> param subject)
(-> Duration Duration Duration)
- (@abstract (<op> (@repr param) (@repr subject))))]
+ (@abstraction (<op> (@representation param) (@representation subject))))]
[merge i/+]
[frame i/%]
@@ -34,21 +34,21 @@
(def: #export (scale scalar duration)
(-> Int Duration Duration)
- (@abstract (i/* scalar (@repr duration))))
+ (@abstraction (i/* scalar (@representation duration))))
(def: #export (query param subject)
(-> Duration Duration Int)
- (i// (@repr param) (@repr subject)))
+ (i// (@representation param) (@representation subject)))
(struct: #export _ (Eq Duration)
(def: (= param subject)
- (i/= (@repr param) (@repr subject))))
+ (i/= (@representation param) (@representation subject))))
(struct: #export _ (Order Duration)
(def: eq Eq<Duration>)
(do-template [<name> <op>]
[(def: (<name> param subject)
- (<op> (@repr param) (@repr subject)))]
+ (<op> (@representation param) (@representation subject)))]
[< i/<]
[<= i/<=]
@@ -59,7 +59,7 @@
(do-template [<name> <op>]
[(def: #export (<name> duration)
(-> Duration Bool)
- (<op> 0 (@repr duration)))]
+ (<op> 0 (@representation duration)))]
[positive? i/>]
[negative? i/<]
diff --git a/stdlib/source/lux/time/instant.lux b/stdlib/source/lux/time/instant.lux
index 381820058..334b05115 100644
--- a/stdlib/source/lux/time/instant.lux
+++ b/stdlib/source/lux/time/instant.lux
@@ -24,37 +24,37 @@
(def: #export from-millis
(-> Int Instant)
- (|>> @abstract))
+ (|>> @abstraction))
(def: #export to-millis
(-> Instant Int)
- (|>> @repr))
+ (|>> @representation))
(def: #export (span from to)
(-> Instant Instant duration.Duration)
- (duration.from-millis (i/- (@repr from) (@repr to))))
+ (duration.from-millis (i/- (@representation from) (@representation to))))
(def: #export (shift duration instant)
(-> duration.Duration Instant Instant)
- (@abstract (i/+ (duration.to-millis duration) (@repr instant))))
+ (@abstraction (i/+ (duration.to-millis duration) (@representation instant))))
(def: #export (relative instant)
(-> Instant duration.Duration)
- (|> instant @repr duration.from-millis))
+ (|> instant @representation duration.from-millis))
(def: #export (absolute offset)
(-> duration.Duration Instant)
- (|> offset duration.to-millis @abstract))
+ (|> offset duration.to-millis @abstraction))
(struct: #export _ (Eq Instant)
(def: (= param subject)
- (:: number.Eq<Int> = (@repr param) (@repr subject))))
+ (:: number.Eq<Int> = (@representation param) (@representation subject))))
(struct: #export _ (Order Instant)
(def: eq Eq<Instant>)
(do-template [<name>]
[(def: (<name> param subject)
- (:: number.Order<Int> <name> (@repr param) (@repr subject)))]
+ (:: number.Order<Int> <name> (@representation param) (@representation subject)))]
[<] [<=] [>] [>=]
))
@@ -63,7 +63,7 @@
(def: order Order<Instant>)
(do-template [<name>]
[(def: <name>
- (|>> @repr (:: number.Enum<Int> <name>) @abstract))]
+ (|>> @representation (:: number.Enum<Int> <name>) @abstraction))]
[succ] [pred]
))
diff --git a/stdlib/source/lux/type/abstract.lux b/stdlib/source/lux/type/abstract.lux
index 9a991a2a9..d3f3eb118 100644
--- a/stdlib/source/lux/type/abstract.lux
+++ b/stdlib/source/lux/type/abstract.lux
@@ -49,8 +49,8 @@
plist'
(#.Cons [k' v'] (remove k plist')))))
-(def: down-cast Text "@abstract")
-(def: up-cast Text "@repr")
+(def: down-cast Text "@abstraction")
+(def: up-cast Text "@representation")
(def: macro-anns Code (' {#.macro? true}))
(def: representation-name
@@ -164,3 +164,13 @@
(` ((~! install-casts) (~ (code.local-symbol name)) [(~+ type-varsC)]))
(list/compose primitives
(list (` ((~! un-install-casts)))))))))
+
+(syntax: #export (^@representation [name (s.form s.local-symbol)] body [branches (p.some s.any)])
+ (let [g!representation (code.local-symbol name)]
+ (do @
+ [current-module macro.current-module-name
+ #let [g!@representation (code.symbol [current-module "@representation"])]]
+ (wrap (list& g!representation
+ (` (.let [(~ g!representation) ((~ g!@representation) (~ g!representation))]
+ (~ body)))
+ branches)))))
diff --git a/stdlib/source/lux/type/unit.lux b/stdlib/source/lux/type/unit.lux
index c483ad71b..e8092a4e6 100644
--- a/stdlib/source/lux/type/unit.lux
+++ b/stdlib/source/lux/type/unit.lux
@@ -21,11 +21,11 @@
(def: #export in
(All [unit] (-> Int (Qty unit)))
- (|>> @abstract))
+ (|>> @abstraction))
(def: #export out
(All [unit] (-> (Qty unit) Int))
- (|>> @repr)))
+ (|>> @representation)))
(sig: #export (Scale s)
(: (All [u] (-> (Qty u) (Qty (s u))))
diff --git a/stdlib/source/lux/world/net/tcp.jvm.lux b/stdlib/source/lux/world/net/tcp.jvm.lux
index dfc27a0f2..fcd037578 100644
--- a/stdlib/source/lux/world/net/tcp.jvm.lux
+++ b/stdlib/source/lux/world/net/tcp.jvm.lux
@@ -42,7 +42,7 @@
#out OutputStream}
(def: #export (read data offset length self)
- (let [in (get@ #in (@repr self))]
+ (let [in (get@ #in (@representation self))]
(P.future
(do (e.ErrorT io.Monad<IO>)
[bytes-read (InputStream::read [data (nat-to-int offset) (nat-to-int length)]
@@ -50,7 +50,7 @@
(wrap (int-to-nat bytes-read))))))
(def: #export (write data offset length self)
- (let [out (get@ #out (@repr self))]
+ (let [out (get@ #out (@representation self))]
(P.future
(do (e.ErrorT io.Monad<IO>)
[_ (OutputStream::write [data (nat-to-int offset) (nat-to-int length)]
@@ -58,7 +58,7 @@
(Flushable::flush [] out)))))
(def: #export (close self)
- (let [(^open) (@repr self)]
+ (let [(^open) (@representation self)]
(P.future
(do (e.ErrorT io.Monad<IO>)
[_ (AutoCloseable::close [] in)
@@ -70,9 +70,9 @@
(do (e.ErrorT io.Monad<IO>)
[input (Socket::getInputStream [] socket)
output (Socket::getOutputStream [] socket)]
- (wrap (@abstract {#socket socket
- #in input
- #out output}))))
+ (wrap (@abstraction {#socket socket
+ #in input
+ #out output}))))
(def: #export (client address port)
(-> //.Address //.Port (T.Task TCP))
diff --git a/stdlib/source/lux/world/net/udp.jvm.lux b/stdlib/source/lux/world/net/udp.jvm.lux
index 468d3b2b9..caec294cd 100644
--- a/stdlib/source/lux/world/net/udp.jvm.lux
+++ b/stdlib/source/lux/world/net/udp.jvm.lux
@@ -59,7 +59,7 @@
(def: #export (read data offset length self)
(-> Blob Nat Nat UDP (T.Task [Nat //.Address //.Port]))
- (let [(^open) (@repr self)
+ (let [(^open) (@representation self)
packet (DatagramPacket::new|receive [data (nat-to-int offset) (nat-to-int length)])]
(P.future
(do (e.ErrorT io.Monad<IO>)
@@ -74,13 +74,13 @@
(P.future
(do (e.ErrorT io.Monad<IO>)
[address (resolve address)
- #let [(^open) (@repr self)]]
+ #let [(^open) (@representation self)]]
(DatagramSocket::send (DatagramPacket::new|send [data (nat-to-int offset) (nat-to-int length) address (nat-to-int port)])
socket))))
(def: #export (close self)
(-> UDP (T.Task Unit))
- (let [(^open) (@repr self)]
+ (let [(^open) (@representation self)]
(P.future
(AutoCloseable::close [] socket))))
@@ -89,12 +89,12 @@
(P.future
(do (e.ErrorT io.Monad<IO>)
[socket (DatagramSocket::new|client [])]
- (wrap (@abstract (#socket socket))))))
+ (wrap (@abstraction (#socket socket))))))
(def: #export (server port)
(-> //.Port (T.Task UDP))
(P.future
(do (e.ErrorT io.Monad<IO>)
[socket (DatagramSocket::new|server [(nat-to-int port)])]
- (wrap (@abstract (#socket socket))))))
+ (wrap (@abstraction (#socket socket))))))
)