Guest User

Untitled

a guest
Oct 20th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 3.00 KB | None | 0 0
  1. #lang racket
  2. (require rackunit)
  3.  
  4. (define (setof type?)
  5.   (λ (seq) (sequence-andmap type? seq)))
  6.  
  7. (provide (contract-out
  8.           [epsilon char?]
  9.           [powerset (-> set? (setof set?))]
  10.           [dfa-transitions->function
  11.            (-> (setof list?) (-> any/c any/c any/c))]
  12.           [nfa-transitions->function
  13.            (-> (setof list?) (-> any/c any/c any/c))]
  14.           [set-disjoint? (-> set? set? boolean?)])          
  15.          [struct-out 5-tuple]
  16.          group-by
  17.          
  18.          set-grab)
  19.  
  20.  
  21. #|;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  22.  
  23.     These are used in several modules so I have collected them here for
  24.     convenience.
  25.  
  26. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;|#
  27.  
  28. ;;;;;;;;;;;
  29. ;;; UTILITY
  30. ;;;;;;;;;;;
  31.  
  32. ;Implementation of Haskells' "groupBy"
  33. (define (group-by pred xs)
  34.   (match `(,pred ,xs)
  35.     [(list _ '()) '()]
  36.     [(list pred (cons x xs))
  37.      (let-values ([(ys zs) (partition (curry pred x) xs)])
  38.        (cons (cons x ys) (group-by pred zs)))]))
  39.  
  40. (define epsilon #\ε) ;Avoding a "magic character"
  41.  
  42. (define (powerset ss)
  43.   (for/fold ([accum (set(set))]) ([s ss])
  44.     (set-union accum
  45.                (for/set ([a accum])
  46.                         (set-add a s)))))
  47.  
  48. (check-equal? (powerset (set))
  49.               (set (set)))
  50.  
  51. (check-equal? (powerset (set 1 2 3))
  52.               (set
  53.                (set) (set 1) (set 2) (set 3) (set 1 2) (set 1 3) (set 2 3) (set 1 2 3)))
  54.  
  55. ;"We say that A and B are disjoint if A does not intersect B"
  56. (define (set-disjoint? s1 s2)
  57.   (equal? (set-intersect s1 s2) (set)))
  58.  
  59. (check-equal? (set-disjoint? (set 1 2) (set 3 4))
  60.               #t)
  61. (check-equal? (set-disjoint? (set 1 2) (set 3 1))
  62.               #f)
  63.  
  64. ;Used to get some arbitrary element from a set
  65. (define (set-grab ss)
  66.   (car (set->list ss)))
  67.  
  68. ;These use the standard "5 tuple" order, which is different from the order used
  69. ;in the DFA files
  70. (struct 5-tuple
  71.   (states alphabet transitions start-state accept-states) #:transparent)
  72.  
  73. ;;;;;;;;;;;;;;;;
  74. ;;;; TRANSITIONS
  75. ;;;;;;;;;;;;;;;;
  76.  
  77. (define (transitions->table ts)
  78.   (define (transform t) (cons (cons (first t) (second t)) (third t)))
  79.   (make-immutable-hash (map transform [sequence->list ts])))
  80.  
  81. (check-equal? (transitions->table '((1 2 3)))
  82.               #hash([(1 . 2) . 3]) '(1 . 2))
  83.  
  84. ;;;Not meant to be used directly - helper function for the two below it
  85. ;;;Takes a list of transitions, outputs a function
  86. (define (fa-transitions->function error ts)  
  87.   (let ([table (transitions->table ts)])
  88.     (λ (state input-symbol)
  89.       (hash-ref table (cons state input-symbol) error))))
  90.  
  91. (define (dfa-transitions->function ts)
  92.   (fa-transitions->function "" ts))
  93.  
  94. (define (nfa-transitions->function ts)
  95.   (fa-transitions->function (set) ts))
  96.  
  97. (check-equal? [(dfa-transitions->function '((a b c))) 'a 'b]
  98.               'c)
  99.  
  100. (check-equal? [(nfa-transitions->function `((a b ,(set 'c)))) 'a 'b]
  101.               (set 'c))
Add Comment
Please, Sign In to add comment