On the Origins of Objects by Means of Careful Selection - arXiv

Page created by Gabriel Sharp
 
CONTINUE READING
On the Origins of Objects by Means of
 Careful Selection
 Yegor Bugayenko
 yegor256@gmail.com
 Huawei
 Russia, Moscow
 Abstract • Memory abstractions (Section 7)
arXiv:2206.02585v1 [cs.PL] 6 Jun 2022

 We introduce a taxonomy of objects for EO program- • Math functions and algorithms (Section 8)
 ming language. This taxonomy is designed with a few • String manipulations (Section 9)
 principles in mind: non-redundancy, simplicity, and so • Collections (Section 10)
 on. The taxonomy is supposed to be used as a naviga- • I/O Streams (Section 11)
 tion map by EO programmers. It may also be helpful • File System abstractions (Section 12)
 as a guideline for designers of other object-oriented • Net Sockets (Section 13)
 languages or libraries for them. • Synchronization in multi-threading (Section 14)
 An object in EO is either a composition of other
 Keywords: Object-Oriented Programming objects or an atom, which is a foreign function interface
 to a lower-level runtime such as, for example, JVM or
 1 Introduction Assembly. In this paper, we denote atoms by a star, for
 Each object-oriented programming language offers its example: ★times .
 own set of objects, classes, types, functions, constants, Everywhere in the paper we use dark blue color to
 traits, templates, and so on, which programmers can highlight an object when it’s mentioned for the first
 use off-the-shelf to model their specific use cases: “De- time.
 velopment Kit” in Java, “Standard Library” in C++, In this paper2 we don’t provide exact specification
 Python, Swift, Rust, and Ruby, “.NET Standard” in C#, of each object, in order to avoid conflicts with their
 “Built-in Objects” in JavaScript, and so on. exact definitions in Objectionary3.
 Each Standard Library (SL) freely defines its own
 principles of abstraction. For example, to get an ab- 2 Principles
 solute value of a numeric object x in Java, one has
 We adhere to a few principles:
 to call a static method of another class Math.abs(x) ,
 while in Ruby it would be a property of the same object • Wherever possible, objects must be implemented
 x.abs . For example, in Java, an attempt to get a value in EO, instead of being atoms.
 from a hash map by a key will produce null in case of • There should be no functionality implemented
 its absence, while in C++ it will return an empty iter- two or more times in the entire taxonomy.
 ator. There are many similar examples demonstrating • Object names must not be abbreviated and must
 the absence of common ground between SLs. constitute an English sentence when being used
 As a humble attempt to create such a common ground through dot notation, for example:
 1 x.times.y > z
 for the world of objects, we suggest our own taxon-
 omy of objects for EO1 , an object-oriented program- means “ times is .” There are a few exceptions
 ming language with an intentionally reduced feature though, like eq , seq , lt , and a few others.
 set (Bugayenko, 2021). All objects are grouped as such: In case of any runtime error, an object is supposed
 • Primitives (Section 3) to “return” an error object, with message attribute
 • Array (Section 4) present (as string ) and @ attribute absent.
 • Flow control abstractions (Section 5) 2 Version 0.1.0
 3 https://www.objectionary.com
 • Non-standard bit-width numbers (Section 6)
 1 https://www.eolang.org
Yegor Bugayenko

3 Primitives The attribute ★lt is a bool object TRUE if it’s “less
There are five primitive data objects described below. than” another int and FALSE otherwise. Attributes
All of them have ★as-bytes attribute, which is an ab- gt , lte , and gte are “greater than,” “less than or
straction of their representation as a sequence of bytes. equal,” and “greater than or equal” comparisons re-
All primitives also have as-hash attribute, which, be- spectively.
ing an int , is their own implementation of a hash Integers with a different bit-width are explained in
function. They also have eq attribute, which is TRUE Section 6. All other manipulations with integers can be
if the primitive is equal to another object, through the done in their decorators. Some of them are explained
use of ˆ.as-bytes.eq . in Section 8.

3.1 Bytes 3.3 Bool
The center of the taxonomy is bytes data object, The object bool is a one-bit abstraction of a Boolean
which is an abstraction of an sequence of bytes. The se- value. Its ★as-bytes is either 00- or 01- .
quence can either be empty or contain an theoretically There are two pre-defined objects TRUE and FALSE .
unlimited number of bytes. A byte is a unit of informa- The attribute not is a Boolean inversion. Attributes
tion that consists of eight adjacent binary digits (bits), and , or , xor are logical conjunction, disjunction, and
each of which consists of a 0 or 1. In EO syntax a se- exclusive disjunction of the object itself with an a array
quence of bytes is denoted as either – (double dash) of other Boolean values; they use ★and , ★or , and
for an empty one or as a concatenation of xx- , where ★xor from ˆ.as-bytes respectively.
 xx is a hexadecimal representation of a byte. The attribute ★if is a branching mechanism, expect-
 The object bytes has these four attributes, to turn ing three arguments: a condition as bool , a “positive”
