Your First Program

Write a social-network reachability query in under 5 minutes.

What You'll Build

A simple social network that tracks who follows whom, and computes who can reach whom through any chain of follows.

Step 1: Start the Client

inputlayer-client

You'll see:

Connecting to server at http://127.0.0.1:8080...
Connected!

Server status: healthy
Authenticated as: admin
Current knowledge graph: default

Step 2: Create a Knowledge Graph

Every InputLayer program runs in a knowledge graph. Let's create one:

.kg create social

Output:

Knowledge graph 'social' created.
Switched to knowledge graph: social

You can verify with:

.kg

Output:

Current knowledge graph: social

Step 3: Add Facts

Facts are the base data in your knowledge graph. Let's add some "follows" relationships:

+follows(1, 2)

Output:

Inserted 1 fact(s) into 'follows'.

Let's add more facts using bulk insert:

+follows[(2, 3), (3, 4), (1, 3)]

Output:

Inserted 3 fact(s) into 'follows'.

Step 4: Query Facts

Use ? to query data:

?follows(1, X)

Output:

1X
12
13

2 rows

This shows everyone that user 1 directly follows.

Step 5: Define a Rule

Rules derive new data from existing facts. Let's define "reachable" - who can you reach through any chain of follows?

+reachable(X, Y) <- follows(X, Y)

Output:

Rule 'reachable' registered.

This says: "X can reach Y if X follows Y directly."

But we also want transitive reachability. Add another clause:

+reachable(X, Z) <- follows(X, Y), reachable(Y, Z)

Output:

Rule 'reachable' registered.

This says: "X can reach Z if X follows someone Y who can reach Z."

Step 6: Query the Rule

Now query the derived relation:

?reachable(1, X)

Output:

1X
12
13
14

3 rows

User 1 can reach users 2, 3, and 4! Even though user 1 doesn't directly follow user 4, they can reach them through the chain: 1 -> 2 -> 3 -> 4.

Step 7: View Your Rules

See what rules are defined:

.rule

Output:

Rules:
  reachable (2 clause(s))

See the rule definition:

.rule def reachable

Output:

Rule: reachable
Clauses:
  1. reachable(X, Y) <- follows(X, Y)
  2. reachable(X, Z) <- follows(X, Y), reachable(Y, Z)

Step 8: Add More Data and See Incremental Updates

Add a new follows relationship:

+follows(4, 5)

Now query reachable again:

?reachable(1, X)

Output:

1X
12
13
14
15

4 rows

User 1 can now reach user 5! InputLayer automatically recomputed the derived relation when you added new data.

Complete Program

Here's everything we did in one script:

// Create and use knowledge graph
.kg create social
.kg use social

// Add base facts
+follows[(1, 2), (2, 3), (3, 4), (1, 3)]

// Define transitive reachability
+reachable(X, Y) <- follows(X, Y)
+reachable(X, Z) <- follows(X, Y), reachable(Y, Z)

// Query: who can user 1 reach?
?reachable(1, X)

You can save this to a file social.idl and run it:

inputlayer-client --script social.idl

Key Takeaways

  1. Facts (+relation(...)) are base data you insert
  2. Rules (+head(...) <- body) derive new data from existing data
  3. Queries (?pattern) ask questions about your data
  4. Incremental - When you add/remove facts, derived data updates automatically
  5. Persistent - Facts and rules are saved to disk

What's Different from SQL?

SQLInputLayer
INSERT INTO follows VALUES (1, 2)+follows(1, 2)
CREATE VIEW (limited recursion)+rule(...) <- body (full recursion)
SELECT * FROM follows WHERE a = 1?follows(1, X)
Explicit JOINsImplicit joins via shared variables

Next Steps

  • Core Concepts - Deeper understanding of facts, rules, and queries
  • REPL Guide - All the commands available
  • Recursion - Learn about recursive queries and graph traversal