chore: Version 0.3 #136

Open
aniva wants to merge 487 commits from dev into main
4 changed files with 64 additions and 26 deletions
Showing only changes of commit 0f946880ae - Show all commits

View File

@ -55,7 +55,7 @@ and when unpickling, we build a fresh `Environment` from the imports,
and then add the new constants. and then add the new constants.
-/ -/
@[export pantograph_env_pickle_m] @[export pantograph_env_pickle_m]
def env_pickle (env : Environment) (path : System.FilePath) : IO Unit := def environmentPickle (env : Environment) (path : System.FilePath) : IO Unit :=
Pantograph.pickle path (env.header.imports, env.constants.map₂) Pantograph.pickle path (env.header.imports, env.constants.map₂)
/-- /--
@ -65,7 +65,7 @@ We construct a fresh `Environment` with the relevant imports,
and then replace the new constants. and then replace the new constants.
-/ -/
@[export pantograph_env_unpickle_m] @[export pantograph_env_unpickle_m]
def env_unpickle (path : System.FilePath) : IO (Environment × CompactedRegion) := unsafe do def environmentUnpickle (path : System.FilePath) : IO (Environment × CompactedRegion) := unsafe do
let ((imports, map₂), region) ← Pantograph.unpickle (Array Import × PHashMap Name ConstantInfo) path let ((imports, map₂), region) ← Pantograph.unpickle (Array Import × PHashMap Name ConstantInfo) path
let env ← importModules imports {} 0 let env ← importModules imports {} 0
return (← env.replay (Std.HashMap.ofList map₂.toList), region) return (← env.replay (Std.HashMap.ofList map₂.toList), region)

View File

@ -90,10 +90,10 @@ def execute (command: Protocol.Command): MainM Lean.Json := do
Environment.addDecl args Environment.addDecl args
env_save (args: Protocol.EnvSaveLoad): MainM (CR Protocol.EnvSaveLoadResult) := do env_save (args: Protocol.EnvSaveLoad): MainM (CR Protocol.EnvSaveLoadResult) := do
let env ← Lean.MonadEnv.getEnv let env ← Lean.MonadEnv.getEnv
env_pickle env args.path environmentPickle env args.path
return .ok {} return .ok {}
env_load (args: Protocol.EnvSaveLoad): MainM (CR Protocol.EnvSaveLoadResult) := do env_load (args: Protocol.EnvSaveLoad): MainM (CR Protocol.EnvSaveLoadResult) := do
let (env, _) ← env_unpickle args.path let (env, _) ← environmentUnpickle args.path
Lean.setEnv env Lean.setEnv env
return .ok {} return .ok {}
expr_echo (args: Protocol.ExprEcho): MainM (CR Protocol.ExprEchoResult) := do expr_echo (args: Protocol.ExprEcho): MainM (CR Protocol.ExprEchoResult) := do

View File

@ -128,6 +128,13 @@ abbrev TestT := StateT LSpec.TestSeq
def addTest [Monad m] (test: LSpec.TestSeq) : TestT m Unit := do def addTest [Monad m] (test: LSpec.TestSeq) : TestT m Unit := do
set $ (← get) ++ test set $ (← get) ++ test
def checkEq [Monad m] [DecidableEq α] (desc : String) (lhs rhs : α) : TestT m Unit := do
addTest $ LSpec.check desc (lhs == rhs)
def checkTrue [Monad m] (desc : String) (flag : Bool) : TestT m Unit := do
addTest $ LSpec.check desc flag
def fail [Monad m] (desc : String) : TestT m Unit := do
addTest $ LSpec.check desc false
def runTest [Monad m] (t: TestT m Unit): m LSpec.TestSeq := def runTest [Monad m] (t: TestT m Unit): m LSpec.TestSeq :=
Prod.snd <$> t.run LSpec.TestSeq.done Prod.snd <$> t.run LSpec.TestSeq.done
def runTestWithResult { α } [Monad m] (t: TestT m α): m (α × LSpec.TestSeq) := def runTestWithResult { α } [Monad m] (t: TestT m α): m (α × LSpec.TestSeq) :=

View File

