-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
252 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/- | ||
Copyright (c) 2024 Lean FRO, LLC. All rights reserved. | ||
Released under Apache 2.0 license as described in the file LICENSE. | ||
Authors: Kim Morrison | ||
-/ | ||
|
||
namespace EStateM | ||
|
||
namespace Result | ||
|
||
/-- Map a function over an `EStateM.Result`, preserving states and errors. -/ | ||
def map {ε σ α β : Type u} (f : α → β) (x : Result ε σ α) : Result ε σ β := | ||
match x with | ||
| .ok a s' => .ok (f a) s' | ||
| .error e s' => .error e s' | ||
|
||
@[simp] theorem map_ok {ε σ α β : Type u} (f : α → β) (a : α) (s : σ) : | ||
(Result.ok a s : Result ε σ α).map f = .ok (f a) s := rfl | ||
|
||
@[simp] theorem map_error {ε σ α β : Type u} (f : α → β) (e : ε) (s : σ) : | ||
(Result.error e s : Result ε σ α).map f = .error e s := rfl | ||
|
||
@[simp] theorem map_eq_ok {ε σ α β : Type u} (f : α → β) (x : Result ε σ α) (b : β) (s : σ) : | ||
x.map f = .ok b s ↔ ∃ a, x = .ok a s ∧ b = f a := by | ||
cases x <;> simp [and_assoc, and_comm, eq_comm] | ||
|
||
@[simp] theorem map_eq_error {ε σ α β : Type u} (f : α → β) (x : Result ε σ α) (e : ε) (s : σ) : | ||
x.map f = .error e s ↔ x = .error e s := by | ||
cases x <;> simp [eq_comm] | ||
|
||
end Result | ||
|
||
@[simp] theorem run_map (f : α → β) (x : EStateM ε σ α) : | ||
(f <$> x).run s = (x.run s).map f := rfl | ||
|
||
@[ext] theorem ext {ε σ α : Type u} (x y : EStateM ε σ α) (h : ∀ s, x.run s = y.run s) : x = y := by | ||
funext s | ||
exact h s | ||
|
||
end EStateM |
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,28 @@ | ||
/- | ||
Copyright (c) 2024 Lean FRO, LLC. All rights reserved. | ||
Released under Apache 2.0 license as described in the file LICENSE. | ||
Authors: Kim Morrison | ||
-/ | ||
import Batteries.Classes.SatisfiesM | ||
import Lean.Elab.Command | ||
|
||
/-! | ||
# Construct `Satisfying` instances for the Lean monad stack. | ||
-/ | ||
|
||
open Lean Elab Term Tactic Command | ||
|
||
instance : Satisfying CoreM := | ||
inferInstanceAs <| Satisfying (ReaderT _ <| StateRefT' _ _ (EIO _)) | ||
|
||
instance : Satisfying MetaM := | ||
inferInstanceAs <| Satisfying (ReaderT _ <| StateRefT' _ _ CoreM) | ||
|
||
instance : Satisfying TermElabM := | ||
inferInstanceAs <| Satisfying (ReaderT _ <| StateRefT' _ _ MetaM) | ||
|
||
instance : Satisfying TacticM := | ||
inferInstanceAs <| Satisfying (ReaderT _ $ StateRefT' _ _ TermElabM) | ||
|
||
instance : Satisfying CommandElabM := | ||
inferInstanceAs <| Satisfying (ReaderT _ $ StateRefT' _ _ (EIO _)) |
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,21 @@ | ||
import Batteries.Lean.SatisfiesM | ||
import Batteries.Data.Array.Monadic | ||
|
||
open Lean Meta Array Elab Term Tactic Command | ||
|
||
-- For now these live in the test file, as it's not really clear we want people relying on them! | ||
instance : LawfulMonad (EIO ε) := inferInstanceAs <| LawfulMonad (EStateM _ _) | ||
instance : LawfulMonad CoreM := | ||
inferInstanceAs <| LawfulMonad (ReaderT _ <| StateRefT' _ _ (EIO Exception)) | ||
instance : LawfulMonad MetaM := | ||
inferInstanceAs <| LawfulMonad (ReaderT _ <| StateRefT' _ _ CoreM) | ||
instance : LawfulMonad TermElabM := | ||
inferInstanceAs <| LawfulMonad (ReaderT _ <| StateRefT' _ _ MetaM) | ||
instance : LawfulMonad TacticM := | ||
inferInstanceAs <| LawfulMonad (ReaderT _ $ StateRefT' _ _ $ TermElabM) | ||
instance : LawfulMonad CommandElabM := | ||
inferInstanceAs <| LawfulMonad (ReaderT _ $ StateRefT' _ _ $ EIO _) | ||
|
||
example (xs : Array Expr) : MetaM { ts : Array Expr // ts.size = xs.size } := do | ||
let r ← satisfying (xs.mapM inferType) (size_mapM _ _) | ||
return r |