Created
January 22, 2025 19:59
-
-
Save rntz/afd42848c82c074f870d9b1028ee5804 to your computer and use it in GitHub Desktop.
minikanren program for generating balanced ordered trees
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define (treeo x) | |
(conde | |
((== x 'n)) | |
((fresh (a y z) (== x `(,y ,a ,z)) (into a) (treeo y) (treeo z))) | |
)) | |
(define (lto x y) | |
(conde | |
((== x 0) (== y 1)) | |
((== x 0) (== y 2)) | |
((== x 0) (== y 3)) | |
((== x 1) (== y 2)) | |
((== x 1) (== y 3)) | |
((== x 2) (== y 3)) | |
)) | |
(define (leo x y) (conde ((== x y)) ((lto x y)))) | |
(define (ordered-treeo x lo hi) | |
(conde | |
((== x 'n)) | |
((fresh (a y yhi z zlo) | |
(== x `(,y ,a ,z)) | |
(leo lo a) | |
(leo a hi) | |
(ordered-treeo y lo a) | |
(ordered-treeo z a hi) | |
)))) | |
(define (balanced-treeo x d) | |
(conde | |
((== x 'n) (== d 'zero)) | |
((fresh (a y z n) | |
(== x `(,y ,a ,z)) | |
(== d `(succ ,n)) | |
(balanced-treeo y n) | |
(balanced-treeo z n) | |
)))) | |
(run 10 (q) | |
(balanced-treeo q `(succ (succ zero))) | |
(ordered-treeo q 0 3) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment