PostGödel’s β Function [0001]

Today in the Introduction to Recursion Theory course I was told about Gödel’s β Function, which makes it possible to encode a finite sequence into a single natural number.

The Definition

Before the function it self, there are some auxiliary functions that need to be defined.

First we come up with the quotient and remainder functions. For all 𝑚,𝑛𝜔, let quo(𝑚,𝑛) and rem(𝑚,𝑛) be the quotient and remainder of 𝑚 divided by 𝑛, respectively, if 𝑛0, and 0 otherwise.

Then follows the (floored) square root and exceedence functions. For all 𝑛𝜔, let [𝑛] be the largest number 𝑘 that 𝑘2𝑛, and K(𝑛)=𝑛[𝑛]2.

Now comes the J and L functions, named after what Gödel named them. Let J(𝑚,𝑛)=((𝑚+𝑛)2+𝑛)2+𝑚, and L(𝑛)=K([𝑛]). The definitions may seems weird at first glance, but they would soon show their power. And they have great properties: for all 𝑚,𝑛𝜔, K(J(𝑚,𝑛))=𝑚 and L(J(𝑚,𝑛))=𝑛. The proof goes as follows.

For that property of K and J, it suffices to show that [J(𝑚,𝑛)]=(𝑚+𝑛)2+𝑛, and this can be brought out by the fact that

((𝑚+𝑛)2+𝑛+1)2=((𝑚+𝑛)2+𝑛)2+2((𝑚+𝑛)2+𝑛+1)+12>((𝑚+𝑛)2+𝑛)2+𝑚.

That property of L and J is also easy following similar approach. These two properties means that the arguments of J can be extracted from the result of it, and that means J is an 1-1 function.

Finally, the Gödel’s β function is defined by 𝛽(𝑚,𝑖)=rem(K(𝑚),1+(𝑖+1)·L(𝑚)). (Note that this is not the original definition (3-ary) but a simplified 2-ary one.) The definition is also a bit of complicated, but the following lemma tells us about why it is amazing.

Gödel’s β Function Lemma

For all 𝑚0,,𝑚𝑛1𝜔, there exists a 𝑚𝜔 such that

𝛽(𝑚,𝑖)=𝑚𝑖

for all 𝑖<𝑛.

And the the proof entails an algorithm to calculate such an 𝑚.

Before the proof, we need to prove two preliminary propositions.

Bézout’s Lemma

(Note that this lemma is a weaken version of the original theorem.)

If 𝑚,𝑛𝜔 and they are coprime, then 𝑎𝑚+𝑏𝑛=1 for some integers 𝑎 and 𝑏.

Proof: Let 𝑆={𝑎𝑚+𝑏𝑛𝑎𝑚+𝑏𝑛𝜔}, i.e. the set of all positive (integral) linear composition of 𝑚 and 𝑛. Thus there must be a minimum value 𝑝𝑆 for this non-empty set (the non-emptiness is trivial). What this proof wants to show is that 𝑝=1. We prove this by contradiction. Assume that 𝑝1. Take 𝑞𝑆, let 𝑟=rem(𝑞,𝑝), we have 0𝑟<𝑝. And rem(𝑞,𝑝)𝑆 since 𝑝 and 𝑞 are both (integral) linear composition of 𝑚 and 𝑛, so rem(𝑞,𝑝) must be 0, otherwise it contradicts with the minimality of 𝑝. Then we have 𝑝𝑞 for all 𝑞𝑆. Note that 𝑚𝑆 and 𝑛𝑆, so 𝑝 is a common divisor of 𝑚 and 𝑛, which leads to the contradiction toward the coprimality of 𝑚 and 𝑛 if 𝑝1.

Chinese Remainder Theorem

For all 𝑘0,,𝑘𝑛1𝜔, if 𝑑0,,𝑑𝑛1𝜔 are pairwise coprime and 𝑘𝑖<𝑑𝑖 for all 𝑖<𝑛, then there exists a 𝑘𝜔 such that rem(𝑘,𝑑𝑖)=𝑘𝑖 for all 𝑖<𝑛.

