|
Warning: this is an htmlized version!
The original is here, and the conversion rules are here. |
% (find-LATEX "2026eoo.tex")
% (defun c () (interactive) (find-LATEXsh "lualatex -record 2026eoo.tex" :end))
% (defun C () (interactive) (find-LATEXsh "lualatex 2026eoo.tex" "Success!!!"))
% (defun D () (interactive) (find-pdf-page "~/LATEX/2026eoo.pdf"))
% (defun d () (interactive) (find-pdftools-page "~/LATEX/2026eoo.pdf"))
% (defun e () (interactive) (find-LATEX "2026eoo.tex"))
% (defun o () (interactive) (find-LATEX "2026eoo.tex"))
% (defun u () (interactive) (find-latex-upload-links "2026eoo"))
% (defun v () (interactive) (find-2a '(e) '(d)))
% (defun d0 () (interactive) (find-ebuffer "2026eoo.pdf"))
% (defun cv () (interactive) (C) (ee-kill-this-buffer) (v) (g))
% (defun oe () (interactive) (find-2a '(o) '(e)))
% (code-eec-LATEX "2026eoo")
% (find-pdf-page "~/LATEX/2026eoo.pdf")
% (find-sh0 "cp -v ~/LATEX/2026eoo.pdf /tmp/")
% (find-sh0 "cp -v ~/LATEX/2026eoo.pdf /tmp/pen/")
% (find-xournalpp "/tmp/2026eoo.pdf")
% file:///home/edrx/LATEX/2026eoo.pdf
% file:///tmp/2026eoo.pdf
% file:///tmp/pen/2026eoo.pdf
% http://anggtwu.net/LATEX/2026eoo.pdf
% https://anggtwu.net/LATEX/2026eoo.pdf
% (find-LATEX "2019.mk")
% (find-Deps1-links "Caepro5 Piecewise2 Maxima2")
% (find-Deps1-cps "Caepro5 Piecewise2 Maxima2")
% (find-Deps1-anggs "Caepro5 Piecewise2 Maxima2")
% (find-MM-aula-links "2026eoo" "2" "eoo" "eoo")
% «.geometry» (to "geometry")
% «.edrx26a» (to "edrx26a")
% «.biber» (to "biber")
% «.edrx26b» (to "edrx26b")
% «.edrx26c» (to "edrx26c")
% «.defs» (to "defs")
% «.footer» (to "footer")
% «.towidthcentered» (to "towidthcentered")
% «.translate-utf8» (to "translate-utf8")
%
% «.title» (to "title")
% «.toc» (to "toc")
% «.links» (to "links")
%
% «.writetoc» (to "writetoc")
% «.references» (to "references")
% ;-- defs
\documentclass[oneside,12pt]{article}
\usepackage[colorlinks,citecolor=DarkRed,urlcolor=DarkRed]{hyperref} % (find-es "tex" "hyperref")
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{pict2e}
\usepackage[x11names,svgnames]{xcolor} % (find-es "tex" "xcolor")
\usepackage{colorweb} % (find-es "tex" "colorweb")
\usepackage[T1]{fontenc}
\usepackage{fontspec}
%\setmonofont{Liberation Mono} % Or Fira Code, Liberation Mono, etc.
%\setmonofont{DejaVu Sans Mono} % Or Fira Code, Liberation Mono, etc.
%\usepackage[nomap]{FiraMono} % (find-es "tex" "firamono")
\setmonofont{Noto Sans Mono} % (find-es "tex" "notosansmono")
%
% «geometry» (to ".geometry")
% (find-es "tex" "geometry")
\usepackage[a6paper, landscape,
top=1.5cm, bottom=.25cm, left=1cm, right=1cm, includefoot
]{geometry}
%
% «edrx26a» (to ".edrx26a")
\usepackage{edrx26a} % (find-LATEX "edrx26a.sty")
%\usepackage{courier}
\begin{document}
% «edrx26b» (to ".edrx26b")
\input edrx26b.tex % (find-LATEX "edrx26b.tex")
% «edrx26c» (to ".edrx26c")
% (find-LATEX "edrx26c.tex")
%L processsubfile "edrx26c.tex" -- runs the "%L"s
\input edrx26c % loads the defs
% «defs» (to ".defs")
% (find-LATEX "edrx21defs.tex" "colors")
% (find-LATEX "edrx21.sty")
% «footer» (to ".footer")
% (find-LATEX "edrxheadfoot.tex")
\def\drafturl{http://anggtwu.net/LATEX/2026-1-C2.pdf}
\def\drafturl{http://anggtwu.net/2026.1-C2.html}
\def\draftfooter{\tiny \href{\drafturl}{\jobname{}} \ColorBrown{\shorttoday{} \hours}}
% «towidthcentered» (to ".towidthcentered")
\def\towidthcentered#1#2{%
\setbox0\hbox{#1}%
\hbox to \wd0{\hss#2\hss}%
}
\def\ttOne#1{\towidthcentered{\tt a}{\tt#1}}
% «translate-utf8» (to ".translate-utf8")
% (find-angg "LUA/Verbatim3.lua" "translate-utf8")
%L Co.__index.pat = ".[\128-\191]*"
%L Verbatim.__index.co:add("⇝", "\\ttOne{⇝}")
%L Verbatim.__index.co:add("⇒", "\\ttOne{⇒}")
%L Verbatim.__index.co:add("•", "\\ttOne{•}")
\pu
% ;-- title
% _____ _ _ _
% |_ _(_) |_| | ___ _ __ __ _ __ _ ___
% | | | | __| |/ _ \ | '_ \ / _` |/ _` |/ _ \
% | | | | |_| | __/ | |_) | (_| | (_| | __/
% |_| |_|\__|_|\___| | .__/ \__,_|\__, |\___|
% |_| |___/
%
% «title» (to ".title")
% (eoop 1 "title")
% (eooa "title")
\thispagestyle{empty}
\begin{center}
\vspace*{1.2cm}
{\bf \Large eoo.lua}
\ssk
Edrx's implementation of OO for Lua
\bsk
Eduardo Ochs % - RCN/PURO/UFF
\url{https://anggtwu.net/luaforth.html}
\end{center}
% (find-angg "LUA/lua50init.lua" "eoo")
% (find-angg "LUA/eoo.lua")
% (find-google-links "monospaced fonts for latex that have the box drawing characters")
% (find-angg "LUA/eoo.lua" "syntax-sugars")
% 1. Syntax sugars
% ================
% The best way to explain _in all details_ how this system of classes
% and objects works is by introducing some new notations, or "syntax
% sugars", that will be valid only in these comments; they are not
% implemented in the Lua parser.
%V The squiggly arrow "⇝" will mean "desugars to",
%V and is used like this:
%V
%V new notation ⇝ more basic notation
%V
%V In Lua these three syntax sugars are standard:
%V
%V T.foo ⇝ T["foo"]
%V function foo (...) ... end ⇝ foo = function (...) ... end
%V foo:bar(...) ⇝ foo.bar(foo, ...)
%L
%L defvbt "desugars-to"
\pu
$\vbt{desugars-to}$
%
% They are explained in these sections of the manual:
%
% (find-lua51manual "#2.2" "field name as an index" "a.name" "a[\"name\"]")
% (find-lua51manual "#2.3" "var.Name" "var[\"Name\"]")
% (find-lua51manual "#2.5.8" "v:name(args)" "v.name(v, args)")
% (find-lua51manual "#2.5.9" "function f () body end")
% (find-lua51manual "#2.5.9" "colon syntax")
%
% Our two first syntax sugars are these: "λ" will be an abbreviation for
% "function", and "⇒" will be an abbreviation for "return". So:
%
% Our third syntax sugar will be the "__mt" notation for metatables - we
% will pretend that the field ".__mt" of a table accesses its metatable.
% So:
%V λ(a,b) ⇒ expr end ⇝ function (a,b) return expr end
%V
%V B.__mt ⇝ getmetatable(B)
%V B.__mt = A ⇝ setmetatable(B, A)
%L
%L defvbt "3-std-sugars"
\pu
$\vbt{3-std-sugars}$
%
% Now some diagrammatic notations. We will draw tables as boxes with two
% coluns in which each line represents a key-value pair, like this:
%
%V ╭──────────╮
%V │ 1 ┆ 10 │
%V │ 2 ┆ 20 │ ⇝ {10, 20, a=30, ["!"]=40}
%V │ "a" ┆ 30 │
%V │ "!" ┆ 40 │
%V ╰──────────╯
%L
%L defvbt "sugar-table"
\pu
$\vbt{sugar-table}$
%
% This Lua code
%
%V A = {10, 20, 30}
%V B = {40, 50, a=A}
%L
%L defvbt "tables-A-B-lua"
\pu
$\vbt{tables-A-B-lua}$
%
% builds two tables, that we will draw like this:
%
%V ╭──────────╮ ╭──────────╮
%V B = │ 1 ┆ 40 │ A = │ 1 ┆ 10 │
%V │ 2 ┆ 50 │ │ 2 ┆ 20 │
%V │ "a" ┆ • ───────> │ 3 ┆ 30 │
%V ╰──────────╯ ╰──────────╯
%L
%L defvbt "tables-A-B-diag"
\pu
$\vbt{tables-A-B-diag}$
%
% A table with a metatable will be drawn in special way, with an "mt"
% at its bottom left pointing to its metatable. This code
%
%V A = {10, 20, 30}
%V C = {40, 50}
%V setmetatable(A, C)
%L
%L defvbt "tables-A-C-lua"
\pu
$\vbt{tables-A-C-lua}$
%
% generates the structures below; note that the "⇝" in the middle
% indicates that the diagram at the left is a syntax sugar for the
% diagram at the right...
%
%V ╭──────────╮ ╭─────────────╮
%V C = │ 1 ┆ 40 │ C = │ 1 ┆ 40 │
%V │ 2 ┆ 50 │ │ 2 ┆ 50 │
%V ╰mt────────╯ │ "__mt" ┆ • │
%V │ ⇝ ╰───────────│─╯
%V ╰──╮ ╭─────╯
%V v v
%V ╭──────────╮ ╭──────────╮
%V A = │ 1 ┆ 10 │ A = │ 1 ┆ 10 │
%V │ 2 ┆ 20 │ │ 2 ┆ 20 │
%V │ 3 ┆ 30 │ │ 3 ┆ 30 │
%V ╰──────────╯ ╰──────────╯
%L
%L defvbt "tables-A-C-diag"
\pu
$\vbt{tables-A-C-diag}$
%
% We will use the notation <func> to denote a function - that may or may
% not have a global name. For example, this code,
%
%V Class = {
%V type = "Class",
%V __call = function (class, o) return setmetatable(o, class) end,
%V }
%V setmetatable(Class, Class)
%L
%L defvbt "Class-lua"
\pu
$\vbt{Class-lua}$
%
% creates this in the memory:
%
%V <fcal> = function (class, o) return setmetatable(o, class) end
%V
%V ╭────────────────────╮
%V Class = │ "type" ┆ "Class" │
%V │ "__call" ┆ <fcal> │
%V ╰mt──────────────────╯
%V │ ^
%V ╰────╯
%L
%L defvbt "Class-diag"
\pu
$\vbt{Class-diag}$
%
% If we didn't have the trick of giving names like "<bla>" to function
% objects we would have to use bullets an arrows, like "• ──> ...", to
% show that the field "__call" above points to a certain function.
%
% From here onwards "Class" will always denote the table "Class" above.
%
% Our last (diagrammatic) syntax sugar will give us a convenient way to
% draw "normal" classes, i.e., classes that are not the class "Class".
% Every "normal" class - for example: Vector - is made of two tables,
% one for "the class itself" and another one for its "methods". When we
% run this code,
%
%V Vector = Class {
%V type = "Vector",
%V __add = function (V, W) return Vector {V[1]+W[1], V[2]+W[2]} end,
%V __tostring = function (V) return "("..V[1]..","..V[2]..")" end,
%V __index = {
%V norm = function (V) return math.sqrt(V[1]^2 + V[2]^2) end,
%V },
%V }
%L
%L defvbt "Vector-lua"
\pu
$\vbt{Vector-lua}$
%
% this will create these structures in the memory:
%
%V <fcal> = function (class, o) return setmetatable(o, class) end
%V <fadd> = function (V, W) return Vector {V[1]+W[1], V[2]+W[2]} end
%V <ftos> = function (V) return "("..V[1]..","..V[2]..")" end,
%V <fnrm> = function (V) return math.sqrt(V[1]^2 + V[2]^2) end
%V
%V ╭─Class───────────────────╮ ╭─────────────────────────╮
%V Vector = │ "type" ┆ "Vector" │ Vector = │ "type" ┆ "Vector" │
%V │ "__add" ┆ <fadd> │ │ "__add" ┆ <fadd> │
%V │ "__tostring" ┆ <ftos> │ │ "__tostring" ┆ <ftos> │
%V ├─────────────────────────│ ⇝ │ "__index" ┆ • │
%V │ "norm" ┆ <fnrm> │ ╰mt─────────────────│─────╯
%V ╰─────────────────────────╯ │ ╭──╯
%V │ v
%V │ ╭─────────────────╮
%V │ │ "norm" ┆ <fnrm> │
%V │ ╰─────────────────╯
%V v
%V ╭────────────────────╮
%V Class = │ "type" ┆ "Class" │
%V │ "__call" ┆ <fcal> │
%V ╰mt──────────────────╯
%V │ ^
%V ╰────╯
%L
%L defvbt "Vector-diag"
\pu
$\vbt{Vector-diag}$
%
% Note the "⇝" indicating that the diagram at the left is a
% (diagrammatic) syntax sugar for the diagram at the right... the upper
% part of the box, above the horizontal line, is "the class itself", and
% the part below the horizontal line is its "table of methods". The
% metatable of the "class itself" points to the table Class, that is
% omitted from the drawing at the left, and ".__index" field points to
% the "table of methods".
%
%
%
% «__mt» (to ".__mt")
%
% 2. The __mt notation
% ====================
% Metatables are explained in detail in section 2.8 of the Lua manual,
%
% (find-lua51manual "#2.8" "Metatables")
% (find-lua51manual "#pdf-setmetatable")
%
% but in a way that I find quite difficult. I found out that the "__mt
% notation", described above, simplifies A LOT the description of _most_
% of what happens when metatables are used and the metamethods like
% "__index" or "__add" are called.
%
% If A and B are tables having the same non-nil metatable, then modulo
% details, we have these "reductions":
%
%V A + B ⇝ A.__mt.__add(A, B)
%V A - B ⇝ A.__mt.__sub(A, B)
%V A * B ⇝ A.__mt.__mul(A, B)
%V A / B ⇝ A.__mt.__div(A, B)
%V A % B ⇝ A.__mt.__mod(A, B)
%V A ^ B ⇝ A.__mt.__pow(A, B)
%V - A ⇝ A.__mt.__unm(A, A)
%V A .. B ⇝ A.__mt.__concat(A, B)
%V #A ⇝ A.__mt.__len(A, A)
%V A == B ⇝ A.__mt.__eq(A, B)
%V A ~= B ⇝ not (A == B)
%V A < B ⇝ A.__mt.__lt(A, B)
%V A <= B ⇝ A.__mt.__le(A, B)
%V (or not (B < A) in the absence of __le)
%V A[k] ⇝ A.__mt.__index(A, k)
%V A[k] = v ⇝ A.__mt.__newindex(A, k, v)
%V A(...) ⇝ A.__mt.__call(A, ...)
%V tostring(A) ⇝ A.__mt.__tostring(A)
%L
%L defvbt "mt-ops"
\pu
$\vbt{mt-ops}$
%
% The term "reduction" was borrowed from λ-calculus, but I am using it
% here in an imprecise way. The listing above is a translation to the
% __mt-notation of the "main cases" that are mentioned in these sections
% of the manual:
%
% (find-lua51manual "#2.8" "op1 + op2")
% (find-lua51manual "#pdf-tostring")
%
% and we follow the order in which these cases are listed in the manual.
% The "main cases" are the cases in which both A and B are tables with
% the same metatable, the expected method - like "__add" - is a field in
% that metatable, and no errors happen. The manual also describes what
% happens when we are not in the "main case"; then the expressions at
% the left in the table above may "reduce" to other expressions.
%
%
%
%
%
%
% «__mt-box» (to ".__mt-box")
%
% 3. The __mt notation and box diagrams
% =====================================
% If we combine the reductions of the previous section with the box
% diagrams from section 1 we can visualize how some quite intrincate
% operations happen.
%
% For example, we can depict the values of a and b after these
% assignments,
%
%V a = {10, 20, 30}
%V b = {11, a, "foo", print}
%V T = {__index = b}
%V a.__mt = T
%L
%L defvbt "mt-a-b-T-lua"
\pu
$\vbt{mt-a-b-T-lua}$
%
% as:
%
%V ╭─────────────╮ ╭────────╮
%V b = │ 1 ┆ 11 │ a = │ 1 ┆ 10 │
%V │ 2 ┆ • ──────────> │ 2 ┆ 20 │
%V │ 3 ┆ "foo" │ │ 3 ┆ 30 │
%V │ 4 ┆ <print> │ ╰mt──────╯
%V ╰─────────────╯ │
%V ^ v
%V │ ╭────────────────╮
%V │ T = │ "__index" ┆ • ─────╮
%V │ ╰────────────────╯ │
%V │ │
%V ╰──────────────────────────────────────╯
%L
%L defvbt "mt-a-b-T-diag"
\pu
$\vbt{mt-a-b-T-diag}$
%
% Now let's try to calculate a[4] by a series of reductions. Without
% metatables we would have a[4] = nil, so:
%
%V a[4] ⇝ a.__mt.__index[4]
%V ⇝ T.__index[4]
%V ⇝ b[4]
%V ⇝ <print>
%L
%L defvbt "mt-a-b-T-reductions"
\pu
$\vbt{mt-a-b-T-reductions}$
%
% So a[4] yields <print>.
%
%
%
%
% «Vector» (to ".Vector")
%
% 4. A class "Vector"
% ===================
% Consider this demo code:
%
% --[[
%
% * (eepitch-lua51)
% * (eepitch-kill)
% * (eepitch-lua51)
% dofile "eoo.lua" -- this file
%V Vector = Class {
%V type = "Vector",
%V __add = function (V, W) return Vector {V[1]+W[1], V[2]+W[2]} end,
%V __tostring = function (V) return "("..V[1]..","..V[2]..")" end,
%V __index = {
%V norm = function (V) return math.sqrt(V[1]^2 + V[2]^2) end,
%V },
%V }
%V v = Vector {3, 4} -- v = { 3, 4, __mt = Vector}
%V w = Vector {20, 30} -- w = {20, 30, __mt = Vector}
%V print(v) --> (3,4)
%V print(v + w) --> (23,34)
%V print(v:norm()) --> 5
%V print( type(v)) --> table
%V print(otype(v)) --> Vector
%V print( type("")) --> string
%V print(otype("")) --> string
%L
%L defvbt "Vector-v-w-lua"
\pu
$\vbt{Vector-v-w-lua}$
%
% --]]
%
% After running the line "w = ..." we will have this:
%
%V <fcal> = function (class, o) return setmetatable(o, class) end
%V <fadd> = function (V, W) return Vector {V[1]+W[1], V[2]+W[2]} end
%V <ftos> = function (V) return "("..V[1]..","..V[2]..")" end,
%V <fnor> = function (V) return math.sqrt(V[1]^2 + V[2]^2) end
%V
%V ╭───────╮ ╭────────╮
%V v = │ 1 ┆ 3 │ w = │ 1 ┆ 20 │
%V │ 2 ┆ 4 │ │ 2 ┆ 30 │
%V ╰mt─────╯ ╰mt──────╯
%V │╭─────────────╯
%V ││
%V vv
%V ╭─────────────────────────╮
%V Vector = │ "type" ┆ "Vector" │
%V │ "__add" ┆ <fadd> │
%V │ "__tostring" ┆ <ftos> │
%V │ "__index" ┆ • │
%V ╰mt────────────────│──────╯
%V │ v
%V │ ╭─────────────────╮
%V │ │ "norm" ┆ <fnor> │
%V │ ╰─────────────────╯
%V v
%V ╭────────────────────╮
%V Class = │ "type" ┆ "Class" │
%V │ "__call" ┆ <fcal> │
%V ╰mt──────────────────╯
%V │ ^
%V ╰────╯
%L
%L defvbt "Vector-v-w-diag"
\pu
$\vbt{Vector-v-w-diag}$
%
%
%
% «Vector-reductions» (to ".Vector-reductions")
%
% 5. The "Vector" demo: reductions
% ================================
% We can now use the diagram above and our notation for reductions to
% understand how expressions like these ones
%
%V v = Vector {3, 4}
%V v:norm()
%L
%L defvbt "norm-example"
\pu
$\vbt{norm-example}$
%
% work. Remember that we can calculate the result of applying a
% λ-expression to its arguments in two steps, like this:
%
%V (λ(a,b) ⇒ a+b) (3, 4) ⇝ (a+b) [a:=3, b:=4]
%V ⇝ (3+4)
%L
%L defvbt "3-4-example"
\pu
$\vbt{3-4-example}$
%
% The "[a:=3, b:=4]" means "substitute all `a's by 3 and all `b's by 4
% in the preceding expression".
%
% Being slightly informal at some steps, we have this:
%
%V Vector {3, 4}
%V ⇝ Vector({3, 4})
%V ⇝ Vector.__mt.__call(Vector, {3, 4})
%V ⇝ Class.__call(Vector, {3, 4})
%V ⇝ <fcal>(Vector, {3, 4})
%V ⇝ (λ(class, o) ⇒ setmetatable(o, class)) (Vector, {3, 4})
%V ⇝ (λ(class, o) o.__mt=class; ⇒ o) (Vector, {3, 4})
%V ⇝ (o.__mt=class; ⇒ o) [o:={3, 4}, class:=Vector]
%V ⇝ {3, 4, __mt=Vector}
%L
%L defvbt "Vector-3-4-reductions"
\pu
$\vbt{Vector-3-4-reductions}$
%
% and so:
%
%V v = Vector {3, 4}
%V ⇝ v = {3, 4, __mt=Vector}
%L
%L defvbt "Vector-3-4-reductions-2"
\pu
$\vbt{Vector-3-4-reductions-2}$
%
% I am using ideas borrowed from basic λ-calculus to talk about mutable
% objects, so I had to improvise at some points.
%
% Here's what happens when we calculate "v:norm()":
%
%V v:norm()
%V ⇝ {3, 4, __mt=Vector}:norm()
%V ⇝ {3, 4, __mt=Vector}.norm ({3, 4, __mt=Vector})
%V ⇝ {3, 4, __mt=Vector}.__mt.__index.norm({3, 4, __mt=Vector})
%V ⇝ Vector.__index.norm({3, 4, __mt=Vector})
%V ⇝ (λ(V) ⇒ math.sqrt(V[1]^2+V[2]^2) end)({3, 4, __mt=Vector})
%V ⇝ (math.sqrt(V[1]^2+V[2]^2)) [V:={3, 4, __mt=Vector}]
%V ⇝ math.sqrt( 3^2+ 4^2)
%V ⇝ math.sqrt( 25)
%V ⇝ 5
%L
%L defvbt "v-norm-reductions"
\pu
$\vbt{v-norm-reductions}$
\GenericWarning{Success:}{Success!!!} % Used by `M-x cv'
\end{document}
% (find-pdfpages2-links "~/LATEX/" "2026eoo")
% Local Variables:
% coding: utf-8-unix
% outline-regexp: "% +;--"
% ee-tla: "eoo"
% End: