SlideShare a Scribd company logo
Sum type (coproduct)
Dmytro Mitin
https://stepik.org/course/Introduction-to-programming-
with-dependent-types-in-Scala-2294/
March 2017
Dmytro Mitin Sum type (coproduct)
Sets
Union
A ∪ B = {x | x ∈ A ∨ x ∈ B}
Disjoint union
A B = (0, a) a ∈ A ∪ (1, b) b ∈ B
= {0} × A ∪ {1} × B
Dmytro Mitin Sum type (coproduct)
Sum type (coproduct)
1 Type Formation
Γ A : ∗ Γ B : ∗
Γ A + B : ∗
2 Constructors
Γ a : A Γ B : ∗
Γ inl a : A + B
Γ A : ∗ Γ b : B
Γ inr b : A + B
3 Eliminator
Γ C : ∗ Γ, a : A c1 : C Γ, b : B c2 : C
Γ, s : A + B case+(s)(λa.c1, λb.c2) : C
s match { case Inl(a) => c1(a); case Inr(b) => c2(b) }
Dmytro Mitin Sum type (coproduct)
Sum type (coproduct)
4 Computation rules (”β-reduction“)
Γ C : ∗ Γ, a : A c1 : C Γ, b : B c2 : C Γ a : A
Γ case+(inl a )(λa.c1, λb.c2) ≡ c1[a ← a ] : C
Γ C : ∗ Γ, a : A c1 : C Γ, b : B c2 : C Γ b : B
Γ case+(inr b )(λa.c1, λb.c2) ≡ c2[b ← b ] : C
5 Uniqueness principle (”η-conversion“)
Dmytro Mitin Sum type (coproduct)
Haskell, Scala and Java
Haskell
data Sum a b = Inl a | Inr b
Scala
sealed trait Sum[A, B]
case class Inl[A, B](a: A) extends Sum[A, B]
case class Inr[A, B](b: B) extends Sum[A, B]
Java
public interface Sum<A, B> {}
public class Inl<A, B> implements Sum<A, B> {
private final A a;
}
public class Inr<A, B> implements Sum<A, B> {
private final B b;
}
Dmytro Mitin Sum type (coproduct)
ProvingGround. Built-in type
git clone https://github.com/siddhartha-gadgil/ProvingGround.git
cd ProvingGround
sbt mantle/test:console
val A = "A" :: Type
val B = "B" :: Type
val a = "a" :: A
val b = "b" :: B
val a1 = PlusTyp(A, B).incl1(a) !: PlusTyp(A, B)
val b1 = PlusTyp(A, B).incl2(b) !: PlusTyp(A, B)
val recAorBA = PlusTyp(A, B).rec(A)
val recAorBB = PlusTyp(A, B).rec(B)
val a0 = "a0" :: A
val b0 = "b0" :: B
val idAOrDefault = recAorBA(a :-> a)(b :-> a0)
val idBOrDefault = recAorBB(a :-> b0)(b :-> b)
Dmytro Mitin Sum type (coproduct)
ProvingGround. Built-in type
idAOrDefault(a1) == a
idAOrDefault(b1) == a0
idBOrDefault(a1) == b0
idBOrDefault(b1) == b
val recAorBAorB = PlusTyp(A, B).rec(PlusTyp(A, B))
val id = recAorBAorB(a :-> PlusTyp(A, B).incl1(a))(b :->
PlusTyp(A, B).incl2(b))
id(a1) == a1
id(b1) == b1
Dmytro Mitin Sum type (coproduct)
ProvingGround. Custom type
import TLImplicits.
import shapeless.
val A = "A" :: Type
val B = "B" :: Type
val a = "a" :: A
val b = "b" :: B
val AorB = "A + B" :: Type
val SumInd = ("inl" ::: A ->>: AorB) |: ("inr" ::: B ->>: AorB)
=: AorB
val inl :: inr :: HNil = SumInd.intros
val a1 = inl(a) !: AorB
val b1 = inr(b) !: AorB
val recAorBA = SumInd.rec(A)
val recAorBB = SumInd.rec(B)
Dmytro Mitin Sum type (coproduct)
ProvingGround. Custom type
val a0 = "a0" :: A
val b0 = "b0" :: B
val idAOrDefault = recAorBA(a :-> a)(b :-> a0)
val idBOrDefault = recAorBB(a :-> b0)(b :-> b)
idAOrDefault(a1) == a
idAOrDefault(b1) == a0
idBOrDefault(a1) == b0
idBOrDefault(b1) == b
val recAorBAorB = SumInd.rec(AorB)
val id = recAorBAorB(a :-> inl(a))(b :-> inr(b))
id(a1) == a1
id(b1) == b1
Dmytro Mitin Sum type (coproduct)