Proof: For each 𝑖<𝑛, let 𝑚𝑖=𝑑0𝑑𝑛1𝑑𝑖. Since 𝑑0,,𝑑𝑛1 are pairwise coprime, 𝑚𝑖 and 𝑑𝑖 are coprime. By Bézout’s Lemma, there are integers 𝑎𝑖 and 𝑏𝑖 such that 𝑎𝑖𝑚𝑖+𝑏𝑖𝑑𝑖=1. The insight here is that for each 𝑑𝑖, we construct the number that its remainder divided by 𝑑𝑖 is 𝑘𝑖, and compose these constructed numbers in a way that they won’t affect each other. Note that 𝑚𝑖 cannot be divided by 𝑑𝑖, can be divided by 𝑑𝑗 for any 𝑗<𝑛 where 𝑗𝑖, and there is an 1 on the right hand side of the equation we got from the Bézout’s Lemma. It is easy to see that, now it suffices to take

𝑘=𝑖<𝑛𝑘𝑖𝑎𝑖𝑚𝑖+𝑙𝑑0𝑑𝑛1,

where 𝑙 is any natural number such that 𝑘0.

The Proof of Gödel’s β Function Lemma

Finally we come to the proof of the lemma. From the definition of J and 𝛽, we can see that we only need to find a proper value of L(𝑚) such that the numbers 1+(𝑖+1)·L(𝑚) for each 𝑖<𝑛 coprime, then construct the value of K(𝑚) by Chinese Remainder Theorem, and calculate the 𝑚=J(K(𝑚),L(𝑚)). For convenience, denote 1+(𝑖+1)·L(𝑚) by 𝑑𝑖 for each 𝑖<𝑛.

What conditions should the value of L(𝑚) satisfy? First, it must make 𝑑𝑖=1+(𝑖+1)·L(𝑚)>𝑚𝑖, which is one of the conditions asked by Chinese Remainder Theorem. Second, it makes those 𝑑𝑖’s pairwise coprime. To prove propositions like ‘there is no prime 𝑝 such that 𝑝𝑑𝑖 and 𝑝𝑑𝑗 for different 𝑖,𝑗<𝑛’, consider proof by contradiction. Think about what if there is some prime 𝑝 such that 𝑝𝑑𝑖 and 𝑝𝑑𝑗 for different 𝑖,𝑗<𝑛, i.e. 𝑝1+(𝑖+1)·L(𝑚) and 𝑝1+(𝑗+1)·L(𝑚). Assume 𝑖<𝑗 without loss of generality, we have 𝑝(𝑗𝑖)·L(𝑚). It will be good if we have 𝑝L(𝑚), which would lead to the contradiction that 𝑝1. So things become evident: find a construction of L(𝑚) such that (𝑗𝑖)L(𝑚). This can be achived by making L(𝑚) divisible by the product of all the possible values of 𝑗𝑖, which ranges from 1 to 𝑛1. This is to say, making (𝑛1)!L(𝑚) would be a good idea. With the first condition that L(𝑚) should satisfy, it suffices to take L(𝑚)=𝑙!, where 𝑙=max{𝑛,𝑚0,,𝑚𝑛1}. I take 𝑛 instead of 𝑛1 there for better elegance with the same effect.

The above is not a strictly stated proof, however it is easy to rewrite it into one.

Mumbles (Again)

The reason I write this post is not that the ability of Gödel’s β Function to encode a finite sequence into a number is amazing (there are many other techniques to achive this, e.g. encode the sequence into the exponents of an initial segment of primes), nor that the proof is elegant (though it is). It is just because I recalled the confusion that those two preliminary propositions caused to me, and realized that they seems easy for me now, and can be used to prove such lemmas. This feels strange, so I decided to write it down.

Contexts

Blog [0005]

Post什么是“缠绕沈子纲之铁链” [000W]

如果在一些在线英汉汉英词典(如欧路词典)上查询 dangles(注意并非 dangle)这个词,会得到一个奇怪的义项:“n. 缠绕沈子纲之铁链”。这个义项令人十分迷惑:谁是沈子纲(这听上去很像一个人名),或者说,什么是沈子纲?为什么要用铁链缠绕沈子纲?为什么 dangles 会被联系到这个义项?

