logic 1.0.0

logic.dart #

This is a practice project of porting logic.js (minikanren) to dart.

Usages #

Core #

lvar, run, and, or, eq

import "package:logic/logic.dart";

final x = lvar("x"); // define logic variable with id 'x'

run([x], eq(x, 1)); // query 'x' => [{x: 1}]
run([x], () {
  final y = lvar("y");
  return and(
    eq(y, 1),
    eq(x, y));
}); // => [{x: 1}]

run([x], or(eq(x, 1), eq(x, 2)));    // [{x: 1}, {x: 2}]
run([x], or(eq(x, 1), eq(x, 2), count: 1)); // [{x: 1}]
run([x], or(eq(x, 1), eq(x, 2)), count: 1); // [{x: 1}]

Facts #

facts

// FACTS
final parent = facts([
  ['Steve', 'Bob'],      // Steve is Bob's parent
  ['Steve', 'Henry'],    // Steve is Henry's parent
  ['Henry', 'Alice']     // Henry is Alice's parent
]);
run([x], parent(x, 'Alice'));     // who is Alice's parent => ['Henry']
run([x], parent('Steve', x));     // who are Steve's children => ['Bob', 'Henry']

// RULES
var grandparent = (x, y) {
  final z = lvar();
  return and(parent(x, z), parent(z, y));
};

run([x], grandparent(x, 'Alice'));  // who is Alice's grandparent => ['Steve']

Array manipulation #

conso, firsto, resto, emptyo, membero, appendo

final x = lvar('x');
final y = lvar('y');

run([x], membero(x, [1, 2, 3]));
// [{x: 1}, {x: 2}, {x: 3}]

run([x, y], conso(x, y, [1, 2, 3]));
// [{x: 1, y: [2, 3]}]

run([x, y], appendo(x, y, [1, 2]));
/*
[ {x: [], y: [1, 2]},
  {x: [1], y: [2]}
  {x: [1, 2], y: []} ]
*/

Arithmetic & Comparison

add, sub, mul, div, lt, le, gt, ge

run([x], add(2, x, 5));
// [{x: 3}]

Extra #

succeed, fail, anyo

run([x], and(eq(x, 1), succeed()));
// [{x: 1}]

run([x], and(eq(x, 1), fail()));
// []

run([x], or(
  eq(x, 1),
  eq(x, 2),
  eq(x, 3)
)); // [{x: 1}, {x: 2}, {x: 3}]

run([x], or(
  eq(x, 1),
  and(eq(x, 2), fail()),
  eq(x, 3)
)); // [{x: 1}, {x: 3}]

run([x], anyo(or(eq(x, 1), eq(x, 2), eq(x, 3))), count: 4);
// [{x: 1}, {x: 2}, {x: 3}, {x: 1}]

Summary #

I encountered a few problems while rewriting the logic.js to logic.dart.

  • The dart language doesn't support variadic functions. So I hacked the and and or functions. #16253.

  • ...

References #

logic.js

0.0.1 #

  • Initial release.

1.0.0 #

  • Initial 1.0.0 release.

example/logic_example.dart

import 'package:logic/logic.dart';

main() {
  // ====== CORE ======
  final x = lvar("x"); // define logic variable with id 'x'

  run([x], eq(x, 1)); // query 'x' => [{x: 1}]
  run([x], () {
    final y = lvar("y");
    return and(
      eq(y, 1),
      eq(x, y));
  }); // => [{x: 1}]

  run([x], or(eq(x, 1), eq(x, 2)));    // [{x: 1}, {x: 2}]
  run([x], or(eq(x, 1), eq(x, 2), count: 1)); // [{x: 1}]
  run([x], or(eq(x, 1), eq(x, 2)), count: 1); // [{x: 1}]

  // ====== FACTS ======
  final parent = facts([
    ['Steve', 'Bob'],      // Steve is Bob's parent
    ['Steve', 'Henry'],    // Steve is Henry's parent
    ['Henry', 'Alice']     // Henry is Alice's parent
  ]);
  run([x], parent(x, 'Alice'));     // who is Alice's parent => ['Henry']
  run([x], parent('Steve', x));     // who are Steve's children => ['Bob', 'Henry']

  // ====== RULES ======
  var grandparent = (x, y) {
    final z = lvar();
    return and(parent(x, z), parent(z, y));
  };

  run([x], grandparent(x, 'Alice'));  // who is Alice's grandparent => ['Steve']


  // ====== ARRAY ======
  final y = lvar('y');

  run([x], membero(x, [1, 2, 3]));
  // [{x: 1}, {x: 2}, {x: 3}]

  run([x, y], conso(x, y, [1, 2, 3]));
  // [{x: 1, y: [2, 3]}]

  run([x, y], appendo(x, y, [1, 2]));

  // ====== ARITHMETIC ======
  run([x], add(2, x, 5));

  // ====== EXTRA ======
  run([x], and(eq(x, 1), succeed()));
  // [{x: 1}]

  run([x], and(eq(x, 1), fail()));
  // []

  run([x], or(
    eq(x, 1),
    eq(x, 2),
    eq(x, 3)
  )); // [{x: 1}, {x: 2}, {x: 3}]

  run([x], or(
    eq(x, 1),
    and(eq(x, 2), fail()),
    eq(x, 3)
  )); // [{x: 1}, {x: 3}]

  run([x], anyo(or(eq(x, 1), eq(x, 2), eq(x, 3))), count: 4);
  // [{x: 1}, {x: 2}, {x: 3}, {x: 1}]
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  logic: ^1.0.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:logic/logic.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
99
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
50
Learn more about scoring.

We analyzed this package on Aug 18, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.4.0
  • pana: 0.12.19

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:logic/logic.dart.

Health suggestions

Fix lib/src/logic_base.dart. (-1 points)

Analysis of lib/src/logic_base.dart reported 2 hints:

line 10 col 24: Extending 'Function' is deprecated.

line 109 col 30: Equality operator == invocation with references of unrelated types.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.2.2 <3.0.0
built_collection ^4.2.2 4.2.2
Transitive dependencies
collection 1.14.12
matcher 0.12.5
meta 1.1.7
path 1.6.4
quiver 2.0.5
stack_trace 1.9.3
Dev dependencies
pedantic ^1.0.0
test ^1.0.0