-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Small changes for the presentation (#1456)
- Loading branch information
1 parent
963d488
commit c6a43e6
Showing
13 changed files
with
202 additions
and
176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
module Logic.Board; | ||
|
||
open import Stdlib.Prelude; | ||
open import Logic.Square public; | ||
open import Logic.Symbol public; | ||
open import Logic.Extra; | ||
|
||
--- A 3x3 grid of ;Square;s | ||
inductive Board { | ||
board : List (List Square) → Board; | ||
}; | ||
|
||
--- Returns the list of numbers corresponding to the empty ;Square;s | ||
possibleMoves : List Square → List ℕ; | ||
possibleMoves nil ≔ nil; | ||
possibleMoves ((empty n) ∷ xs) ≔ n ∷ possibleMoves xs; | ||
possibleMoves (_ ∷ xs) ≔ possibleMoves xs; | ||
|
||
--- ;true; if all the ;Square;s in the list are equal | ||
full : List Square → Bool; | ||
full (a ∷ b ∷ c ∷ nil) ≔ (==Square a b) && (==Square b c); | ||
|
||
diagonals : List (List Square) → List (List Square); | ||
diagonals ((a1 ∷ _ ∷ b1 ∷ nil) ∷ (_ ∷ c ∷ _ ∷ nil) ∷ (b2 ∷ _ ∷ a2 ∷ nil) ∷ nil) ≔ (a1 ∷ c ∷ a2 ∷ nil) ∷ (b1 ∷ c ∷ b2 ∷ nil) ∷ nil; | ||
|
||
columns : List (List Square) → List (List Square); | ||
columns ≔ transpose; | ||
|
||
rows : List (List Square) → List (List Square); | ||
rows ≔ id; | ||
|
||
--- Textual representation of a ;List Square; | ||
showRow : List Square → String; | ||
showRow xs ≔ concat (surround "|" (map showSquare xs)); | ||
|
||
showBoard : Board → String; | ||
showBoard (board squares) ≔ unlines (surround "+---+---+---+" (map showRow squares)); | ||
|
||
end; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
--- Some generic helper definitions. | ||
module Logic.Extra; | ||
|
||
open import Stdlib.Data.Nat.Ord; | ||
open import Stdlib.Prelude; | ||
|
||
infixr 5 ++str; | ||
--- Primitive concatenation of ;String;s | ||
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) ""; | ||
|
||
--- It inserts the first ;String; at the beginning, in between, and at the end of the second list | ||
surround : String → List String → List String; | ||
surround x xs ≔ (x ∷ intersperse x xs) ++ (x ∷ nil); | ||
|
||
--- It inserts the first ;String; in between the ;String;s in the second list and concatenates the result | ||
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"; | ||
|
||
end; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
module Logic.GameState; | ||
|
||
open import Stdlib.Prelude; | ||
open import Logic.Extra; | ||
open import Logic.Board; | ||
|
||
inductive Error { | ||
--- no error occurred | ||
noError : Error; | ||
--- a non-fatal error occurred | ||
continue : String → Error; | ||
--- a fatal occurred | ||
terminate : String → Error; | ||
}; | ||
|
||
inductive GameState { | ||
state : Board → Symbol → Error → GameState; | ||
}; | ||
|
||
--- Textual representation of a ;GameState; | ||
showGameState : GameState → String; | ||
showGameState (state b _ _) ≔ showBoard b; | ||
|
||
--- Projects the player | ||
player : GameState → Symbol; | ||
player (state _ p _) ≔ p; | ||
|
||
--- initial ;GameState; | ||
beginState : GameState; | ||
beginState ≔ state | ||
(board (map (map empty) ((one ∷ two ∷ three ∷ nil) ∷ (four ∷ five ∷ six ∷ nil) ∷ (seven ∷ eight ∷ nine ∷ nil) ∷ nil))) | ||
X | ||
noError; | ||
|
||
--- ;true; if some player has won the game | ||
won : GameState → Bool; | ||
won (state (board squares) _ _) ≔ any full (diagonals squares ++ rows squares ++ columns squares); | ||
|
||
--- ;true; if there is a draw | ||
draw : GameState → Bool; | ||
draw (state (board squares) _ _) ≔ null (possibleMoves (flatten squares)); | ||
|
||
end; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
module Logic.Square; | ||
|
||
open import Stdlib.Prelude; | ||
open import Logic.Symbol; | ||
open import Stdlib.Data.Nat.Ord; | ||
open import Logic.Extra; | ||
|
||
--- A square is each of the holes in a board | ||
inductive Square { | ||
--- An empty square has a ;ℕ; that uniquely identifies it | ||
empty : ℕ → Square; | ||
--- An occupied square has a ;Symbol; in it | ||
occupied : Symbol → Square; | ||
}; | ||
|
||
--- Equality for ;Square;s | ||
==Square : Square → Square → Bool; | ||
==Square (empty m) (empty n) ≔ m == n; | ||
==Square (occupied s) (occupied t) ≔ ==Symbol s t; | ||
==Square _ _ ≔ false; | ||
|
||
--- Textual representation of a ;Square; | ||
showSquare : Square → String; | ||
showSquare (empty n) ≔ " " ++str natToStr n ++str " "; | ||
showSquare (occupied s) ≔ " " ++str showSymbol s ++str " "; | ||
|
||
replace : Symbol → ℕ → Square → Square; | ||
replace player k (empty n) ≔ if (n Stdlib.Data.Nat.Ord.== k) (occupied player) (empty n); | ||
replace _ _ s ≔ s; | ||
|
||
|
||
end; |
Oops, something went wrong.