这激起了我的好奇心。此时,如果是熟悉渔业专业英语的人,已经能大致知道背后的来龙去脉甚至可能并不觉得奇怪。遗憾的是,我并不熟悉渔业专业英语。

于是我在网络上查询“缠绕沈子纲之铁链”。搜索结果几乎全部是无关结果,主要来自“缠绕”和“铁链”这两个关键词,然而结果中也有其他若干英汉汉英词典或翻译网站,无一例外在它们的“网络释义”中将 dangles 这个词解释为“缠绕沈子纲之铁链”。幸运的是,必应词典提供了这个“网络释义”义项的来源:“食品论坛” BBS 的“水产”板块中,有用户提问“谁能提供关于水产的英语词汇”,而回复中有一位神秘人贴出了大量独家汉英词汇对照。在页面上搜索 dangles,果然该神秘人将其解释为“缠绕沈子纲之铁链”。到了这一步,这个义项的来源已经解明。剩下的问题是:什么是“沈子纲”?什么又是“缠绕沈子纲之铁链”?为什么要用铁链缠绕沈子纲?

此时,如果是更聪明的人,已经能快速知道“沈子纲”对应的是什么。遗憾的是,我并没有这么聪明。

于是我在网络上查询“沈子纲”。可以看出这个词汇的确很像个人名,因为搜索结果几乎都与各种人物有关。不过,其中出现了一个英文词汇 footrope 的百度百科页面,其解释为:“footrope,英语单词,主要用作为名词,用作名词译为‘沈子纲’。”

此时,如果是熟悉渔业专业英语的人,已经能几乎完全明白背后的来龙去脉。遗憾的是,我并不熟悉渔业专业英语。

我恍然大悟,回到食品论坛的那个页面搜索“沈子纲”,果然该神秘人同样列出了这对词汇。将两个英文词语在渔业领域的意思交叉对比,可以得知 footrope 指的是渔网底部带有配重以使得网具下沉并张开成特定形状的绳索,而 dangles,如神秘人所言,是缠绕 footrope 之铁链,起到配重与惊起海中的底栖生物以便于打捞的双重作用。

问题是,这个东西为什么会被神秘人称为“沈子纲”呢?如果这是通用的译名或译法,为什么会查询不到任何结果?

此时,如果是对汉字足够敏感的人,已经能从上下文中猜测到正确答案。遗憾的是,我对汉字同样不够敏感。

于是我继续查看“沈子纲”的搜索结果。令人意外的是,这个日文网页提到了“沈子綱”在日文中的存在。到了这一步,在日文汉字的提示下,我终于还是对汉字产生了一定的敏感:这个“沈子纲”,在中国大陆的语境下,是不是应该被写成“沉子纲”呢

于是我搜索“沉子纲”,得到大量与渔业有关的搜索结果,简单阅读之后确认和之前的 footrope 这个英文词汇指的是同一个东西。结合这一点搜索,可以得知 dangles 所对应的东西并没有一个通用的专名或译法,但或许可称为“垂链”“吊链”“悬垂索具”等等,或者,如一开始的神秘义项所云,是“缠绕沉子纲之铁链”。只是不知为何,食品论坛的神秘人可能受到日语影响,将“沉子纲”写成了“沈子纲”。

PostMagit in Terminal [0004]

The following is a script I use to open Magit in current directory from terminal:

#!/usr/bin/env bash
git_root=$(git rev-parse --show-toplevel)
emacsclient -c -n -a emacs -e "(progn (magit-status \"${git_root}\") (delete-other-windows))"
if [[ -f `which osascript` ]]; then
  osascript -e "tell application \"Emacs\" to activate"
fi

I put this script in my PATH as magit, so I can just run magit in any git repository from terminal to open Magit in Emacs. The original source for this script comes from Christian Tietze’s post, and I have modified it to make it create a new frame with only one window showing Magit status. I also created another script with -nw in place of -c -n and removed the part that activates Emacs to open Magit in TTY Emacs directly.