itself into one of four primitive data objects: ★as-int , object, and a “negative” object. When being dataized,
★as-bool , ★as-float , and ★as-string . The at- the object ★if first dataizes the condition and then,
tribute ★as-int expects the size of the sequence to be according to the result obtained, either dataizes the pos-
less or equal to eight. The attribute ★as-bool expects itive object or the negative one, returning the result as
exactly one byte to be in the sequence. The attribute a result of its own dataization. Both positive and nega-
★as-float expects exactly eight bytes. tive objects must be copies of the same abstract object.
 There are also attributes for bitwise operations: ★and , This code give a title to a random number:
★or , ★xor , ★left , and ★right . 2 QQ.io.stdout
 The object also has attribute ★eq for comparing 3 QQ.txt.sprintf
itself with another sequence of bytes. 4 "Coin toss: %s"
 The attribute ★size is the total number of bytes in 5 if.
the sequence. 6 random.gte 0.5
 The attribute ★slice represents a sub-sequence in- 7 "heads"
side the current one. 8 "tail"
 The attribute ★concat is a new sequence, which The attribute ★while is an iterating mechanism,
is a concatenation of two byte sequences: the current expecting two attributes: a condition as bool and an
and the provided one. abstract object with one free attribute. When being
 dataized, the object ★while first dataizes the condi-
3.2 Int tion and then either stops if it is FALSE or dataizes
The object int is an eight-bytes abstraction of a two’s the abstract object with an int equal to the number
complement signed integer in a big-endian order. of the iterating cycle. This code makes a few attempts
 The attribute ★plus is a summary of this number to find a random number that is smaller than 0.1:
and another int . The attribute minus is a subtrac- 9 while.
tion of another int from this number. The attributes 10 random.gte 0.1
★times and ★div are a multiplication and a division 11 [i]
of this number and another int . The attribute neg is 12 QQ.io.stdout > @
the same number with a different sign. 13 QQ.txt.sprintf
 14 "Attempt #%d" i
On the Origins of Objects by Means of Careful Selection

 The attribute switch is an abstraction of a multi- Strings in other encodings, such as UTF-16 or CP1251,
 case branching, where the object itself is compared may be implemented through direct manipulations
 sequentially with in provided ( , ) pairs and the with bytes .
 dataization result is equal to of the first matching All other manipulations with strings are explained
 pair, while the last pair is the one to be used if no other in Section 9.
 pairs match:
15 QQ.io.stdout 4 Array
16 TRUE.switch The object array is an abstraction of an immutable
17 * sequence of objects. For example, this code represents
18 * a two-dimensional array x of numbers and strings:
19 password.eq "swordfish" 27 * > x
20 "password is correct!" 28 * "green" 0x0D1
21 * 29 * "red" 0xF21
22 password.eq "" 30 * "blue" 0x11C
23 "empty password is not allowed"
 The attribute ★with is a new array with all ele-
24 *
 ments from the current array and a new element added
25 FALSE
 to the end of it.
26 "password is wrong"
 The attribute ★at is the -th element of the array.
 3.4 Float The attribute ★length is the total amount of ele-
 ments in the array.
 The object float is an abstraction of a floating point
 All other manipulations with arrays can be imple-
 number, which takes eight bytes and fills them up ac-
 mented in their decorators. Some of them are explained
 cording to the format suggested by IEEE 754 (2019).
 in Section 10.
 The attribute ★plus is a summary of this number
 and another float . The attribute minus is a sub-
 traction of another float from this number. The at-
 5 Flow Control
 tributes ★times and ★div are a multiplication and Here we define objects that help control dataization
 a division of this number and another float . The at- flow.
 tribute neg is the same number with a different sign. The object ★seq is an abstraction of sequence of
 The attribute ★lt is a bool object TRUE if it’s objects to be dataized sequentially. For example, this
 “less than” another float and FALSE otherwise. At- code prints one message to the console and then ter-
 tributes gt , lte , and gte are “greater than,” “less minates the program due to the inability to dataize the
 than or equal,” and “greater than or equal” compar- division by zero in the middle:
 isons respectively. 31 seq
 Floating-point numbers with a different bit-width 32 QQ.io.stdout "Hello, world!
 are explained in Section 6. All other manipulations 33 42.div 0
 with floating-point numbers can be done in their dec- 34 QQ.io.stdout "Bye!"
 orators. Some of them are explained in Section 8. The object ★goto enables forward and backward
 “jumps” either immediately finishing dataization or
 3.5 String restarting it. For example, this code won’t print a mes-
 The object string is an abstraction of a piece of Uni- sage to the console if x is equal to zero:
 code text in UTF-8 encoding, according to ISO 10646 35 goto
 (2020). UTF-8 uses one byte for the first 128 code points, 36 [g]
 and up to 4 bytes for other characters. 37 seq > @
 The attribute ★slice takes a piece of a string as 38 if. > y
 another string . The attribute ★length is a total 39 x.eq 0
 count of characters in the string. 40 g.forward 0
 41 42.div x
