GithubHelp home page GithubHelp logo

jdevuyst / kripke Goto Github PK

View Code? Open in Web Editor NEW
13.0 2.0 0.0 20 KB

Clojure library for constructing and manipulating sequences of similar structures

License: Eclipse Public License 1.0

Clojure 100.00%
clojure clojure-library

kripke's Introduction

Kripke

Kripke a Clojure library for manipulating sequences of similar-enough structures.

Installation

Add the following dependency to project.clj:

Clojars Project

Next, load the kripke namespace:

(require '[kripke :refer :all])

Basic usage

A typical workflow is as follows:

  1. Start with a prototype structure
  2. Abstract the prototype into a frame and a sequence of substitution maps
  3. Optionally, manipulate the frame
  4. Construct a sequence of structures

Here's a simple example. Let us construct three maps: {:key 1}, {:key 2}, and {:key 3}. We begin by constructing a prototype using the function alt (for 'alternatives'):

(def prototype {:key (alt 1 2 3)})
;=> {:key #<kripke$alt$...>}

Next, we abstract the frame and the substitution maps:

(def frame+smaps (abstract prototype))
;=> [{:key abstract__11051}
;    ({abstract__11051 1} {abstract__11051 2} {abstract__11051 3})]

The frame resembles the prototype but contains symbols for every 'choice' that needs to be made. The possible values for these choices are encoded in the substitution maps.

We can use the function model to put everything back together:

(apply model frame+smaps)
;=> ({:key 1} {:key 2} {:key 3})

Changing frames

One advantage of working with substitution maps is that the frame can be changed after abstracting a prototype:

(let [[frame smaps] frame+smaps
      new-frame (clojure.set/map-invert frame)]
  (model new-frame smaps))
;=> ({1 :key} {2 :key} {3 :key})

More possibilites

From here on, we'll use make, a built-in shorthand for modeling a prototype. (make x) is short for (apply model (abstract x)).

It's possible to include alt in many places in your prototype. Moreover, alt can be used recursively or without arguments. Observe:

(make (alt 1 2 [(alt 3 4 (alt 5 6) (alt))]))
;=> (1 2 [3] [4] [5] [6])

Notice that alt can function as a filter when used without arguments:

(make #{[(alt)]})
;=> ()

Tables

alt is useful, but sometimes it's desirable to add more constraints. Tables allow you to correlate choices made in different locations in a prototype.

(make {(tab :t 1 2 3) (tab :t 'a 'b 'c)})
;=> ({1 a} {2 b} {3 c})

As you can see, tables are named. Here's an example with two tables:

(make {(tab :x 1 2 3) (tab :x 'a 'b 'c)
       (tab :y "Aardvark" "Zebra") (tab :y :wine :beer)})
;=> ({1 a "Aardvark" :wine}
;    {1 a "Zebra" :beer}
;    {2 b "Aardvark" :wine}
;    {2 b "Zebra" :beer}
;    {3 c "Aardvark" :wine}
;    {3 c "Zebra" :beer})

Store and load

If you want more power, you can use store and load to smuggle data from one position in a prototype to another. Just make sure store is called before load, going by depth-first order.

(make [(alt (store [:a (alt 1 2 3 4)] :even))
       (alt (retr [a :a] (if (even? a) a (alt))))])
;=> ([:even 2] [:even 4])

Summarize

Given a collection of structures, summarize returns a frame and a sequence of substitution maps that represent the same information.

(def form '([1 2 [3 #{4}]] [:a 2 [3 (4)]]))

(summarize form)
;=> [[summarize__14972 2 [3 summarize__14973]]
;    ({summarize__14972 1 summarize__14973 #{4}}
;     {summarize__14972 :a summarize__14973 (4)})]

(= form (apply model (summarize form)))
;=> true

License

Copyright © 2015 Jonas De Vuyst

Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.

kripke's People

Contributors

jdevuyst avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.