Raw File
Tip revision: cda4560576f3b975008fe892fe4fcaaaa71019fe authored by Anish Tondwalkar on 16 March 2020, 20:43:43 UTC
fix tests
Tip revision: cda4560
#lang racket

(define tabsize (make-parameter 2))

(define (indent depth)
  (make-string (* depth (tabsize)) #\space))

(define (add-newlines lines)
  (add-between lines "\n"))

(define (surround start end body)
  (append (list start) body (list end)))

(define (parens body)
  (surround "(" ")" body))

;; formula, depth -> list string
(define (pretty-print-depth formula depth)
  (list* (indent depth)
         (match formula
           [(list 'and conjuncts ...)
             (list* "and"
                      (map (lambda (x) (pretty-print-depth x (+ depth 1)))
           [(list 'forall head body)
             (list* "forall"
                    " "
                    (~a head)
                    (pretty-print-depth body (+ depth 1))))]
           [(list 'exists head body)
             (list* "exists"
                    " "
                    (~a head)
                    (pretty-print-depth body (+ depth 1))))]
            (list (~a formula))])))

(define (pretty-print formula)
  (apply string-append (pretty-print-depth formula 0)))

(define (cubify constraints pruning)
  ;; EHC -> (LinearEHC -> LinearEHC) -> List LinearEHC
  (define (cubify-help formula path-builder)
    (match formula
      [(list 'and conjuncts ...)
       (apply append (map (lambda (conjunct) (cubify-help conjunct path-builder)) conjuncts))]

      [(list 'forall head body)
       (cubify-help body (lambda (x) (path-builder (list 'forall head x))))]

      [(list 'exists head body)
       (cubify-help body (lambda (x) (path-builder (list 'exists head x))))]

      ['((true)) (if pruning '() '((true)))]
      [formula (list (path-builder formula))]))

  (cubify-help constraints identity))

(define input-file (make-parameter #f))
(define hypercube (make-parameter #f))
(define prune-true (make-parameter #f))

 #:program "pretty-print"
 [("-i" "--input") file-name
                   "read input from file"
                   (input-file file-name)]
 [("--hypercube") "print the constraints as a hyper cube"
                  (hypercube #t)]
 [("--prune-true") "remove the constraints with head true"
                   (prune-true #t)])

(current-input-port (if (input-file)
                        (open-input-file (input-file))
(define constraints (read))

(if (hypercube)
  (for-each displayln (cubify constraints (prune-true)))
  (displayln (pretty-print constraints)))
back to top