1 Embedded Parenlog
define-model
: -
query-model
model?
2 Standalone Parenlog
?
next

Parenlog

Jay McCarthy <jay@plt-scheme.org>

This package contains an implementation of a language very similar to pure Prolog, except with parenthetical notation.

1 Embedded Parenlog

The easiest way to get started using Parenlog for PLT Scheme is with the main module:

 (require (planet jaymccarthy/parenlog:1:0))

Here is a basic example of using Parenlog:

Examples:

  > (define-model family-tree
      (parent rogue moria)
      (parent rogue larn)
      (parent rogue omega)
      (parent rogue hack)
      (parent moria angband)
      (parent hack nethack)
      (parent angband tome)
      (parent angband zangband)
      (parent omega adom)
      (parent nethack adom)
      (parent nethack zapm)
      (parent nethack slashem)
      (parent nethack crawl)
    
      (:- (sibling X Y)
          (parent Z X)
          (parent Z Y)
          (,(compose not equal?) X Y)))
  > (query-model family-tree
                 (sibling adom zapm))

  (#hasheq())

  > (query-model family-tree
                 #:limit 4
                 (sibling X Y))

  (#hasheq((X. moria)            (Y. larn)) #hasheq((X. moria)            (Y. omega)) #hasheq((X. moria)            (Y. hack)) #hasheq((X. larn)            (Y. moria)))

(define-model id stmt ...)
 
stmt = head-query
  | (:- head-query body-query ...)
     
head-query = s-expr
     
body-query = s-expr
  | (,fun s-expr ...)
 
  id : identifier?
  fun : (any/c ... -> boolean?)
Defines id as a Parenlog model.

:-
Syntax that may only appear within define-model.

(query-model model-expr maybe-limit body-query)
 
maybe-limit = 
  | #:limit limit-expr
 
  model-expr : model?
  limit-expr : number?
Queries model-expr with body-query until limit-expr results are found or no results remain.

Returns a value matching the contract: (listof (hash/c symbol? anc/c)). Each value in this list is a substitution of body-query that model-expr proves.

(model? v)  boolean?
  v : any/c
Returns #t if v was bound by define-model; #f otherwise.

2 Standalone Parenlog

 (require (planet jaymccarthy/parenlog:1:0/lang))

Parenlog can also be used as a standalone module language.

#lang planet jaymccarthy/parenlog:1:0

At a high level, the body of a Parenlog module is the body of a define-model form and any REPL interaction is placed within a query-model form. There are, of course, a few caveats.

First, occurrences of (? body-query) in the module body are delayed and used as queries.

Second, all query results are printed. no is printed when there are no answeres. yes is printed when there is an empty substitution as an answer. Otherwise, the substitution is printed using (hash-for-each subst (curry printf "~a=~a~n")), resulting in displays like:
  X=moria
  Y=larn

Third, next can be input at the REPL to search for another answer to the last query. If there was no last query, this evaluates to an error. If there are no more answers, done is printed.

Here is a sample module:
  #lang planet jaymccarthy/parenlog:1:0
  (type Gamma numConst num)
  (type Gamma boolConst bool)
  
  (:- (type Gamma (if Test Then Else) Tau)
      (type Gamma Test bool)
      (type Gamma Then Tau)
      (type Gamma Else Tau))
  
  (? (type mt numConst num))
  (? (type mt (if boolConst numConst numConst) num))
  (? (type mt (if boolConst numConst boolConst) num))
  
  (? (type mt boolConst T))
  (? (type mt (if boolConst numConst numConst) T))
  (? (type mt (if boolConst numConst boolConst) T))

If this module is evaluated, it prints:
  yes
  yes
  no
  T=bool
  T=num
  no

We can then query the model from the REPL:

  > next
  done
  > (type mt T num)
  T=numConst
  > next
  T=(if boolConst numConst numConst)
  > next
  T=(if boolConst numConst (if boolConst numConst numConst))

?
Syntax that may only appear within the body of a Parenlog module.

next
Syntax that may only appear within the REPL of a Parenlog module.