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 6637459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:19:05+00:00 2026-05-25T23:19:05+00:00

I am trying to implement a unify function with an algorithm that is specified

  • 0

I am trying to implement a unify function with an algorithm that is specified as

unify α α = idSubst
unify α β = update (α, β) idSubst
unify α (τ1 ⊗ τ2) =
    if α ∈ vars(τ1 ⊗ τ2) then
      error ”Occurs check failure”
    else
      update (α, τ1 ⊗ τ2) idSubst
unify (τ1 ⊗ τ2) α = unify α (τ1 ⊗ τ2)
unify (τ1 ⊗1 τ2) (τ3 ⊗2 τ4) = if ⊗1 == ⊗2 then
                                   (subst s2) . s1
                                  else
                                   error ”not unifiable.”
          where s1 = unify τ1 τ3
                s2 = unify (subst s1 τ2) (subst s1 τ4)

with ⊗ being one of the type constructors {→, ×}.

However I do not understand how to implement this in haskell. How would I go about this?

import Data.List
import Data.Char

data  Term =   Var String | Abs  (String,Term) | Ap Term Term  | Pair Term Term | Fst Term | Snd Term
        deriving (Eq,Show)

data Op = Arrow | Product deriving (Eq)


data  Type =   TVar  String |  BinType Op  Type   Type
        deriving (Eq)

instance Show Type where
   show (TVar x) = x
   show (BinType Arrow t1 t2) = "(" ++ show t1 ++ " -> " ++ show t2 ++ ")"
   show (BinType Product t1 t2) = "(" ++ show t1 ++ " X " ++ show t2 ++ ")"

type Substitution = String -> Type

idSubst :: Substitution
idSubst x = TVar x

update :: (String, Type) -> Substitution -> Substitution
update (x,y) f = (\z -> if z == x then y else f z)


-- vars collects all the variables occuring in a type expression
vars :: Type -> [String]
vars ty = nub (vars' ty) 
    where vars' (TVar x) = [x]
          vars' (BinType op t1 t2) = vars' t1 ++ vars' t2

subst :: Substitution -> Type -> Type
subst s (TVar x) = s x
subst s (BinType op t1 t2) = BinType op (subst s t1) (subst s t2)

unify :: Type -> Type -> Substitution
unify (TVar x) (TVar y) = update (x, TVar y) idSubst
  • 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-25T23:19:05+00:00Added an answer on May 25, 2026 at 11:19 pm
    unify :: Type -> Type -> Substitution
    unify (TVar x) (TVar y) = update (x, TVar y) idSubst
    

    This is a great start!

    Now you just need to handle the other cases:

    Here’s how you’d represent the first one:

    unify (TVar x) (TVar y) | x == y = idSubst
    

    You can do the rest similarly using pattern matching to decompose your Type into the appropriate constructors and guards to handle specific cases.

    Haskell has an error :: String -> a function that works the same as in your pseudo-code above, and the if/then/else syntax is the same, so you’re almost there!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to implement a function that will return a list of ints the represent
Trying to implement a function in my linkedlist class that will return total amount
when trying to implement an Aspect, that is responsible for catching and logging a
im trying to implement a custom error page, what i want to be able
Ok well I'm trying implement something similar to the 'undo' function in many image
SetFocus I'm trying implement the above Se Focus code in a Class Library that
Trying to implement a MSMQ-backed WCF PubSub. I understand that net.msmq is one-way; however
Trying to implement the new FP 10.1 Global error handler into my projects but
When trying to implement a simple OpenGL application, I was surprised that while it
im trying to implement a hash function in python. Would you consider the following

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.