Skip to content

Commit

Permalink
Stuff that made it into the article.
Browse files Browse the repository at this point in the history
  • Loading branch information
debiatan committed Mar 11, 2014
1 parent 3d35c73 commit 2edaca4
Show file tree
Hide file tree
Showing 18 changed files with 1,616 additions and 4 deletions.
7 changes: 7 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Unit-Test-Based Programming
===========================

UTBP is a new subparadigm of Declarative Programming, in which code is generated from a description of its intended behavior, specified through unit tests. This work was presented at SIGBOVIK 2014.

For more information, read doc/article.pdf or point your browser to http://blog.debiatan.net/utbp.html .

4 changes: 0 additions & 4 deletions README.md

This file was deleted.

13 changes: 13 additions & 0 deletions defs.exh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
add: ((number number) number (define add (lambda (a b) (if a (add (pred a) (succ b)) b))))
mul: ((number number) number (define mul (lambda (a b) (if a (add (mul (pred a) b) b) 0))))
logical_not: ((number) number (define logical_not (lambda (a) (if a 0 (succ a)))))
logical_or: ((number number) number (define logical_or (lambda (a b) (if a a b))))
logical_and: ((number number) number (define logical_and (lambda (a b) (if a b 0))))
logical_nand: ((number number) number (define logical_nand (lambda (a b) (logical_not (logical_and b a)))))
logical_xor: ((number number) number (define logical_xor (lambda (a b) (if a (logical_not b) b))))
logical_parity: ((list) number (define logical_parity (lambda (l) (if l (logical_xor (logical_parity (cdr l)) (car l)) 0))))
index: ((list number) S-expression (define index (lambda (l a) (if a (index (cdr l) (pred a)) (car l)))))
length: ((list) number (define length (lambda (l) (if l (succ (length (cdr l))) 0))))
sum: ((list) number (define sum (lambda (l) (if l (add (sum (cdr l)) (car l)) 0))))
power: ((number number) number (define power (lambda (a b) (if b (mul (power a (pred b)) a) 1))))
factorial: ((number) number (define factorial (lambda (n) (if n (mul (factorial (pred n)) n) 1))))
Binary file added doc/article.pdf
Binary file not shown.
577 changes: 577 additions & 0 deletions doc/article.tex

Large diffs are not rendered by default.

43 changes: 43 additions & 0 deletions ex_arithmetic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from utbp import UTBP

@UTBP
def add(a, b):
"""
add(2, 2) == 4
add(3, 3) == 6
"""

@UTBP
def mul(a, b):
"""
mul(2, 2) == 4
mul(3, 5) == 15
"""

@UTBP
def power(a, b):
"""
power(1, 1) == 1
power(2, 3) == 8
power(5, 3) == 125
power(5, 2) == 25
power(5, 0) == 1
"""

@UTBP
def factorial(n):
"""
factorial(0) == 1
factorial(1) == 1
factorial(2) == 2
factorial(3) == 6
"""

if __name__ == '__main__':
assert add(23, 23) == 46
assert mul(8, 8) == 64
assert power(2, 4) == 16
print(factorial(4))
40 changes: 40 additions & 0 deletions ex_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from utbp import UTBP

@UTBP
def index(l, a):
"""
index((4, 7, 8), 1) == 7
index((4, 7, 8), 2) == 8
index(((4, 7), 8), 0) == (4, 7)
"""

@UTBP
def length(l):
"""
length(()) == 0
length((2, 2, 2, 2, 2)) == 5
"""

@UTBP
def add(a, b):
"""
add(2, 2) == 4
add(3, 3) == 6
"""

@UTBP
def sum(l):
"""
sum((2, 2)) == 4
sum((3, 3, 3)) == 9
"""


if __name__ == '__main__':
assert index((2, 3, 4), 1) == 3
assert length((4, 4, 4)) == 3
assert add(23, 23) == 46
assert sum((8, 8, 8, 8)) == 32
67 changes: 67 additions & 0 deletions ex_logical.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from utbp import UTBP

@UTBP
def logical_not(a):
"""
logical_not(1) == 0
logical_not(0) == 1
"""

@UTBP
def logical_or(a, b):
"""
logical_or(0, 0) == 0
logical_or(0, 1) == 1
logical_or(1, 0) == 1
logical_or(1, 1) == 1
"""

@UTBP
def logical_and(a, b):
"""
logical_and(0, 0) == 0
logical_and(0, 1) == 0
logical_and(1, 0) == 0
logical_and(1, 1) == 1
"""

@UTBP
def logical_nand(a, b):
"""
logical_nand(0, 0) == 1
logical_nand(0, 1) == 1
logical_nand(1, 0) == 1
logical_nand(1, 1) == 0
"""

@UTBP
def logical_xor(a, b):
"""
logical_xor(0, 0) == 0
logical_xor(0, 1) == 1
logical_xor(1, 0) == 1
logical_xor(1, 1) == 0
"""

@UTBP
def logical_parity(l):
"""
logical_parity((0,)) == 0
logical_parity((1,)) == 1
logical_parity((0, 0)) == 0
logical_parity((0, 1)) == 1
logical_parity((0, 0, 0)) == 0
logical_parity((0, 0, 1)) == 1
logical_parity((0, 1, 1)) == 0
"""

if __name__ == '__main__':
assert logical_not(1) == 0
assert logical_or(1, 1) == 1
assert logical_and(1, 1) == 1
assert logical_nand(1, 1) == 0
assert logical_xor(1, 1) == 0
assert logical_parity((1, 1, 1, 1, 0, 0, 1, 1)) == 0
23 changes: 23 additions & 0 deletions factorial_classic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

def factorial(n):
"""
Returns the factorial of n
int -> int
>>> factorial(2)
2
>>> factorial(3)
6
"""
if n:
return n*factorial(n-1)
else:
return 1

print(factorial(1))
print(factorial(2))
print(factorial(3))
print(factorial(4))
15 changes: 15 additions & 0 deletions factorial_utbp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from utbp import UTBP

@UTBP
def add(a, b):
"""
add(2, 2) == 4
add(3, 3) == 6
"""

assert add(23, 23) == 46


Loading

0 comments on commit 2edaca4

Please sign in to comment.