Skip to content

Commit

Permalink
handler2 - new custom method SubstituteRedex
Browse files Browse the repository at this point in the history
  • Loading branch information
Vince committed Feb 20, 2024
1 parent d44bc48 commit 6c14663
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Server/Handler2/CustomMethod.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import qualified Server.Handler2.CustomMethod.Inspect as Inspect (handler)
import qualified Server.Handler2.CustomMethod.Refine as Refine (slowHandler)
import qualified Server.Handler2.CustomMethod.InsertProofTemplate
as InsertProofTemplate (slowHandler)
import qualified Server.Handler2.CustomMethod.SubstituteRedex
as SubstituteRedex (handler)
import qualified Server.Handler2.CustomMethod.HelloWorld as HelloWorld (handler)


Expand All @@ -42,6 +44,7 @@ handler params responder = do
ReqInspect range -> Inspect.handler range respondResult respondError
ReqRefine' range text -> Refine.slowHandler range text respondResult respondError
ReqInsertProofTemplate range hash -> InsertProofTemplate.slowHandler filePath range hash respondResult respondError
ReqSubstitute redexNumber -> SubstituteRedex.handler redexNumber respondResult respondError
ReqHelloWorld range -> HelloWorld.handler range respondResult respondError
_ -> respondError (Others "Not implemented yet.")
where
Expand Down
36 changes: 36 additions & 0 deletions src/Server/Handler2/CustomMethod/SubstituteRedex.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

module Server.Handler2.CustomMethod.SubstituteRedex (handler) where

import qualified Syntax.Abstract as A
import Error (Error(..))

import Server.Monad (ServerM, LoadedProgram(..))
import Server.CustomMethod (ResKind(..))
import Server.Handler2.Utils
import Server.Handler2.CustomMethod.Utils


import qualified GCL.Substitution as Substitution

handler :: FilePath -> Int -> ([ResKind] -> ServerM ()) -> (Error -> ServerM ()) -> ServerM ()
handler filePath redexNumber onFinish onError = do
maybeLoadedProgram <- dumpProgram filePath
case maybeLoadedProgram of
Nothing -> onError (Others "Please reload before inspect.")
Just loadedProgram -> do
let redexes :: IntMap (Int, A.Expr) = _redexes loadedProgram
let abstract :: A.Program = _abstractProgram loadedProgram
let variableCounter :: Int = _variableCounter loadedProgram
case IntMap.lookup redexNumber redexes of
Nothing -> onError (Others "Redex not found.")
Just (_, redex) -> do
let scope :: Map Text (Maybe Expr) = A.programToScopeForSubstitution abstract
let (newExpr, variableCounter') =
runState (Substitution.step scope redex) variableCounter
let redexesInNewExpr = Substitution.buildRedexMap newExpr
let loadedProgram' = loadedProgram
{ _variableCounter = variableCounter'
, _redexes = redexes <> redexesInNewExpr
}
_ <- cacheLoadedProgram filePath loadedProgram'
onFinish [ResSubstitute i (render newExpr)]

0 comments on commit 6c14663

Please sign in to comment.