PostFinding Room for Keybindings in Emacs [0003]

There are so many commands that I want to bind to keys in Emacs, but the keyboard is crowded already. In the Key Binding Conventions section in the GNU Emacs Lisp Reference Manual, it says that C-c followed by a letter as well as F5 through F9 are reserved for users. However, I, just like any sane Emacs user would do, tries to put groups of related commands under prefixes, which means that such a commmand needs at least three keystrokes to invoke (the C-c, the letter prefix, and the keybinding in the prefix). This is anti-productive.

Luckily, there are some keybindings in Emacs that are not that useful for me (and maybe others as well), so I can repurpose them for my own use. The following are some of such keybindings. Note that many of these are useless for me because they are more relevant to terminal Emacs, while I almost always use Emacs in a graphical environment, so YMMV.

  • C-z: By default, this is bound to suspend-frame, which could be replaced by the window manager’s own shortcut for minimizing windows on most systems. It may still be useful on terminal Emacs though.
  • C-t: This is bound to transpose-chars, which turns a|bc into ba|c (where | is the cursor). Does anyone really use this? I never do. (But I do find other transpose commands useful.)
  • M-`: This is bound to tmm-menubar, which shows a text-based menu bar, which is not very useful in graphical Emacs.
  • C-i and C-m: Due to historical reasons, these are, by default, recognized as TAB and RET respectively. This is still relevant in terminal Emacs, since some terminals may not be able to distinguish them. But in graphical Emacs, it is possible to distinguish these control characters from TAB and RET, see these relevant discussions on Emacs Stack Exchange.

That gives me at least five more prefixes to use for my own keybindings, which saves me a lot of keystrokes, and I hope it helps you as well. Happy hacking!

PostHow to Find a Color Scheme Quickly [0002]

There are always things in the world that require some level of visual design. Taking some things I’ve had to do recently as examples: creating slideshows, drawing charts for papers, making posters, and designing my homepage – having some design is always better than having none. In the design process, choosing fonts is relatively simple. After doing this a few times, you slowly build up a set of fonts that can be combined for various occasions. While layout has many details, as long as you follow some basic principles like alignment, white space, contrast, and unity, the result won’t be poor. However, choosing a color scheme is often the hardest part for me.

Criteria for Choosing a Color Scheme

A “color scheme” isn’t just a set of colors found randomly via a search engine or produced by a generator. Most such schemes are just a collection of color blocks and RGB values. They might look okay at first glance, but for a non-professional like me, it’s hard to directly apply them to actual designs. This is just a mere collection of colors, not a truly usable color scheme. For me, a color scheme should meet these criteria:

  • Clearly defines the purpose of each color, such as background color, text color, accent color, etc.
  • Based on the defined color purposes, meets certain accessibility standards, including:

    • Sufficient contrast between text and background.
    • Colorblind-friendly.

Where to Find Color Schemes

After several fruitless attempts, I decided not to waste time on search engines anymore. My inspiration came one day when I was staring at my Emacs. At that moment, I suddenly realized that code editor color schemes are arguably a perfect solution because they:

  • Have very clear color purposes.
  • Usually don’t have too many primary colors.
  • Every code editor has a huge number of color schemes to choose from.
  • These schemes are usually carefully designed rather than randomly generated.
  • Many schemes meet certain accessibility requirements.
  • You can experience the effect of these schemes right in the code editor.
  • Provide a sense of visual unity with one’s daily workflow.

The only concern is the copyright issue. Although colors themselves are not protected by copyright (at least as far as I know), to avoid unnecessary trouble, one should try to use schemes that are explicitly licensed for use.

How to Evaluate

Palette Tester is a tool for testing the contrast of a color scheme. It can test contrast under WCAG 2.1 (AA) or APCA Contrast standards. Coloring for Colorblindness and Who Can Use are two tools for testing the colorblind-friendliness of colors. They can show how the same set of colors appears to people with different types of color blindness.

My Choice

I have always used the Modus Themes color scheme in Emacs. Coincidentally, this color scheme was designed with accessibility in mind, achieving the WCAG AAA standard, and provides variants friendly to two types of color blindness (deuteranopia and tritanopia). The color scheme of the webpage you are currently viewing comes from the tinted variant of Modus Themes.

PostGödel’s β Function [0001]

Today in the Introduction to Recursion Theory course I was told about Gödel’s β Function, which makes it possible to encode a finite sequence into a single natural number.

The Definition

Before the function it self, there are some auxiliary functions that need to be defined.

First we come up with the quotient and remainder functions. For all 𝑚,𝑛𝜔, let quo(𝑚,𝑛) and rem(𝑚,𝑛) be the quotient and remainder of 𝑚 divided by 𝑛, respectively, if 𝑛0, and 0 otherwise.

Then follows the (floored) square root and exceedence functions. For all 𝑛𝜔, let [𝑛] be the largest number 𝑘 that 𝑘2𝑛, and K(𝑛)=𝑛[𝑛]2.

Now comes the J and L functions, named after what Gödel named them. Let J(𝑚,𝑛)=((𝑚+𝑛)2+𝑛)2+𝑚, and L(𝑛)=K([𝑛]). The definitions may seems weird at first glance, but they would soon show their power. And they have great properties: for all 𝑚,𝑛𝜔, K(J(𝑚,𝑛))=𝑚 and L(J(𝑚,𝑛))=𝑛. The proof goes as follows.

For that property of K and J, it suffices to show that [J(𝑚,𝑛)]=(𝑚+𝑛)2+𝑛, and this can be brought out by the fact that

((𝑚+𝑛)2+𝑛+1)2=((𝑚+𝑛)2+𝑛)2+2((𝑚+𝑛)2+𝑛+1)+12>((𝑚+𝑛)2+𝑛)2+𝑚.

That property of L and J is also easy following similar approach. These two properties means that the arguments of J can be extracted from the result of it, and that means J is an 1-1 function.

Finally, the Gödel’s β function is defined by 𝛽(𝑚,𝑖)=rem(K(𝑚),1+(𝑖+1)·L(𝑚)). (Note that this is not the original definition (3-ary) but a simplified 2-ary one.) The definition is also a bit of complicated, but the following lemma tells us about why it is amazing.

Gödel’s β Function Lemma

For all 𝑚0,,𝑚𝑛1𝜔, there exists a 𝑚𝜔 such that

𝛽(𝑚,𝑖)=𝑚𝑖

for all 𝑖<𝑛.

And the the proof entails an algorithm to calculate such an 𝑚.

Before the proof, we need to prove two preliminary propositions.

Bézout’s Lemma

(Note that this lemma is a weaken version of the original theorem.)

If 𝑚,𝑛𝜔 and they are coprime, then 𝑎𝑚+𝑏𝑛=1 for some integers 𝑎 and 𝑏.

Proof: Let 𝑆={𝑎𝑚+𝑏𝑛𝑎𝑚+𝑏𝑛𝜔}, i.e. the set of all positive (integral) linear composition of 𝑚 and 𝑛. Thus there must be a minimum value 𝑝𝑆 for this non-empty set (the non-emptiness is trivial). What this proof wants to show is that 𝑝=1. We prove this by contradiction. Assume that 𝑝1. Take 𝑞𝑆, let 𝑟=rem(𝑞,𝑝), we have 0𝑟<𝑝. And rem(𝑞,𝑝)𝑆 since 𝑝 and 𝑞 are both (integral) linear composition of 𝑚 and 𝑛, so rem(𝑞,𝑝) must be 0, otherwise it contradicts with the minimality of 𝑝. Then we have 𝑝𝑞 for all 𝑞𝑆. Note that 𝑚𝑆 and 𝑛𝑆, so 𝑝 is a common divisor of 𝑚 and 𝑛, which leads to the contradiction toward the coprimality of 𝑚 and 𝑛 if 𝑝1.

Chinese Remainder Theorem

For all 𝑘0,,𝑘𝑛1𝜔, if 𝑑0,,𝑑𝑛1𝜔 are pairwise coprime and 𝑘𝑖<𝑑𝑖 for all 𝑖<𝑛, then there exists a 𝑘𝜔 such that rem(𝑘,𝑑𝑖)=𝑘𝑖 for all 𝑖<𝑛.

Proof: For each 𝑖<𝑛, let 𝑚𝑖=𝑑0𝑑𝑛1𝑑𝑖. Since 𝑑0,,𝑑𝑛1 are pairwise coprime, 𝑚𝑖 and 𝑑𝑖 are coprime. By Bézout’s Lemma, there are integers 𝑎𝑖 and 𝑏𝑖 such that 𝑎𝑖𝑚𝑖+𝑏𝑖𝑑𝑖=1. The insight here is that for each 𝑑𝑖, we construct the number that its remainder divided by 𝑑𝑖 is 𝑘𝑖, and compose these constructed numbers in a way that they won’t affect each other. Note that 𝑚𝑖 cannot be divided by 𝑑𝑖, can be divided by 𝑑𝑗 for any 𝑗<𝑛 where 𝑗𝑖, and there is an 1 on the right hand side of the equation we got from the Bézout’s Lemma. It is easy to see that, now it suffices to take

𝑘=𝑖<𝑛𝑘𝑖𝑎𝑖𝑚𝑖+𝑙𝑑0𝑑𝑛1,

where 𝑙 is any natural number such that 𝑘0.

The Proof of Gödel’s β Function Lemma

Finally we come to the proof of the lemma. From the definition of J and 𝛽, we can see that we only need to find a proper value of L(𝑚) such that the numbers 1+(𝑖+1)·L(𝑚) for each 𝑖<𝑛 coprime, then construct the value of K(𝑚) by Chinese Remainder Theorem, and calculate the 𝑚=J(K(𝑚),L(𝑚)). For convenience, denote 1+(𝑖+1)·L(𝑚) by 𝑑𝑖 for each 𝑖<𝑛.

What conditions should the value of L(𝑚) satisfy? First, it must make 𝑑𝑖=1+(𝑖+1)·L(𝑚)>𝑚𝑖, which is one of the conditions asked by Chinese Remainder Theorem. Second, it makes those 𝑑𝑖’s pairwise coprime. To prove propositions like ‘there is no prime 𝑝 such that 𝑝𝑑𝑖 and 𝑝𝑑𝑗 for different 𝑖,𝑗<𝑛’, consider proof by contradiction. Think about what if there is some prime 𝑝 such that 𝑝𝑑𝑖 and 𝑝𝑑𝑗 for different 𝑖,𝑗<𝑛, i.e. 𝑝1+(𝑖+1)·L(𝑚) and 𝑝1+(𝑗+1)·L(𝑚). Assume 𝑖<𝑗 without loss of generality, we have 𝑝(𝑗𝑖)·L(𝑚). It will be good if we have 𝑝L(𝑚), which would lead to the contradiction that 𝑝1. So things become evident: find a construction of L(𝑚) such that (𝑗𝑖)L(𝑚). This can be achived by making L(𝑚) divisible by the product of all the possible values of 𝑗𝑖, which ranges from 1 to 𝑛1. This is to say, making (𝑛1)!L(𝑚) would be a good idea. With the first condition that L(𝑚) should satisfy, it suffices to take L(𝑚)=𝑙!, where 𝑙=max{𝑛,𝑚0,,𝑚𝑛1}. I take 𝑛 instead of 𝑛1 there for better elegance with the same effect.

The above is not a strictly stated proof, however it is easy to rewrite it into one.

Mumbles (Again)

The reason I write this post is not that the ability of Gödel’s β Function to encode a finite sequence into a number is amazing (there are many other techniques to achive this, e.g. encode the sequence into the exponents of an initial segment of primes), nor that the proof is elegant (though it is). It is just because I recalled the confusion that those two preliminary propositions caused to me, and realized that they seems easy for me now, and can be used to prove such lemmas. This feels strange, so I decided to write it down.