์ด ๋ฌธ์๋ Unison ์ฝ๋๋ฅผ ์์ฑํ๊ธฐ ์ํ ๊ธฐ๋ณธ ์ํฌํ๋ก๋ฅผ ์๋ดํฉ๋๋ค. ์งํํ๋ฉด์ Unison ์ธ์ด์ ํต์ฌ ์์์ ๋ฌธ๋ฒ์ ์กฐ๊ธ์ฉ ์๊ฐํฉ๋๋ค.
์ด ๋ฌธ์๋ Unison ์ฝ๋๋ฅผ ์์ฑํ๊ธฐ ์ํ ๊ธฐ๋ณธ ์ํฌํ๋ก๋ฅผ ์๋ดํฉ๋๋ค. ์งํํ๋ฉด์ Unison ์ธ์ด์ ํต์ฌ ์์์ ๋ฌธ๋ฒ์ ์กฐ๊ธ์ฉ ์๊ฐํฉ๋๋ค. ์ง๋ฌธ์ด ์๊ฑฐ๋ ๋ ๋ฐฐ์ฐ๊ณ ์ถ๋ค๋ฉด ๋ ์์ธํ ์๋ฃ์ธ Unison ์ธ์ด ๋ฌธ์๋ฅผ ์ฐธ๊ณ ํ์ธ์.
์ด ๊ฐ์ด๋๋ ์ด๋ฏธ ํต์คํํธ ๊ฐ์ด๋์ ๋จ๊ณ๋ฅผ ์๋ฃํ๊ณ , ํฐ ์์ด๋์ด๋ฅผ ํ์ด๋ณด์๋ค๊ณ ๊ฐ์ ํฉ๋๋ค.
Unison Codebase Manager(์ค์ฌ์ UCM)๋ Unison ํ๋ก๊ทธ๋๋ฐ ์ธ์ด๋ฅผ ์คํํ๊ณ , ์ฌ๋ฌ๋ถ์ด ์์ฑํ Unison ์ฝ๋์ ์ํธ์์ฉํ ์ ์๊ฒ ํด์ฃผ๋ ์ปค๋งจ๋๋ผ์ธ ๋๊ตฌ์ ๋๋ค. Unison ์ฝ๋๋ฒ ์ด์ค์ ๋ํ ์ธํฐํ์ด์ค ์ญํ ์ ํฉ๋๋ค.
UCM์ด ๋งก๋ ์ฃผ์ ์ญํ ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
๐ก ๊ธฐ์ตํ์ธ์: Unison ์ฝ๋๋ ํ ์คํธ ๊ธฐ๋ฐ ํ์ผ ๋ด์ฉ์ผ๋ก ์ ์ฅ๋์ง ์์ต๋๋ค. ๊ทธ๋์ Unison ํ๋ก๊ทธ๋จ์ ๋ณ๊ฒฝํ๊ณ ์คํํ๋ ค๋ฉด ์ด๋ฌํ ๋๊ตฌ๊ฐ ํ์ํฉ๋๋ค.
์์ํ๋ ค๋ฉด ํฐ๋ฏธ๋์์ ucm์ ์
๋ ฅํ์ธ์. ๊ธฐ๋ณธ์ ์ผ๋ก ucm์ ํ ๋๋ ํฐ๋ฆฌ์ ์ฝ๋๋ฒ ์ด์ค๋ฅผ ์์ฑํ๊ฑฐ๋ ์ฝ๋๋ค.
์๋๋ Unison Codebase Manager๊ฐ ์์๋๋ฉด์ ์ ์ฝ๋๋ฒ ์ด์ค๋ฅผ ์ด๊ธฐํํ๋ ๋ชจ์ต์ ๋๋ค.
Now starting the Unison Codebase Manager (UCM)...
[โฆ]
Get started:
๐ Type help to list all commands, or help <cmd> to view help for one command
๐จ Type ui to open the Codebase UI in your default browser
๐ Read the official docs at https://www.unison-lang.org/docs/
๐ Visit Unison Share at https://share.unison-lang.org to discover libraries
๐ I'm watching for changes to .u files under ~/unisonCode
scratch/main>
์ฐ๋ฆฌ๋ ๋ณดํต ์ฝ๋๋ฒ ์ด์ค๋ฅผ ํ ์คํธ ํ์ผ๋ค์ ๋ฌถ์์ผ๋ก ์๊ฐํ๊ณ , ์ฝ๋๊ฐ ๋ฐ๋๋ฉด ๊ทธ ํ์ผ๋ค์ด ๋ณ๊ฒฝ(mutate)๋๋ค๊ณ ์ฌ๊น๋๋ค. ํ์ง๋ง Unison์์๋ ์ด CLI์ ๋์์ ๋ฐ์ ์ฝ๋๋ฒ ์ด์ค๊ฐ ํ๋ก๊ทธ๋จ์ ์ผ๋ก ์ ์ฅ๋๊ณ ์ ๋ฐ์ดํธ๋ฉ๋๋ค.
Unison ์ฝ๋๋ฒ ์ด์ค ํฌ๋งท์๋ ๋ช ๊ฐ์ง ํต์ฌ ์์ฑ์ด ์์ต๋๋ค.
์ ์ฝ๋๋ฒ ์ด์ค๋ฅผ ๋ง๋ค์๋ค๋ฉด ํ๋กฌํํธ > ๋ scratch/main์ผ๋ก ํ์๋ ๊ฒ์
๋๋ค. ์ด๋ ํ์ฌ ์์
์ค์ธ ํ๋ก์ ํธ์ ๋ธ๋์น ์ด๋ฆ์ ๋ปํ๋๋ฐ, ๋ค์์์ ๋ค๋ฃฐ ๊ฐ๋
์
๋๋ค.
Unison์ ์ ํ๋ฆฌ์ผ์ด์ ๋ง๋ค ์ฌ๋ฌ ์ฝ๋๋ฒ ์ด์ค๋ฅผ ๋ง๋๋ ๋์ , ํ๋์ ์ฝ๋๋ฒ ์ด์ค๋ฅผ "ํ๋ก์ ํธ"๋ก ๋๋๋๋ค. Unison ํ๋ก์ ํธ๋ ์์ค ์ฝ๋ ์ ์ฅ์(repository)์ ๋น์ทํฉ๋๋ค. ํ๋ก์ ํธ๋ ์ฑ, ๋ผ์ด๋ธ๋ฌ๋ฆฌ, ๊ทธ๋ฆฌ๊ณ ํ์ ํ๊ณ ์ถ์ ๋ค๋ฅธ ์์ ๋จ์๋ฅผ ๋ถ๋ฆฌํฉ๋๋ค. ํ๋ก์ ํธ๋ ๋ํ ์๋ก ๋ ๋ฆฝ์ ์ธ ์์ ํ๋ฆ์ ๋ํ๋ด๋ ๋ธ๋์น๋ค๋ก ๋ ๋๋ฉ๋๋ค.
ํ๋ก์ ํธ ์์์ ์ฌ๋ฌ๋ถ์ ์ฝ๋๋ ๋ค์์คํ์ด์ค(namespace)๋ก ๋ฌถ์
๋๋ค. ๋ค์์คํ์ด์ค๋ ์ปดํจํฐ์ ๋๋ ํฐ๋ฆฌ์ฒ๋ผ Unison์ term๊ณผ type์ ํธ๋ฆฌ ๊ตฌ์กฐ๋ก ์ ๋ฆฌํฉ๋๋ค. ์๋ฅผ ๋ค์ด math.sqrt๋ math ๋ค์์คํ์ด์ค ์์ sqrt ์ ์๋ฅผ ๊ฐ๋ฆฌํต๋๋ค. ์ ์๋ base.Optional.Some์ฒ๋ผ ์ (.)์ผ๋ก ๊ตฌ๋ถ๋ ์ฌ๋ฌ ๋ค์์คํ์ด์ค ์ธ๊ทธ๋จผํธ๋ฅผ ๊ฐ์ง ์ ์์ต๋๋ค.
UCM ์ฝ์์์ project.create ๋ช
๋ น์ผ๋ก ์ tour ํ๋ก์ ํธ๋ฅผ ๋ง๋ค์ด ๋ด
์๋ค.
scratch/main> project.create tour
๐ I've created the project tour.
I'll now fetch the latest version of the base Unison library...
๐จ Type `ui` to explore this project's code in your browser.
๐ Discover libraries at https://share.unison-lang.org
๐ Use `help-topic projects` to learn more about projects.
Write your first Unison code with UCM:
1. Open scratch.u.
2. Write some Unison code and save the file.
3. In UCM, type `update` to save it to your new project.
๐ ๐ฅณ Happy coding!
tour/main>
ํ๋กฌํํธ๊ฐ tour/main>์ผ๋ก ๋ฐ๋ ๊ฒ์ ํ์ธํ์ธ์. ํ์ฌ ํ๋ก์ ํธ๋ tour, ํ์ฌ ๋ธ๋์น๋ /main์
๋๋ค. ์ด์ UCM ๋ช
๋ น๊ณผ ์ฝ๋๋ ์ด ํ๋ก์ ํธ์ ๋ธ๋์น ๋ฒ์(scope)์ ์ ์ฉ๋ฉ๋๋ค.
์ ํ๋ก์ ํธ๋ฅผ ๋ง๋ค๋ฉด UCM์ด base ํ์ค ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์๋์ผ๋ก ์ค์นํด ์ค๋๋ค. ์ด ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ lib๋ผ๋ ํน๋ณํ ๋ค์์คํ์ด์ค ์๋์ ์์นํฉ๋๋ค.
๐ง
lib ๋ค์์คํ์ด์ค๋ UCM์ด ํ๋ก์ ํธ์ ๋ชจ๋ ์์กด์ฑ์ ์ค์นํ๋ ๊ณณ์
๋๋ค. ํญ์ ํ๋ก์ ํธ ๋ฃจํธ ๋ฐ๋ก ์๋์ ์์นํฉ๋๋ค.
๋ค์์ผ๋ก ๋ฐฉ๊ธ ๋ด๋ ค๋ฐ์ base ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ํ์ํด ๋ณด๋ฉฐ Unison ์ฝ๋๋ฒ ์ด์ค ํ์์ ์ต์ํด์ ธ ๋ด
์๋ค.
ls ๋ช
๋ น์ผ๋ก ๋ค์์คํ์ด์ค์ ์๋ term๊ณผ type ์ด๋ฆ๋ค์ ๋ณผ ์ ์์ต๋๋ค.
tour/main> ls lib.base.data.List
์ถ๋ ฅ์ ์ ์์ ๊ทธ์ ์ฐ๊ด๋ ์๊ทธ๋์ฒ(signature)๊ฐ ๋ฒํธ ๋ชฉ๋ก์ผ๋ก ํ์๋ฉ๋๋ค.
tour/main> ls lib.base.data.List
1. ++ ([a] -> [a] -> [a])
2. ++. (1 term)
3. +: (a -> [a] -> [a])
[โฆ]
Unison ์ฝ๋๋ UCM์ ํตํด ์ ์ฅ๋๊ธฐ ๋๋ฌธ์ ๋ค์๊ณผ ๊ฐ์ ์ ๋ณด๋ฅผ ์บ์ํ๊ณ ์กฐํํ ์ ์์ต๋๋ค.
์ค์ ๋ก find์ view ๋ช
๋ น์ ์ด์ฉํด ๊ฒ์ํ๊ณ ์ฝ๋๋ฅผ ํ์ธํด ๋ด
์๋ค.
tour/main> find reverse
I couldn't find matches in this namespace, searching in 'lib'...
1. lib.base.data.Graph.reverse : Graph v ->{Exception} Graph v
2. lib.base.data.Graph.tests.reverse : [Result]
3. lib.base.data.List.Nonempty.reverse : List.Nonempty a -> List.Nonempty a
4. lib.base.data.List.reverse : [a] -> [a]
find ๋ช
๋ น์ ์ด๋ฆ์ reverse๋ฅผ ํฌํจํ๋ ์ ์๋ฅผ ์ฐพ๊ณ ์์ต๋๋ค. ๋จผ์ ํ๋ก์ ํธ ์์ ์ฐ๋ฆฌ ์ฝ๋์์ ์ฐพ๊ณ , ๊ทธ๋ค์ lib์์ ๊ฒ์ํฉ๋๋ค.
tour/main> view List.reverse
lib.base.data.List.reverse : [a] -> [a]
lib.base.data.List.reverse as =
List.foldLeft (acc a -> a +: acc) [] as
์ฌ๊ธฐ์๋ view ๋ช
๋ น์ผ๋ก ํจ์์ ์์ค ์ฝ๋๋ฅผ ํ์ธํ์ต๋๋ค.
Unison ๋ฌธ๋ฒ์ ๋ช ๊ฐ์ง ์๊ฐํฉ๋๋ค.
List.reverse : [a] -> [a] ๋ ํ์
์๊ทธ๋์ฒ๋ฅผ ์ฐ๋ ๋ฌธ๋ฒ์
๋๋ค. Unison์์๋ ํ์
์๊ทธ๋์ฒ๋ฅผ ์ ์ ์์ ์์ฑํฉ๋๋ค. : ๊ธฐํธ๋ "ํ์
์ ๊ฐ์ง๋ค"๋ก ์ฝ์ต๋๋ค. ์ฆ "reverse๋ [a] -> [a] ํ์
์ ๊ฐ์ง๋ค"๋ผ๋ ๋ป์
๋๋ค.[ ] ๋ Unison์์ ๋ฆฌ์คํธ๋ฅผ ๋ํ๋
๋๋ค.[a] -> [a] ๋ ์ด๋ค ํ์
์ ์์๋ค๋ก ์ด๋ฃจ์ด์ง ๋ฆฌ์คํธ๋ฅผ ๋ฐ์ ๊ฐ์ ํ์
์ ๋ฆฌ์คํธ๋ฅผ ๋๋ ค์ฃผ๋ ํจ์๋ผ๋ ๋ป์
๋๋ค.List.reverse as ์์ ํจ์์ ์ธ์ ํ๋์ as๋ผ๋ ์ด๋ฆ์ ๋ถ์์ต๋๋ค. = ๋ค์ ๋ชจ๋ ๊ฒ์ด ํจ์ ๋ณธ๋ฌธ์
๋๋ค. ์ฌ๊ธฐ์๋ ๊ณต๋ฐฑ ๋ค์ฌ์ฐ๊ธฐ๋ก ๊ตฌ๋ถ๋๋ ๋ธ๋ก์
๋๋ค.(acc a -> ..) ๋ ์ต๋ช
ํจ์(anonymous function) ๋ฌธ๋ฒ์
๋๋ค.(acc a -> ...) [] as ๋ List.foldLeft์ ์ ๋ฌ๋๋ ์ธ ๊ฐ์ ์ธ์์
๋๋ค.โจ
์ธ์ ์์ด view ๋ช
๋ น์ ์
๋ ฅํ๋ฉด, ์ฝ๋๋ฒ ์ด์ค์์ ์ ์๋ฅผ ์ฐพ๊ธฐ ์ํ fzf ๊ฒ์ ์ธํฐํ์ด์ค๊ฐ ์ด๋ฆฝ๋๋ค. ์ฝ๋๋ฒ ์ด์ค๋ฅผ ํ์ํ๊ณ ์ด๋ฆ์ผ๋ก ์ ์๋ฅผ ์ฐพ๋ ๋น ๋ฅธ ๋ฐฉ๋ฒ์
๋๋ค.
์ฝ๋๋ฒ ์ด์ค ๋งค๋์ ๋ ํ์ฌ ๋๋ ํฐ๋ฆฌ์์ .u๋ก ๋๋๋ ๋ชจ๋ ํ์ผ์ ๋ณ๊ฒฝ์ ๊ฐ์ํฉ๋๋ค. ๊ทธ๋ฐ ํ์ผ(์ด๋ฅผ "์คํฌ๋์น ํ์ผ"์ด๋ผ๊ณ ๋ถ๋ฆ
๋๋ค)์ด ์ ์ฅ๋๋ฉด Unison์ ํด๋น ํ์ผ์ ํ์ฑํ๊ณ ํ์
์ฒดํฌํฉ๋๋ค.
ucm ํฐ๋ฏธ๋์ ๊ณ์ ์คํํด ๋ ์ฑ๋ก, ์ ํธํ๋ ํ
์คํธ ํธ์ง๊ธฐ์์ scratch.u(๋๋ foo.u ๋ฑ ์ํ๋ ์ด๋ฆ) ํ์ผ์ ์ฌ์ธ์.
์คํฌ๋์น ํ์ผ์ ๋ค์์ ์ ๋ ฅํ์ธ์.
square : Nat -> Nat
square x =
use Nat *
x * x
์ด๋ square๋ผ๋ ํจ์๋ฅผ ์ ์ํฉ๋๋ค. x๋ผ๋ ์ธ์๋ฅผ ๋ฐ๊ณ x๋ฅผ ์๊ธฐ ์์ ๊ณผ ๊ณฑํ ๊ฐ์ ๋ฐํํฉ๋๋ค.
use Nat * ๋ ์ด๋ค * ์ฐ์ฐ์๋ฅผ ์ฌ์ฉํ ์ง ์ง์ ํ๋ use ์ ์
๋๋ค. ์ด๋ lib.base ํ์ค ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ Nat ๋ค์์คํ์ด์ค์์ ๊ฐ์ ธ์จ ๊ฒ์
๋๋ค. use ์ ์ ์ฌ์ฉํ๋ฉด base.Nat์ ๊ฐ๋จํ Nat์ผ๋ก, Nat.*์ฒ๋ผ ์ ๋์ด๋ฅผ ๋ถ์ด์ง ์๊ณ *๋ฅผ ์ฐธ์กฐํ ์ ์์ต๋๋ค.
ํ์ผ์ ์ ์ฅํ๋ฉด Unison์ ๋ณ๊ฒฝ์ ์ ์ฉํ ์ ์๋์ง ์๋ ค์ค๋๋ค.
I found and typechecked these definitions in ~/unisoncode/scratch.u. If you do an `update`, here's how your codebase would change:
โ New definitions:
square : Nat -> Nat
์ฝ๋๋ฒ ์ด์ค์ square๋ฅผ ์ ์ฅํ๊ธฐ ์ ์, scratch.u ํ์ผ ์์์ ๋ฐ๋ก ํธ์ถํด ๋ด
์๋ค. >๋ก ์์ํ๋ ์ค์ ์์ฑํ๋ฉด ๋ฉ๋๋ค.
square : Nat -> Nat
square x =
use Nat *
x * x
> square 4
ํ์ผ์ ์ ์ฅํ๋ฉด Unison์ด ๋ค์๊ณผ ๊ฐ์ด ์ถ๋ ฅํฉ๋๋ค.
6 | > square 4
โงฉ
16
6๋ฒ์งธ ์ค์ > square 4๋ "watch expression"(๊ด์ฐฐ ํํ์)์ด๋ผ๊ณ ๋ถ๋ฆ
๋๋ค. Unison์ ๋ณ๋์ REPL(์ฝ๊ธฐ-ํ๊ฐ-์ถ๋ ฅ ๋ฃจํ) ์์ด, watch expression์ผ๋ก ์ธ๋ผ์ธ์์ ์ฝ๋๋ฅผ ์คํํฉ๋๋ค. ์คํฌ๋์น ํ์ผ๊ณผ ํ๋ก์ ํธ์ ๋ชจ๋ ๊ฒ์ด ์ค์ฝํ ์์ ์์ผ๋ฏ๋ก, ํ
์คํธ ํธ์ง๊ธฐ์ ๋ชจ๋ ๊ธฐ๋ฅ์ ํ์ฉํ๋ฉฐ ์ฝ๋๋ฅผ ํ
์คํธํ ์ ์์ต๋๋ค.
์ง๋ฌธ: ์ ์ฅํ ๋๋ง๋ค watch expression์ ๋ชจ๋ ์ฌํ๊ฐํด์ผ ํ ๊น์(๋น์ฉ์ด ํฌ๋๋ผ๋)? Unison์ ๊ฐ ํํ์์ ํด์๋ฅผ ํค๋ก ๊ฒฐ๊ณผ๋ฅผ ์บ์ํจ์ผ๋ก์จ ์ด๋ฅผ ํผํฉ๋๋ค. ํด์๊ฐ ๋ณํ์ง ์์๋ค๋ฉด, Unison์ ์บ์๋ ๊ฒฐ๊ณผ๋ฅผ ์ฌ์ฌ์ฉํฉ๋๋ค. ์บ์๋ ์ธ์ ๋ ๋ฌธ์ ์์ด ๋น์ธ ์ ์์ต๋๋ค. .u ํ์ผ์ ์คํ๋ ๋์ํธ์ฒ๋ผ ์๊ฐํด ๋ณด์ธ์. ์์กด์ฑ์ด ๋ฐ๋ ์
๋ง ๋ค์ ๊ณ์ฐ๋ฉ๋๋ค.
๐ค
์ด๊ฒ์ด ํจ๊ณผ์ ์ผ๋ก ๋์ํ๋ ๋ฐ ํ์ํ ์ฌ๋ฃ๊ฐ ํ๋ ๋ ์๋๋ฐ, ๋ฐ๋ก ํจ์ํ ํ๋ก๊ทธ๋๋ฐ์ ๋๋ค. ํํ์์ ๋ถ์์ฉ(side effect)์ด ์์ผ๋ฉด ๊ฒฐ๊ณผ๋ ๊ฒฐ์ ์ (deterministic)์ด๋ฉฐ, Unison์ ์ฝํ ์ธ ๊ธฐ๋ฐ ํด์์ฒ๋ผ ์บ์ ํค๊ฐ ํ์คํ๊ธฐ๋ง ํ๋ฉด ๊ฒฐ๊ณผ๋ฅผ ์บ์ํ ์ ์์ต๋๋ค. Unison์ ํ์ ์์คํ ์ ์ด๋ฐ watch expression ์์์ I/O๋ฅผ ์ํํ๊ฑฐ๋, ํ๊ฐํ ๋๋ง๋ค ๊ฒฐ๊ณผ๊ฐ ๋ฌ๋ผ์ง๊ฒ ๋ง๋๋ ์์ ์ ํ์ฉํ์ง ์์ต๋๋ค.
๋ช ๊ฐ์ง watch expression์ ๋ ์๋ํด ๋ณด๊ณ UCM์ด ์ด๋ป๊ฒ ๋ฐ์ํ๋์ง ํ์ธํด ๋ณด์ธ์.
-- Unison์ด ๋ฌด์ํ๋ ์ฃผ์
> List.reverse [1,2,3,4]
> 4 + 6
> 5.0 / 2.0
> not true
์ด์ square ํจ์์ ๋ํ ํ
์คํธ๋ฅผ ์ถ๊ฐํด ๋ด
์๋ค.
test> square.tests.ex1 =
verify do ensureEqual (square 4) 16
test> ๋ square.tests.ex1์ด๋ผ๋ ์ด๋ฆ์ ํ
์คํธ watch expression์ ๋์
ํฉ๋๋ค.
foo์ ํ
์คํธ๋ foo.tests ์๋์ ๋ก๋๋ค. ๊ทธ ์ธ์๋ square.tests.ex1์ด๋ผ๋ ์ด๋ฆ ์์ฒด์ ํน๋ณํ ์๋ฏธ๋ ์์ต๋๋ค.test.verify๋ ์ง์ฐ ๊ณ์ฐ์ผ๋ก, ํ
์คํธํ ํจ์๋ฅผ ๊ธฐ๋ ๊ฒฐ๊ณผ์ ํจ๊ป ํธ์ถํ๋ ๊ฒ์ ๊ธฐ๋ํฉ๋๋ค.
do๋ ์ง์ฐ ๊ณ์ฐ์ ์์ํฉ๋๋ค. ์ธ์๊ฐ ์๋ ํจ์ () -> a์ฒ๋ผ ์๊ฐํ๋ฉด ๋๋ฉฐ, ๊ฒฐ๊ณผ๋ฅผ ์ง์ฐ(lazily) ๋ฐํํ๋ ๋ฐ ์ฌ์ฉ๋ฉ๋๋ค.test.ensureEqual์ ๋ ๊ฐ์ด ๊ฐ์ง ์์ผ๋ฉด Exception์ ๋ฐ์์์ผ ํ
์คํธ๋ฅผ ์คํจ์ํต๋๋ค.
ํ์ผ์ ์ ์ฅํ๋ฉด UCM์ด ๋ค์๊ณผ ๊ฐ์ด ์๋ตํฉ๋๋ค.
8 | test> square.tests.ex1 =
verify do ensureEqual (square 4) 16
โ
Passed
์ข ๋ ์ฒ ์ ํ ํ
์คํธํด ๋ด
์๋ค. square๋ ๋ชจ๋ a, b์ ๋ํด square a * square b == square (a * b)๋ผ๋ ์ฑ์ง์ ๊ฐ์ ธ์ผ ํฉ๋๋ค. ํ
์คํธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ ์ด๋ฐ ํ๋กํผํฐ ๊ธฐ๋ฐ ํ
์คํธ๋ฅผ ์ง์ํฉ๋๋ค. ์๋์๋ ์ ๋ฌธ๋ฒ์ด ์กฐ๊ธ ๋ฑ์ฅํ๋ฉฐ, ๋ค์์ ์ค๋ช
ํฉ๋๋ค.
test> square.tests.prop1 =
verify do
Each.repeat 100
a = Random.natIn 0 100
b = Random.natIn 0 100
ensure (square a * square b == square (a * b))
์ด๋ Each์ Random ์ด๋น๋ฆฌํฐ(ability)๋ฅผ ์ฌ์ฉํด ๋ค์ํ ์
๋ ฅ์ผ๋ก ํจ์๋ฅผ ํ
์คํธํฉ๋๋ค. Each๋ 100๊ฐ์ Random ์ซ์๋ฅผ ์์ฑํ๋ ๋ฐ ์ฌ์ฉ๋๋ฉฐ, ํ
์คํธ๋ ์ด๋ฅผ ๊ฒ์ฆํฉ๋๋ค.
= ๋ค์ ์์๋๋ Unison ์ฝ๋ ๋ธ๋ก์๋ ๊ฐ์ ๋ค์ฌ์ฐ๊ธฐ ์์ค์์ ์ฌ๋ฌ ๋ฐ์ธ๋ฉ(a = โฆ)์ ๋ ์ ์์ต๋๋ค. ๋ง์ง๋ง ํํ์(ensure (square โฆ))์ด ๋ธ๋ก์ ๊ฒฐ๊ณผ๊ฐ ๋ฉ๋๋ค.do ํค์๋๋ ๊ณต๋ฐฑ ๋ค์ฌ์ฐ๊ธฐ๋ก ๊ตฌ๋ถ๋๋ ์ง์ฐ ๊ณ์ฐ ์ฝ๋ ๋ธ๋ก์ ๋์
ํฉ๋๋ค.์คํฌ๋์น ํ์ผ์ ์ ์ฅํ์ฌ ํ๋กํผํฐ ํ ์คํธ๊ฐ ์ฑ๊ณตํ๋์ง ํ์ธํ์ธ์.
square ํจ์์ ํ
์คํธ๋ค์ ์์ง ์ฝ๋๋ฒ ์ด์ค์ ํฌํจ๋์ง ์์์ต๋๋ค. ์ง๊ธ๊น์ง๋ ์คํฌ๋์น ํ์ผ์๋ง ์กด์ฌํฉ๋๋ค. ์ด์ ์ถ๊ฐํด ๋ด
์๋ค. UCM์ผ๋ก ๋์๊ฐ update๋ฅผ ์
๋ ฅํ์ธ์.
tour/main> update
โ I've added these definitions:
square : Nat -> Nat
square.tests.ex1 : [Result]
square.tests.prop1 : [Result]
UCM์์ view square.tests.prop1์ ์
๋ ฅํด ๋ณด์ธ์.
tour/main> view square.tests.prop1
square.tests.prop1 : [Result]
square.tests.prop1 = test.verify do
use Nat * ==
use Random natIn
Each.repeat 100
a = natIn 0 100
b = natIn 0 100
ensure (square a * square b == square (a * b))
Unison์ ์ฝ๋๋ฅผ ๋ ๋๋งํ ๋ ์ ํํ use ๋ฌธ์ ์ฝ์
ํ๊ณ , ์์ฒด pretty-print ๊ท์น์ ๋ฐ๋ผ ํฌ๋งทํ
ํฉ๋๋ค.
UCM ํ๋กฌํํธ์์ test๋ฅผ ์
๋ ฅํด ํ
์คํธ ์ค์ํธ๋ฅผ "์คํ"ํด ๋ด
์๋ค.
tour/main> test
Cached test results (`help testcache` to learn more)
โ square.tests.ex1 : Passed
โ square.tests.prop1 : Passed
โ
2 test(s) passing
Tip: Use view square.tests.ex1 to view the source of a test.
์ฝ์์๋ ๊ฒฐ๊ณผ๊ฐ ์ถ๋ ฅ๋์ง๋ง, ์ค์ ๋ก ์ฝ๋๋ฅผ ์คํํ ๊ฒ์ ์๋๋๋ค. ํ ์คํธ๋ ์ด์ ์ ์คํ๋์๊ณ Unison ํด์์ ๋ฐ๋ผ ์บ์๋์ด ์์๊ธฐ ๋๋ฌธ์ ๋๋ค. Unison ๊ฐ์ ์์ ํจ์ํ ์ธ์ด์์๋ ์ด๋ฐ ํ ์คํธ๋ค์ด ๊ฒฐ์ ์ ์ด์ด์, ์์กดํ๋ ์ฝ๋๊ฐ ๋ฐ๋๊ธฐ ์ ๊น์ง๋ ์ค์ ๋ก ๋ค์ ์คํ๋์ง ์์ต๋๋ค.
move ๋ช
๋ น์ผ๋ก square์ ๊ทธ ํ
์คํธ๋ค์ ํ๋ก์ ํธ ๋ด ๋ค๋ฅธ ์์น๋ก ์ฎ๊ฒจ ๋ด
์๋ค. move๋ ์ฌ๊ท์ ์ด๋ฏ๋ก, term๊ณผ square ๋ค์์คํ์ด์ค ์์ ๋ชจ๋ ๊ฒ๋ค์ ์ ์ด๋ฆ์ผ๋ก ํจ๊ป ์ฎ๊น๋๋ค.
tour/main> move square mySquare
Done.
๊ทธ ๋ค์ ์ธ์ ์์ด find๋ฅผ ์ฌ์ฉํด ํ์ฌ ๋ค์์คํ์ด์ค ์๋์ ๋ชจ๋ ์ ์๋ฅผ ํ์ธํด ๋ณด์ธ์.
tour/main> find
1. mySquare : Nat -> Nat
2. mySquare.tests.ex1 : [Result]
3. mySquare.tests.prop1 : [Result]
๋ค์ test๋ฅผ ์คํํ๋ฉด ํ
์คํธ ๊ฒฐ๊ณผ๊ฐ ์ฌ์ ํ ์บ์๋์ด ์์์ ๋ณผ ์ ์์ต๋๋ค. ์ ์๋ ์ด๋ฆ์ด ์๋๋ผ ๊ตฌํ์ผ๋ก ์๋ณ๋๊ธฐ ๋๋ฌธ์
๋๋ค.
tour/main> test
Cached test results (`help testcache` to learn more)
โ mySquare.tests.ex1 : Passed
โ mySquare.tests.prop1 : Passed
โ
2 test(s) passing
์ด์ mySquare ํจ์์ ๊ตฌํ์ ๋ฐ๊ฟ ๋ด
์๋ค.
scratch.u ํ์ผ ๋งจ ์์ ์ ์ค๋ก ---(๋์ 3๊ฐ)๋ฅผ ์ถ๊ฐํ์ธ์. ์ด๋ฅผ fold๋ผ๊ณ ํฉ๋๋ค. --- ์๋์ ๋ชจ๋ ๋ด์ฉ์ UCM์ด ๋ฌด์ํฉ๋๋ค.
๊ทธ ๋ค์ UCM์์ edit mySquare๋ฅผ ์
๋ ฅํ์ธ์.
tour/main> edit mySquare
โ๏ธ
I added 1 definition to the top of ~/unisoncode/scratch.u
You can edit them there, then run `update` to replace the definitions currently in this branch.
์ด ๋ช
๋ น์ mySquare์ pretty-print๋ ์ ์๋ฅผ ์คํฌ๋์น ํ์ผ ๋งจ ์์ ๋ง๋ถ์
๋๋ค.
์ฌ๋ฏธ๋ก mySquare x๋ฅผ ์ฒ์ x๊ฐ์ ํ์๋ฅผ ๋ํ ๊ฐ์ผ๋ก ์ ์ํด ๋ด
์๋ค. (์ ๊ฐ์ ๊ฒฐ๊ณผ๊ฐ ๋์ค๋์ง์ ๋ํ ์ข์ ๊ธฐํํ์ ์ค๋ช
๋ ์์ต๋๋ค.)
use base
mySquare : Nat -> Nat
mySquare x =
sum (map (x -> x * 2 + 1) (range 0 x))
sum : [Nat] -> Nat
sum = foldLeft (+) 0
ํ์ผ์ ์ ์ฅํ๋ฉด UCM์ mySquare๋ฅผ ์
๋ฐ์ดํธํด๋ ์ข๋ค๋ ๋ฉ์์ง๋ฅผ ๋ณด์ฌ์ค ๊ฒ์
๋๋ค.
I found and typechecked these definitions in ~/unisoncode/scratch.u. If you
run `update`, here's how your codebase would change:
โ These new definitions are ok to `update`:
sum : [Nat] -> Nat
โ These names already exist. You can `update` them to your new definition:
mySquare : Nat -> Nat
์ด์ ์ค์ ๋ก ์ ๋ฐ์ดํธํด ๋ด ์๋ค.
tour/main> update
โ I've added these definitions:
sum : [Nat] -> Nat
โ I've updated these names to your new definition:
mySquare : Nat -> Nat
update ์ ์ํฅ์ ๋ฐ๋ ํ
์คํธ๋ง ์ฌ์คํ๋จํ ์คํธ๋ฅผ ๋ค์ ์คํํด์ผ ํ๋์ง ์ฌ๋ถ๋ฅผ ๊ฒฐ์ ํ๋ ์์กด์ฑ ์ถ์ ์ 100% ์ ํํ๋ฉฐ, ๊ฐ๋ณ ์ ์ ์์ค์์ ์ถ์ ๋ฉ๋๋ค.
์ด์ test ๋ช
๋ น์ ๋ค์ ์คํํ๋ฉด, ํ
์คํธ๋ ์บ์๋์ด ์์ง ์์ ๊ฒ์
๋๋ค. ์์กด์ฑ ์ค ํ๋๊ฐ ์ค์ ๋ก ๋ฐ๋์๊ธฐ ๋๋ฌธ์
๋๋ค.
tour/main> test
โ
New test results:
โ mySquare.tests.ex1 : Passed
โ mySquare.tests.prop1 : Passed
โ
2 test(s) passing
Tip: Use view 1 to view the source of a test.
๐
๋ง์ง๋ง์ผ๋ก ํ ์ผ์ Unison Share์ ๊ฐ์ ํ๋ ๊ฒ์ ๋๋ค. Unison Share๋ Unison์ ์ํ ์ฝ๋ ํธ์คํ ํ๋ซํผ์ ๋๋ค. ์ด๊ณณ์์ ์ฌ๋ฌ๋ถ์ ์ฝ๋ ํ์ ์ ๊ด๋ฆฌํ๊ณ , ์๋ก์ด Unison ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ๋ฐ๊ฒฌํ ์ ์์ต๋๋ค. Unison Share๋ก ์ด๋ํด ์๋ด์ ๋ฐ๋ผ ๋ก์ปฌ ์ฝ๋๋ฒ ์ด์ค๋ฅผ ์ฝ๋ ํธ์คํ ์ ์ํด ์ฐ๊ฒฐํ์ธ์.
์ฝ๋๋ push ๋ช
๋ น์ ์ฌ์ฉํด Unison์ ์์ฒด ์ฝ๋ ํธ์คํ
์๋ฃจ์
์ธ Unison Share์ ๋ฐฐํฌ๋๊ณ , ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ lib.install ๋ช
๋ น์ผ๋ก ์ค์นํฉ๋๋ค. ์์กด์ฑ ๊ด๋ฆฌ๋ ์ฝ๋ ๋ฐฐํฌ๋ฅผ ์ํด ๋ณ๋์ ๋๊ตฌ๊ฐ ํ์ํ์ง ์์ต๋๋ค. Unison Share์์ ์ํธ์์ฉ์ ๋ชจ๋ UCM์ ๋ด์ฅ๋์ด ์์ต๋๋ค.
Unison ๋๋ฌ๋ณด๊ธฐ๋ฅผ ๋ง์น ๊ฒ์ ์ถํํฉ๋๋ค! ์ด์ Unison ์ฝ๋๋ฅผ ์์ฑํ ์ค๋น๊ฐ ๋์์ต๋๋ค. ์ฌ๋ฌ๋ถ์ด ๋ฌด์์ ๋ง๋ค์ง ๊ธฐ๋๋ฉ๋๋ค! ๐ฅณ