-
Notifications
You must be signed in to change notification settings - Fork 26
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
20 changed files
with
399 additions
and
51 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
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
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
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,80 @@ | ||
/- | ||
Copyright (c) 2024 Jannis Limperg. All rights reserved. | ||
Released under Apache 2.0 license as described in the file LICENSE. | ||
Authors: Jannis Limperg | ||
-/ | ||
|
||
import Batteries.Data.HashMap | ||
|
||
open Lean Lean.Meta | ||
|
||
namespace Aesop | ||
|
||
structure FVarIdSubst where | ||
map : Batteries.HashMap FVarId FVarId | ||
deriving Inhabited | ||
|
||
namespace FVarIdSubst | ||
|
||
def isEmpty (s : FVarIdSubst) : Bool := | ||
s.map.isEmpty | ||
|
||
def contains (s : FVarIdSubst) (fvarId : FVarId) : Bool := | ||
s.map.contains fvarId | ||
|
||
def find? (s : FVarIdSubst) (fvarId : FVarId) : Option FVarId := | ||
s.map.find? fvarId | ||
|
||
def get (s : FVarIdSubst) (fvarId : FVarId) : FVarId := | ||
s.find? fvarId |>.getD fvarId | ||
|
||
def apply (s : FVarIdSubst) (e : Expr) : Expr := | ||
if s.isEmpty || ! e.hasFVar then | ||
e | ||
else | ||
e.replace λ | ||
| .fvar fvarId => s.find? fvarId |>.map mkFVar | ||
| _ => none | ||
|
||
def applyToLocalDecl (s : FVarIdSubst) : LocalDecl → LocalDecl | ||
| .cdecl i id n t bi k => .cdecl i id n (s.apply t) bi k | ||
| .ldecl i id n t v nd k => .ldecl i id n (s.apply t) (s.apply v) nd k | ||
|
||
def domain (s : FVarIdSubst) : HashSet FVarId := | ||
s.map.fold (init := ∅) λ r k _ => r.insert k | ||
|
||
def codomain (s : FVarIdSubst) : HashSet FVarId := | ||
s.map.fold (init := ∅) λ r _ v => r.insert v | ||
|
||
protected def empty : FVarIdSubst := | ||
⟨∅⟩ | ||
|
||
instance : EmptyCollection FVarIdSubst := | ||
⟨.empty⟩ | ||
|
||
def insert (s : FVarIdSubst) (old new : FVarId) : FVarIdSubst := | ||
let map : Batteries.HashMap _ _ := s.map.mapVal λ _ v => | ||
if v == old then new else v | ||
⟨map.insert old new⟩ | ||
|
||
def erase (s : FVarIdSubst) (fvarId : FVarId) : FVarIdSubst := | ||
⟨s.map.erase fvarId⟩ | ||
|
||
def append (s t : FVarIdSubst) : FVarIdSubst := | ||
let map := s.map.mapVal λ _ v => t.get v | ||
⟨t.map.fold (init := map) λ s k v => s.insert k v⟩ | ||
|
||
def ofFVarSubstIgnoringNonFVarIds (s : FVarSubst) : FVarIdSubst := .mk $ | ||
s.map.foldl (init := ∅) λ map k v => | ||
if let .fvar fvarId := v then map.insert k fvarId else map | ||
|
||
def ofFVarSubst? (s : FVarSubst) : Option FVarIdSubst := Id.run do | ||
let mut result := ∅ | ||
for (k, v) in s.map do | ||
if let .fvar fvarId := v then | ||
result := result.insert k fvarId | ||
else | ||
return none | ||
return some result | ||
|
||
end Aesop.FVarIdSubst |
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
Oops, something went wrong.