Scallop Documentation

repository·master·Indexed 19 days ago

https://github.com/scallop-lang/scallop

A DataLog-based language for differentiable logical and relational reasoning using a generalized Provenance Semiring framework. It integrates with Python and PyTorch via the scallopy bindings and supports a modular architecture consisting of a compiler (with front, back, and ram IRs), a runtime (including a Static Runtime for optimal performance), and an integrate module for high-level execution. The ecosystem includes a plugin system for extending capabilities with external models like GPT, Gemini, and CodeQL.

Tokens
40.5K
Snippets
137
Records
173
Agent score
68%

What's inside Scallop

  1. What is Scallop?

    master

    Scallop is a language based on DataLog that supports differentiable logical and relational reasoning. It is designed for neurosymbolic programming, allowing users to integrate logical reasoning with neural components.

    Key capabilities include:

    • Logic Programming: Use it as a Datalog engine with support for relational programming, negation, aggregation, and queries.
    • Probabilistic Programming: Tag facts with probabilities to perform probabilistic reasoning.
    • Neurosymbolic Integration: Use the scallopy Python binding to integrate Scallop programs with machine learning libraries like PyTorch.
    • Hybrid Reasoning: Combine knowledge base facts, rules, and probabilistic facts (e.g., from image recognition) in a single program.
  2. Integrate Scallop with Foundation Models

    master

    Scallop can be extended with foundation models to handle unstructured data. This includes:

    • OpenAI GPT: Integrating large language models.
    • Text Embeddings: Using vector representations of text.
    • Vision Models and Image Processing: Handling and processing image data.
    • Scallop Plugins: Creating custom plugins to extend Scallop's capabilities with foreign functions, predicates, and attributes.
  3. Learn the Scallop Language Reference Guide

    master

    Scallop is a logic programming language that supports advanced features like provenance and probabilistic programming. The language reference guide covers:

    Core Logic Programming

    • Relations and Facts: Defining the basic building blocks of the program.
    • Writing Rules: Implementing logic via rules.
    • Values and Types: Understanding the data model.
    • Writing a Query: How to extract information from the program.
    • Recursive Rules: Implementing recursion.
    • Negations and Aggregations: Advanced logical operations.
    • Algebraic Data Types (ADT) and Entities: Complex data structures.
    • On-Demand Predicates: Using magic sets for efficiency.
    • Loading from CSV: Importing data.
    • Foreign Functions and Predicates: Integrating external code.

    Provenance and Probabilistic Programming

    • Provenance: Tracking the origin of facts.
    • Proofs Provenance: Understanding the logical proofs behind facts.
    • Facts with Probability: Working with probabilistic logic.
    • Logic and Probability: Combining reasoning with uncertainty.
    • Sampling with Probability: Using probabilistic sampling techniques.
  4. Use Scallop with Python via `scallopy`

    master

    The scallopy package provides Python bindings for Scallop. It allows you to integrate Scallop's logic engine into Python workflows. Key capabilities include:

    • Scallop Context: Managing the execution environment.
    • Branching Executions: Handling different execution paths.
    • Configuring Provenance: Controlling how provenance data is tracked.
    • Module Management: Creating modules and configuring input/output relations.
    • Foreign Integration: Using Python foreign functions and predicates within Scallop.
    • Persistence: Saving and loading Scallop states.
    • Debugging: Inspecting and debugging proofs.
  5. Explore the Scallop Toolchain

    master

    The Scallop toolchain provides several interfaces for interacting with the language:

    • Scallop CLI: Command-line interface for managing tasks.
    • Scallop Interpreter (scli): For executing Scallop programs.
    • Scallop REPL (sclrepl): An interactive read-eval-print loop for testing logic.
    • Scallop Compiler (sclc): For compiling Scallop code.
  6. Define boundness patterns for Foreign Predicates

    master

    When working with foreign predicates, Scallop uses a boundness pattern to determine the flow of data. A boundness pattern is a string of characters with a length equal to the relation's arity, consisting of:

    • b (bounded): The variable at this position is an input provided to the predicate.
    • f (free): The variable at this position is generated as an output by the predicate.

    For example, the range predicate is defined as range<T: Integer>(begin: T, end: T, i: T)[bbf]. This indicates that begin and end are inputs (b), and i is the generated output (f).

  7. Understand Scallop Rule Syntax

    master

    Rules are the fundamental unit of computation in Scallop. A rule defines how data flows from a source relation (the body) to a target relation (the head).

    Basic syntax follows the pattern: rel HEAD_RELATION = BODY_FORMULA

    Rules are read from right to left: when the body formula holds, the head also holds. The body can consist of atoms, negated atoms, constraints, aggregations, and logical connectives (and, or).

    Scallop also supports traditional Datalog syntax by using :- instead of = and , instead of and.

    // Standard Scallop syntax
    rel path(a, b) = edge(a, b)
    
    // Traditional Datalog syntax
    rel path(a, b) :- edge(a, b)
  8. How Group By works in Scallop

    master

    In Scallop, aggregation functions like count and exists use a 'Group By' mechanism to determine how results are partitioned.

    1. Identifying Group-By Variables: A variable is considered a group-by variable if it is bound in the body of the aggregation and appears in the head of the rule, but is not part of the aggregation's internal logic (i.e., it is not the 'to-aggregate' variable or an 'argument' value).
    2. To-Aggregate Variables: These are the variables that are being operated on by the aggregation function (e.g., the o in count(o: ...)).
    3. Filtering with where: You can use the where clause within an aggregation to explicitly define which variables are part of the grouping logic or to provide additional context for the aggregation.

    If a variable is bound in the body but does not appear in the rule head, it is not treated as a group-by variable.

    // Example of a count with a group-by variable 'c'
    rel num_cars_of_color(c, n) :- n = count(o: is_a(o, "car"), color(o, c))
  9. Perform atomic queries to retrieve specific relation elements

    master

    An atomic query allows you to retrieve a specific subset or a single element of a relation by providing constant values for some arguments. Instead of returning the entire relation, the output will only contain the tuples that match the provided constraints. This is useful for looking up specific values, such as a specific index in a sequence.

    type fib(x: i32, y: i32)
    rel fib = {(0, 1), (1, 1)}
    rel fib(x, y1 + y2) = fib(x - 1, y1) and fib(x - 2, y2) and x <= 10
    
    // Atomic query: find the y value for the 8th fibonacci number
    query fib(8, y) // Result: {(8, 34)}
  10. Declare facts and rules in Scallop

    master

    Scallop uses a syntax based on DataLog and logic programming.

    Fact Declaration

    • Single non-probabilistic fact: rel digit(0, 1)
    • Single probabilistic fact: rel 0.3::digit(0, 1)
    • Set of facts:
      rel digit = {
        0.4::(0, 1),
        0.3::(0, 2),
      }

    Rule Declaration

    • Traditional Datalog: rel path(a, b) :- edge(a, b)
    • Logic Programming style: rel path(a, c) = edge(a, c) or (path(a, b) and edge(b, c))
    • Probabilistic rules: rel 0.3::path(a, b) = edge(a, b)
    // Knowledge base facts
    rel is_a("giraffe", "mammal")
    rel is_a("tiger", "mammal")
    rel is_a("mammal", "animal")
    
    // Knowledge base rules
    rel name(a, b) :- name(a, c), is_a(c, b)
    
    // Recognized from an image, maybe probabilistic
    rel name = {
      0.3::(1, "giraffe"),
      0.7::(1, "tiger"),
      0.9::(2, "giraffe"),
      0.1::(2, "tiger"),
    }
    
    // Count the animals
    rel num_animals(n) :- n = count(o: name(o, "animal"))
  11. Use boolean values and logical operations

    master

    Boolean values are true and false.

    Supported Operations:

    • Comparisons: ==, !=
    • Logical: ! (unary negate), && (binary and), || (binary or), ^ (binary xor)
    type variable_assign(String, bool)
    rel variable_assign = {("a", true), ("b", false)}
    
    // Example using XOR
    rel result(a ^ b) = variable_assign("a", a) and variable_assign("b", b)