# \`#lang clingo\` for integration with the Clingo ASP solver

**URL:** https://racket.discourse.group/t/lang-clingo-for-integration-with-the-clingo-asp-solver/2590
**Category:** Show & Tell
**Created:** [December 11, 2023, 11:55am UTC](https://racket.discourse.group/t/lang-clingo-for-integration-with-the-clingo-asp-solver/2590 "2023-12-11T11:55:26Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![kiranandcode](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/kiranandcode/32/1504_2.png) [@kiranandcode](https://racket.discourse.group/u/kiranandcode)
#### Post date: [December 11, 2023, 11:55am UTC](https://racket.discourse.group/t/lang-clingo-for-integration-with-the-clingo-asp-solver/2590/1 "2023-12-11T11:55:26Z")

</div>

Hi all!! Just wanted to share a little lang that I've been working on over the weekend: `#lang clingo`

> **[GitHub - Gopiandcode/clingo-lang: #lang clingo for Racket (WIP)](https://github.com/Gopiandcode/clingo-lang)**
>
> \#lang clingo for Racket (WIP). Contribute to Gopiandcode/clingo-lang development by creating an account on GitHub.

This library/language provides FFI bindings to the libclingo Answer Set programming solver (think something like datalog with some funky semantics).

I've written a little wrapper lang ontop of these bindings to provide a racket-friendly syntax to it:

```scheme
#lang clingo

(set-clingo-option! "solve.models" 0)

(person (|| 'alice 'bob))

(evil 'alice)

(:- (good p)
    (person p) (not (evil p)))

;; returns '((person alice) (person bob)
;; (evil alice)
;; (good bob))

```

This corresponds to the following clingo program if you're familiar with it:

```clingo
person(alice;bob).
evil(alice).
good(P) :- person(P), not evil(P).

```

---

<div class="post-metadata">

### Author: ![kiranandcode](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/kiranandcode/32/1504_2.png) [@kiranandcode](https://racket.discourse.group/u/kiranandcode)
#### Post date: [December 11, 2023, 12:02pm UTC](https://racket.discourse.group/t/lang-clingo-for-integration-with-the-clingo-asp-solver/2590/2 "2023-12-11T12:02:22Z")

</div>

Happy to field any questions!

I'd like to also ask for a help question as well...

In the DSL, there are these prolog like rules --- I'd like to setup the syntax objects such that Racket interprets them as proper identifiers with the lines and everything.

Currently what I'm trying is:

> <https://github.com/Gopiandcode/clingo-lang/blob/2874782edb680c442922ece286558bafdb78a90c/clingo-lib/lang.rkt#L80>

Where this attribute `datum`, returns a function that sends the syntax object being visited back via a callback.

This callback is setup in the outermost step of the parsing, so when parsing a rule, before parsing the LHS, we setup a callback to store these syntax objects into a list:

```scheme
[(_ lhs:constraint rhs ...)
     (define defined-variables '())
     (define (handle-definition stx)
       (set! defined-variables (cons stx defined-variables))
       stx)
     (define data
       (parameterize ([current-resolve-symbol-fun handle-definition])
         ((attribute lhs.datum))))
     (with-syntax ([lhs-datum data]
                   [(var ...) defined-variables])
       #'(let-syntax ([var #'var] ...)
           (rule lhs-datum
               (list (clingo-constraint rhs) ...))))]

```

I then bind these syntax variables with let-syntax so that later when I run `syntax-local-value` they'll retrieve the syntax objects from the LHS, and can thus use them for source location information.

This is based off some examples I've seen in the docs, but honestly, I'm really don't have a very good clue of what I'm doing here... I would appreciate if anyone could point me in the right direction w.r.t this...

---

<div class="post-metadata">

### Author: ![jbclements](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/jbclements/32/11_2.png) [@jbclements](https://racket.discourse.group/u/jbclements)
#### Post date: [December 12, 2023, 1:07am UTC](https://racket.discourse.group/t/lang-clingo-for-integration-with-the-clingo-asp-solver/2590/3 "2023-12-12T01:07:12Z")

</div>

So, am I going to be excited to use this extension when I finally get past day 3 of Advent of Code? Perhaps so...
