npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

logic_js_dcg

v1.0.0

Published

Definite clause grammars for logic.js

Downloads

3

Readme

logic.js definite clause grammars

This library is a DCG package for logic.js (not LogicJS!). Its functionality is similar to Prolog's DCGs.

Usage

npm add logic_js_dcg

The equivalent of ,//2 and ;//2 are and_ and or_. There is no need for -->, just use and_, or_, or phrase if there is just one argument. You can specify terminals in [] just as in Prolog.

import { run, lvar } from "logic_js"
import { and_, or_, phrase } from "logic_js_dcg"
const
  verb = phrase(['chases']),
  noun = or_(['cat'], ['mouse']),
  det = or_(['the'], ['a']),
  noun_phrase = and_(det, noun),
  verb_phrase = and_(verb, noun_phrase),
  sentence = and_(noun_phrase, verb_phrase)
const x = lvar('x')
run([x], sentence(x, []))
  // => [ { x: [ 'the', 'cat', 'chases', 'the', 'cat' ] }, { x: [ 'the', 'cat', 'chases', 'the', 'mouse' ] }... 

If you want your productions to have arguments, or you need to declare local logic variables, turn them into functions. For conditions, no need for Prolog's {}, just use a logic value as it is:

import { run, lvar, eq } from "logic_js"
import { and_, or_ } from "logic_js_dcg"
const
  det2 = (num) => or_(
    ['the'],
    and_(eq(num, 'sg'), ['a']),
    and_(eq(num, 'pl'), []),
  ),
  noun2 = (num) => or_(
    and_(eq(num, 'sg'), or_(['cat'], ['mouse'])),
    and_(eq(num, 'pl'), or_(['cats'], ['mice']))
  ),
  noun_phrase2 = (num = lvar()) => and_(det2(num), noun2(num))
const x = lvar('x')
run([x], noun_phrase2()(x, []))
[
  { x: [ 'the', 'cat' ] },
  { x: [ 'the', 'mouse' ] },
  { x: [ 'the', 'cats' ] },
  { x: [ 'the', 'mice' ] },
  { x: [ 'a', 'cat' ] },
  { x: [ 'a', 'mouse' ] },
  { x: [ 'cats' ] },
  { x: [ 'mice' ] }
]

Productions are two-argument functions returning a logic value, the two arguments are the difference list just like in Prolog (but in Prolog the two arguments are appended to the explicit arguments, and here they are curried to a separate function call)

const num = lvar('num')
run([num, x], det2(num)(x, []))

So the different types you can pass into phrase or any argument of and_ and or_.

// list of terminals
and_(['a', 'cat'], /* ... */)
// logic value
and_(eq(a, b), /* ... */)
// production (possibly returned by a factory)
and_(det, /* ... */)
and_(det2('pl'), /* ... */)
// nullary function returning a logic value, possibly with optional arguments
and_((x = lvar()) => eq(x, a), /* ... */)
// nullary function returning a production, possibly with optional arguments
and_((x = lvar()) => det2(x), /* ... */)

Also, just like in logic.js, you can pass a number as the first argument of or_ to limit the number of solutions.

run([x], or_(2, noun_phrase2)(x, []))
// => [ { x: [ 'the', 'cat' ] }, { x: [ 'the', 'mouse' ] } ]

Note that you don't need to call the nullary noun_phrase2 as it appears as an argument to or_.