More Related Content

What's hot

11 - Scala. Function type
11 - Scala. Function type11 - Scala. Function type
11 - Scala. Function type
Roman Brovko
 
AP Calculus January 5, 2009
AP Calculus January 5, 2009AP Calculus January 5, 2009
AP Calculus January 5, 2009
Darren Kuropatwa
 
C# 8 and Beyond
C# 8 and BeyondC# 8 and Beyond
C# 8 and Beyond
Filip Ekberg
 
Funções 2
Funções 2Funções 2
Funções 2
KalculosOnline
 
12 - Scala. Empty and unit types
12 - Scala. Empty and unit types12 - Scala. Empty and unit types
12 - Scala. Empty and unit types
Roman Brovko
 
program on Function overloading in java
program on  Function overloading in javaprogram on  Function overloading in java
program on Function overloading in java
One97 Communications Limited
 
Presentation 2(power point presentation) dis2016
Presentation 2(power point presentation) dis2016Presentation 2(power point presentation) dis2016
Presentation 2(power point presentation) dis2016
Daniel Omunting
 
Rethinking metrics: metrics 2.0 @ Lisa 2014
Rethinking metrics: metrics 2.0 @ Lisa 2014Rethinking metrics: metrics 2.0 @ Lisa 2014
Rethinking metrics: metrics 2.0 @ Lisa 2014
Dieter Plaetinck
 
Basics of Computer graphics lab
Basics of Computer graphics labBasics of Computer graphics lab
Basics of Computer graphics lab
Priya Goyal
 
Transport in india
Transport in indiaTransport in india
Transport in india
RamnarayanSingh11
 
Conversion from infix to prefix using stack
Conversion from infix to prefix using stackConversion from infix to prefix using stack
Conversion from infix to prefix using stackHaqnawaz Ch
 
Answers+of+C+sample+exam.docx
Answers+of+C+sample+exam.docxAnswers+of+C+sample+exam.docx
Answers+of+C+sample+exam.docxismailaboshatra
 
Flood Filling Algorithm in C
Flood Filling Algorithm in CFlood Filling Algorithm in C
Flood Filling Algorithm in C
Kasun Ranga Wijeweera
 
Infix to Prefix (Conversion, Evaluation, Code)
Infix to Prefix (Conversion, Evaluation, Code)Infix to Prefix (Conversion, Evaluation, Code)
Infix to Prefix (Conversion, Evaluation, Code)Ahmed Khateeb
 

What's hot (17)

11 - Scala. Function type
11 - Scala. Function type11 - Scala. Function type
11 - Scala. Function type
 
AP Calculus January 5, 2009
AP Calculus January 5, 2009AP Calculus January 5, 2009
AP Calculus January 5, 2009
 
C# 8 and Beyond
C# 8 and BeyondC# 8 and Beyond
C# 8 and Beyond
 
Funções 2
Funções 2Funções 2
Funções 2
 
12 - Scala. Empty and unit types
12 - Scala. Empty and unit types12 - Scala. Empty and unit types
12 - Scala. Empty and unit types
 
week-23x
week-23xweek-23x
week-23x
 
program on Function overloading in java
program on  Function overloading in javaprogram on  Function overloading in java
program on Function overloading in java
 
Presentation 2(power point presentation) dis2016
Presentation 2(power point presentation) dis2016Presentation 2(power point presentation) dis2016
Presentation 2(power point presentation) dis2016
 
Rethinking metrics: metrics 2.0 @ Lisa 2014
Rethinking metrics: metrics 2.0 @ Lisa 2014Rethinking metrics: metrics 2.0 @ Lisa 2014
Rethinking metrics: metrics 2.0 @ Lisa 2014
 
Basics of Computer graphics lab
Basics of Computer graphics labBasics of Computer graphics lab
Basics of Computer graphics lab
 
