Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Constraint Solving in the context of programming language | Exercise 5.
Dec 12, 2024
230 views
Written by Prashant Basnet
<section class="bg-white dark:bg-gray-900 px-4 py-8 max-w-2xl mx-auto text-gray-800 dark:text-gray-200">
<h1 class="text-2xl sm:text-3xl font-signature italic font-semibold text-center mb-4">
👋 Welcome — You’ve Landed on My Signature Page
</h1>
<p class="text-base sm:text-lg mb-4">
Hey, I’m <strong class="text-black dark:text-white">Prashant Basnet</strong> — software developmemt engineer at
<a href="https://unisala.com" class="text-indigo-600 dark:text-indigo-400 underline hover:no-underline" target="_blank" rel="noopener noreferrer">
Unisala.com
</a>.
</p>
<p class="text-base sm:text-lg mb-6">
You’re viewing my <strong>Signature</strong>, a digital space where I share what I’m learning, building, and reflecting on, all in one place.
</p>
<div class="border-l-4 border-indigo-400 dark:border-indigo-500 pl-4 italic mb-6 text-sm sm:text-base text-gray-700 dark:text-gray-400">
📍 Found this page via LinkedIn, my personal site, or a shared link?
<br />
This isn’t a traditional portfolio. It’s my public digital notebook where I document useful ideas, experiments, and lessons I’ve learned as I build.
</div>
<h2 class="text-lg font-semibold mb-2">What You’ll Find Here:</h2>
<ul class="list-disc list-inside space-y-1 text-sm sm:text-base">
<li>✍️ Thoughts on algorithms, systems, and software design</li>
<li>🧠 Insights from building at Unisala</li>
<li>🔗 Direct links to everything I’ve published on Unisala</li>
</ul>
</section>
Solve each of the following constraints.
Let's first understand the tools to solve it:
Constraint Solving in type inference involves finding a way to make different type expressions agree with each other according to a set of rules.
What are constraints?
Constraints are condition specified that types must satisfy for the program to be considered type correct.
When you write a program, the type system tries to ensure that operations are performed on compatible types, for example not trying to add number to a string.
How do Constraints Arise?
In the type inference process, especially in languages like Haskell, the compiler automatically determines the types of expressions without explicit type annotations from the programmer. It does this by generating constraints:
Example in Lambda Calculus:
Here, the lambda calculus doesn't specify types directly, but we can infer:
Constraint Solving Process
To make this more formal, let's say:
Solving the Constraints
Example to Illustrate These Steps
Program:
Assumptions:
Step 1. Collection
Step 2: Unification
Step 3: Substitute:
Why is this important?
The ability to automatically infer and check types is powerful because it:
Understanding =?
In the type inference system, the symbol =? represents a unification attempt between two types.
The goal of unification is to find a consistent assignment of types to type variables (like a1, a2, a3... etc) that satisfies all such equations in a given set.
Set 1:
Which is a Collection Result.
this set consist of 3 constraints:
Resulting Substitution:
Set 2:
this set consist of 3 constraints:
Resulting Substitution:
All types are equivalent so they can be any type as long as they are the same.
Set 3:
Final Type Assignments:
What Does a Free Type Variable Mean?
A "free" type variable is one that is not restricted by any specific type constraints beyond those possibly inherited from more general contexts (like the constraints on other variables that it interacts with). In the context of type inference:
Set 4: