/-
Copyright (c) 2022 Eric Wieser. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Eric Wieser
-/
module
public import Lean.Data.Json.FromToJson.Basic
public import Mathlib.Init
set_option doc.verso true
set_option doc.verso.suggestions false
/-!
# Json serialization typeclass for `PUnit` & `Fin n` & `Subtype p`
-/
public section
universe u
namespace Lean
deriving
instance FromJson, ToJson for PUnit
-- See https://github.com/leanprover/lean4/issues/10295
attribute [nolint unusedArguments] Lean.instToJsonPUnit_mathlib.toJson
instance {n : Nat} : FromJson (Fin n)
where
fromJson? j := do
let i : Nat ← fromJson? j
if h : i < n then
return ⟨i, h⟩
else
throw s!
"must be less than {n}"
instance {n : Nat} : ToJson (Fin n)
where
toJson i := toJson i.val
instance {α : Type u} [FromJson α] (p : α → Prop) [DecidablePred p] : FromJson (Subtype p)
where
fromJson? j := do
let i : α ← fromJson? j
if h : p i then
return ⟨i, h⟩
else
throw
"condition does not hold"
instance {α : Type u} [ToJson α] (p : α → Prop) : ToJson (Subtype p)
where
toJson x := toJson x.val
end Lean
¤ Dauer der Verarbeitung: 0.5 Sekunden
¤
*© Formatika GbR, Deutschland