GithubHelp home page GithubHelp logo

dwrensha / lean4-maze Goto Github PK

View Code? Open in Web Editor NEW
43.0 2.0 4.0 92 KB

maze game encoded in Lean 4 syntax

Home Page: https://live.lean-lang.org/#url=https%3A%2F%2Fraw.githubusercontent.com%2Fdwrensha%2Flean4-maze%2Fmain%2FMaze.lean

License: Apache License 2.0

Lean 100.00%
lean4

lean4-maze's Introduction

lean4-maze

This repo shows how maze solving can be encoded as theorem proving using the Lean 4 programming language.

It draws inspiration from https://github.com/kbuzzard/maze-game.

Setup

On The Web

Try it in your browser on the Lean 4 Playground.

Locally

First, install Lean 4 on your computer: https://leanprover.github.io/lean4/doc/setup.html

Then open Maze.lean in emacs or VSCode.

Playing

You can define a maze like this:

def maze := ┌────────┐
            │▓▓▓▓▓▓▓▓│
            │▓░▓@▓░▓▓│
            │▓░▓░░░▓▓│
            │▓░░▓░▓▓▓│
            │▓▓░▓░▓░░│
            │▓░░░░▓░▓│
            │▓░▓▓▓▓░▓│
            │▓░░░░░░▓│
            │▓▓▓▓▓▓▓▓│
            └────────┘

The @ symbol denotes your current location. You are free to move within the cells. The cells are walls.

Your goal is to escape the maze at any of its borders.

You can interactively solve a maze like this:

example : can_escape maze :=
 by south
    east
    south
    south

As you make progress, Lean's goal view will display your current state. For example, after the moves made above, the state is shown as:

⊢ can_escape
    (
        ┌────────┐
        │▓▓▓▓▓▓▓▓│
        │▓░▓░▓░▓▓│
        │▓░▓░░░▓▓│
        │▓░░▓░▓▓▓│
        │▓▓░▓@▓░░│
        │▓░░░░▓░▓│
        │▓░▓▓▓▓░▓│
        │▓░░░░░░▓│
        │▓▓▓▓▓▓▓▓│
        └────────┘
        )

The main moves available to you at any point are north, south, east, and west.

When you reach the boundary, you can finish your proof with out.

how does it work?

As you traverse a maze, you are constructing a proof that the maze satisfies the can_escape predicate, defined as

def can_escape (state : GameState) : Prop :=
  ∃ (gs : List Move), is_win (List.foldl make_move state gs)

The mazes as drawn above are actual valid Lean 4 syntax!

We define new syntax categories and some macro_rules for elaborating them into valid values.

To get Lean to render the values back in the above format, we define a delaboration function and register it with the pretty printer.

Lean 4 lets us do all of this in-line, in ordinary Lean 4 code.

lean4-maze's People

Contributors

dwrensha avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

lean4-maze's Issues

Last maze triggers simp heartbeat error

Hello! I was playing around a bit after today's talk and tried to bored-ly solve the long maze just to pass some time.

I'm not sure if there's anything to fix really, but just to note, if you do:

example : can_escape maze3 :=
 by west
    west
    west
    south
    south
    south
    south
    east
    east
    north
    north
    east
    east
    south
    south
    south
    south
    south
    south
    south
    south
    west
    west
    west
    west
    west
    west
    west
    west
    west
    west
    west
    west
    west
    west
    west
    west
    west
    west
    west
    west
    west
    west
    west
    north
    north
    north
    north
    north
    north
    north
    north
    east
    east
    south
    south
    south
    south
    south
    south
    east
    east
    east
    east
    north
    north
    north
    north
    east
    east
    east
    north
    north
    east
    east
    east
    east
    east
    east

the last east says cannot move east, even though clearly you can from the picture.

The issue seems to be simp failing -- if I apply step_east and then simp, the infoview says:

▶ 473:5-473:9: error:
tactic 'simp' failed, nested error:
(deterministic) timeout at 'simp', maximum number of heartbeats (200000) has been reached (use 'set_option maxHeartbeats <num>' to set the limit)

and indeed set_option maxHeartbeats 999999999 fixes the issue (lets me east).

Not sure as I say that there's anything that needs tweaking, I take it changing the default globally is silly, but yeah just sharing :)

Thanks for the fun.

unable to update to lean nightly 2021-11-25

This works in nightly 2021-11-24:

def maze1 := ┌───┐
             │▓▓▓│
             │░@▓│
             │▓▓▓│
             └───┘

example : can_escape maze1 := by
  apply step_west
  simp
  out

but in 2021-11-25, the simp step errors out with:

application type mismatch
  Eq.ndrec
    (Eq.refl
      (update_state_with_row_aux 0 (0 + 1) [CellContents.wall, CellContents.wall]
          (game_state_from_cells_aux { x := 3, y := 3 } (0 + 1)
            [[CellContents.empty, CellContents.player, CellContents.wall],
              [CellContents.wall, CellContents.wall, CellContents.wall]])).position)
argument has type
  (update_state_with_row_aux 0 (0 + 1) [CellContents.wall, CellContents.wall]
        (game_state_from_cells_aux { x := 3, y := 3 } (0 + 1)
          [[CellContents.empty, CellContents.player, CellContents.wall],
            [CellContents.wall, CellContents.wall, CellContents.wall]])).position =
    (update_state_with_row_aux 0 (0 + 1) [CellContents.wall, CellContents.wall]
        (game_state_from_cells_aux { x := 3, y := 3 } (0 + 1)
          [[CellContents.empty, CellContents.player, CellContents.wall],
            [CellContents.wall, CellContents.wall, CellContents.wall]])).position
but function has type
  (fun s =>
        (update_state_with_row_aux 0 (0 + 1) [CellContents.wall, CellContents.wall]
                (game_state_from_cells_aux { x := 3, y := 3 } (0 + 1)
                  [[CellContents.empty, CellContents.player, CellContents.wall],
                    [CellContents.wall, CellContents.wall, CellContents.wall]])).position.2 =
          s.2)
      (update_state_with_row_aux 0 (0 + 1) [CellContents.wall, CellContents.wall]
          (game_state_from_cells_aux { x := 3, y := 3 } (0 + 1)
            [[CellContents.empty, CellContents.player, CellContents.wall],
              [CellContents.wall, CellContents.wall, CellContents.wall]])).position →
    ∀ {b : Coords},
      (update_state_with_row_aux 0 (0 + 1) [CellContents.wall, CellContents.wall]
              (game_state_from_cells_aux { x := 3, y := 3 } (0 + 1)
                [[CellContents.empty, CellContents.player, CellContents.wall],
                  [CellContents.wall, CellContents.wall, CellContents.wall]])).position =
          b →
        (fun s =>
            (update_state_with_row_aux 0 (0 + 1) [CellContents.wall, CellContents.wall]
                    (game_state_from_cells_aux { x := 3, y := 3 } (0 + 1)
                      [[CellContents.empty, CellContents.player, CellContents.wall],
                        [CellContents.wall, CellContents.wall, CellContents.wall]])).position.2 =
              s.2)

One of these commits must be to blame:
leanprover/lean4@9a939f9
leanprover/lean4@f7decd2
leanprover/lean4@17f99e3
leanprover/lean4@292d321
leanprover/lean4@a8f4146

On a cursory glance, the last one seems most suspicious to me.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.