• Stars
    star
    344
  • Rank 119,015 (Top 3 %)
  • Language
    Emacs Lisp
  • Created almost 8 years ago
  • Updated 11 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

discover elisp functions that do what you want

suggest.el

Build Status Coverage Status MELPA

suggest.el is an Emacs package for discovering elisp functions based on examples. You supply an example input and output, and it makes suggestions.

Interested readers may enjoy my blog posts:

suggest

Examples

suggest.el knows many string functions:

;; Inputs (one per line):
"foo bar"

;; Desired output:
"Foo Bar"

;; Suggestions:
(capitalize "foo bar") ;=> "Foo Bar"

suggest.el can also help you find tricky dash.el functions:

;; Inputs (one per line):
(list 'a 'b 'c 'd)
'c

;; Desired output:
2

;; Suggestions:
(-elem-index 'c (list 'a 'b 'c 'd)) ;=> 2

suggest.el is particularly handy for path manipulation, using both built-in functions as well as f.el:

;; Inputs (one per line):
"/foo/bar/baz.txt"

;; Desired output:
"baz.txt"

;; Suggestions:
(file-name-nondirectory "/foo/bar/baz.txt") ;=> "baz.txt"
(f-filename "/foo/bar/baz.txt") ;=> "baz.txt"

It can even suggest calling functions with apply:

;; Inputs (one per line):
'(1 2 3 4 5)

;; Desired output:
15

;; Suggestions:
(-sum '(1 2 3 4 5)) ;=> 15
(apply #'+ '(1 2 3 4 5)) ;=> 15

It can also suggest composing functions:

;; Inputs (one per line):
'(a b c)

;; Desired output:
'c

;; Suggestions:
(cadr (cdr '(a b c))) ;=> 'c
(car (last '(a b c))) ;=> 'c
(cl-third '(a b c)) ;=> 'c

It will also suggest additional arguments of basic values (0 in this example):

;; Inputs (one per line):
'(a b c d)

;; Desired output:
'a

;; Suggestions:
(elt '(a b c d) 0) ;=> 'a
(nth 0 '(a b c d)) ;=> 'a
(car '(a b c d)) ;=> 'a
(cl-first '(a b c d)) ;=> 'a
(-first-item '(a b c d)) ;=> 'a

How it works

suggest.el uses enumerative program synthesis. It tries your inputs (in any order) against every function in suggest-functions.

suggest-functions is a carefully chosen function list: they're all pure functions with a small number of arguments using only simple data types. We only include functions that users could 'stumble upon' with the right set of inputs.

Contributing

To work on the code, clone the repo, then you can eval suggest.el in your current Emacs instance.

To run the tests, you will need Cask. Once you've installed Cask, you can install the suggest.el dependencies and run the tests as follows:

$ cask
$ cask exec ert-runner

License

GPLv3.

Related projects

Program synthesis or inductive programming is a computer science research topic, with a range of tools taking very different approaches to generate code.

Smalltalk: Finder

This project was inspired by the Finder in Smalltalk, which does something similar. There's a great demo video here.

You give a list of values (inputs followed by an output):

3. 4. 7.

The Finder iterates over all possible calls (with arguments in any order) to a list of known safe messages, and returns suggestions:

3 + 4 --> 7
3 bitOr: 4 --> 7
3 bitXor: 4 --> 7
3 | 4 --> 7

This only returns single messages that meet the requirements, no nesting (e.g. it won't find '(data1 reversed asUppercase)' from 'foo'. 'OOF').

You can also supply multiple examples, using expressions as inputs:

MethodFinder methodFor: { {'29 Apr 1999' asDate}. 'Thursday'.
		{'30 Apr 1999' asDate}. 'Friday' }.

This returns '(data1 weekday)'.

Amusingly, it will sometimes find '(data1 shuffled)' from 'fo'. 'of'., which is a random sort.

Python: cant

There are some other niche tools that take other approaches. For example, cant for Python tries every function in scope (without a safety whitelist) to find functionality.

Scheme: barliman

barliman takes this idea of synthesis much, much further for Scheme. There's an incredible demo video here.

C-like: sketch

sketch allows the user to specify value placeholders in code (examples).

harness void doubleSketch(int x){
  int t = x * ??;
  assert t == x + x;
}

Generally you provide the structure of the code.

bit[W] firstLeadingZeroSketch (bit[W] x) implements firstLeadingZero {	
	return !(x + ??) & (x + ??); 
}

Or you can specify a set of operators for sketch to explore. Each line here is an assignment to x or tmp of an expression that may contain ! & + with x, tmp or a constant as arguments.

bit[W] firstLeadingZeroSketch (bit[W] x) implements firstLeadingZero {	
	bit[W] tmp=0;
       {| x | tmp |} = {| (!)?((x | tmp) (& | +) (x | tmp | ??)) |};
       {| x | tmp |} = {| (!)?((x | tmp) (& | +) (x | tmp | ??)) |};
       {| x | tmp |} = {| (!)?((x | tmp) (& | +) (x | tmp | ??)) |};
       return tmp;
}

Constraints are fed to a SAT solver, then sketch finds a solution. The output can be converted to C.

Liquid Haskell: Synquid

Synquid (the first half of this Microsoft Research talk gives a good overview) is a program synthesis tool leveraging refinement types.

The user provides the type of the function they want to generate, and a collection of 'components', the building blocks that Synquid tries to combine.

Synquid then lazily generates ASTs and type checks them. This allows it to prune the search tree by ignoring program structures that are never valid types.

(This is the extent of my understanding: I have probably oversimplified.)

Impressively, Synquid can generate recursive functions.

More Repositories

1

difftastic

a structural diff that understands syntax 🟥🟩
Rust
15,946
star
2

helpful

A better Emacs *help* buffer
Emacs Lisp
972
star
3

deadgrep

fast, friendly searching with ripgrep and Emacs
Emacs Lisp
671
star
4

ag.el

An Emacs frontend to The Silver Searcher
Emacs Lisp
521
star
5

babyc

A toy C compiler
C
496
star
6

bfc

An industrial-grade brainfuck compiler
Rust
479
star
7

emacs-refactor

language-specific refactoring in Emacs
Emacs Lisp
341
star
8

ht.el

The missing hash table library for Emacs
Emacs Lisp
235
star
9

elisp-refs

semantic code search for emacs lisp
Emacs Lisp
111
star
10

refine

interactive value editing in emacs lisp
Emacs Lisp
85
star
11

tco.el

Tail call optimisation in Emacs lisp
Emacs Lisp
76
star
12

pyimport

Manage Python imports from Emacs!
Emacs Lisp
73
star
13

mustache.el

A mustache templating library for Emacs
Emacs Lisp
68
star
14

elisp-def

Find Emacs Lisp definitions
Emacs Lisp
61
star
15

propcheck

Quickcheck/hypothesis style testing for elisp
Emacs Lisp
59
star
16

.emacs.d

My emacs configuration
Emacs Lisp
52
star
17

pip-requirements.el

Major mode for editing pip requirements files
Emacs Lisp
51
star
18

tree-sitter-elisp

tree-sitter grammar for emacs lisp
C
46
star
19

loop.el

friendly imperative loop structures for Emacs lisp
Emacs Lisp
36
star
20

simpla-vortaro

Inteligenta vortaro de Esperanto / An intelligent Esperanto dictionary
Python
34
star
21

python-info

The python manual in texinfo format, particularly for Emacs users
Makefile
31
star
22

Brainfrack

BF implementations in different languages
Smalltalk
29
star
23

bison-mode

Emacs major mode for Bison/Jison, Yacc and Lex grammars
Emacs Lisp
27
star
24

garden

an interactive programming language
Rust
26
star
25

company-try-hard

Get all completions from company-mode backends
Emacs Lisp
24
star
26

Minimal-scheme

A simple scheme interpreter in Python
Python
23
star
27

logstash-conf.el

Emacs major mode for editing logstash configuration files
Emacs Lisp
21
star
28

trifle

A friendly lisp with a focus on interactivity and collaboration
Python
21
star
29

django-test-mixins

Additional assertions and test conveniences for testing django sites
Python
19
star
30

linkdoc

Rust tool for finding dead links on a site
Rust
14
star
31

cask-mode

Major mode for editing Cask files
Emacs Lisp
12
star
32

flymake-jshint.el

JSHint with flymake for Emacs
Emacs Lisp
12
star
33

emacsbench

Benchmarking Emacs itself
C
12
star
34

Blackjack

Simple blackjack simulator in Haskell
Haskell
11
star
35

flycheck-title

show flycheck errors in the frame title
Emacs Lisp
11
star
36

flycheck-pyflakes

flycheck support for pyflakes
Emacs Lisp
10
star
37

flycheck-pkg-config

Configure flycheck using pkg-config
Emacs Lisp
10
star
38

dotfiles

Configuration files for unix utilities
Shell
10
star
39

ReVo-utilities

a few handy scripts to work with XML dumps from ReVo
XSLT
10
star
40

Lython

Toy Lisp to Python compiler
Python
9
star
41

ez-query-replace

query-replace in Emacs with smarts and history!
Emacs Lisp
9
star
42

mal_pharo

A Make A Lisp implementation in Pharo
Smalltalk
8
star
43

FuzzyBranch

git-checkout with fuzzy branch names
Haskell
7
star
44

peval

partial evaluator for elisp
Emacs Lisp
6
star
45

wilfred.github.com

My personal blog
HTML
6
star
46

versor

Mirror of https://sourceforge.net/projects/emacs-versor/
Emacs Lisp
5
star
47

django-function-cache

Cache (memoize) a function's output using Django's caching API
Python
5
star
48

elisp-complete

Emacs Lisp
5
star
49

metawiki

Proof-of-concept self-hosting metawiki
JavaScript
5
star
50

line-numbers

accessing line numbers by string offsets
Rust
4
star
51

the_end_times

comparing programming language implementation performance
Ruby
4
star
52

autotoc

Generate and insert tables of contents in markdown files
Clojure
4
star
53

elisp-fu

Emacs Lisp
4
star
54

oosh

Object Oriented Shell
Python
4
star
55

company-smart-elisp

a contextual company backend for elisp
Emacs Lisp
4
star
56

podkastaro

Podcast aggregator, targeted at Esperanto radio
PHP
3
star
57

gameboy_emulator

Rust code following along with http://imrannazar.com/GameBoy-Emulation-in-JavaScript
Rust
3
star
58

HandyBot

IRC Bot in Clojure
Clojure
3
star
59

with-namespace.el

interoperable elisp namespaces
Emacs Lisp
3
star
60

advent-of-code

Racket
2
star
61

not-quite-modal.el

Modal-inspired movement in Emacs
Emacs Lisp
2
star
62

srch

Emacs Lisp
2
star
63

sudoku

A sudoku solver in CoffeeScript
CSS
2
star
64

userinfo

cross-platform user details library in Rust
Rust
2
star
65

nmap-service

A service for telling clients what ports they have open
Python
2
star
66

smart-eval.el

helpful eval and re-eval for elisp
Emacs Lisp
2
star
67

Jeweled

A Bejeweled bot
Python
2
star
68

elisp-index

Emacs Lisp
2
star
69

Picky

An opinionated wiki
JavaScript
2
star
70

expenses

Categorising and analysing personal expenses
Python
1
star
71

ankiqt

Anki on the desktop (Qt)
Python
1
star
72

ButlerBot

Haskell news email bot
Haskell
1
star
73

syntax-cafe

programming languages to suit all tastes
TypeScript
1
star
74

podkastaro2

Nova versio de Podkastaro, uzanta Django-n
Python
1
star
75

commonmark-extract-text

extract plain text from commonmark/markdown source
JavaScript
1
star
76

hubot-tube-status

A hubot script for getting the current tube status
CoffeeScript
1
star
77

bf_bench

BF implementation benchmarking
Brainfuck
1
star
78

rustls.el

Emacs Lisp
1
star
79

emailomatic

web scraper for collecting interesting events to attend
Clojure
1
star
80

sooty

a dumb lisp with a handwritten GC
Rust
1
star
81

chrome_crash_example

JavaScript
1
star
82

parsimonious

Simple command line calculator using hand written parser
Java
1
star
83

commonmark-wikiwords

Convert WikiWords to links in commonmark source
JavaScript
1
star
84

zorklike

a text adventure game inspired web page
JavaScript
1
star
85

is_my_internet_filtered

A website that tests whether any content is being filtered
JavaScript
1
star
86

dumb-code-quick

more tests, less code
TypeScript
1
star
87

blog-comments

a simple blog comments system in Clojure
Clojure
1
star