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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:24:54+00:00 2026-05-18T21:24:54+00:00

EDIT: Solved. I was unware that enabling a language extension in the source file

  • 0

EDIT: Solved. I was unware that enabling a language extension in the source file did not enable the language extension in GHCi. The solution was to :set FlexibleContexts in GHCi.


I recently discovered that type declarations in classes and instances in Haskell are Horn clauses. So I encoded the arithmetic operations from The Art of Prolog, Chapter 3, into Haskell. For instance:

fac(0,s(0)).
fac(s(N),F) :- fac(N,X), mult(s(N),X,F).

class Fac x y | x -> y
instance Fac Z (S Z)
instance (Fac n x, Mult (S n) x f) => Fac (S n) f

pow(s(X),0,0) :- nat(X).
pow(0,s(X),s(0)) :- nat(X).
pow(s(N),X,Y) :- pow(N,X,Z), mult(Z,X,Y).

class Pow x y z | x y -> z
instance (N n) => Pow (S n) Z Z
instance (N n) => Pow Z (S n) (S Z)
instance (Pow n x z, Mult z x y) => Pow (S n) x y

In Prolog, values are instantiated for (logic) variable in a proof. However, I don’t understand how to instantiate type variables in Haskell. That is, I don’t understand what the Haskell equivalent of a Prolog query

?-f(X1,X2,...,Xn)

is. I assume that

:t undefined :: (f x1 x2 ... xn) => xi

would cause Haskell to instantiate xi, but this gives a Non type-variable argument in the constraint error, even with FlexibleContexts enabled.

  • 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-18T21:24:55+00:00Added an answer on May 18, 2026 at 9:24 pm

    No sure about Prolog samples, but I would define this in Haskell in the following way:

    {-# LANGUAGE MultiParamTypeClasses, EmptyDataDecls, FlexibleInstances,
    FlexibleContexts, UndecidableInstances, TypeFamilies, ScopedTypeVariables #-}
    
    data Z
    data S a
    type One = S Z
    type Two = S One
    type Three = S Two
    type Four = S Three 
    
    
    class Plus x y r
    instance (r ~ a) => Plus Z a r
    instance (Plus a b p, r ~ S p) => Plus (S a) b r
    
    p1 = undefined :: (Plus Two Three r) => r
    
    
    class Mult x y r
    instance (r ~ Z) => Mult Z a r
    instance (Mult a b m, Plus m b r) => Mult (S a) b r
    
    m1 = undefined :: (Mult Two Four r) => r
    
    
    class Fac x r
    instance (r ~ One) => Fac Z r
    instance (Fac n r1, Mult (S n) r1 r) => Fac (S n) r
    
    f1 = undefined :: (Fac Three r) => r
    
    
    class Pow x y r
    instance (r ~ One) => Pow x Z r
    instance (r ~ Z) => Pow Z y r
    instance (Pow x y z, Mult z x r) => Pow x (S y) r
    
    pw1 = undefined :: (Pow Two Four r) => r
    
    -- Handy output
    class (Num n) => ToNum a n where
        toNum :: a -> n
    instance (Num n) => ToNum Z n where
        toNum _ = 0
    instance (ToNum a n) => ToNum (S a) n where
        toNum _ = 1 + toNum (undefined :: a) 
    
    main = print $ (toNum p1, toNum m1, toNum f1, toNum pw1)
    

    Update:

    As danportin noted in his comment below TypeFamilies “Lazy pattern” (in instance context) is not needed here (his initial code is shorter and much cleaner).

    One application of this pattern though, which I can think of in the context of this question is this: Say we want to add Boolean logic to our type-level arithmetic:

    data HTrue
    data HFalse
    
    -- Will not compile
    class And x y r | x y -> r
    instance And HTrue HTrue HTrue
    instance And a b HFalse -- we do not what to enumerate all the combination here - they all HFalse
    

    But this will not compile due to “Functional dependencies conflict”.
    And it looks to me that we still can express this overlapping case without fundeps:

    class And x y r
    instance (r ~ HTrue) => And HTrue HTrue r
    instance (r ~ HFalse) => And a b r
    
    b1 = undefined :: And HTrue HTrue r => r   -- HTrue
    b2 = undefined :: And HTrue HFalse r => r  -- HFalse
    

    It’s definitely not a nicest way (it requires IncoherentInstances). So maybe somebody can suggest another, less ‘traumatic’ approach.

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

Sidebar

Related Questions

Edit: I have solved this by myself. See my answer below I have set
EDIT: Learned that Webmethods actually uses NLST, not LIST, if that matters Our business
EDIT: Solved My problem is that I need to find a way to constantly
Edit : Solved, there was a trigger with a loop on the table (read
edit #2: Question solved halfways. Look below As a follow-up question, does anyone know
Edit: From another question I provided an answer that has links to a lot
EDIT: This was formerly more explicitly titled: - Best solution to stop Kontiki's KHOST.EXE
EDIT: This question is more about language engineering than C++ itself. I used C++
EDIT: Solved with a hack for now. Added at line 473: if (isset($this->_termsFreqs[$termId][$docId])) {
Edit: Solved. Hi, I'm starting with Qt, I try to connect a slot to

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.