section ‹ Examples using Prisms ›
theory Prisms_Examples
imports Prisms
begin
text ‹ We demonstrate prisms for manipulating an algebraic data type with three constructors . ›
datatype D = A int | B string | C bool
fun fromA :: "D ==> int option" where "fromA (A x) = Some x" | "fromA _ = None"
fun fromB :: "D ==> string option" where "fromB (B x) = Some x" | "fromB _ = None"
fun fromC :: "D ==> bool option" where "fromC (C x) = Some x" | "fromC _ = None"
text ‹ We create a prism for each of the three constructors. ›
definition A_ prism :: "int ==> \ <triangle> D" ("A\ <triangle>" ) where
[lens_defs]: "A\ <triangle> = ( prism_match = fromA, prism_build = A ) "
definition B_prism :: "string ==> \ <triangle> D" ("B\ <triangle>" ) where
[lens_defs]: "B\ <triangle> = ( prism_match = fromB, prism_build = B ) "
definition C_prism :: "bool ==> \ <triangle> D" ("C\ <triangle>" ) where
[lens_defs]: "C\ <triangle> = ( prism_match = fromC, prism_build = C ) "
text ‹ All three are well-behaved. ›
lemma A_wb_prism [simp]: "wb_prism A\ <triangle>"
using fromA.elims by (unfold_locales, auto simp add: lens_defs)
lemma B_wb_prism [simp]: "wb_prism B\ <triangle>"
using fromB.elims by (unfold_locales, auto simp add: lens_defs)
lemma C_wb_prism [simp]: "wb_prism C\ <triangle>"
using fromC.elims by (unfold_locales, auto simp add: lens_defs)
text ‹ All three prisms are codependent with each other. ›
lemma D_codeps: "A\ <triangle> ∇ B\ <triangle>" "A\ <triangle> ∇ C\ <triangle>" "B\ <triangle> ∇ C\ <triangle>"
by (rule prism_diff_intro, simp add: lens_defs)+
end
Messung V0.5 in Prozent C=89 H=100 G=94
¤ Dauer der Verarbeitung: 0.9 Sekunden
(vorverarbeitet am 2026-06-10)
¤
*© Formatika GbR, Deutschland