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

  • Home
  • SEARCH
  • 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 6206685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:29:35+00:00 2026-05-24T05:29:35+00:00

I’m working on a simple dataflow based system (imagine it like a LabView editor/runtime)

  • 0

I’m working on a simple dataflow based system (imagine it like a LabView editor/runtime) written in Java. The user can wire blocks together in an editor and I need type inference to ensure the dataflow graph is correct, however, most type inference examples are written in mathematical notations, ML, Scala, Perl, etc., which I don’t “speak”.

I read about the Hindley-Milner algorithm and found this document with a nice example I could implement. It works on a set of T1 = T2 like constraints. However, my dataflow graphs translate to T1 >= T2 like constraints (or T2 extends T1, or covariance, or T1 <: T2 as I saw it in various articles). No lambdas just type variables (used in generic functions like T merge(T in1, T in2)) and concrete types.

To recap the HM algorithm:

Type = {TypeVariable, ConcreteType}
TypeRelation = {LeftType, RightType}
Substitution = {OldType, NewType}
TypeRelations = set of TypeRelation
Substitutions = set of Substitution

1) Initialize TypeRelations to the constraints, Initialize Substitutions to empty
2) Take a TypeRelation
3) If LeftType and RightType are both TypeVariables or are concrete 
      types with LeftType <: RightType Then do nothing
4) If only LeftType is a TypeVariable Then
    replace all occurrences of RightType in TypeRelations and Substitutions
    put LeftType, RightType into Substitutions
5) If only RightType is a TypeVariable then
    replace all occurrences of LeftType in TypeRelations and Substitutions
    put RightType, LeftType into Substitutions
6) Else fail