Practical no 4
Practical no 4Practical no 4
Practical no 4
 
F(2)
F(2)F(2)
F(2)
 
Transport in india
Transport in indiaTransport in india
Transport in india
 
Conversion from infix to prefix using stack
Conversion from infix to prefix using stackConversion from infix to prefix using stack
Conversion from infix to prefix using stack
 
Answers+of+C+sample+exam.docx
Answers+of+C+sample+exam.docxAnswers+of+C+sample+exam.docx
Answers+of+C+sample+exam.docx
 
Flood Filling Algorithm in C
Flood Filling Algorithm in CFlood Filling Algorithm in C
Flood Filling Algorithm in C
 
Infix to Prefix (Conversion, Evaluation, Code)
Infix to Prefix (Conversion, Evaluation, Code)Infix to Prefix (Conversion, Evaluation, Code)
Infix to Prefix (Conversion, Evaluation, Code)
 

Similar to 10 - Scala. Co-product type (sum type)

Sigma type
Sigma typeSigma type
Sigma type
Dmytro Mitin
 
13 - Scala. Dependent pair type (Σ-type)
13 - Scala. Dependent pair type (Σ-type)13 - Scala. Dependent pair type (Σ-type)
13 - Scala. Dependent pair type (Σ-type)
Roman Brovko
 
Eliminators into dependent types
Eliminators into dependent typesEliminators into dependent types
Eliminators into dependent types
Dmytro Mitin
 
19 - Scala. Eliminators into dependent types (induction)
19 - Scala. Eliminators into dependent types (induction)19 - Scala. Eliminators into dependent types (induction)
19 - Scala. Eliminators into dependent types (induction)
Roman Brovko
 
03 - Scala. Boolean type
03 - Scala. Boolean type03 - Scala. Boolean type
03 - Scala. Boolean type
Roman Brovko
 
Boolean type
Boolean typeBoolean type
Boolean type
Dmytro Mitin
 
C++20 the small things - Timur Doumler
C++20 the small things - Timur DoumlerC++20 the small things - Timur Doumler
C++20 the small things - Timur Doumler
corehard_by
 
05 - Scala. List type
05 - Scala. List type05 - Scala. List type
05 - Scala. List type
Roman Brovko
 
Oops Quiz
Oops QuizOops Quiz
C++ TUTORIAL 8
C++ TUTORIAL 8C++ TUTORIAL 8
C++ TUTORIAL 8
Farhan Ab Rahman
 
15 - Scala. Dependent function type (Π-type)
15 - Scala. Dependent function type (Π-type)15 - Scala. Dependent function type (Π-type)
15 - Scala. Dependent function type (Π-type)
Roman Brovko
 
Week 5
Week 5Week 5
Week 5
준성 조
 
C++ 11 Features
C++ 11 FeaturesC++ 11 Features
C++ 11 Features
Jan Rüegg
 
Why you should use super() though it sucks
Why you should use super() though it sucksWhy you should use super() though it sucks
Why you should use super() though it sucks
Eunchong Yu
 
Technical questions
Technical questionsTechnical questions
Technical questions
Kirthan S Holla
 
Basic maths
Basic mathsBasic maths
Basic maths
Nale Vikas
 
C test
C testC test
New presentation oop
New presentation oopNew presentation oop
New presentation oop
Ch shampi Ch shampi
 

Similar to 10 - Scala. Co-product type (sum type) (20)

Sigma type
Sigma typeSigma type
Sigma type
 
13 - Scala. Dependent pair type (Σ-type)
13 - Scala. Dependent pair type (Σ-type)13 - Scala. Dependent pair type (Σ-type)
13 - Scala. Dependent pair type (Σ-type)
 
Eliminators into dependent types
Eliminators into dependent typesEliminators into dependent types
Eliminators into dependent types
 
19 - Scala. Eliminators into dependent types (induction)
19 - Scala. Eliminators into dependent types (induction)19 - Scala. Eliminators into dependent types (induction)
19 - Scala. Eliminators into dependent types (induction)
 
03 - Scala. Boolean type
03 - Scala. Boolean type03 - Scala. Boolean type
03 - Scala. Boolean type
 
Boolean type
Boolean typeBoolean type
Boolean type
 
C++20 the small things - Timur Doumler
C++20 the small things - Timur DoumlerC++20 the small things - Timur Doumler
C++20 the small things - Timur Doumler
 
05 - Scala. List type
05 - Scala. List type05 - Scala. List type
05 - Scala. List type
 
Oops Quiz
Oops QuizOops Quiz
Oops Quiz
 
C++ TUTORIAL 8
C++ TUTORIAL 8C++ TUTORIAL 8
C++ TUTORIAL 8
 
15 - Scala. Dependent function type (Π-type)
15 - Scala. Dependent function type (Π-type)15 - Scala. Dependent function type (Π-type)
15 - Scala. Dependent function type (Π-type)
 
Week 5
Week 5Week 5
Week 5
 
C++ 11 Features
C++ 11 FeaturesC++ 11 Features
C++ 11 Features
 
Why you should use super() though it sucks
Why you should use super() though it sucksWhy you should use super() though it sucks
Why you should use super() though it sucks
 
Revision1 C programming
Revision1 C programmingRevision1 C programming
Revision1 C programming
 
Revision1schema C programming
Revision1schema C programmingRevision1schema C programming
Revision1schema C programming
 
Technical questions
Technical questionsTechnical questions
Technical questions
 
Basic maths
Basic mathsBasic maths
Basic maths
 
C test
C testC test
C test
 
New presentation oop
New presentation oopNew presentation oop
New presentation oop
 

More from Roman Brovko

Individual task Networking
Individual task NetworkingIndividual task Networking
Individual task Networking
Roman Brovko
 
Networking essentials lect3
Networking essentials lect3Networking essentials lect3
Networking essentials lect3
Roman Brovko
 
Gl embedded starterkit_ethernet
Gl embedded starterkit_ethernetGl embedded starterkit_ethernet
Gl embedded starterkit_ethernet
Roman Brovko
 
Networking essentials lect2
Networking essentials lect2Networking essentials lect2
Networking essentials lect2
Roman Brovko
 
Networking essentials lect1
Networking essentials lect1Networking essentials lect1
Networking essentials lect1
Roman Brovko
 
Bare metal training_07_spi_flash
Bare metal training_07_spi_flashBare metal training_07_spi_flash
Bare metal training_07_spi_flash
Roman Brovko
 
Bare metal training_06_I2C
Bare metal training_06_I2CBare metal training_06_I2C
Bare metal training_06_I2C
Roman Brovko
 
Glesk worshop
Glesk worshopGlesk worshop
Glesk worshop
Roman Brovko
 
Bare metal training_05_uart
Bare metal training_05_uartBare metal training_05_uart
Bare metal training_05_uart
Roman Brovko
 
Bare metal training_04_adc_temp_sensor
Bare metal training_04_adc_temp_sensorBare metal training_04_adc_temp_sensor
Bare metal training_04_adc_temp_sensor
Roman Brovko
 
Bare metal training_03_timers_pwm
Bare metal training_03_timers_pwmBare metal training_03_timers_pwm
Bare metal training_03_timers_pwm
Roman Brovko
 
Bare metal training_02_le_ds_and_buttons
Bare metal training_02_le_ds_and_buttonsBare metal training_02_le_ds_and_buttons
Bare metal training_02_le_ds_and_buttons
Roman Brovko
 
Bare metal training_01_hello_world
Bare metal training_01_hello_worldBare metal training_01_hello_world
Bare metal training_01_hello_world
Roman Brovko
 
Bare metal training_00_prerequisites
Bare metal training_00_prerequisitesBare metal training_00_prerequisites
Bare metal training_00_prerequisites
Roman Brovko
 
C language lect_23_advanced
C language lect_23_advancedC language lect_23_advanced
C language lect_23_advanced
Roman Brovko
 
C language lect_22_advanced
C language lect_22_advancedC language lect_22_advanced
C language lect_22_advanced
Roman Brovko
 
C language lect_21_advanced
C language lect_21_advancedC language lect_21_advanced
C language lect_21_advanced
Roman Brovko
 
подготовка рабочего окружения
подготовка рабочего окруженияподготовка рабочего окружения
подготовка рабочего окружения
Roman Brovko
 
