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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:58:50+00:00 2026-06-02T18:58:50+00:00

So I am new to prolog and I am suppose to implement a type

  • 0

So I am new to prolog and I am suppose to implement a type checker. How exactly should I go about it? This would be an example:

String s; int i; i = s.length(); // OK (example given in the homework)

When I asked the professor how things will be input, it would look something like this:

instance(s, string).

Thats great, except if this is done, the unification for i is lost at the close of the query, so if I were to make a say, equals fact and call it like so,

equals(i, s, '.', 'length').

how can i check what i is. So I am just having a hard time knowing where to start. Its a homework, so just want some advice, help on kind of understanding how to go about my first prolog project. Thanks in advance.

EDIT: Assignment

Write a Prolog program that can type check the method calls for a given Java program
according to the JLS . The fact base can be any encoding of the methods defined in any
non-trivial Java program you have written, plus, minimally, those listed below. In query
mode, it must check potential matches; for example allowing “println(string).” You need
not encode those JLS rules that you don’t need. (One of the examples given is above.)

  • 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-06-02T18:58:51+00:00Added an answer on June 2, 2026 at 6:58 pm

    I intended what follows as a starting point. Here is my formalisation:

    type(string).
    

    means that string is an available type for a variable to be an instance of

    signature(=, [X, X, nil]).
    

    means that the infix operator = takes two arguments of same type and returns nothing.

    is_instance(X, Y)
    

    means that X is an instance of type Y
    To test things out, I created a list of “statements” contained in my variable Input of my test/0 predicate. Then I recursively test out if things are well. You have to implement the third clause as a recursive call to find out if types are ok in expressions now.

    What I did atm is that in my first main check/2 clause, I handle instance/2 terms, and in the following one, all the rest.

    :- dynamic is_instance/2.
    
    type(string).
    type(int).
    
    signature(=, [X, X, nil]).
    
    test :-
        retractall(is_instance(_, _)),
        Input = [instance(s, string), instance(i, int), =(i, length(s))],
        check(Input, ReturnTypes),
    
    check([], []).
    check([instance(Variable, Type)|Terms], [nil|ReturnTypes]) :-
        !,
        ( is_instance(Variable, _) -> syntax_error('Variable already declared')
        ; \+ type(Type) -> syntax_error('Using a non-existing type'),
        ; Term =.. [is_instance, Variable, Type],
          assertz(Term)),
        check(Terms, ReturnTypes).
    
    check([Term|Terms], [Type|ReturnTypes]) :-
        Term =.. [Name|Arguments],
        % Here we have to call ourselves with our list of arguments
        % and then check that everything is fine and then we'll unify Type
        % with the return value of Name.
        check(Terms, ReturnTypes).
    

    I hope it’ll help to get you started.

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

Sidebar

Related Questions

I am new to prolog and i want to solve this problem. Suppose I
I'm new to Prolog. Using this basic 'database' structure, I thought I would be
I'm new to prolog and I just can't figure this out. I'm trying to
I am a bit confused about this function definition in Prolog: sample(X,[X|Tail]) :- member(X,Tail).
I'm new to Prolog and stuck on some programming homework. one of them should
I am new in Prolog and I was just thinking that why this rule
I have a prolog homework, which should work like this: singles([1,2,3,2,2,4,1], [3,4]). true Now
(Pardon if my terminology is wrong... I'm new to Prolog.) Suppose you have a
I'm new to Prolog and my assignment requires us to implement a function as
I am new into the world of Prolog, and I would like to write

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.