Skip to content

Commit

Permalink
fix: work around leanprover#2044 in derive handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Vtec234 committed Dec 15, 2023
1 parent 7d004a8 commit 73b614d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Lean/Server/Rpc/Deriving.lean
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,13 @@ private def deriveStructureInstance (indVal : InductiveVal) (params : Array Expr

let paramIds ← params.mapM fun p => return mkIdent (← getFVarLocalDecl p).userName

let indName := mkIdent indVal.name
`(structure RpcEncodablePacket where
$[($fieldIds : $fieldTys)]*
deriving FromJson, ToJson

-- Workaround for https://github.com/leanprover/lean4/issues/2044
namespace $indName
variable $encInstBinders* in
instance : RpcEncodable (@$(mkCIdent indVal.name) $paramIds*) :=
{ rpcEncode := enc, rpcDecode := dec }
Expand All @@ -55,6 +58,7 @@ private def deriveStructureInstance (indVal : InductiveVal) (params : Array Expr
dec j := do
let a : RpcEncodablePacket ← fromJson? j
return { $[$decInits],* }
end $indName
)

private def matchAltTerm := Lean.Parser.Term.matchAlt (rhsParser := Lean.Parser.termParser)
Expand Down Expand Up @@ -92,10 +96,13 @@ private def deriveInductiveInstance (indVal : InductiveVal) (params : Array Expr
let paramIds ← params.mapM fun p => return mkIdent (← getFVarLocalDecl p).userName
let typeId ← `(@$(mkIdent indVal.name) $paramIds*)

let indName := mkIdent indVal.name
`(inductive RpcEncodablePacket where
$[$ctors:ctor]*
deriving FromJson, ToJson

-- Workaround for https://github.com/leanprover/lean4/issues/2044
namespace $indName
variable $encInstBinders* in
partial instance : RpcEncodable $typeId :=
{ rpcEncode := enc, rpcDecode := dec }
Expand All @@ -107,6 +114,7 @@ private def deriveInductiveInstance (indVal : InductiveVal) (params : Array Expr
have inst : RpcEncodable $typeId := { rpcEncode := enc, rpcDecode := dec }
let pkt : RpcEncodablePacket ← fromJson? j
id <| match pkt with $[$decodes:matchAlt]*
end $indName
)

/-- Creates an `RpcEncodablePacket` for `typeName`. For structures, the packet is a structure
Expand Down
13 changes: 13 additions & 0 deletions src/Lean/Widget/UserWidget.lean
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@ Authors: E.W.Ayers, Wojciech Nawrocki
import Lean.Elab.Eval
import Lean.Server.Rpc.RequestHandling

namespace Lean.Server
open Elab Command in
/-- Derive `Lean.Server.RpcEncodable` for a type.
TODO: remove after update-stage0 -/
elab "#mkrpcenc" n:ident : command => do
elabCommand <| ← `(
namespace $n
deriving instance Lean.Server.RpcEncodable for $n
end $n
)
end Lean.Server

namespace Lean.Widget
open Meta Elab

Expand Down

0 comments on commit 73b614d

Please sign in to comment.