Skip to content

Commit

Permalink
🥔
Browse files Browse the repository at this point in the history
  • Loading branch information
AlephAlpha committed Aug 2, 2024
1 parent e70d62a commit b39e912
Show file tree
Hide file tree
Showing 11 changed files with 114 additions and 13 deletions.
8 changes: 5 additions & 3 deletions analysis/Ideas.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
此处总结一下 Code Page 中已有但还没有用上的字符:

```
.W`w
¤.W`w
```

`¤` 原本是用于 `\normalForm` 函数。但这个函数由于定义不清晰且在现有的答案中没有用到,因此被移除了。这个字符空了出来,可以用于其它的内置函数。

如果 CodePage 中的 256 个字符都用完了,可以考虑用像 05AB1E 那样,用 `.` 开头的字符来表示双字节的内置函数。现在的 CodePage 还没有用完,先不考虑这个问题,但 `.` 也先不要用掉。

LiberationMono 字体所支持的字符列举于[此文件](analysis/LiberationMonoGlyphs.txt),以供选取。
Expand Down Expand Up @@ -185,7 +187,7 @@ LiberationMono 字体所支持的字符列举于[此文件](analysis/LiberationM

自动开启 block 是很大的 breaking change,会影响到大量的解答。最好能有一个改动没有这么大的方案。

可以考虑如果一个助词出现在程序的结尾,其修饰的就是除它之外的整个程序。比如说 `ᶦ{Ƃ2ŗɔƃ3M` 可以写成 `Ƃ2ŗɔƃ3Mᶦ`。这样解决不了所有的问题,但可以解决一些问题。不过这会降低可读性,而且 parser 也会变得复杂
可以考虑如果一个助词出现在程序的结尾,其修饰的就是除它之外的整个程序。比如说 `ᶦ{Ƃ2ŗɔƃ3M` 可以写成 `Ƃ2ŗɔƃ3Mᶦ`。这样解决不了所有的问题,但可以解决一些问题。不过这会降低可读性(虽然 golfing language 谈可读性本就有点搞笑),而且 parser 也不好写

另一个问题是很多助词的 arity 限制太严格了,比如说 `\map` 只能用于 arity 为 `m -> 1` 的函数。这限制了函数的使用方式。下面的关于 arity 的部分作了一些讨论。不过,也许有另一种改动没那么大的方案。

Expand Down Expand Up @@ -222,6 +224,6 @@ LiberationMono 字体所支持的字符列举于[此文件](analysis/LiberationM

目前的文档还很不完善。需要增加更多的例子,以及更详细的说明。

- [ ] 为每个函数增加例子。可以考虑把这些例子用作单元测试。现有的函数太多,不可能一下子全写完,可以慢慢来
- [ ] 为每个函数和助词增加例子,并把这些例子用作单元测试。目前函数的部分已完成,助词的部分还没有
- [ ] 重写现有的 Tutorial。考虑分成多个文件,每个文件只讲一个主题。
- [ ] REPL 的 `\Info` 命令,可以考虑改成能一次输出多个函数的信息。
1 change: 1 addition & 0 deletions analysis/corpus.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ RtƊjo1cɗ
→rᵉÇË*
2ᵚR1cʳ×
1ᶦ{ƊY$Ĭɗ
¬õ@
ˡ∑
ˡ½
Ňŧ
Expand Down
6 changes: 3 additions & 3 deletions analysis/freq_1gram.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ E : 10
ž : 10
ŋ : 10
? : 10
@ : 10
% : 10
ᶠ : 10
∏ : 10
Expand All @@ -81,19 +82,18 @@ C : 10
z : 10
ᵈ : 10
ᶜ : 9
¬ : 9
≥ : 9
‼ : 9
ŗ : 9
ᵗ : 9
≈ : 9
i : 9
@ : 9
P : 8
f : 8
ᵚ : 8
× : 8
ů : 8
¬ : 8
∕ : 8
£ : 8
ƒ : 8
Expand Down Expand Up @@ -138,6 +138,7 @@ d : 5
Ç : 5
ř : 5
ʳ : 5
õ : 5
G : 5
Å : 5
s : 5
Expand Down Expand Up @@ -167,7 +168,6 @@ D : 4
ň : 4
Ṗ : 4
U : 4
õ : 4
Ṃ : 3
Y : 3
T : 3
Expand Down
2 changes: 2 additions & 0 deletions analysis/freq_2gram.txt
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,8 @@ cʳ : 1
{Ɗ : 1
ƊY : 1
Ĭɗ : 1
¬õ : 1
õ@ : 1
ˡ∑ : 1
ˡ½ : 1
$Z : 1
Expand Down
1 change: 1 addition & 0 deletions analysis/freq_3gram.txt
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ cʳ× : 1
ƊY$ : 1
Y$Ĭ : 1
$Ĭɗ : 1
¬õ@ : 1
ʷ{$ : 1
{$Z : 1
$Zᵉ : 1
Expand Down
2 changes: 2 additions & 0 deletions app/Doc.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ docBuiltins =
++ "Nekomata is still in an early stage. The full names, short names, "
++ "and meanings of built-in functions and particles are subject "
++ "to change.\n\n"
++ "- [Functions](#functions)\n"
++ "- [Particles](#particles)\n\n"
++ "## Functions\n\n"
++ concat [infoMarkdown b | b <- builtins]
++ "## Particles\n\n"
Expand Down
23 changes: 23 additions & 0 deletions doc/Builtins.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

Nekomata is still in an early stage. The full names, short names, and meanings of built-in functions and particles are subject to change.

- [Functions](#functions)
- [Particles](#particles)

## Functions

### `choice` (`?`, `2 -> 1`)
Expand Down Expand Up @@ -2314,22 +2317,42 @@ Apply a function to the top two values of the stack.

If the function takes no argument, simply apply it twice.

__Examples__:

- `1 2 ᵃ{1+} Ð``[2,3]`

### `noPop` (`ˣ`, `(m -> n) -> (m -> m + n)`)

Apply a function without popping the stack.

__Examples__:

- `1 ˣ{1+} Ð``[1,2]`

### `dip` (``, `(m -> n) -> (m + 1 -> n + 1)`)

Pop the top value of the stack, apply a function to the rest, and push the popped value back.

__Examples__:

- `1 2 ᵈ{1+} Ð``[2,2]`

### `dupDip` (``, `(m -> n) -> (m -> n + 1)`)

Apply a function to the stack, and then push the original top value back.

__Examples__:

- `1 ᵈ{1+} Ð``[2,1]`

### `dupDip2` (``, `(m -> n) -> (m -> n + 2)`)

Apply a function to the stack, and then push the original top two values back.

__Examples__:

- `1 2 ᵋ{+} ÐÐ``[3,[1,2]]`

### `map` (``, `(0 -> 1) -> (1 -> 1) or (m -> 1) -> (m -> 1) where m > 0`)

Apply a function to each value in a list.
Expand Down
6 changes: 0 additions & 6 deletions src/Nekomata/Builtin.hs
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,6 @@ infoByName name' =
'\\' : name'' -> Map.lookup name'' builtinMap
_ -> Map.lookup name' builtinMap

all_ :: [String] -> Result
all_ = All False

truncate_ :: [String] -> Result
truncate_ = All True

-- | The list of all builtin functions
builtins :: [Builtin]
builtins =
Expand Down
50 changes: 50 additions & 0 deletions src/Nekomata/Particle.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import Nekomata.Data
import Nekomata.Function hiding (arity)
import qualified Nekomata.Function as Function
import Nekomata.NonDet
import Nekomata.Result

{- | A particle is a higher-order function that modifies a function
Expand All @@ -39,6 +40,8 @@ data BuiltinParticle = BuiltinParticle
-- ^ The arity of the particle
, help :: String
-- ^ The help message for the particle
, examples :: [(String, Result)]
-- ^ Some examples for the particle
}

instance Show BuiltinParticle where
Expand All @@ -54,6 +57,19 @@ info b =
++ arity b
++ "):\n"
++ help b
++ if null (examples b)
then ""
else
"\nExamples:\n"
++ unlines
[ " "
++ example
++ " -> "
++ if result == all_ []
then "Fail"
else show result
| (example, result) <- examples b
]

-- | Get the info string for a builtin particle in Markdown format
infoMarkdown :: BuiltinParticle -> String
Expand All @@ -66,6 +82,20 @@ infoMarkdown b =
++ arity b
++ "`)\n\n"
++ concatMap (++ "\n\n") (lines (help b))
++ if null (examples b)
then ""
else
"__Examples__:\n\n"
++ unlines
[ "- `"
++ example
++ "` → "
++ if result == all_ []
then "Fail"
else "`" ++ show result ++ "`"
| (example, result) <- examples b
]
++ "\n"

-- | Get the info string for a builtin particle by name
infoByName :: String -> Maybe String
Expand All @@ -86,33 +116,38 @@ builtinParticles =
\or (m -> n) -> (m + 1 -> 2 * n) where m > 0"
"Apply a function to the top two values of the stack.\n\
\If the function takes no argument, simply apply it twice."
[("1 2 ᵃ{1+} Ð", all_ ["[2,3]"])]
, BuiltinParticle
"noPop"
'ˣ'
noPop
"(m -> n) -> (m -> m + n)"
"Apply a function without popping the stack."
[("1 ˣ{1+} Ð", all_ ["[1,2]"])]
, BuiltinParticle
"dip"
'ᵈ'
dip
"(m -> n) -> (m + 1 -> n + 1)"
"Pop the top value of the stack, apply a function to the rest, \
\and push the popped value back."
[("1 2 ᵈ{1+} Ð", all_ ["[2,2]"])]
, BuiltinParticle
"dupDip"
'ᵉ'
dupDip
"(m -> n) -> (m -> n + 1)"
"Apply a function to the stack, \
\and then push the original top value back."
[("1 ᵈ{1+} Ð", all_ ["[2,1]"])]
, BuiltinParticle
"dupDip2"
'ᵋ'
dupDip2
"(m -> n) -> (m -> n + 2)"
"Apply a function to the stack, \
\and then push the original top two values back."
[("1 2 ᵋ{+} ÐÐ", all_ ["[3,[1,2]]"])]
, BuiltinParticle
"map"
'ᵐ'
Expand All @@ -124,6 +159,7 @@ builtinParticles =
\from 0 to the input minus 1.\n\
\If the function takes no argument, return a list of n copies \
\of the result of the function, where n is the length of the input."
[]
, BuiltinParticle
"mapWith"
'ᵚ'
Expand All @@ -134,6 +170,7 @@ builtinParticles =
\If the function is unary, return a list of n copies of the \
\result of applying the function to the second argument, where \
\n is the length of the first argument."
[]
, BuiltinParticle
"zipWith"
'ᶻ'
Expand All @@ -143,6 +180,7 @@ builtinParticles =
\Fail if the lists have different lengths.\n\
\If one of the input is an number, apply the function to each \
\integer from 0 to the input minus 1."
[]
, BuiltinParticle
"zipWithTrunc"
'ᶾ'
Expand All @@ -153,6 +191,7 @@ builtinParticles =
\to the length of the shorter list.\n\
\If one of the input is an number, apply the function to each \
\integer from 0 to the input minus 1."
[]
, BuiltinParticle
"outer"
'ᵒ'
Expand All @@ -162,6 +201,7 @@ builtinParticles =
\and return a list of lists.\n\
\If one of the input is an number, apply the function to each \
\integer from 0 to the input minus 1."
[]
, BuiltinParticle
"predicate"
'ᵖ'
Expand All @@ -170,6 +210,7 @@ builtinParticles =
"Check if a function would succeed without actually applying it.\n\
\If the function fails, replace the top value with Fail.\n\
\Otherwise, do nothing."
[]
, BuiltinParticle
"predicateNot"
'ᵗ'
Expand All @@ -178,6 +219,7 @@ builtinParticles =
"Check if a function would fail without actually applying it.\n\
\If the function does not fail, replace the top value with Fail.\n\
\Otherwise, do nothing."
[]
, BuiltinParticle
"filter"
'ᶠ'
Expand All @@ -187,12 +229,14 @@ builtinParticles =
\without actually applying it, and remove the value if it fails.\n\
\If the input is an number, convert it to a list of integers \
\from 0 to the input minus 1 before filtering."
[]
, BuiltinParticle
"orApply"
'ᶜ'
orApply
"(n -> n) -> (n -> n)"
"Apply a function zero or one time non-deterministically."
[]
, BuiltinParticle
"iterate"
'ᶦ'
Expand All @@ -202,13 +246,15 @@ builtinParticles =
\until the top value of the stack is Fail.\n\
\This is different from `while` in that it returns \
\the intermediate results."
[]
, BuiltinParticle
"nTimes"
'ᵑ'
nTimes
"(n -> n) -> (n + 1 -> n)"
"Take an integer from the top of the stack, \
\and apply a function that many times."
[]
, BuiltinParticle
"while"
'ʷ'
Expand All @@ -218,6 +264,7 @@ builtinParticles =
\until the top value of the stack is Fail.\n\
\This is different from `iterate` in that it does not \
\return the intermediate results."
[]
, BuiltinParticle
"lengthWhile"
'ˡ'
Expand All @@ -226,13 +273,15 @@ builtinParticles =
"Apply a function zero or more times, \
\until the top value of the stack is Fail, \
\and return the number of times the function was applied."
[]
, BuiltinParticle
"firstInt"
'ᵏ'
firstInt
"(m -> n) -> (0 -> 1)"
"Find the smallest non-negative integer for which a function \
\does not fail, and return it."
[]
, BuiltinParticle
"fold1"
'ʳ'
Expand All @@ -243,6 +292,7 @@ builtinParticles =
\and so on until the end of the list.\n\
\If the input is an number, convert it to a list of integers \
\from 0 to the input minus 1 before folding."
[]
]

-- | The map of from names to builtin particles
Expand Down
Loading

0 comments on commit b39e912

Please sign in to comment.