Yegor Bugayenko

42 QQ.io.stdout ★write implements writing operation, while the ob-
43 QQ.txt.sprintf "42/x = %d" y ject ★memory itself decorates the data object stored in
 In this example, the number will be read from the it. This code writes a number into memory and then
 console again, if it’s equal to zero: reads it back and prints:
44 goto 65 memory > m

45 [g] 66 seq

46 seq > @ 67 m.write 42
47 as-int. > x 68 QQ.io.stdout
48 QQ.txt.parsed 69 QQ.txt.sprintf "Memory contains %d" m
49 QQ.io.stdin The object ★cage is similar to ★memory , but it can
50 if. store any object, not only five primitives. The use of
51 x.eq 0 ★cage is strongly discouraged. The following code
52 g.backward stores a “book” in a ★cage , then retrieves it back, and
53 QQ.io.stdout prints one of its attribute:
54 QQ.txt.sprintf 70 [] > book
55 "Number %d is OK!" x 71 title > "Object Thinking"
 The object ★try enables catching of dataization fail- 72 author > "David West"
 ures and encapsulates three abstract objects. For exam- 73 cage > c
 ple, the following code prints "division by zero" and 74 seq
 then "finally": 75 c.write book
56 try 76 QQ.io.stdout
57 [] 77 QQ.txt.sprintf
58 42.div 0 > @ 78 "Title is %s" c.title
59 [ex] The object ram is an abstraction of a random-access
60 QQ.io.stdout > @ memory of certain size, which has two attributes ★write
61 ex.msg and ★read enabling writing and reading respectively.
62 [] In this example, one kilobyte of memory is allocated
63 QQ.io.stdout > @ when a copy of ★ram is created, 13 bytes is written
64 "finally" into it starting with the 200-th byte, and then only five
 of them read back and "Hello" is printed:
 6 Digits 79 ram 1024 > d

 Here we define objects that represent numbers with 80 seq

 smaller or larger bitwise width comparing with the 81 d.write 200 "Hello, world!"
 default 64-bits convention. All objects presented in this 82 QQ.io.stdout
 Section belong to QQ.digits package. 83 as-string.
 Objects int8 , int16 , int32 , and int128 are dec- 84 d.read 200 5
 orators of bytes with a predefined size. They imple- The object heap encapsulates a copy of ★ram and is
 ment the same numeric operations as int . Objects an abstraction of a memory management as it exists in
 float16 , float32 , and float128 are also decora- IEEE 1003.1 (2018). There are two attributes malloc
 tors of bytes , implementing the same operations as and free , which allocate and free memory blocks
 float . respectively. An allocation of a memory block creates a
 copy of heap.pointer object, which is a decorator of
 7 Memory int with two additional attributes write and read .
 Here we define objects that manage memory. In this code, 13 bytes are allocated, a string is stored
 The object ★memory is an abstraction of a read- into them, then read and printed, and then the bytes
 write memory capable of storing one of five primi- are freed:
 tive data objects mentioned in Section 3. The attribute 85 heap (ram 1024) > h
 86 seq
On the Origins of Objects by Means of Careful Selection

