Skip to content

Commit

Permalink
Merge branch 'release/0.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
dented42 committed Dec 15, 2014
2 parents c5842c3 + eee2014 commit f857382
Show file tree
Hide file tree
Showing 14 changed files with 563 additions and 568 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
compiled
4 changes: 0 additions & 4 deletions Makefile

This file was deleted.

119 changes: 59 additions & 60 deletions derp-core.rkt
Original file line number Diff line number Diff line change
@@ -1,61 +1,60 @@
(module derp-core
racket

(require "memoization.rkt")
(require "fixed-points.rkt")
(require "lazy-structs.rkt")
#lang racket

(provide (all-defined-out))

; Atomic parsers:
(define-struct ∅ {}) ; empty set
(define-struct ε {tree-set}) ; empty string
(define-struct token {value?}) ; token class

; Compound parsers:
(define-lazy-struct δ {lang})
(define-lazy-struct ∪ {this that})
(define-lazy-struct ∘ {left right})
(define-lazy-struct ★ {lang})
(define-lazy-struct → {lang reduce})

; Derivative:
(define/memoize (D c p)
#:order ([p #:eq] [c #:equal])
(match p
[(∅) (∅)]
[(ε _) (∅)]
[(δ _) (∅)]
[(token p?) (cond
[(p? c) (ε (set c))]
[else (∅)])]

[(∪ p1 p2) (∪ (D c p1)
(D c p2))]
[(★ p1) (∘ (D c p1) p)]
[(→ p1 f) (→ (D c p1) f)]
[(∘ p1 p2) (∪ (∘ (δ p1) (D c p2))
(∘ (D c p1) p2))]))

; Parsing null:
(define/fix (parse-null p)
#:bottom (set)
(match p
[(ε S) S]
[(∅) (set)]
[(δ p) (parse-null p)]
[(token _) (set)]

[(★ _) (set '())]
[(∪ p1 p2) (set-union (parse-null p1) (parse-null p2))]
[(∘ p1 p2) (for*/set ([t1 (parse-null p1)]
[t2 (parse-null p2)])
(cons t1 t2))]
[(→ p1 f) (for/set ([t (parse-null p1)])
(f t))]))

; Parse a list of tokens:
(define (parse w p)
(if (null? w)
(parse-null p)
(parse (cdr w) (D (car w) p)))))
(require "memoization.rkt")
(require "fixed-points.rkt")
(require "lazy-structs.rkt")

(provide (all-defined-out))

; Atomic parsers:
(define-struct ∅ {}) ; empty set
(define-struct ε {tree-set}) ; empty string
(define-struct token {value?}) ; token class

; Compound parsers:
(define-lazy-struct δ {lang})
(define-lazy-struct ∪ {this that})
(define-lazy-struct ∘ {left right})
(define-lazy-struct ★ {lang})
(define-lazy-struct → {lang reduce})

; Derivative:
(define/memoize (D c p)
#:order ([p #:eq] [c #:equal])
(match p
[(∅) (∅)]
[(ε _) (∅)]
[(δ _) (∅)]
[(token p?) (cond
[(p? c) (ε (set c))]
[else (∅)])]

[(∪ p1 p2) (∪ (D c p1)
(D c p2))]
[(★ p1) (∘ (D c p1) p)]
[(→ p1 f) (→ (D c p1) f)]
[(∘ p1 p2) (∪ (∘ (δ p1) (D c p2))
(∘ (D c p1) p2))]))

; Parsing null:
(define/fix (parse-null p)
#:bottom (set)
(match p
[(ε S) S]
[(∅) (set)]
[(δ p) (parse-null p)]
[(token _) (set)]

[(★ _) (set '())]
[(∪ p1 p2) (set-union (parse-null p1) (parse-null p2))]
[(∘ p1 p2) (for*/set ([t1 (parse-null p1)]
[t2 (parse-null p2)])
(cons t1 t2))]
[(→ p1 f) (for/set ([t (parse-null p1)])
(f t))]))

; Parse a list of tokens:
(define (parse w p)
(if (null? w)
(parse-null p)
(parse (cdr w) (D (car w) p))))
157 changes: 77 additions & 80 deletions derp-optimize.rkt
Original file line number Diff line number Diff line change
@@ -1,82 +1,79 @@
(module derp-optimize
racket

(provide (all-defined-out))

(require (except-in "derp-core.rkt" ∅? ε?))
(require "memoization.rkt")
(require "fixed-points.rkt")

(define/fix (ε? L)
#:bottom #t
(match L
[(∅) #f]
[(ε _) #t]
[(token _) #f]
[(∪ L1 L2) (and (ε? L1) (ε? L2))]
[(∘ L1 L2) (and (ε? L1) (ε? L2))]
[(★ L1) (or (ε? L1) (∅? L1))]
[(→ L1 _) (ε? L1)]))

; Compute the size of a set:
(define (set-choose s)
(define el #f)
(for ([el* s])
(set! el el*))
el)

; Matches a language if it is *exactly* the empty string:
(define-match-expander nullp
(syntax-rules ()
[(_) (? ε?)]
[(_ el) (and (? ε?)
(app parse-null (and (app set-count 1)
(app set-choose el))))]))

; Checks whether a language is the empty set:
(define/fix (∅? L)
#:bottom #f
(match L
[(∅) #t]
[(ε _) #f]
[(token _) #f]
[(★ L1) #f]
[(∪ L1 L2) (and (∅? L1) (∅? L2))]
[(∘ L1 L2) (or (∅? L1) (∅? L2))]
[(→ L1 _) (∅? L1)]))

; Optimizing compaction.
; (K L) is an equivalent, compacted version of L.
(define/memoize (K [L #:eq])
(match L
[(∅) L]
[(ε _) L]
[(? ∅?) (∅)]
[(? ε?) (ε (parse-null L))]
[(token _) L]

[(★ (? ∅?)) (ε (set '()))]
[(★ L) (★ (K L))]

[(∪ (? ∅?) L2) (K L2)]
[(∪ L1 (? ∅?)) (K L1)]

[(∘ (nullp t) L2) (→ (K L2) (λ (w2) (cons t w2)))]
[(∘ L1 (nullp t)) (→ (K L1) (λ (w1) (cons w1 t)))]

[(∪ L1 L2) (∪ (K L1) (K L2))]
[(∘ L1 L2) (∘ (K L1) (K L2))]

[(→ (and e (? ε?)) f)
(ε (for/set ([t (parse-null e)]) (f t)))]

[(→ (∘ (nullp t) L2) f) (→ (K L2) (λ (w2) (f (cons t w2))))]
[(→ (→ L f) g) (→ (K L) (compose g f))]
[(→ L f) (→ (K L) f)]))
#lang racket

(define (parse/compact w L #:compactor [compact K])
(if (null? w)
(parse-null L)
(parse/compact (cdr w) (compact L)))))
(provide (all-defined-out))

(require (except-in "derp-core.rkt" ∅? ε?))
(require "memoization.rkt")
(require "fixed-points.rkt")

(define/fix (ε? L)
#:bottom #t
(match L
[(∅) #f]
[(ε _) #t]
[(token _) #f]
[(∪ L1 L2) (and (ε? L1) (ε? L2))]
[(∘ L1 L2) (and (ε? L1) (ε? L2))]
[(★ L1) (or (ε? L1) (∅? L1))]
[(→ L1 _) (ε? L1)]))

; Compute the size of a set:
(define (set-choose s)
(define el #f)
(for ([el* s])
(set! el el*))
el)

; Matches a language if it is *exactly* the empty string:
(define-match-expander nullp
(syntax-rules ()
[(_) (? ε?)]
[(_ el) (and (? ε?)
(app parse-null (and (app set-count 1)
(app set-choose el))))]))

; Checks whether a language is the empty set:
(define/fix (∅? L)
#:bottom #f
(match L
[(∅) #t]
[(ε _) #f]
[(token _) #f]
[(★ L1) #f]
[(∪ L1 L2) (and (∅? L1) (∅? L2))]
[(∘ L1 L2) (or (∅? L1) (∅? L2))]
[(→ L1 _) (∅? L1)]))

; Optimizing compaction.
; (K L) is an equivalent, compacted version of L.
(define/memoize (K [L #:eq])
(match L
[(∅) L]
[(ε _) L]
[(? ∅?) (∅)]
[(? ε?) (ε (parse-null L))]
[(token _) L]


[(★ (? ∅?)) (ε (set '()))]
[(★ L) (★ (K L))]

[(∪ (? ∅?) L2) (K L2)]
[(∪ L1 (? ∅?)) (K L1)]

[(∘ (nullp t) L2) (→ (K L2) (λ (w2) (cons t w2)))]
[(∘ L1 (nullp t)) (→ (K L1) (λ (w1) (cons w1 t)))]

[(∪ L1 L2) (∪ (K L1) (K L2))]
[(∘ L1 L2) (∘ (K L1) (K L2))]

[(→ (and e (? ε?)) f)
(ε (for/set ([t (parse-null e)]) (f t)))]

[(→ (∘ (nullp t) L2) f) (→ (K L2) (λ (w2) (f (cons t w2))))]
[(→ (→ L f) g) (→ (K L) (compose g f))]
[(→ L f) (→ (K L) f)]))

(define (parse/compact w L #:compactor [compact K])
(if (null? w)
(parse-null L)
(parse/compact (cdr w) (compact L))))
Loading

0 comments on commit f857382

Please sign in to comment.