@ -7,30 +7,60 @@ open Lean
namespace Pantograph.Test.Serial namespace Pantograph.Test.Serial
def tempPath : IO System.FilePath := do
Prod.snd <$> IO.FS.createTempFile
structure MultiState where structure MultiState where
coreContext : Core.Context coreContext : Core.Context
coreStates : Array Core.State env: Environment
abbrev TestM := StateRefT MultiState $ TestT $ EIO LSpec.TestSeq abbrev TestM := TestT $ StateRefT MultiState $ IO
def runCoreM { α } (id : Nat) (testCoreM: TestT CoreM α) : TestM α := do instance : MonadEnv TestM where
let multiState ← get getEnv := return (← getThe MultiState).env
let state ← match multiState.coreStates[id]? with modifyEnv f := do modifyThe MultiState fun s => { s with env := f s.env }
| .some state => pure state
| .none => def runCoreM { α } (state : Core.State) (testCoreM : TestT CoreM α) : TestM (α × Core.State) := do
let test := LSpec.test "Invalid index" (id < multiState.coreStates.size) let multiState ← getThe MultiState
throw test
let coreM := runTestWithResult testCoreM let coreM := runTestWithResult testCoreM
match ← (coreM.run' multiState.coreContext state).toBaseIO with match ← (coreM.run multiState.coreContext state).toBaseIO with
| .error _ => do | .error e => do
let test := LSpec.test "Exception" false throw $ .userError $ ← e.toMessageData.toString
throw test | .ok ((a, tests), state') => do
| .ok (a, tests) => do
set $ (← getThe LSpec.TestSeq) ++ tests set $ (← getThe LSpec.TestSeq) ++ tests
return a return (a, state')
def simple : TestM Unit := do def test_environment_pickling : TestM Unit := do
return let stateSrc: Core.State := { env := ← getEnv }
let stateDst: Core.State := { env := ← getEnv }
let name := `mystery
let envPicklePath ← tempPath
let ((), _) ← runCoreM stateSrc do
let type: Expr := .forallE `p (.sort 0) (.forallE `h (.bvar 0) (.bvar 1) .default) .default
let value: Expr := .lam `p (.sort 0) (.lam `h (.bvar 0) (.bvar 0) .default) .default
let c := Lean.Declaration.defnDecl <| Lean.mkDefinitionValEx
(name := name)
(levelParams := [])
(type := type)
(value := value)
(hints := Lean.mkReducibilityHintsRegularEx 1)
(safety := Lean.DefinitionSafety.safe)
(all := [])
let env' ← match (← getEnv).addDecl (← getOptions) c with
| .error e => do
let error ← (e.toMessageData (← getOptions)).toString
throwError error
| .ok env' => pure env'
environmentPickle env' envPicklePath
let _ ← runCoreM stateDst do
let (env', _) ← environmentUnpickle envPicklePath
checkTrue s!"Has symbol {name}" (env'.find? name).isSome
let anotherName := `mystery2
checkTrue s!"Doesn't have symbol {anotherName}" (env'.find? anotherName).isNone
IO.FS.removeFile envPicklePath
structure Test where structure Test where
name : String name : String
@ -41,15 +71,16 @@ protected def Test.run (test: Test) (env: Lean.Environment) : IO LSpec.TestSeq :
-- Create the state -- Create the state
let state : MultiState := { let state : MultiState := {
coreContext := ← createCoreContext #[], coreContext := ← createCoreContext #[],
coreStates := Array.range test.nInstances |>.map λ _ => { env }, env,
} }
match ← (runTest $ test.routine.run' state).toBaseIO with match ← ((runTest $ test.routine).run' state).toBaseIO with
| .ok e => return e | .ok e => return e
| .error e => return e | .error e =>
return LSpec.check "Emitted exception" (e.toString == "")
def suite (env : Lean.Environment): List (String × IO LSpec.TestSeq) := def suite (env : Lean.Environment): List (String × IO LSpec.TestSeq) :=
let tests: List Test := [ let tests: List Test := [
{ name := "simple", nInstances := 2, routine := simple } { name := "environment_pickling", nInstances := 2, routine := test_environment_pickling },
] ]
tests.map (fun test => (test.name, test.run env)) tests.map (fun test => (test.name, test.run env))