C language lect_20_advanced
C language lect_20_advancedC language lect_20_advanced
C language lect_20_advanced
Roman Brovko
 
C language lect_19_basics
C language lect_19_basicsC language lect_19_basics
C language lect_19_basics
Roman Brovko
 

More from Roman Brovko (20)

Individual task Networking
Individual task NetworkingIndividual task Networking
Individual task Networking
 
Networking essentials lect3
Networking essentials lect3Networking essentials lect3
Networking essentials lect3
 
Gl embedded starterkit_ethernet
Gl embedded starterkit_ethernetGl embedded starterkit_ethernet
Gl embedded starterkit_ethernet
 
Networking essentials lect2
Networking essentials lect2Networking essentials lect2
Networking essentials lect2
 
Networking essentials lect1
Networking essentials lect1Networking essentials lect1
Networking essentials lect1
 
Bare metal training_07_spi_flash
Bare metal training_07_spi_flashBare metal training_07_spi_flash
Bare metal training_07_spi_flash
 
Bare metal training_06_I2C
Bare metal training_06_I2CBare metal training_06_I2C
Bare metal training_06_I2C
 
Glesk worshop
Glesk worshopGlesk worshop
Glesk worshop
 
Bare metal training_05_uart
Bare metal training_05_uartBare metal training_05_uart
Bare metal training_05_uart
 
Bare metal training_04_adc_temp_sensor
Bare metal training_04_adc_temp_sensorBare metal training_04_adc_temp_sensor
Bare metal training_04_adc_temp_sensor
 
Bare metal training_03_timers_pwm
Bare metal training_03_timers_pwmBare metal training_03_timers_pwm
Bare metal training_03_timers_pwm
 
Bare metal training_02_le_ds_and_buttons
Bare metal training_02_le_ds_and_buttonsBare metal training_02_le_ds_and_buttons
Bare metal training_02_le_ds_and_buttons
 
Bare metal training_01_hello_world
Bare metal training_01_hello_worldBare metal training_01_hello_world
Bare metal training_01_hello_world
 
Bare metal training_00_prerequisites
Bare metal training_00_prerequisitesBare metal training_00_prerequisites
Bare metal training_00_prerequisites
 
C language lect_23_advanced
C language lect_23_advancedC language lect_23_advanced
C language lect_23_advanced
 
C language lect_22_advanced
C language lect_22_advancedC language lect_22_advanced
C language lect_22_advanced
 
C language lect_21_advanced
C language lect_21_advancedC language lect_21_advanced
C language lect_21_advanced
 
подготовка рабочего окружения
подготовка рабочего окруженияподготовка рабочего окружения
подготовка рабочего окружения
 
C language lect_20_advanced
C language lect_20_advancedC language lect_20_advanced
C language lect_20_advanced
 
C language lect_19_basics
C language lect_19_basicsC language lect_19_basics
C language lect_19_basics
 

Recently uploaded

Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Globus
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
Fermin Galan
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Globus
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
abdulrafaychaudhry
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
rickgrimesss22
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
Cyanic lab
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
Donna Lenk
 
Enterprise Resource Planning System in Telangana
Enterprise Resource Planning System in TelanganaEnterprise Resource Planning System in Telangana
Enterprise Resource Planning System in Telangana
NYGGS Automation Suite
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
Globus
 
top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownload
vrstrong314
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
Globus
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns
 
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Globus
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
Ortus Solutions, Corp
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Globus
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
wottaspaceseo
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
Globus
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Shahin Sheidaei
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
Paco van Beckhoven
 
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdfEnhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Jay Das
 

Recently uploaded (20)

Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
 
Enterprise Resource Planning System in Telangana
Enterprise Resource Planning System in TelanganaEnterprise Resource Planning System in Telangana
Enterprise Resource Planning System in Telangana
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
 
top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownload
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
 
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
 
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdfEnhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
 

