Monorepo for Aesthetic.Computer aesthetic.computer
at main 530 lines 19 kB view raw
1% !TEX program = xelatex 2\documentclass[10pt,letterpaper,twocolumn]{article} 3 4\usepackage[top=0.75in, bottom=0.75in, left=0.75in, right=0.75in]{geometry} 5\usepackage{fontspec} 6\usepackage{unicode-math} 7\setmainfont{Latin Modern Roman} 8\setsansfont{Latin Modern Sans} 9\newfontfamily\acbold{ywft-processing-bold}[Path=../../system/public/type/webfonts/,Extension=.ttf] 10\newfontfamily\aclight{ywft-processing-light}[Path=../../system/public/type/webfonts/,Extension=.ttf] 11\newfontfamily\kidlispfont{ComicRelief-Regular}[ 12 Path=../arxiv-kidlisp/fonts/, 13 Extension=.ttf 14] 15\newfontfamily\kidlispbold{ComicRelief-Bold}[ 16 Path=../arxiv-kidlisp/fonts/, 17 Extension=.ttf 18] 19\setmonofont{Latin Modern Mono}[Scale=0.85] 20 21\usepackage{xeCJK} 22\setCJKmainfont{Droid Sans Fallback} 23\setCJKsansfont{Droid Sans Fallback} 24\setCJKmonofont{Droid Sans Fallback} 25 26\usepackage{xcolor} 27\usepackage{titlesec} 28\usepackage{enumitem} 29\usepackage{booktabs} 30\usepackage{tabularx} 31\usepackage{multicol} 32\usepackage{fancyhdr} 33\usepackage{hyperref} 34\usepackage{graphicx} 35\graphicspath{{figures/}{../../papers/arxiv-ac/figures/}} 36\usepackage{ragged2e} 37\usepackage{microtype} 38\usepackage{listings} 39\usepackage{natbib} 40\usepackage[colorspec=0.92]{draftwatermark} 41 42% === COLORS === 43\definecolor{acpink}{RGB}{180,72,135} 44\definecolor{acpurple}{RGB}{120,80,180} 45\definecolor{acdark}{RGB}{64,56,74} 46\definecolor{acgray}{RGB}{119,119,119} 47\definecolor{klbrand}{RGB}{205,92,155} % kidlisp.com primary (--ac-purple) 48\definecolor{klcyan}{RGB}{112,214,255} % kidlisp.com ".com" cyan (#70D6FF) 49\definecolor{kldark}{RGB}{48,43,58} % kidlisp.com dark purple 50\definecolor{draftcolor}{RGB}{205,92,155} 51 52% KidLisp syntax colors 53\definecolor{klparen}{RGB}{140,140,160} 54\definecolor{klkw}{RGB}{119,51,170} 55\definecolor{klfn}{RGB}{0,136,170} 56\definecolor{klcolor}{RGB}{204,80,0} 57\definecolor{klnum}{RGB}{204,0,102} 58\definecolor{klstr}{RGB}{44,145,44} 59\definecolor{klcmt}{RGB}{140,140,140} 60\definecolor{klvar}{RGB}{50,50,50} 61\definecolor{klspecial}{RGB}{180,72,135} 62\definecolor{klbg}{RGB}{250,248,252} 63 64\DraftwatermarkOptions{text=WORKING DRAFT,fontsize=3cm,color=draftcolor!18,angle=45} 65 66\hypersetup{colorlinks=true,linkcolor=acpurple,urlcolor=acpurple,citecolor=acpurple, 67 pdftitle={KidLisp 语言参考:12 个类别中的 118 个内置函数}} 68 69\titleformat{\section}{\normalfont\bfseries\normalsize\uppercase}{\thesection.}{0.5em}{} 70\titlespacing{\section}{0pt}{1.2em}{0.3em} 71\titleformat{\subsection}{\normalfont\bfseries\small}{\thesubsection}{0.5em}{} 72\titlespacing{\subsection}{0pt}{0.8em}{0.2em} 73 74\pagestyle{fancy}\fancyhf{} 75\renewcommand{\headrulewidth}{0pt} 76\fancyhead[C]{\footnotesize\color{acpink}\textit{工作草稿 --- 请勿引用}} 77\fancyfoot[C]{\footnotesize\thepage} 78 79\newcommand{\ac}{\textsc{Aesthetic.Computer}} 80\newcommand{\kl}{\textsc{KidLisp}} 81 82% === KIDLISP LISTING STYLE === 83\lstdefinelanguage{kidlisp}{ 84 morekeywords=[1]{def,if,repeat,once,later,die,let,do,fn}, 85 morekeywords=[2]{wipe,ink,line,box,circle,tri,plot,flood,shape,write,type,paste,print}, 86 morekeywords=[3]{scroll,zoom,spin,blur,contrast,suck,pan,sort,resetSpin,smoothspin,unpan,fill,outline,stroke,nofill,nostroke}, 87 morekeywords=[4]{random,wiggle,sin,cos,tan,floor,ceil,round,abs,sqrt,min,max,mod}, 88 morekeywords=[5]{width,height,frame,time,resolution,fps,screen,pen,hand,gamepad,mic,amplitude}, 89 morekeywords=[6]{cube,form,trans,move,scale,hop,delay,jump,melody,overtone,speaker,sound}, 90 morekeywords=[7]{embed,layer,fade,bake,list,get,set,tap}, 91 sensitive=true, 92 morecomment=[l]{;}, 93 morestring=[b]", 94 literate= 95 {(}{{{\color{klparen}(}}}1 96 {)}{{{\color{klparen})}}}1 97 {\$}{{{\color{klspecial}\$}}}1, 98} 99 100\lstdefinestyle{kidlispstyle}{ 101 language=kidlisp, 102 basicstyle=\ttfamily\small, 103 keywordstyle=[1]\color{klkw}\bfseries, 104 keywordstyle=[2]\color{klfn}\bfseries, 105 keywordstyle=[3]\color{acpurple}, 106 keywordstyle=[4]\color{klnum}, 107 keywordstyle=[5]\color{klspecial}, 108 keywordstyle=[6]\color{klfn}, 109 keywordstyle=[7]\color{acpink}, 110 commentstyle=\color{klcmt}\itshape, 111 stringstyle=\color{klstr}, 112 numberstyle=\tiny\color{klcmt}, 113 breaklines=true, 114 frame=single, 115 rulecolor=\color{acgray!30}, 116 backgroundcolor=\color{klbg}, 117 xleftmargin=0.5em, 118 xrightmargin=0.5em, 119 aboveskip=0.5em, 120 belowskip=0.5em, 121 showstringspaces=false, 122} 123 124\lstset{style=kidlispstyle} 125 126\setlist[itemize]{nosep, leftmargin=1.2em, itemsep=0.1em} 127\setlength{\columnsep}{1.8em} 128\setlength{\parindent}{1em} 129\setlength{\parskip}{0.3em} 130 131% Hyphenation for narrow two-column layout 132\tolerance=800 133\emergencystretch=1em 134\hyphenpenalty=50 135 136\begin{document} 137 138\twocolumn[{% 139\begin{center} 140\includegraphics[height=4em]{pals}\par\vspace{0.5em} 141{\kidlispbold\fontsize{26pt}{30pt}\selectfont\color{kldark} Kid{\color{klbrand}Lisp} 语言参考}\par 142\vspace{0.2em} 143{\kidlispfont\fontsize{11pt}{13pt}\selectfont\color{klbrand} 12 个类别中的 118 个生成艺术内置函数}\par 144\vspace{0.3em} 145{\kidlispfont\fontsize{9pt}{11pt}\selectfont\color{acgray} 语法、语义、时序、色彩与 \texttt{\$}-Code 组合系统}\par 146\vspace{0.6em} 147{\normalsize\href{https://prompt.ac/@jeffrey}{@jeffrey}}\par 148{\small\color{acgray} Aesthetic.Computer}\par 149{\small\color{acgray} ORCID: \href{https://orcid.org/0009-0007-4460-4913}{0009-0007-4460-4913}}\par 150\vspace{0.3em} 151{\small\color{acpurple} \url{https://aesthetic.computer}}\par 152\vspace{0.6em} 153\rule{\textwidth}{1.5pt} 154\vspace{0.5em} 155\end{center} 156 157\begin{center} 158{\small\color{acpink}\textbf{[ 工作草稿 --- 请勿引用 ]}} 159\end{center} 160\vspace{0.3em} 161 162\begin{quote} 163\small\noindent\textbf{摘要。} 164\kl{} 是一种用于生成艺术的极简 Lisp 方言,拥有 12 个类别中的 118 个内置函数,没有用户自定义函数、没有递归、没有文件 I/O、没有网络访问。它在设计上就是安全的:\kl{} 程序无法突破其沙箱、无法使宿主崩溃、也无法访问画布之外的任何资源。本文作为完整的语言参考,以语法高亮的示例记录了每个内置函数、时序系统(基于帧和基于秒的调度,无需回调)、色彩模型(命名颜色、RGB、渐变、图案)、\texttt{\$}-code 组合系统(通过引用嵌入程序作为主要的抽象机制)以及求值模型(每帧对完整 AST 重新求值,使用确定性伪随机数生成器)。我们将 \kl{} 呈现为一种围绕\emph{省略}而设计的语言:它不能做什么与它能做什么同样经过深思熟虑,而这些限制产生了一个创作空间,59 位作者已用它编写了 16,634 个程序。 165\end{quote} 166\vspace{0.5em} 167}] 168 169\section{语法} 170 171\kl{} 使用 S-表达式语法~\citep{mccarthy1960recursive}。每个表达式要么是原子(数字、字符串、符号),要么是用括号括起来的列表: 172 173\begin{lstlisting} 174; Atoms 17542 ; number 176"hello" ; string 177red ; symbol (color name) 178 179; Lists (function application) 180(wipe "navy") ; clear screen navy 181(ink "red") ; set drawing color 182(box 10 10 50 50) ; draw rectangle 183(circle 64 64 30) ; draw circle 184\end{lstlisting} 185 186没有运算符、没有中缀表示法、除了括号、字符串、数字和符号之外没有任何特殊语法。算术使用前缀表示法: 187 188\begin{lstlisting} 189(+ 1 2) ; => 3 190(* 3 (+ 1 2)) ; => 9 191(- 10 3) ; => 7 192(/ 100 4) ; => 25 193(% 17 5) ; => 2 (modulo) 194\end{lstlisting} 195 196\section{12 个类别} 197 198\subsection{图形(9 个函数)} 199 200绘图原语以即时模式运行——每次调用直接渲染到帧缓冲区。 201 202\begin{lstlisting} 203(wipe "black") ; clear canvas 204(ink "cyan") ; set color 205(line 0 0 128 128) ; diagonal line 206(box 10 10 40 40) ; rectangle 207(circle 64 64 20) ; circle at center 208(tri 32 10 10 54 54 54) ; triangle 209(plot 64 64) ; single pixel 210(flood 64 64) ; flood fill 211(shape 10 10 50 10 30 50) ; polygon 212\end{lstlisting} 213 214\texttt{fill}\texttt{outline} 控制形状是实心还是描边(与 Processing 兼容): 215 216\begin{lstlisting} 217(fill) ; filled shapes 218(ink "red") 219(circle 64 64 30) ; solid red circle 220 221(outline) ; outlined shapes 222(ink "white") 223(circle 64 64 30) ; white ring 224\end{lstlisting} 225 226\subsection{变换(11 个函数)} 227 228像素级变换在绘制之后作用于帧缓冲区: 229 230\begin{lstlisting} 231(scroll 1 0) ; shift pixels right 232(zoom 1.01) ; slow zoom in 233(spin 1) ; rotate 1 degree 234(blur 2) ; gaussian blur 235(contrast 1.2) ; increase contrast 236(suck 0.5) ; radial displacement 237(sort) ; sort pixels by brightness 238\end{lstlisting} 239 240变换在帧之间\emph{累积},无需显式状态管理即可产生动画: 241 242\begin{lstlisting} 243(wipe "black") 244(ink "white") 245(circle 64 64 10) ; draw circle 246(spin 2) ; rotate 2 deg/frame 247(zoom 1.005) ; grow slightly/frame 248; Result: spiraling, growing circle 249\end{lstlisting} 250 251\subsection{数学(14 个函数)} 252 253\begin{lstlisting} 254(sin (* frame 0.05)) ; sine wave 255(cos (* frame 0.03)) ; cosine wave 256(random) ; 0-255 257(random 10) ; 0-9 258(random 5 15) ; 5-14 259(wiggle 10) ; random +/-5 260(floor 3.7) ; => 3 261(abs -5) ; => 5 262(sqrt 16) ; => 4 263(min 3 7 1) ; => 1 264(max 3 7 1) ; => 7 265\end{lstlisting} 266 267\texttt{random} 使用由程序短代码初始化的种子伪随机数生成器,使所有生成输出具有\textbf{确定性}:同一个程序始终产生相同的视觉效果。 268 269\subsection{色彩(19+ 个命名颜色)} 270 271颜色可以用名称、RGB 值或渐变字符串来指定: 272 273\begin{lstlisting} 274(ink "red") ; named color 275(ink 255 0 0) ; RGB 276(ink 255 0 0 128) ; RGBA (semi-transparent) 277(ink "fade:red-blue") ; horizontal gradient 278(ink (fade "cyan" "magenta" "vertical")) 279(ink "rainbow") ; repeating rainbow 280(ink "zebra") ; striped pattern 281\end{lstlisting} 282 283可用的颜色名称包括所有 CSS 命名颜色:\texttt{red}\texttt{green}\texttt{blue}\texttt{yellow}\texttt{orange}\texttt{purple}\texttt{pink}\texttt{cyan}\texttt{magenta}\texttt{black}\texttt{white}\texttt{gray}\texttt{brown}\texttt{lime}\texttt{navy}\texttt{teal}\texttt{gold}\texttt{coral}\texttt{salmon}\texttt{crimson}\texttt{lavender}\texttt{indigo}\texttt{violet}\texttt{turquoise}\texttt{tomato} 等。 284 285在第一行使用裸颜色名称可设置背景: 286 287\begin{lstlisting} 288"navy" 289(ink "gold") 290(circle 64 64 30) 291; Navy background, gold circle 292\end{lstlisting} 293 294\subsection{系统与显示(9 个函数)} 295 296\begin{lstlisting} 297(def w width) ; screen width 298(def h height) ; screen height 299frame ; current frame number 300time ; seconds elapsed 301 302(resolution "half") ; half-resolution 303(resolution 64 64) ; explicit 64x64 304(fps 30) ; set frame rate 305\end{lstlisting} 306 307\subsection{控制流(7 个函数)} 308 309\begin{lstlisting} 310; Variable definition 311(def x 50) 312(def y (+ 10 (* frame 0.5))) 313 314; Conditional 315(if (> frame 60) 316 (ink "red") 317 (ink "blue")) 318 319; Loop 320(repeat 10 i 321 (circle (* i 12) 64 5)) 322 323; Execute once (not every frame) 324(once (wipe "black")) 325 326; Local binding 327(let ((cx 64) (cy 64)) 328 (circle cx cy 30)) 329\end{lstlisting} 330 331\texttt{repeat} 循环将其计数器变量(上面的 \texttt{i})绑定为从 0 到 $n-1$。嵌套的 repeat 使用不同的变量名: 332 333\begin{lstlisting} 334(repeat 8 i 335 (repeat 8 j 336 (box (* i 16) (* j 16) 14 14))) 337; 8x8 grid of squares 338\end{lstlisting} 339 340\subsection{时序} 341 342\kl{} 的时序系统使用\emph{内联调度}而非回调。时序表达式包裹代码块: 343 344\begin{lstlisting} 345; Frame-based timing 346(0 (wipe "black")) ; every frame 347(1 (spin 5)) ; every 2nd frame 348(30f (ink "red")) ; after 30 frames 349 350; Second-based timing 351(1s (wipe "red")) ; after 1 second 352(2.5s (ink "gold")) ; after 2.5 seconds 353(1s! (write "!" 10 10)) ; fire once at 1s 354 355; Repeating intervals 356(0.5s... (spin 45)) ; every 0.5 seconds 357(1s... (scroll 1 0)) ; every second 358\end{lstlisting} 359 360这消除了对 \texttt{setTimeout}\texttt{setInterval} 或动画帧回调的需求。时间是表达式的\emph{属性},而非对其调用的函数。 361 362\subsection{文本(4 个函数)} 363 364\begin{lstlisting} 365(write "hello" 10 20) ; text at position 366(write "centered" ; centered text 367 (/ width 2) (/ height 2)) 368(type "typed" 10 10) ; typewriter effect 369\end{lstlisting} 370 371\subsection{输入(3 个函数)} 372 373\begin{lstlisting} 374; Pen/mouse position and state 375(if pen 376 (circle pen.x pen.y 5)) 377 378; Touch input 379(if hand 380 (line hand.x hand.y 64 64)) 381\end{lstlisting} 382 383\subsection{音频(6 个函数)} 384 385\begin{lstlisting} 386; Microphone input 387(def vol (mic)) ; amplitude 0-255 388(circle 64 64 vol) ; sound-reactive 389 390; Melody playback 391(melody "C4 E4 G4 C5") 392 393; Overtone harmonics 394(overtone 440 3) ; 3rd harmonic of A4 395\end{lstlisting} 396 397\subsection{3D 图形(8 个函数)} 398 399\begin{lstlisting} 400(cube "mycube") ; create 3D cube 401(form "mycube" 402 (move 0 0 -5) ; position 403 (scale 2) ; size 404 (spin (* frame 0.5) 1 1 0)) ; rotate 405\end{lstlisting} 406 407\subsection{通过 \texttt{\$}-Code 进行组合} 408 409\kl{} 最独特的特性是其组合系统。程序不使用用户自定义函数,而是通过短代码引用其他程序: 410 411\begin{lstlisting} 412; Embed another program as a layer 413($cow) ; execute program "cow" 414($nece 10 20) ; pass parameters 415 416; Multi-layer composition 417(wipe "black") 418($cow) ; background layer 419($faim) ; foreground layer 420 421; Chain multiple programs 422(resolution 64 64) 423($zobi) 424(spin 1) 425($grel) 426\end{lstlisting} 427 428程序从 MongoDB 获取,缓存在 RAM 和 IndexedDB 中,并通过 SHA-256 进行内容寻址。\texttt{\$}-code 系统用单一的社交机制取代了用户自定义函数、模块和库:\emph{找到别人制作的程序,通过代码引用它,与之组合}429 430被引用最多的程序是简单的视觉原语,充当构建模块。该系统产生了一个社交依赖图:程序 A 嵌入程序 B,程序 B 又嵌入程序 C,在观察到的使用中深达 7 层。 431 432\section{求值模型} 433 434\kl{}\textbf{每一帧重新求值其整个 AST}。除了显式定义的变量外,没有保留状态。\texttt{frame}\texttt{time} 变量自动递增。\texttt{zoom}\texttt{scroll}\texttt{spin} 等变换在帧缓冲区中累积。 435 436该模型消除了 Processing~\citep{reas2007processing} 中常见的 \texttt{setup()}/\texttt{draw()} 分离。\kl{} 程序的每一行在每一帧都会执行。动画源于依赖帧的表达式: 437 438\begin{lstlisting} 439(wipe "black") 440(ink "white") 441; Circle orbits center using sin/cos 442(circle 443 (+ 64 (* 30 (cos (* frame 0.03)))) 444 (+ 64 (* 30 (sin (* frame 0.03)))) 445 8) 446\end{lstlisting} 447 448\subsection{确定性随机} 449 450\texttt{random} 的种子来自程序的短代码。同一个程序在每台设备上、每一次都产生相同的输出。这使 \kl{} 程序成为\emph{可复现的生成艺术}:代码\emph{就是}艺术品,而不仅仅是生成它的指令。 451 452\subsection{混沌模式} 453 454无效输入——乱码、不匹配的括号、随机字符——不会产生错误消息。相反,\kl{} 检测低识别率($<30\%$ 的已知词汇)和高特殊字符比率($>50\%$),并渲染艺术化的视觉输出。用户永远不会看到"SyntaxError"。他们看到的是色彩。 455 456\section{省略式设计} 457 458\kl{} 刻意缺少: 459 460\begin{itemize} 461 \item \textbf{用户自定义函数}:由 \texttt{\$}-code 组合取代 462 \item \textbf{递归}:防止无限循环和栈溢出 463 \item \textbf{文件 I/O}:画布是唯一的输出 464 \item \textbf{网络}:程序无法获取外部数据 465 \item \textbf{字符串操作}:没有 \texttt{concat}\texttt{split}\texttt{replace} 466 \item \textbf{错误消息}:由混沌模式取代 467 \item \textbf{Import/require}:由 \texttt{\$}-code 取代 468 \item \textbf{对象/类}:数据仅有数字、字符串和列表 469\end{itemize} 470 471每一项省略都是设计决策。缺少用户自定义函数迫使通过社交层进行组合。缺少错误消息意味着初学者永远不会遇到满屏红色文字。缺少网络意味着每个程序都是自包含且安全运行的。 472 473这遵循了一个原则:语言的限制比其特性更能定义其创作空间~\citep{papert1980mindstorms, compton2015casual}\kl{} 不是一种碰巧能做艺术的通用语言。它是一种\emph{不能}做其他任何事情的艺术语言。 474 475\section{完整的内置函数索引} 476 477\begin{table}[h] 478\small 479\centering 480\begin{tabularx}{\columnwidth}{Xlr} 481\toprule 482\textbf{类别} & \textbf{数量} & \textbf{核心函数} \\ 483\midrule 484图形 & 9 & wipe, ink, line, box, circle \\ 485变换 & 11 & scroll, zoom, spin, blur, suck \\ 486数学 & 14 & +, -, *, /, random, sin, cos \\ 487色彩 & 19+ & red, blue, fade, rainbow, zebra \\ 488系统/显示 & 9 & width, height, frame, time \\ 489控制流 & 7 & def, if, repeat, once, let \\ 490文本/输出 & 4 & write, type, paste, print \\ 491输入 & 3 & pen, hand, gamepad \\ 492音频 & 6 & mic, melody, overtone, sound \\ 4933D 图形 & 8 & cube, form, trans, move, scale \\ 494组合 & 4 & \$code, layer, bake, embed \\ 495数据 & 4 & list, get, set, let \\ 496\midrule 497\textbf{合计} & \textbf{118} & \\ 498\bottomrule 499\end{tabularx} 500\caption{按类别划分的全部 118 个内置函数。} 501\label{tab:builtins} 502\end{table} 503 504\section{示例:完整程序} 505 506一个完整的 \kl{} 程序,绘制具有轨道运动的声音响应式粒子场: 507 508\begin{lstlisting} 509"black" 510(def vol (+ 5 (* (mic) 0.2))) 511(repeat 50 i 512 (def angle (* i 0.1256)) 513 (def r (+ 20 (* vol (sin (+ (* frame 0.02) angle))))) 514 (def cx (+ 64 (* r (cos (+ angle (* frame 0.01)))))) 515 (def cy (+ 64 (* r (sin (+ angle (* frame 0.01)))))) 516 (ink (+ 100 (* i 3)) 50 (+ 150 (* i 2))) 517 (circle cx cy (+ 1 (random 3)))) 518(blur 1) 519(zoom 1.002) 520\end{lstlisting} 521 522该程序:设置黑色背景,读取麦克风振幅,绘制 50 个在声音调制下沿轨道运动的粒子,用计算出的渐变为它们着色,应用轻微模糊,并缓慢放大。它以 60 FPS 运行。它只有 12 行。它不需要导入、不需要设置函数、不需要动画循环、也不需要事件监听器。 523 524\vspace{0.5em} 525\noindent\textbf{ORCID:} \href{https://orcid.org/0009-0007-4460-4913}{0009-0007-4460-4913} 526 527\bibliographystyle{plainnat} 528\bibliography{references} 529 530\end{document}