From cf9e3fa6fef24cc0828ab882661fa96dc6f2570d Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Sun, 3 Feb 2019 11:53:21 -0400 Subject: Some refactoring around the identity function. --- stdlib/source/lux.lux | 9 ++-- stdlib/source/lux/data/format/css/selector.lux | 2 +- stdlib/source/lux/data/format/json.lux | 17 +++---- stdlib/source/lux/data/number.lux | 64 +++++++++++------------- stdlib/source/lux/data/number/ratio.lux | 7 +-- stdlib/source/lux/function.lux | 25 +++++++-- stdlib/source/lux/host.jvm.lux | 34 ++++++------- stdlib/source/lux/macro/syntax/common/writer.lux | 3 +- stdlib/test/test/lux.lux | 5 +- 9 files changed, 85 insertions(+), 81 deletions(-) diff --git a/stdlib/source/lux.lux b/stdlib/source/lux.lux index d3a0f1825..b7de70c5d 100644 --- a/stdlib/source/lux.lux +++ b/stdlib/source/lux.lux @@ -3985,10 +3985,7 @@ #None (fail "Wrong syntax for structure:")))) -(def: #export (id x) - {#.doc "Identity function. Does nothing to it's argument and just returns it."} - (All [a] (-> a a)) - x) +(def: (identity x) (All [a] (-> a a)) x) (macro: #export (type: tokens) {#.doc (text$ ($_ "lux text concat" @@ -5302,8 +5299,8 @@ parts-text )])) - ([#Form "(" ")" id] - [#Tuple "[" "]" id] + ([#Form "(" ")" ..identity] + [#Tuple "[" "]" ..identity] [#Record "{" "}" rejoin-all-pairs]) [new-cursor (#Rev value)] diff --git a/stdlib/source/lux/data/format/css/selector.lux b/stdlib/source/lux/data/format/css/selector.lux index 905012aeb..7b80e4973 100644 --- a/stdlib/source/lux/data/format/css/selector.lux +++ b/stdlib/source/lux/data/format/css/selector.lux @@ -1,5 +1,5 @@ (.module: - [lux (#- or and for id is? not) + [lux (#- or and for is? not) [data ["." text format]] diff --git a/stdlib/source/lux/data/format/json.lux b/stdlib/source/lux/data/format/json.lux index 7d2e90270..edafe3178 100644 --- a/stdlib/source/lux/data/format/json.lux +++ b/stdlib/source/lux/data/format/json.lux @@ -296,7 +296,7 @@ [string Text #String "string"] ) -(do-template [
]
+(do-template [      ]
   [(def: #export ( test)
      {#.doc (code.text ($_ text/compose "Asks whether a JSON value is a "  "."))}
      (->  (Reader Bit))
@@ -304,7 +304,7 @@
        [head any]
        (case head
          ( value)
-         (wrap (::  = test (
 value)))
+         (wrap (::  = test value))
 
          _
          (fail ($_ text/compose "JSON value is not "  ".")))))
@@ -316,17 +316,16 @@
        [head any]
        (case head
          ( value)
-         (let [value (
 value)]
-           (if (::  = test value)
-             (wrap [])
-             (fail ($_ text/compose "Value mismatch: " ( test) " =/= " ( value)))))
+         (if (::  = test value)
+           (wrap [])
+           (fail ($_ text/compose "Value mismatch: " ( test) " =/= " ( value))))
 
          _
          (fail ($_ text/compose "JSON value is not a "  ".")))))]
 