87 h.malloc > p 97 "Jeff"
88 p.write "Hello, world!" 98 3.14
89 QQ.io.stdout The object text is a decorator of a string , it has
90 (p.read 13).as-string the following attributes:
91 h.free p • contains : checks whether it contains 
 • ends-with : checks whether it ends with 
 8 Math • join : joins an array with this one as a glue
 Here we define objects that represent math functions • lower-case : makes it lower case
 and algorithms. All objects in this Section belong to • left-trim : removes leading spaces
 QQ.math package. • replace : finds and replaces a sub-string
 The object ★random is a abstraction of a random • right-trim : removes trailing spaces
 float between zero and one inclusively. • split : breaks it into an array of strings
 The object angle is a decorator of float . It as- • starts-with : checks whether it starts with 
 sumes that the angle is in radians and has the following • trim : removes both leading and trailing spaces
 attributes for trigonometric functions: sine , cosine , • upper-case : makes it upper case
 and tangent . There are attributes as-degrees and The object regex is an abstraction of a regular ex-
 as-radians for switching from radians to degrees pression, in accordance with IEEE 1003.1 (2018, Part 2)
 and backwards. (Extended Regular Expressions) and full support of Uni-
 The object rich-float is a decorator of float code. The attribute match is a split of the string into
 with the following additional attributes: parts:
 • abs : absolute value of itself (| |) 99 QQ.txt.regex "/(и([^ ]+))/i" > r
 • ceil : round itself up 100 r.match > m
 • floor : round itself down 101 "Из искры возгорится пламя"
 • ln : natural logarithm (ln ) 102 eq.
 • log : logarithm (log ) 103 m
 • max : the greater of itself and another number 104 *
 • min : the smaller of itself and another number 105 *
 • power : itself raised to the power ( ) 106 "Из"
 • signum : the sign of itself 0
 √ 107
 • sqrt : square root of itself ( ) 108 *
 The object rich-int is a decorator of int and has 109 "И"
 the same attributes as rich-float . 110 * "з" 1 *
 111 * " "
 9 Strings 112 *
 All objects presented in this Section belong to QQ.txt 113 "искры"
 package. 114 3
 The object parsed encapsulates a string and has 115 *
 a few attributes: as-int , as-hex , as-octal , as-bytes ,116 "и"
 as-float , and as-bool . For example, this code parses 117 * "скры" 4 *
 a hexadecimal number from the console: 118 * " возгор"
 119 *
92 as-hex.
 120 "ится"
93 QQ.txt.parsed
 121 15
94 QQ.io.stdin
 122 *
 The object sprintf builds a string according to the 123 "и"
 format provided and compliant with IEEE 1003.1 (2018, 124 "тся" 16 *
 Chapter 5), for example: 125 * " пламя"
95 QQ.txt.sprintf
96 "Hi, %d! The weight is %0.2f."
Yegor Bugayenko

10 Collections 11 I/O Streams
Here we discuss abstractions of lists, maps, sets, and Here we define manipulations with input and output
other “collections.” All objects presented in this Section streams. All objects presented in this Section belong to
belong to QQ.collections package. QQ.io package.
 It is expected that an input stream has the following
10.1 List interface:
The object list is a decorator of array . 126 [] > input
 The attribute is-empty is TRUE if the length of the 127 data > @
array is zero. 128 [max-count] > read /input
 The attribute eq is TRUE if each element of the 129 [] > close
array is equal to the corresponding element of another It is expected that an output stream has the following
array and the lengths of both arrays are the same. interface:
 The attribute without is a new array with the -th
 130 [] > output
element removed.
 131 [bytes-to-write] > write /input
 The attributes each , reduced , found , filtered ,
 132 [] > close
and mapped are respectively similar to forEach, reduce ,
 find , reduce , and map methods of Array object The object ★stdout is an “output” that prints a
in JavaScript (ECMA, 2011). A few “twin” attributes string to the standard output stream, while the ob-
 eachi , reducedi , filteredi , foundi , and mappedi ject ★stderr is an “output” that prints to the standard
are semantically the same, but with an extra int ar- error stream.
gument as a counter of a cycle. The object ★stdin is an abstraction of a string
 The attribute slice is a part of the array. currently available in the standard input stream until
 The attribute sorted is a new array with elements the EOL character ( \n ) and is an “input.” If there is
sorted using given comparison abstract object with no string in the stream, the object blocks dataization
two free attributes. and waits. This code endlessly reads strings from the
 The attribute reversed is a new array with ele- console and immediately prints them back:
ments positioned in a reverse order. 133 TRUE.while
 134 [i]
10.2 Map 135 QQ.io.stdout > @
The object map is a decorator of array of pairs ( , ). 136 QQ.io.stdin
The map ensures that all are always unique. It is ex- The object ★bytes-as-input is an “input” made
pected that each has as-hash attribute that behaves from bytes .
as int . The object ★memory-as-output is an “output” di-
 The attributes with , without , found , and foundi rected towards a copy of memory .
