mirror of
https://github.com/anoma/juvix.git
synced 2024-09-11 16:26:33 +03:00
Add Towers of Hanoi and Pascal triangle examples (#1446)
* Add new examples of Juvix programs * Build documentation for Hanoi and Pascal examples
This commit is contained in:
parent
7bf729a9b5
commit
1ba72b4d9b
2
Makefile
2
Makefile
@ -15,6 +15,8 @@ EXAMPLEHTMLOUTPUT=_docs/examples/html
|
||||
EXAMPLES= HelloWorld/HelloWorld.juvix \
|
||||
Collatz/Collatz.juvix \
|
||||
Fibonacci/Fibonacci.juvix \
|
||||
Hanoi/Hanoi.juvix \
|
||||
PascalsTriangle/PascalsTriangle.juvix \
|
||||
TicTacToe/CLI/TicTacToe.juvix \
|
||||
ValidityPredicates/SimpleFungibleToken.juvix
|
||||
|
||||
|
@ -43,6 +43,8 @@ The following links are clickable versions of their corresponding Juvix programs
|
||||
|
||||
- [[https://docs.juvix.org/examples/html/HelloWorld/HelloWorld.html][HelloWorld.juvix]]
|
||||
- [[https://docs.juvix.org/examples/html/Fibonacci/Fibonacci.html][Fibonacci.juvix]]
|
||||
- [[https://docs.juvix.org/examples/html/Hanoi/Hanoi.html][Hanoi.juvix]]
|
||||
- [[https://docs.juvix.org/examples/html/PascalsTriangle/PascalsTriangle.html][PascalsTriangle.juvix]]
|
||||
- [[https://docs.juvix.org/examples/html/Collatz/Collatz.html][Collatz.juvix]]
|
||||
- [[https://docs.juvix.org/examples/html/TicTacToe/CLI/CLI.TicTacToe.html][TicTacToe.juvix]]
|
||||
- [[https://docs.juvix.org/examples/html/ValidityPredicates/SimpleFungibleToken.html][SimpleFungibleToken.juvix]]
|
||||
|
@ -4,6 +4,8 @@ The following links are clickable versions of their corresponding Juvix programs
|
||||
|
||||
- [[https://docs.juvix.org/examples/html/HelloWorld/HelloWorld.html][HelloWorld.juvix]]
|
||||
- [[https://docs.juvix.org/examples/html/Fibonacci/Fibonacci.html][Fibonacci.juvix]]
|
||||
- [[https://docs.juvix.org/examples/html/Hanoi/Hanoi.html][Hanoi.juvix]]
|
||||
- [[https://docs.juvix.org/examples/html/PascalsTriangle/PascalsTriangle.html][PascalsTriangle.juvix]]
|
||||
- [[https://docs.juvix.org/examples/html/Collatz/Collatz.html][Collatz.juvix]]
|
||||
- [[https://docs.juvix.org/examples/html/TicTacToe/CLI/CLI.TicTacToe.html][TicTacToe.juvix]]
|
||||
- [[https://docs.juvix.org/examples/html/ValidityPredicates/SimpleFungibleToken.html][SimpleFungibleToken.juvix]]
|
||||
|
74
examples/milestone/Hanoi/Hanoi.juvix
Normal file
74
examples/milestone/Hanoi/Hanoi.juvix
Normal file
@ -0,0 +1,74 @@
|
||||
--- Towers of Hanoi is a puzzle with three rods and n disks of decresing size.
|
||||
---
|
||||
--- The disks are stacked ontop of each other through the first rod.
|
||||
--- The aim of the game is to move the stack of disks to another rod while
|
||||
--- following these rules:
|
||||
---
|
||||
--- 1. Only one disk can be moved at a time
|
||||
--- 2. You may only move a disk from the top of one of the stacks to the top of another stack
|
||||
--- 3. No disk may be moved on top of a smaller disk
|
||||
---
|
||||
--- The function ;hanoi; computes the sequence of moves to solve puzzle.
|
||||
|
||||
module Hanoi;
|
||||
|
||||
open import Stdlib.Prelude;
|
||||
|
||||
--- Concatenation of ;String;s
|
||||
infixr 5 ++str;
|
||||
axiom ++str : String → String → String;
|
||||
compile ++str {
|
||||
c ↦ "concat";
|
||||
};
|
||||
|
||||
--- Concatenates a list of strings
|
||||
---
|
||||
--- ;concat (("a" ∷ nil) ∷ ("b" ∷ nil)); evaluates to ;"a" ∷ "b" ∷ nil;
|
||||
concat : List String → String;
|
||||
concat ≔ foldl (++str) "";
|
||||
|
||||
intercalate : String → List String → String;
|
||||
intercalate sep xs ≔ concat (intersperse sep xs);
|
||||
|
||||
--- Joins a list of strings with the newline character
|
||||
unlines : List String → String;
|
||||
unlines ≔ intercalate "\n";
|
||||
|
||||
--- Produce a singleton List
|
||||
singleton : {A : Type} → A → List A;
|
||||
singleton a ≔ a ∷ nil;
|
||||
|
||||
--- Produce a ;String; representation of a ;List ℕ;
|
||||
showList : List ℕ → String;
|
||||
showList xs ≔ "[" ++str intercalate "," (map natToStr xs) ++str "]";
|
||||
|
||||
--- A Peg represents a peg in the towers of Hanoi game
|
||||
inductive Peg {
|
||||
left : Peg;
|
||||
middle : Peg;
|
||||
right : Peg;
|
||||
};
|
||||
|
||||
showPeg : Peg → String;
|
||||
showPeg left ≔ "left";
|
||||
showPeg middle ≔ "middle";
|
||||
showPeg right ≔ "right";
|
||||
|
||||
|
||||
--- A Move represents a move between pegs
|
||||
inductive Move {
|
||||
move : Peg → Peg → Move;
|
||||
};
|
||||
|
||||
showMove : Move → String;
|
||||
showMove (move from to) ≔ showPeg from ++str " -> " ++str showPeg to;
|
||||
|
||||
--- Produce a list of ;Move;s that solves the towers of Hanoi game
|
||||
hanoi : ℕ → Peg → Peg → Peg → List Move;
|
||||
hanoi zero _ _ _ ≔ nil;
|
||||
hanoi (suc n) p1 p2 p3 ≔ hanoi n p1 p3 p2 ++ singleton (move p1 p2) ++ hanoi n p3 p2 p1;
|
||||
|
||||
main : IO;
|
||||
main ≔ putStrLn (unlines (map showMove (hanoi 5 left middle right)));
|
||||
|
||||
end;
|
0
examples/milestone/Hanoi/juvix.yaml
Normal file
0
examples/milestone/Hanoi/juvix.yaml
Normal file
60
examples/milestone/PascalsTriangle/PascalsTriangle.juvix
Normal file
60
examples/milestone/PascalsTriangle/PascalsTriangle.juvix
Normal file
@ -0,0 +1,60 @@
|
||||
--- Pascal's triangle is the arrangement of binomial coefficients in a triangular array.
|
||||
---
|
||||
--- The rows of the triangle are staggered so that each number can be computed as the
|
||||
--- sum of the numbers to the left and right in the previous row.
|
||||
---
|
||||
--- The function ;pascal; produces the triangle to a given depth.
|
||||
|
||||
module PascalsTriangle;
|
||||
|
||||
open import Stdlib.Prelude;
|
||||
|
||||
zipWith : {A : Type} → {B : Type} → {C : Type} → (A → B → C) -> List A → List B → List C;
|
||||
zipWith f nil _ ≔ nil;
|
||||
zipWith f _ nil ≔ nil;
|
||||
zipWith f (x ∷ xs) (y ∷ ys) ≔ f x y ∷ zipWith f xs ys;
|
||||
|
||||
--- Return a list of repeated applications of a given function
|
||||
iterate : {A : Type} → ℕ → (A → A) → A → List A;
|
||||
iterate zero _ _ ≔ nil;
|
||||
iterate (suc n) f a ≔ a ∷ iterate n f (f a);
|
||||
|
||||
--- Produce a singleton List
|
||||
singleton : {A : Type} → A → List A;
|
||||
singleton a ≔ a ∷ nil;
|
||||
|
||||
--- Concatenation of ;String;s
|
||||
infixr 5 ++str;
|
||||
axiom ++str : String → String → String;
|
||||
compile ++str {
|
||||
c ↦ "concat";
|
||||
};
|
||||
|
||||
--- Concatenates a list of strings
|
||||
---
|
||||
--- ;concat (("a" ∷ nil) ∷ ("b" ∷ nil)); evaluates to ;"a" ∷ "b" ∷ nil;
|
||||
concat : List String → String;
|
||||
concat ≔ foldl (++str) "";
|
||||
|
||||
intercalate : String → List String → String;
|
||||
intercalate sep xs ≔ concat (intersperse sep xs);
|
||||
|
||||
--- Joins a list of strings with the newline character
|
||||
unlines : List String → String;
|
||||
unlines ≔ intercalate "\n";
|
||||
|
||||
showList : List ℕ → String;
|
||||
showList xs ≔ "[" ++str intercalate "," (map natToStr xs) ++str "]";
|
||||
|
||||
--- Compute the next row of Pascal's triangle
|
||||
pascalNextRow : List ℕ → List ℕ;
|
||||
pascalNextRow row ≔ zipWith (+) (singleton zero ++ row) (row ++ singleton zero);
|
||||
|
||||
--- Produce Pascal's triangle to a given depth
|
||||
pascal : ℕ → List (List ℕ);
|
||||
pascal rows ≔ iterate rows pascalNextRow (singleton one);
|
||||
|
||||
main : IO;
|
||||
main ≔ putStrLn (unlines (map showList (pascal 10)));
|
||||
|
||||
end;
|
0
examples/milestone/PascalsTriangle/juvix.yaml
Normal file
0
examples/milestone/PascalsTriangle/juvix.yaml
Normal file
@ -41,5 +41,7 @@ tests =
|
||||
[ ExampleTest "Validity Predicate example" "ValidityPredicates" "Tests.juvix" "ValidityPredicates" "" (WASI StdlibInclude),
|
||||
ExampleTest "TicTacToe CLI example" "TicTacToe" "CLI/TicTacToe.juvix" "TicTacToe" "aaa\n0\n10\n1\n2\n3\n3\n4\n5\n6\n7\n8\n9\n" (WASI StdlibInclude),
|
||||
ExampleTest "Fibonacci example" "Fibonacci" "Fibonacci.juvix" "Fibonacci" "" (WASI StdlibInclude),
|
||||
ExampleTest "Collatz sequence generator" "Collatz" "Collatz.juvix" "Collatz" "123\n" (WASI StdlibInclude)
|
||||
ExampleTest "Collatz sequence generator" "Collatz" "Collatz.juvix" "Collatz" "123\n" (WASI StdlibInclude),
|
||||
ExampleTest "Towers of Hanoi" "Hanoi" "Hanoi.juvix" "Hanoi" "" (WASI StdlibInclude),
|
||||
ExampleTest "Pascal's triangle" "PascalsTriangle" "PascalsTriangle.juvix" "PascalsTriangle" "" (WASI StdlibInclude)
|
||||
]
|
||||
|
31
tests/examplesExpected/Hanoi/expected.golden
Normal file
31
tests/examplesExpected/Hanoi/expected.golden
Normal file
@ -0,0 +1,31 @@
|
||||
left -> middle
|
||||
left -> right
|
||||
middle -> right
|
||||
left -> middle
|
||||
right -> left
|
||||
right -> middle
|
||||
left -> middle
|
||||
left -> right
|
||||
middle -> right
|
||||
middle -> left
|
||||
right -> left
|
||||
middle -> right
|
||||
left -> middle
|
||||
left -> right
|
||||
middle -> right
|
||||
left -> middle
|
||||
right -> left
|
||||
right -> middle
|
||||
left -> middle
|
||||
right -> left
|
||||
middle -> right
|
||||
middle -> left
|
||||
right -> left
|
||||
right -> middle
|
||||
left -> middle
|
||||
left -> right
|
||||
middle -> right
|
||||
left -> middle
|
||||
right -> left
|
||||
right -> middle
|
||||
left -> middle
|
10
tests/examplesExpected/PascalsTriangle/expected.golden
Normal file
10
tests/examplesExpected/PascalsTriangle/expected.golden
Normal file
@ -0,0 +1,10 @@
|
||||
[1]
|
||||
[1,1]
|
||||
[1,2,1]
|
||||
[1,3,3,1]
|
||||
[1,4,6,4,1]
|
||||
[1,5,10,10,5,1]
|
||||
[1,6,15,20,15,6,1]
|
||||
[1,7,21,35,35,21,7,1]
|
||||
[1,8,28,56,70,56,28,8,1]
|
||||
[1,9,36,84,126,126,84,36,9,1]
|
Loading…
Reference in New Issue
Block a user