10 - Scala. Co-product type (sum type)

  • 1. Sum type (coproduct) Dmytro Mitin https://stepik.org/course/Introduction-to-programming- with-dependent-types-in-Scala-2294/ March 2017 Dmytro Mitin Sum type (coproduct)
  • 2. Sets Union A ∪ B = {x | x ∈ A ∨ x ∈ B} Disjoint union A B = (0, a) a ∈ A ∪ (1, b) b ∈ B = {0} × A ∪ {1} × B Dmytro Mitin Sum type (coproduct)
  • 3. Sum type (coproduct) 1 Type Formation Γ A : ∗ Γ B : ∗ Γ A + B : ∗ 2 Constructors Γ a : A Γ B : ∗ Γ inl a : A + B Γ A : ∗ Γ b : B Γ inr b : A + B 3 Eliminator Γ C : ∗ Γ, a : A c1 : C Γ, b : B c2 : C Γ, s : A + B case+(s)(λa.c1, λb.c2) : C s match { case Inl(a) => c1(a); case Inr(b) => c2(b) } Dmytro Mitin Sum type (coproduct)
  • 4. Sum type (coproduct) 4 Computation rules (”β-reduction“) Γ C : ∗ Γ, a : A c1 : C Γ, b : B c2 : C Γ a : A Γ case+(inl a )(λa.c1, λb.c2) ≡ c1[a ← a ] : C Γ C : ∗ Γ, a : A c1 : C Γ, b : B c2 : C Γ b : B Γ case+(inr b )(λa.c1, λb.c2) ≡ c2[b ← b ] : C 5 Uniqueness principle (”η-conversion“) Dmytro Mitin Sum type (coproduct)
  • 5. Haskell, Scala and Java Haskell data Sum a b = Inl a | Inr b Scala sealed trait Sum[A, B] case class Inl[A, B](a: A) extends Sum[A, B] case class Inr[A, B](b: B) extends Sum[A, B] Java public interface Sum<A, B> {} public class Inl<A, B> implements Sum<A, B> { private final A a; } public class Inr<A, B> implements Sum<A, B> { private final B b; } Dmytro Mitin Sum type (coproduct)
  • 6. ProvingGround. Built-in type git clone https://github.com/siddhartha-gadgil/ProvingGround.git cd ProvingGround sbt mantle/test:console val A = "A" :: Type val B = "B" :: Type val a = "a" :: A val b = "b" :: B val a1 = PlusTyp(A, B).incl1(a) !: PlusTyp(A, B) val b1 = PlusTyp(A, B).incl2(b) !: PlusTyp(A, B) val recAorBA = PlusTyp(A, B).rec(A) val recAorBB = PlusTyp(A, B).rec(B) val a0 = "a0" :: A val b0 = "b0" :: B val idAOrDefault = recAorBA(a :-> a)(b :-> a0) val idBOrDefault = recAorBB(a :-> b0)(b :-> b) Dmytro Mitin Sum type (coproduct)
  • 7. ProvingGround. Built-in type idAOrDefault(a1) == a idAOrDefault(b1) == a0 idBOrDefault(a1) == b0 idBOrDefault(b1) == b val recAorBAorB = PlusTyp(A, B).rec(PlusTyp(A, B)) val id = recAorBAorB(a :-> PlusTyp(A, B).incl1(a))(b :-> PlusTyp(A, B).incl2(b)) id(a1) == a1 id(b1) == b1 Dmytro Mitin Sum type (coproduct)
  • 8. ProvingGround. Custom type import TLImplicits. import shapeless. val A = "A" :: Type val B = "B" :: Type val a = "a" :: A val b = "b" :: B val AorB = "A + B" :: Type val SumInd = ("inl" ::: A ->>: AorB) |: ("inr" ::: B ->>: AorB) =: AorB val inl :: inr :: HNil = SumInd.intros val a1 = inl(a) !: AorB val b1 = inr(b) !: AorB val recAorBA = SumInd.rec(A) val recAorBB = SumInd.rec(B) Dmytro Mitin Sum type (coproduct)
  • 9. ProvingGround. Custom type val a0 = "a0" :: A val b0 = "b0" :: B val idAOrDefault = recAorBA(a :-> a)(b :-> a0) val idBOrDefault = recAorBB(a :-> b0)(b :-> b) idAOrDefault(a1) == a idAOrDefault(b1) == a0 idBOrDefault(a1) == b0 idBOrDefault(b1) == b val recAorBAorB = SumInd.rec(AorB) val id = recAorBAorB(a :-> inl(a))(b :-> inr(b)) id(a1) == a1 id(b1) == b1 Dmytro Mitin Sum type (coproduct)