are reimplemented in map . The object ★copied is channel between an “input”
 and an “output,” which moves all available bytes from
10.3 Set the former to the latter when being dataized. For ex-
The object set is a decorator of array . The set ample, this code write a text to a temporary file:
ensures that all elements in the array are always unique. 137 QQ.io.copied
It is expected that each has as-hash attribute that 138 QQ.io.bytes-as-input
behaves as int . 139 as-bytes.
 The attributes with , without , found , and foundi 140 "Hello, world!"
are reimplemented in set . 141 as-output.
 142 QQ.fs.file "/tmp/foo.txt"
10.4 Mutable Arrays
 143 "w+"
The objects array-list and linked-list are the The object content is a string representation of
abstractions of mutable lists that behave like array . the entire content of an “input.”
They have three additional attributes: inject, replace ,
and extract . They encapsulate ★cage object.
On the Origins of Objects by Means of Careful Selection

 12 File System 149 QQ.io.content
 Here we define manipulations with files and directo- 150 s.as-input
 ries. All objects presented in this Section belong to 151 s.close

 QQ.fs package. The attribute ★listen make a copy of the socket
 The object file is an abstraction of a file and is and establishes a server connection. Then, the attribute
 a decorator of string , which is the path of the file. ★accept can be used to wait for an incoming connec-
 The attribute ★exists is TRUE if the file exists. The tion and then make a copy when it arrives. For example,
 attribute ★is-dir is TRUE if the file is a directory. this code binds to the 8080th port on a localhost and
 The attribute ★touch makes sure the file exists. The when a connection arrives, prints a message into it:
 attribute ★rm removes the file. The attribute ★mv re- 152 listen. > s
 names or moves the file to a new place. The attribute 153 QQ.net.socket
 ★size is the size of the file in bytes. The attribute 154 "127.0.0.1"
 ★as-output is an output stream for this file, while 155 8080
 the attribute ★as-input is an input stream. 156 QQ.io.copied
 The object directory is an abstraction of a direc- 157 QQ.io.bytes-as-input
 tory. The attribute mkdir creates the directory with all 158 "Hello, world!".as-bytes
 its parent directories. The attribute rm-rf deletes the 159 s.accept.as-output
 directory recursively with all its subdirectories. The 160 s.close
 attribute walk finds files in the directory using glob The attribute ★close terminates the connection.
 pattern matching. The attribute tmpfile is a tempo-
 rary file in the directory. 14 Synchronization
 The object ★tmpdir is a system directory of tem-
 Here we define objects used for explicit synchroniza-
 porary files.
 tion between threads. All objects presented in this Sec-
 The object path is a decorator of a string and
 tion belong to QQ.sync package.
 is a path of a file. The attribute ★resolve is a new
 The object semaphore is a counting semaphore
 path with a suffix appended to the current one. The
 with two attributes: ★capture and ★release .
 attribute ★dir-name is the name of the directory in
 the path. The attribute ★ext-name is the extension The object atomic-memory is a synchronized dec-
 in the path. The attribute ★base-name is the name of orator of memory with a binary semaphore encapsu-
 the file in the path. lated.

 13 Sockets References
 Here we define objects used for both server-side and Bugayenko, Yegor (2021). EOLANG and phi-calculus.
 client-side TCP sockets in accordance with IEEE 1003.1 arXiv: 2111.13384 [cs.PL].
 (2018). All objects presented in this Section belong to ECMA (June 2011). Standard ECMA-262 — ECMAScript
 QQ.net package. Language Specification. 5.1.
 The object socket is an abstraction of a TCP socket. IEEE 1003.1 (2018). “IEEE Standard for Information
 The attribute ★connect establishes a connection and Technology—Portable Operating System Interface (POSIXTM)
 makes a copy of the socket object. Then, its attributes Base Specifications”. In: IEEE Std 1003.1-2017 (Revi-
 ★as-input and ★as-output may be used for read- sion of IEEE Std 1003.1-2008) (7).
 ing from the socket and for writing to it. For example, IEEE 754 (2019). “IEEE Standard for Floating-Point
 this code opens a connection to the 80th TCP port of Arithmetic”. In: IEEE Std 754-2019 (Revision of IEEE
 google.com and then reads the entire stream as a 754-2008).
 Unicode string: ISO 10646 (2020). “Information Technology — Univer-
 sal Coded Character Set (UCS)”. In: ISO/IEC 10646:2020.
144 connect. > s
145 QQ.net.socket
146 "google.com"
147 80
148 QQ.io.stdout
You can also read