Learn する in one sitting
This tour takes you from “hello” to a compiled program. You don’t need to speak Japanese: each step explains the few words it uses. Every example on this page was run with する, and the output shown is what it printed.
01Setup
Install the interpreter from PyPI (or with Homebrew: brew install suru-lang/suru/suru). It needs Python 3.10 or newer and nothing else.
$ pip install suru-lang $ suru --version
する 0.13.0
Save this line as hello.suru and run it with suru hello.suru.
「こんにちは、世界」 を 表示する
こんにちは、世界
Read it right to left like an English sentence: display (表示する) the string “Hello, world” (「こんにちは、世界」). Strings go in Japanese quotation marks 「」, and を marks the object of the verb. Lines that start with # are comments.
「」 with the [ ] keys, and the typing practice walks you through it.suru with no file to open the interactive shell. Type an expression and it answers with the value and its type, such as 9 は 数 (“9 is a number”).02Values and names
とは means “is defined as.” A name made of kanji, katakana or Latin letters can hold any value. A line that is just an expression prints its value.
税率 とは 0.1 です # tax rate 値段 とは 1200 です # price 値段 * (1 + 税率) 「する」 + 「言語」
1320 する言語
Arithmetic uses the usual + - * / % ^ (full-width + × ÷ work too), and + joins strings. Parentheses may be ASCII or full-width.
03Functions and particles
A function name is a verb that ends in する (“to do”). You define it by writing an example call with parameter names, then とは and the body. The です (“is”) at the end only makes it read as a sentence; it means the same without it.
x を 二乗する とは x * x です # “to square x is x * x” 5 を 二乗する
25
Each parameter is tagged with a particle. When you call the function, the particles match the arguments to the parameters, so the order doesn’t matter:
a を b で 割った数にする とは a / b です # 割った数にする = make it into “the divided number” 10 を 2 で 割った数にする 2 で 10 を 割った数にする
5 5
| Particle | Rough meaning | Example |
|---|---|---|
| を | the main object | x を 二乗する |
| に | to, onto | 3 を 5 に 加算する |
| で | by, with | 10 を 2 で 除算する |
| から | from | 10 から 3 を 減算する |
| へ | toward | s を 「a」 から 「b」 へ 置換する |
| より | than | (free for your own functions) |
A function can use each particle only once. Calling it with a missing argument, or with a particle it doesn’t take, is an error that is reported before the program runs.
04Lists
と means “and,” and it builds a list. の (“of”) reads a property of a list: 先頭 (head), 残り (rest), 最後 (last) and 長さ (length).
数列 とは 1 と 2 と 3 と 4 です 数列 の 先頭 数列 の 長さ 数列 を 加算する 5 を 数列 に 追加する
1 4 10 (1 と 2 と 3 と 4 と 5)
The built-in 加算する (“add”) sums a whole list. 追加する (“append”) returns a new list, because every value in する is immutable. The empty list is 空, and a list of one is written 404 だけ (“only 404”), which prints as (404).
05Pipelines
Change the ending する to して (“and then”) and the result flows into the を of the next verb. Long chains read like a sentence.
x を 二乗する とは x * x です 3 を 二乗して 1 に 加算する # square 3, then add 1 1 と 2 を 加算して 二乗する # add 1 and 2, then square
10 9
06Functions as values
Ending a verb in すること (“the act of doing”) turns it into a value you can pass around. If you give it some arguments first, it is partially applied. An anonymous function is written x を 受け取って … を 返すこと (“take x and return …”).
x を 三倍する とは x * 3 です 数列 とは 1 と 2 と 3 と 4 です 数列 の それぞれ を 三倍する 数列 から (x を 受け取って x > 2 を 返すこと) で 選別する 数列 を 0 から 加算すること で 畳み込みする 数列 を (10 を 加算すること) で 写像する
(3 と 6 と 9 と 12) (3 と 4) 10 (11 と 12 と 13 と 14)
それぞれ means “each”: 数列 の それぞれ を 三倍する triples each element (map). 選別する is filter, 畳み込みする is fold, and 写像する is map with a function value.
07Conditions
もし … ならば … そうでなければ … is “if … then … otherwise ….” It is an expression, so both branches are required. が compares for equality.
A name ending in である (“is …”) defines a predicate, a function that returns true (真) or false (偽). Its subject takes が, you negate it with でない (“is not”), and であること makes it a value.
n が 偶数である とは n % 2 が 0 です # 偶数である = is even # 判定する = judge; 偶数 = even, 奇数 = odd n を 判定する とは もし n が 偶数である ならば 「偶数」 そうでなければ 「奇数」 です 7 を 判定する 4 が 偶数である 3 が 偶数でない (1 と 2 と 3 と 4) から 偶数であること で 選別する
奇数 真 真 (2 と 4)
Combine conditions with かつ (and) and または (or).
08Recursion and definitions by cases
Write a value or a pattern in place of a parameter name to define a function case by case. The first case that matches is used.
0 を 階乗する とは 1 です # 階乗する = take the factorial of n を 階乗する とは n * (n - 1 を 階乗する) です 10 を 階乗する
3628800
Wrap a call in parentheses when you use its result inside arithmetic, as in n * (n - 1 を 階乗する). Lists split into head and rest with 先頭 は x で 残り は 他 (“the head is x and the rest is 他”), and 空 matches the empty list.
09Your own data types
か (“or”) lists the shapes a type can take. もし 値 が followed by indented cases is pattern matching. Indentation decides the structure, as in Python.
図形 とは 半径 の 円 か 幅 と 高 の 長方形 です
形 を 面積にする とは # 面積にする = make it into its area (形 = shape)
もし 形 が
半径 の 円 ならば 3 * 半径 ^ 2 です
幅 と 高 の 長方形 ならば 幅 * 高 です
5 の 円 を 面積にする
幅 は 2 で 高 は 3 の 長方形 を 面積にする
75 6
Here a 図形 (shape) is either a 円 (circle) with a 半径 (radius) or a 長方形 (rectangle) with a 幅 (width) and 高 (height). Build values in order (5 の 円) or by field name (幅 は 2 で 高 は 3 の 長方形). If you leave a shape out of the match, the program is rejected before it runs. Records are written 点 とは 横 と 縦 の 組; to give the fields types, write each type in parentheses after its name, as in 品 とは 番号 (数) と 名前 (文字列) の 組 (the same as 番号 は 数 で 名前 は 文字列 の 組). Type classes are called 性質 (“traits”).
10Actions and procedures
Side effects such as 表示する (display) and 入力する (read a line) don’t happen when they are evaluated. They produce an action (入出力動作, “input/output action”), and actions at the top level are performed. To run several in order, list them one per line between 以下の手順です (“the procedure is as follows”) and 以上です (“that is all”). … した 結果 (“the result of having done …”) gives a name to an action’s result. The type declaration on the first line says “an action that returns nothing” (無).
挨拶する は 無 を 返す 入出力動作 # 挨拶する = greet 挨拶する とは 以下の手順です 名前 とは 「お名前は? 」 を 入力した 結果 # 名前 = name 「こんにちは、」 + 名前 + 「さん」 を 表示する 以上です 挨拶する
お名前は? 太郎 こんにちは、太郎さん
してから (“after doing”) passes an action’s result on to the next verb, as in 入力してから 表示する. This keeps pure code and effects apart, the same idea as monads in Haskell.
11Writing without spaces
Real Japanese has no spaces between words. Once a line contains 。 or 、, する splits words where the script changes (kanji, kana, numbers). 。 ends a sentence and 、 is just a pause for readability. You can also use the polite form: します means the same as する.
aとbを平均するとは、合計/2です。ただし、合計とは、a+bです。 # 平均する = average, 合計 = sum 3と5を平均します。 「できました」を表示します。
4 できました
ただし (“provided that”) adds a local definition to the definition just before it. Because hiragana is used for particles, a name that contains hiragana goes in 『』 brackets, for example 『おやつの数』.
12Types
Before running anything, する infers the type of every expression and stops if something doesn’t fit. Errors are in Japanese, with a line, a column and a marker.
x を 二乗する とは x * x です 「三」 を 二乗する
エラー: 2行目 7文字目: 「二乗する」の「を」には数が必要ですが、文字列が渡されています
「三」 を 二乗する
^The message says: “line 2, column 7: the を of 二乗する needs a number, but a string was given.” Types are optional to write, but you can declare one with は … を 返す (“… returns …”):
数 を 二乗する は 数 を 返す # takes a number (数), returns a number x を 二乗する とは x * x です 4 を 二乗する
16
Type names include 数 (number), 文字列 (string), 真偽 (boolean), 数 の リスト (list of numbers) and 文字列 を 返す 入出力動作 (an action that yields a string; 文字列 の 動作 is the short form). Run suru --検査 file.suru to check without running.
13Modules and the standard library
「数学」 を 読み込む (“load 数学”) loads 数学.suru from the same folder, and its names are used as 数学内の二乗する (“二乗する in 数学”). Write 「数学」 を 読み込んで 使う (“load and use”) instead to use the names without a prefix. The standard library loads the same way:
「標準」 を 読み込んで 使う 3 と 1 と 2 を 整列する # 整列する = sort 「a,b,c」 を 「,」 で 分割する # 分割する = split 1 から 5 を 範囲にする # 範囲にする = make a range
(1 と 2 と 3) (「a」 と 「b」 と 「c」) (1 と 2 と 3 と 4 と 5)
Other modules include ファイル (files), 時間 (time and calendar), JSON, スレッド (threads and channels) and プロセス (processes). A module can end with X と Y は 非公開とする。 to keep names private.
14Calling C functions
する can call any function with a C calling convention (FFI). 外部の「m」 を 読み込む (“load the external m”) links the C library libm; libc needs no loading. Each function is given its する name and the C name in 「」, with the types of its arguments and result.
外部の「m」 を 読み込む # 累乗する = raise to a power x を y で 累乗する とは 外部の「pow」 で 小数 を 小数 で 受け取って 小数 を 返します 以下は外部の関数です 小数 を 平方根にする は 「sqrt」 で 小数 を 返します # 平方根 = square root 小数 を 床にする は 「floor」 で 小数 を 返します # 床 = floor 以上です 以下は外部の入出力動作です 文字列 を 一行出力する は 「puts」 で 整数 を 返します # 一行出力する = output a line 以上です 2 を 10 で 累乗する 2.0 を 平方根にする 2.7 を 床にする 「こんにちは」 を 一行出力する
1024 1.4142135623730951 2 こんにちは
For one function, write it on one line: … とは 外部の「C name」 で, then the argument types with the same particles as the definition, 受け取って (“takes”), and the result type. For several, list them one per line between 以下は外部の関数です (“the following are external functions”) and 以上です. Use 以下は外部の入出力動作です for functions with side effects: they become actions, run only when performed, just like 表示する.
The C types are 数 (int64), 小数 (double), 整数 (int), 単精度 (float), 真偽, 文字列 (char*), 番地 (a pointer), バイト列 (bytes) and 無 (void). To pass arguments to C in another order or add constants, name them: r を 読取する とは 「f」(r と 0) で 番地 を 受け取って 番地 を 返します. A C struct is declared like a record that ends in C構造体, as in 時間 とは 秒 (符号付き64) と ナノ秒 (符号なし32) の C構造体, and external functions can take and return it by value. Libraries such as SQLite, PCRE2, OpenSSL and SDL3 are wrapped this way in pure する; see the suru-lang repositories on GitHub.
15Native binaries
The same programs compile to a single native executable through LLVM. You need clang on your machine. suru --翻訳 uses the reference compiler; suruc, written in Go, does the same job much faster.
$ go install github.com/suru-lang/suru/cmd/suruc@latest $ suruc factorial.suru $ ./factorial
3628800
For a whole project, use kumu (組む, “to assemble”). Its project file 組.suru is itself a する program.
$ go install github.com/suru-lang/suru/cmd/kumu@latest $ kumu 新規 挨拶 # new project “挨拶” (kumu new also works) $ cd 挨拶 $ kumu 実行 # build and run (kumu run)
「挨拶」0.1.0 を組みました(出力/挨拶) こんにちは
16Testing
Tests are written in する itself, next to the code. 例えば … は … です (“for example, … is …”) under a definition is an example of it, and 以下は「name」の試験です … 以上です is a test block. Both are type-checked with the file, skipped when it runs as a program, and run by suru --試験.
数 を 二倍する は 数 を 返す x を 二倍する とは x * 2 です # 二倍する = double 例えば (2 を 二倍する) は 4 です # 例えば = for example 以下は「二倍」の試験です # 試験 = test (-3 を 二倍する) を -6 に 一致確認する # 一致確認する = check that it equals 以上です
$ suru --試験 double.suru
✓ double.suru 二倍するの例(3行) ✓ double.suru 二倍 合格 2・不合格 0
合格 means passed and 不合格 failed. In a kumu project, kumu 試験 runs these and the tests in the 試験/ folder. The testing page covers all of it, including CI.
17Common mistakes
These are the mistakes people make most often when they start. The checker points at each of them with a message; suru --点検 ファイル.suru (the linter) also finds names you never use and lines that are hard to read. For a page of everything that is right, see the cheat sheet.
| Instead of | Write | Why |
|---|---|---|
おやつ とは 3 | 『おやつ』 とは 3 | Names with hiragana go in 『』, or use kanji or katakana. Hiragana is kept for particles. |
1、2、3 | 1 と 2 と 3 | Lists are joined with と. 「、」 and 「・」 mean something else. |
x を 流す とは x | x を 流出する とは x | A function name ends in する. |
a+b | a + b | In the spaced style, a+b with no spaces reads as one name. |
3 を 二乗する + 1 | (3 を 二乗する) + 1 | Put a call in () before using it in arithmetic. |
「値: 」 + n | 「値: 」 + (n を 文字列化する) | + joins two strings, not a string and a number. |
1 と 「a」 | 1 と 2 | A list holds one type. Use a record or your own type for mixed values. |
「数学」 を 読み込む … 二乗する | 数学内の二乗する | Names from a loaded file take its name as a prefix, or load it with 読み込んで 使う. |
以下の手順です …(以上なし) | … 以上です | Close a block of steps with a line 以上です. |
xs の それぞれ を 表示する | (xs の それぞれ を 表示する) を 実行する | Inside steps, the first only makes a list of actions. 実行する runs them. |
xs から 0 を 番目取得する | xs から 1 を 番目取得する | Positions count from 1. |
18Where to go next
You have seen the core of the language. The repository has much more: threads and channels, calling C and Go, JSON, the Japanese calendar, and the full list of design decisions.