---
# System prepended metadata

title: TP Prolog - s12 - ISC IL 3a
tags: [prolog]

---

###### tags: `prolog`

# TP Prolog - s12 - ISC IL 3a
## Baudin Valentin et Braillard Simon

## Exercice 1
Tout d'abord, adaptons le augm.pl
```prolog=
:- foreign(my_square(+integer,?integer)).
:- foreign(mySquare(-integer,+integer), [choice_size(0)]).
```

le choice_size permet de spécifier qu'une fonction implémente un code non déterministe.

Puis voici les fonctions en C:

```C=
PlBool my_square(PlLong n, PlFIOArg *n_square) {
  if (n_square->is_var) {
    n_square->value.l = n * n;
    return PL_TRUE;
  } else {
    return n_square->value.l == (n * n) ? PL_TRUE : PL_FALSE;
  }
}

PlBool mySquare(PlLong *n, PlLong n_square) {
  if (n_square) {
    if (Pl_Get_Choice_Counter() == 0) {
      *n = sqrt((double)n_square);
    }

    if (Pl_Get_Choice_Counter() == 1) {
       *n = -*n;
       Pl_No_More_Choice();  // stop generating solutions
    }
    return PL_TRUE;
  }

  return PL_FALSE;
}
```

### Output 
```raw=

| ?- mySquare(X, 9).
X = 3 ? a
X = -3
yes

| ?- my_square(2, 4).
yes

| ?- my_square(2, 5).
no

| ?- my_square(2, X).
X = 4
yes
```

## Exercice 2

```java=
  static int square(int n) throws IOException {
    String cmd = "mySquare(" + n + ", M).";
    send(cmd);
    String resKey = "M = ";
    String line = fromProlog.readLine();
    while(! line.startsWith(resKey)) {
      line = fromProlog.readLine();          // purge non-result lines
    }
    int res = 0;
    line = line.substring(resKey.length());
    try{
      res = Integer.parseInt(line);
    }
    catch (NumberFormatException ex){
      ex.printStackTrace();
    }
    return res;
  }
```

```java=
  public static void startProlog() throws IOException {
    String[] args = {INTERP_NAME};
    ProcessBuilder pb = new ProcessBuilder(args);
    pb.environment().put("LINEDIT","gui=no");  // no window on Win32
    Process prcss = null;
    try {
      prcss = pb.start();
    } catch (IOException e) {
      System.out.println("Problème de lancement de gprolog: " + e);
    }
    fromProlog = new BufferedReader(
        new InputStreamReader(prcss.getInputStream()));
    toProlog = new PrintWriter(
        new PrintWriter(prcss.getOutputStream(), true));
    send("[" + FILE_NAME + "].");  // to get the file "consulted"
  }
```

### Output
```
9
36
```

## Exercice 3
```java=
public class CalledFromProlog {
  // "Protocol" :    Request             Output
  //                 -------             ------
  //                 "square" \n int     int
  //                 "bye"               -
  // ------------------------------------------------------------
  static PrintWriter toProlog;

  public static void main(String[] args) throws IOException {
    BufferedReader kbd = new BufferedReader(new InputStreamReader(System.in));
    toProlog = new PrintWriter(new OutputStreamWriter(System.out), true);
    String line = kbd.readLine();
    while(!line.equals("bye")) {         // termination request
      if (line.equals("square")) {       // kind of request
        line = kbd.readLine();
        int res = 0;
        try {
          res = Integer.parseInt(line); // read arguments
        } catch (IllegalArgumentException e) {
          System.out.println(e);
        }
        res = square(res); // perform the requested computation
        send(String.valueOf(res)); // write result
      } else {                           // unknown request - ignored
      }
      line =  kbd.readLine();
    }
  }
  // ------------------------------------------------------------
  public static int square(int x) {
    return x*x;
  }

  public static void send(String s) throws IOException {
    toProlog.println(s); 
    toProlog.flush();
  }
}
```

### Output
```
| ?- > go.
output is 9
output is 81

(1 ms) yes
```