How can I change the original HM algorithm to work with these kind of relations instead of simple equality relations?
Java-ish example or explanation would be much appreciated.

  • 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-24T05:29:35+00:00Added an answer on May 24, 2026 at 5:29 am

    I read at least 20 articles and found one (Francois Pottier: Type inference in presence of subtyping: from theory to practice) which I could use:

    Input:

    Type = { TypeVariable, ConcreteType }
    TypeRelation = { Left: Type, Right: Type }
    TypeRelations = Deque<TypeRelation>
    

    Helper functions:

    ExtendsOrEquals = #(ConcreteType, ConcreteType) => Boolean
    Union = #(ConcreteType, ConcreteType) => ConcreteType | fail
    Intersection = #(ConcreteType, ConcreteType) => ConcreteType
    SubC = #(Type, Type) => List<TypeRelation>
    

    ExtendsOrEquals can tell about two concrete types if the first extends or equals the second, e.g., (String, Object) == true, (Object, String) == false.

    Union computes the common subtype of two concrete types if possible, e.g., (Object, Serializable) == Object&Serializable, (Integer, String) == null.

    Intersection computes the nearest supertype of two concrete types, e.g., (List, Set) == Collection, (Integer, String) == Object.

    SubC is the structural decomposition function, which in this simple case will just return a singleton list containing a new TypeRelation of its parameters.

    Tracking structures:

    UpperBounds = Map<TypeVariable, Set<Type>>
    LowerBounds = Map<TypeVariable, Set<Type>>
    Reflexives = List<TypeRelation>
    

    UpperBounds keeps track of types which may be supertypes of a type variable, LowerBounds keeps track of types which may be subtypes of the type variable. Reflexives keeps track of the relations between pairs type variables to help in the bound-rewriting of the algorithm.

    The algorithm is as follows:

    While TypeRelations is not empty, take a relation rel
    
      [Case 1] If rel is (left: TypeVariable, right: TypeVariable) and 
               Reflexives does not have an entry with (left, right) {
    
        found1 = false;
        found2 = false
        for each ab in Reflexives
          // apply a >= b, b >= c then a >= c rule
          if (ab.right == rel.left)
            found1 = true
            add (ab.left, rel.right) to Reflexives
            union and set upper bounds of ab.left 
              with upper bounds of rel.right
    
          if (ab.left == rel.right)
            found2 = true
            add (rel.left, ab.right) to Reflexives
            intersect and set lower bounds of ab.right 
              with lower bounds of rel.left
    
        if !found1
            union and set upper bounds of rel.left
              with upper bounds of rel.right
        if !found2
            intersect and set lower bounds of rel.right
              with lower bounds of rel.left
    
        add TypeRelation(rel.left, rel.right) to Reflexives
    
        for each lb in LowerBounds of rel.left
          for each ub in UpperBounds of rel.right
            add all SubC(lb, ub) to TypeRelations
      }
      [Case 2] If rel is (left: TypeVariable, right: ConcreteType) and 
          UpperBound of rel.left does not contain rel.right {
        found = false
        for each ab in Reflexives
          if (ab.right == rel.left)
            found = true
            union and set upper bounds of ab.left with rel.right
        if !found 
            union the upper bounds of rel.left with rel.right
        for each lb in LowerBounds of rel.left
          add all SubC(lb, rel.right) to TypeRelations
      }
      [Case 3] If rel is (left: ConcreteType, right: TypeVariable) and
          LowerBound of rel.right does not contain rel.left {
        found = false;
        for each ab in Reflexives
          if (ab.left == rel.right)
             found = true;
             intersect and set lower bounds of ab.right with rel.left
        if !found
           intersect and set lower bounds of rel.right with rel.left
        for each ub in UpperBounds of rel.right
           add each SubC(rel.left, ub) to TypeRelations
      }
      [Case 4] if rel is (left: ConcreteType, Right: ConcreteType) and 
          !ExtendsOrEquals(rel.left, rel.right)
        fail
      }
    

    A basic example:

    Merge = (T, T) => T
    Sink = U => Void
    
    Sink(Merge("String", 1))
    

    The relations of this expression:

    String >= T
    Integer >= T
    T >= U
    

    1.) rel is (String, T); Case 3 is activated. Because Reflexives is empty, the LowerBounds of T is set to String. No UpperBounds for T is present, therefore, TypeRelations remains unchanged.

    2.) rel is (Integer, T); Case 3 is activated again. Reflexives is still empty, the Lower bound of T is set to the intersection of String and Integer, yielding Object, Still no upper bounds for T and no changes in TypeRelations

    3.) rel is T >= U. Case 1 is activated. Because Reflexives is empty, the Upper Bounds of T is combined with the Upper bounds of U, which remains empty. Then the lower bounds U is set to the lower bounds ot T, yielding Object >= U. The TypeRelation(T, U) is addet to Reflexives.

    4.) the algorithm terminates. From the bounds Object >= T and Object >= U

    In another example, a type conflict is demonstrated:

    Merge = (T, T) => T
    Sink = Integer => Void
    
    Sink(Merge("String", 1))
    

    The relations:

    String >= T
    Integer >= T
    T >= Integer
    

    Steps 1.) and 2.) are the same as above.

    3.) rel is T >= U. Case 2 is activated. The case tries to union the Upper Bound of T (which is Object at this point) with Integer, that fails and the algorithm fails.

    Extensions to the Type system

    Adding generic types to the type system needs an extension in the main cases and in the SubC function.

    Type = { TypeVariable, ConcreteType, ParametricType<Type,...>)
    

    Some ideas:

    • If a ConcreteType and a ParametricType meets, that is an error.
    • If a TypeVariable and a ParametricType meets, e.g., T = C(U1,…,Un) then create new Type variables and relations as T1 >= U1, … , Tn >= Un and work with them.
    • If two ParametricType meets (D<> and C<>) check if D >= C and the number of type arguments are the same, then extract each pair as relations.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I've got a string that has curly quotes in it. I'd like to replace
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm making a simple page using Google Maps API 3. My first. One marker
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6

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.