PyCLP Module Reference — PyCLP productive documentation

# PyCLP Module Reference

pyclp.addPythonFunction(eclipse_name, func)

  Register a python function to be called from Eclipse using the predicate call_python. It shall be called after `init()`

  E.g. call_python(<eclipse_name>,<list of terms.>).

  The registered python function will be called as:

  <func>(<list of terms>)

  The registered function shall return pyclp.SUCCEED or any other value for reporting FAIL.

  Parameters:

    - eclipse_name – str or byte string. Name to be used to with predicate call_python_function

    - func – It shall be the function to be called.

pyclp.cleanup()

  This shutdown the Eclipse engine. After calling this function any operation on pyclp object or class could crash the program.

  Raise:

    pyclpEx in case of failure or if eclipse engine is already shutdown

  Warning

  If after a cleanup it is called again `pyclp.init()` all terms created before the cleanup are not valid and they need to be rebuilt.

pyclp.cut()

  Cut all choice point of succeeded goal. Equivalent to void ec_cut_to_chp(ec_ref) It can be called only if previous resume call SUCCEED.

  See also

  ec_cut_to_chp

    cut function in ECLiPSe in C interface library

  For an example see Cut Example

pyclp.init()

  This shall be called before calling any other function. This initialize Eclipse engine.

  Raise:

    pyclpEx in case of failure or if eclipse engine is already initialized

pyclp.post_string(string_to_post)

  This should only be used if speed is not critical and if the goal does not contain variables whose values may be needed later.

pyclp.unify(term1, term2)

  Implements unify as described in ec_unify This function shall be used only inside python function tha are called from ECLiPSe

  Returns:

    pyclp.SUCCEED or pyclp.FAIL

pyclp.resume(in_term=None)

  Resume Eclipse engine. Implements the functionality of ec_resume,ec_resume1,ec_resume2. It accepts optional argument in_term. Used to return a value to the prolog predicate yield/2

  Parameters:

    in_term (`PList`, `Atom`, `Compound`) –

    Optional argument in_term. Used to return a value to the prolog predicate yield/2

  Return type:

    tuple

  Returns:

    (pyclp.SUCCEED,None) if execution succeed (equivalent to True). In this case it is possible to call pyclp.cut()

    (pyclp.FAIL,None) if the goal fails.

    (pyclp.FLUSHIO,stream_number) if some data is present in stream identified by stream_number

    (pyclp.WAITIO,stream_number) if eclipse engine try to read data from stream identified by stream_number

    (pyclp.YIELD, yield_returned_value) in case of predicate call yield/2.

    (pyclp.THROW, Term TagExit) an event have been thrown and no one have catched it exit_block/1.

  Warning

  After receiving FLUSHIO or WAITIO a new resume shall be issued before creating variable or calling post_goal to complete the goal execution and avoid unpredictable behavior.

  Note

  PyCLP have a different behavior compared to C/C++/Java default libraries provided by ECLiPsE. Standard resume execution destroys all the terms built before the execution of resume function while PyCLP is preserving them creating a reference and storing the created term in this.

  See also

  ec_resume,ec_resume1,ec_resume2

    Resume functions in ECLiPSe in C interface library.

class pyclp.Atom(atom_id)

  Class to create Atom.

  Parameters:

    atom_id (string) – atom name

  Inherited-members:

  __str__()

    Convert to string for pretty printing

class pyclp.Compound(functor_string, *args)

  Class to create compound terms.

  Parameters:

    - functor_string – A string with functor name.

    - args – Any number of arguments of type integer, float,string and `PList`, `Atom`, `Compound`

  len(arg) function called with a Compound object return the arity of compound term.

  This class support iterator protocol this means that you can iterate over term arguments or get the arguments by index protocol:

  Example:

      init()
      my_compound=Compound("test",1,"dummy")
      for x in my_compound:
          print(x)
      print(my_compound[0])  # Print first argument.

  Warning

  As for all other terms it is not possible to change their values.

  Inherited-members:
    __str__()

      Return str(self).

  arity()
    Returns:

      arity of Compound object.

  functor()
    Returns:

      string storing name of functor

class pyclp.Var

  Class to create Prolog variable.

  Inherited-members:
    __str__()
      Returns:

        Return pretty print string of object unified to this variable. If variable is uninstantiated it returns ‘_’

  value()
    Return type:

      integer, float, string, `PList`, `Atom`, `Compound`, None (if var is uninstantiated)

class pyclp.PList

  Class to create and read Prolog lists.

  When creating a new instance a list or tuple shall be provided. string,float and integer are automatically transformed in term as in Compound class. This class support iterator protocol this means that you can loop on the list as for python list

  Example:

      init()
      my_list=PList([1,2,3])
      for x in my_list:
          print(x)

  This class support retrieving values by indexing.

  Example:

      init()
      my_list=PList([1,2,3])
      print(my_list[3])

  Warning

  As for all other terms it is not possible to change their values.

  Special cases

  Empty prolog list can be created with PList([]) To check that a returned PList is the empty list it avaiable the method `pyclp.PList.isNil()`

  Head Tail

  In prolog it is possible to define a list using the operator |

  Example of prolog list and head tail decomposition:

      %Prolog list example
      [1,2,3|myAtom]
      [1|A]
      %Also regular list have a tail: []
      [1,2,3]=[1,2,3|[]]
      %All list can be decomposed recursively as head tail couple
      [1,2,3]
      [1|[2,3]]
      [1|[2|[3]]]
      [1|[2|[3|[]]]]

  __str__()

    Pretty printing of the list

  getListTail()

    Convert a PList in a python list and the tail.

    Examples:

        prolog              python
        [1,2,3,f(1,2)]  --> ([1,2,3,Compound('f',1,2)],PList([]))
        [1,2,3|A]       --> ([1,2,3],Var())
        [1,2,3|dummy]   --> ([1,2,3],Atom('dummy'))

    Returns:

      a tuple (list,tail). First element is a python list containing each element of prolog list converted to a pyclp object second element is a pyclp object representing the tail of prolog list.

  isNil()

    Check if the PList is the nil list –> []

    Returns:

      True if it is the empty list

  iterHeadTail()
    Returns:

      Iterator that returns a tuple (head,tail) where head is a element of the list and tail is the remaining list

  iterheadtail()

    Deprecated since version 1.5.

    Replaced by `PList.iterHeadTail()`

  post_goal()

    Post goal

class pyclp.Stream(name)

  Class to support streams to and from ECLiPSe. This is class is derived from io.RawIOBase.

  Parameters:

    name – string containing stream name of a previously opened stream by ECLiPSe program. See: Embedded C stream api and get_stream/2

  Note

  The following stream are already opened: ‘input’, ‘output’, ‘error’, ‘warning_output’, ‘log_output’, ‘stdin’, ‘stdout’, ‘stderr’, ‘null’.

  Raises:

    IOError – if name is not matching a previously open stream by open/3 and open/4

  read(n=-1)

    Read all bytes from stream

    Parameters:

      n – Number of bytes to be read if omitted or equal to -1 it will return all avaiable bytes.

    Returns:

      bytes object

  write(buffer)

    Write a bytes object to stream. :type buffer: bytes object. :return: number of bytes written

  __eq__(other)

    Return self==value.

  readall()

    Read all available bytes equivalent to `pyclp.read()`

exception pyclp.pyclpEx(arg)