-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: upstream mathlib definitions used for
ImportGraph
(#481)
- Loading branch information
1 parent
0967fe3
commit 2277a72
Showing
4 changed files
with
104 additions
and
0 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,41 @@ | ||
/- | ||
Copyright (c) 2023 Scott Morrison. All rights reserved. | ||
Released under Apache 2.0 license as described in the file LICENSE. | ||
Authors: Scott Morrison | ||
-/ | ||
|
||
/-! | ||
# Running external commands. | ||
-/ | ||
|
||
namespace IO.Process | ||
|
||
open System (FilePath) | ||
|
||
/-- | ||
Pipe `input` into stdin of the spawned process, | ||
then return `(exitCode, stdout, stdErr)` upon completion. | ||
-/ | ||
def runCmdWithInput' (cmd : String) (args : Array String) | ||
(input : String := "") (throwFailure := true) : IO Output := do | ||
let child ← spawn | ||
{ cmd := cmd, args := args, stdin := .piped, stdout := .piped, stderr := .piped } | ||
let (stdin, child) ← child.takeStdin | ||
stdin.putStr input | ||
stdin.flush | ||
let stdout ← IO.asTask child.stdout.readToEnd Task.Priority.dedicated | ||
let err ← child.stderr.readToEnd | ||
let exitCode ← child.wait | ||
if exitCode != 0 && throwFailure then | ||
throw $ IO.userError err | ||
else | ||
let out ← IO.ofExcept stdout.get | ||
return ⟨exitCode, out, err⟩ | ||
|
||
/-- | ||
Pipe `input` into stdin of the spawned process, | ||
then return the entire content of stdout as a `String` upon completion. | ||
-/ | ||
def runCmdWithInput (cmd : String) (args : Array String) | ||
(input : String := "") (throwFailure := true) : IO String := do | ||
return (← runCmdWithInput' cmd args input throwFailure).stdout |
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 @@ | ||
/- | ||
Copyright (c) 2023 Jon Eugster. All rights reserved. | ||
Released under Apache 2.0 license as described in the file LICENSE. | ||
Authors: Jon Eugster | ||
-/ | ||
import Lean.Data.NameMap | ||
|
||
/-! | ||
# Additional functions on `Lean.NameMap`. | ||
We provide `NameMap.filter` and `NameMap.filterMap`. | ||
-/ | ||
|
||
namespace Lean.NameMap | ||
|
||
instance : ForIn m (NameMap β) (Name × β) := | ||
inferInstanceAs <| ForIn m (RBMap Name β _) _ | ||
|
||
/-- | ||
`filter f m` returns the `NameMap` consisting of all | ||
"`key`/`val`"-pairs in `m` where `f key val` returns `true`. | ||
-/ | ||
def filter (f : Name → α → Bool) (m : NameMap α) : NameMap α := | ||
m.fold process {} | ||
where | ||
/-- see `Lean.NameMap.filter` -/ | ||
process (r : NameMap α) (n : Name) (i : α) := | ||
if f n i then r.insert n i else r | ||
|
||
/-- | ||
`filterMap f m` allows to filter a `NameMap` and simultaneously modify the filtered values. | ||
It takes a function `f : Name → α → Option β` and applies `f name` to the value with key `name`. | ||
The resulting entries with non-`none` value are collected to form the output `NameMap`. | ||
-/ | ||
def filterMap (f : Name → α → Option β) (m : NameMap α) : NameMap β := | ||
m.fold process {} | ||
where | ||
/-- see `Lean.NameMap.filterMap` -/ | ||
process (r : NameMap β) (n : Name) (i : α) := | ||
match f n i with | ||
| none => r | ||
| some b => r.insert n b |
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,17 @@ | ||
/- | ||
Copyright (c) 2023 Scott Morrison. All rights reserved. | ||
Released under Apache 2.0 license as described in the file LICENSE. | ||
Authors: Scott Morrison | ||
-/ | ||
import Lean.Data.SMap | ||
|
||
/-! | ||
# Extra functions on Lean.SMap | ||
-/ | ||
|
||
set_option autoImplicit true | ||
|
||
/-- Monadic fold over a staged map. -/ | ||
def Lean.SMap.foldM {m : Type w → Type w} [Monad m] [BEq α] [Hashable α] | ||
(f : σ → α → β → m σ) (init : σ) (map : SMap α β) : m σ := do | ||
map.map₂.foldlM f (← map.map₁.foldM f init) |