-  [boolean? boolean! Bit  bit.Equivalence     encode-boolean                      #Boolean "boolean" id]
-  [number?  number!  Frac number.Equivalence (:: number.Codec encode) #Number  "number"  id]
-  [string?  string!  Text text.Equivalence   text.encode                         #String  "string"  id]
+  [boolean? boolean! Bit  bit.Equivalence     encode-boolean                      #Boolean "boolean"]
+  [number?  number!  Frac number.Equivalence (:: number.Codec encode) #Number  "number"]
+  [string?  string!  Text text.Equivalence   text.encode                         #String  "string"]
   )
 
 (def: #export (nullable parser)
diff --git a/stdlib/source/lux/data/number.lux b/stdlib/source/lux/data/number.lux
index e745d542a..f297f2788 100644
--- a/stdlib/source/lux/data/number.lux
+++ b/stdlib/source/lux/data/number.lux
@@ -12,14 +12,14 @@
    [data
     ["." error (#+ Error)]
     ["." maybe]
-    ["." text]
+    ["." text (#+ Char)]
     [collection
      ["." array (#+ Array)]]]
+   ["." function]
    ["." math]]
   [/
    ["." i64]])
 
-## [Structures]
 (do-template [ ]
   [(structure: #export _ (Equivalence )
      (def: = ))]
@@ -44,14 +44,38 @@
   [Frac Equivalence f/< f/<= f/> f/>=]
   )
 
+(do-template [   ]
+  [(structure: #export _ (Enum )
+     (def: order )
+     (def: succ )
+     (def: pred ))]
+
+  [Nat  Order  inc dec]
+  [Int  Order  inc dec]
+  [Frac Order (f/+ ("lux frac smallest")) (f/- ("lux frac smallest"))]
+  [Rev  Order  inc dec]
+  )
+
+(do-template [   ]
+  [(structure: #export _ (Interval )
+     (def: enum )
+     (def: top )
+     (def: bottom ))]
+
+  [ Nat Enum  (:coerce Nat -1) 0]
+  [ Int Enum  +9_223_372_036_854_775_807 -9_223_372_036_854_775_808]
+  [Frac Enum ("lux frac max") ("lux frac min")]
+  [ Rev Enum  (:coerce Rev -1) (:coerce Rev 0)]
+  )
+
 (structure: #export _ (Number Nat)
   (def: + n/+)
   (def: - n/-)
   (def: * n/*)
   (def: / n//)
   (def: % n/%)
-  (def: negate id)
-  (def: abs id)
+  (def: (negate value) (n/- (:: Interval top) value))
+  (def: abs function.identity)
   (def: (signum x)
     (case x
       0 0
@@ -88,34 +112,10 @@
   (def: / r//)
   (def: % r/%)
   (def: (negate x) (r/- x (:coerce Rev -1)))
-  (def: abs id)
+  (def: abs function.identity)
   (def: (signum x)
     (:coerce Rev -1)))
 
-(do-template [   ]
-  [(structure: #export _ (Enum )
-     (def: order )
-     (def: succ )
-     (def: pred ))]
-
-  [Nat  Order  inc dec]
-  [Int  Order  inc dec]
-  [Frac Order (f/+ ("lux frac smallest")) (f/- ("lux frac smallest"))]
-  [Rev  Order  inc dec]
-  )
-
-(do-template [   ]
-  [(structure: #export _ (Interval )
-     (def: enum )
-     (def: top )
-     (def: bottom ))]
-
-  [ Nat Enum  (:coerce Nat -1) 0]
-  [ Int Enum  +9_223_372_036_854_775_807 -9_223_372_036_854_775_808]
-  [Frac Enum ("lux frac max") ("lux frac min")]
-  [ Rev Enum  (:coerce Rev -1) (:coerce Rev 0)]
-  )
-
 (do-template [   ]
   [(structure: #export  (Monoid )
      (def: identity )
@@ -177,9 +177,6 @@
   [Frac "lux frac encode" "lux frac decode" "Could not decode Frac"]
   )
 
-## [Values & Syntax]
-(type: Char Nat)
-
 (def: (get-char! full idx)
   (-> Text Nat Char)
   ("lux text char" full idx))
@@ -1011,10 +1008,9 @@
               shifted
               (f/* -1.0 shifted))))))
 
-## [Hash]
 (structure: #export _ (Hash Nat)
   (def: eq Equivalence)
-  (def: hash id))
+  (def: hash function.identity))
 
 (structure: #export _ (Hash Int)
   (def: eq Equivalence)
diff --git a/stdlib/source/lux/data/number/ratio.lux b/stdlib/source/lux/data/number/ratio.lux
index 647eaf4b5..1447040e6 100644
--- a/stdlib/source/lux/data/number/ratio.lux
+++ b/stdlib/source/lux/data/number/ratio.lux
@@ -8,12 +8,13 @@
     monad
     ["p" parser]]
    [data
-    ["E" error]
+    ["." error]
     ["." product]
     ["." maybe]
     [number ("nat/." Codec)]
     ["." text ("text/." Monoid)
      format]]
+   ["." function]
    ["." math]
    ["." macro
     ["." code]
@@ -121,7 +122,7 @@
   (def: (negate (^slots [#numerator #denominator]))
     {#numerator denominator
      #denominator numerator})
-  (def: abs id)
+  (def: abs function.identity)
   (def: (signum x)
     {#numerator 1
      #denominator 1}))
@@ -139,7 +140,7 @@
   (def: (decode input)
     (case (text.split-with separator input)
       (#.Some [num denom])
-      (do E.Monad
+      (do error.Monad
         [numerator (nat/decode num)
          denominator (nat/decode denom)]
         (wrap (normalize {#numerator numerator
diff --git a/stdlib/source/lux/function.lux b/stdlib/source/lux/function.lux
index 4d4af846a..ca6ebb73e 100644
--- a/stdlib/source/lux/function.lux
+++ b/stdlib/source/lux/function.lux
@@ -1,24 +1,39 @@
 (.module:
   [lux #*
-   [control [monoid (#+ Monoid)]]])
+   [control
+    [monoid (#+ Monoid)]]])
+
+(def: #export identity
+  {#.doc (doc "Identity function."
+              "Does nothing to it's argument and just returns it."
+              (= (identity "foo")
+                 "foo"))}
+  (All [a] (-> a a))
+  (|>>))
 
 (def: #export (compose f g)
-  {#.doc "Function composition."}
+  {#.doc (doc "Function composition."
+              (= ((compose f g) "foo")
+                 (f (g "foo"))))}
   (All [a b c]
     (-> (-> b c) (-> a b) (-> a c)))
   (|>> g f))
 
 (def: #export (constant value)
-  {#.doc "Create constant functions."}
+  {#.doc (doc "Create constant functions."
+              (= ((constant "foo") "bar")
+                 "foo"))}
   (All [o] (-> o (All [i] (-> i o))))
   (function (_ _) value))
 
 (def: #export (flip f)
-  {#.doc "Flips the order of the arguments of a function."}
+  {#.doc (doc "Flips the order of the arguments of a function."
+              (= ((flip f) "foo" "bar")
+                 (f "bar" "foo")))}
   (All [a b c]
     (-> (-> a b c) (-> b a c)))
   (function (_ x y) (f y x)))
 
 (structure: #export Monoid (All [a] (Monoid (-> a a)))
-  (def: identity id)
+  (def: identity ..identity)
   (def: compose ..compose))
diff --git a/stdlib/source/lux/host.jvm.lux b/stdlib/source/lux/host.jvm.lux
index 8a6a9f43c..6da77945f 100644
--- a/stdlib/source/lux/host.jvm.lux
+++ b/stdlib/source/lux/host.jvm.lux
@@ -15,7 +15,7 @@
     [collection
      ["." array (#+ Array)]
      ["." list ("list/." Monad Fold Monoid)]]]
-   function
+   ["." function]
    ["." type ("type/." Equivalence)]
    ["." macro (#+ with-gensyms Functor Monad)
     ["." code]
@@ -1388,7 +1388,7 @@
                          (spaced (list (super-class-decl$ super)
                                        (with-brackets (spaced (list/map super-class-decl$ interfaces)))
                                        (with-brackets (spaced (list/map constructor-arg$ constructor-args)))
-                                       (with-brackets (spaced (list/map (method-def$ id super) methods))))))]
+                                       (with-brackets (spaced (list/map (method-def$ function.identity super) methods))))))]
     (wrap (list (` ((~ (code.text def-code))))))))
 
 (syntax: #export (null)
@@ -1398,23 +1398,19 @@
 
 (def: #export (null? obj)
   {#.doc (doc "Test for null object reference."
-              (null? (null))
-              "=>"
-              #1
-              (null? "YOLO")
-              "=>"
-              #0)}
+              (= (null? (null))
+                 true)
+              (= (null? "YOLO")
+                 false))}
   (-> (primitive "java.lang.Object") Bit)
   ("jvm object null?" obj))
 
 (syntax: #export (??? expr)
   {#.doc (doc "Takes a (potentially null) ObjectType reference and creates a (Maybe ObjectType) for it."
-              (??? (: java/lang/String (null)))
-              "=>"
-              #.None
-              (??? "YOLO")
-              "=>"
-              (#.Some "YOLO"))}
+              (= (??? (: java/lang/String (null)))
+                 #.None)
+              (= (??? "YOLO")
+                 (#.Some "YOLO")))}
   (with-gensyms [g!temp]
     (wrap (list (` (let [(~ g!temp) (~ expr)]
                      (if ("jvm object null?" (~ g!temp))
@@ -1424,12 +1420,10 @@
 (syntax: #export (!!! expr)
   {#.doc (doc "Takes a (Maybe ObjectType) and returns a ObjectType."
               "A #.None would get translated into a (null)."
-              (!!! (??? (: java/lang/Thread (null))))
-              "=>"
-              (null)
-              (!!! (??? "YOLO"))
-              "=>"
-              "YOLO")}
+              (= (null)
+                 (!!! (??? (: java/lang/Thread (null)))))
+              (= "foo"
+                 (!!! (??? "foo"))))}
   (with-gensyms [g!value]
     (wrap (list (` ({(#.Some (~ g!value))
                      (~ g!value)
diff --git a/stdlib/source/lux/macro/syntax/common/writer.lux b/stdlib/source/lux/macro/syntax/common/writer.lux
index fe4c961e2..3affd97f7 100644
--- a/stdlib/source/lux/macro/syntax/common/writer.lux
+++ b/stdlib/source/lux/macro/syntax/common/writer.lux
@@ -5,6 +5,7 @@
     [collection
      [list ("list/." Functor)]]
     ["." product]]
+   ["." function]
    [macro
     ["." code]]]
   ["." //])
@@ -23,7 +24,7 @@
 
 (def: #export annotations
   (-> //.Annotations Code)
-  (|>> (list/map (product.both code.tag id))
+  (|>> (list/map (product.both code.tag function.identity))
        code.record))
 
 (def: #export type-variables
diff --git a/stdlib/test/test/lux.lux b/stdlib/test/test/lux.lux
index 72b9f45ef..0e41a961c 100644
--- a/stdlib/test/test/lux.lux
+++ b/stdlib/test/test/lux.lux
@@ -6,6 +6,7 @@
    [data
     [number
      ["." i64]]]
+   ["." function]
    ["." math
     ["r" random (#+ Random) ("r/." Functor)]]
    ["_" test (#+ Test)]]
@@ -21,8 +22,8 @@
     ($_ _.and
         (_.test "Every value is identical to itself."
                 (is? self self))
-        (_.test "The 'id' function doesn't change values in any way."
-                (is? self (id self)))
+        (_.test "The identity function doesn't change values in any way."
+                (is? self (function.identity self)))
         (do @
           [other (r.unicode 1)]
           (_.test "Values created separately can't be identical."
-- 
cgit v1.2.3