Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6917303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:42:48+00:00 2026-05-27T09:42:48+00:00

What are the various use cases for union types and intersection types? There has

  • 0

What are the various use cases for union types and intersection types? There has been lately a lot of buzz about these type system features, yet somehow I have never felt need for either of these!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T09:42:49+00:00Added an answer on May 27, 2026 at 9:42 am

    If you want a more practice-oriented answer:

    With union and recursive types you can encode regular tree types and therefore XML types.

    With intersection types you can type BOTH overloaded functions and refinement types (what in a previous post is called coherent overloading)

    So for instance you can write the function add (that overloads integer sum and string concatenation) as follows

    let add ( (Int,Int)->Int ; (String,String)->String )
          | (x & Int, y & Int) -> x+y
          | (x & String, y & String) -> x@y ;;
    

    Which has the intersection type

    (Int,Int)->Int & (String,String)->String

    But you can also refine the type above and type the function above as

    (Pos,Pos) -> Pos & 
    (Neg,Neg) -> Neg & 
    (Int,Int)->Int & 
    (String,String)->String.
    

    where Pos and Neg are positive and negative integer types.

    The code above is executable in the language CDuce ( http://www.cduce.org ) whose type system includes union, intersections, and negation types (it is mainly targeted at XML transformations).

    If you want to try it and you are on Linux, then it is probably included in your distribution (apt-get install cduce or yum install cduce should do the work) and you can use its toplevel (a la OCaml) to play with union and intersection types. On the CDuce site you will find a lot of practical examples of use of union and intersection types. And since there is a complete integration with OCaml libraries (you can import OCaml libraries in CDuce and export CDuce modules to OCaml) you can also check the correspondence with ML sum types (see here).

    Here you are a complex example that mix union and intersection types (explained in the page “http://www.cduce.org/tutorial_overloading.html#val”), but to understand it you need to understand regular expression pattern matching, which requires some effort.

    type Person   = FPerson | MPerson 
    type FPerson  = <person gender = "F">[ Name Children ] 
    type MPerson  = <person gender = "M">[ Name Children ] 
    type Children = <children>[ Person* ] 
    type Name     = <name>[ PCDATA ]
    
    type Man = <man name=String>[ Sons Daughters ]
    type Woman = <woman name=String>[ Sons Daughters ]
    type Sons = <sons>[ Man* ]
    type Daughters = <daughters>[ Woman* ]
    
    let fun split (MPerson -> Man ; FPerson -> Woman)
      <person gender=g>[ <name>n <children>[(mc::MPerson | fc::FPerson)*] ] ->
      (* the above pattern collects all the MPerson in mc, and all the FPerson in fc *)
         let tag = match g with "F" -> `woman | "M" -> `man in
         let s = map mc with x -> split x in
         let d = map fc with x -> split x in    
         <(tag) name=n>[ <sons>s  <daughters>d ] ;; 
    

    In a nutshell it transforms values of type Person into values of type (Man | Women) (where the vertical bar denotes a union type) but keeping the correspondence between genres: split is a function with intersection type

    MPerson -> Man & FPerson -> Woman
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My program has to read files that use various encodings. They may be ANSI,
This question is not about 'best' barcode library recommendation, we use various products on
Django has various numeric fields available for use in models, e.g. DecimalField and PositiveIntegerField
We use the /proc and /sys file systems in Linux to discover various types
There are a few cases where, for various reasons, user agents make requests to
I have been looking at MapReduce and reading through various papers about it and
We have been discussing Tuple and its various possible uses. In almost all cases
I noticed that various systems use various characters as the replacent for illegal ones
I'm familiar with how to use the various FontMetrics functions to center text vertically,
How do I create a resource that I can reference and use in various

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.