logic-utils.scm 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #| -*-Scheme-*-
  2. Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
  3. 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
  4. 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Massachusetts
  5. Institute of Technology
  6. This file is part of MIT/GNU Scheme.
  7. MIT/GNU Scheme is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or (at
  10. your option) any later version.
  11. MIT/GNU Scheme is distributed in the hope that it will be useful, but
  12. WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with MIT/GNU Scheme; if not, write to the Free Software
  17. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301,
  18. USA.
  19. |#
  20. (declare (usual-integrations))
  21. #|
  22. ;;; In Scheme system: runtime/boole.scm
  23. (define (false? x) (eq? x #f))
  24. |#
  25. (define (true? x) (eq? x #t))
  26. (define* (assert p #:optional error-comment irritant)
  27. (if (not p)
  28. (begin
  29. (if (not (default-object? irritant))
  30. (pp irritant))
  31. (error (if (default-object? error-comment)
  32. "Failed assertion"
  33. error-comment)))))
  34. ;;;; Assumptions made in processing are noted. See kernel/utils.scm .
  35. (define *assumption-tolerance-multiplier* 100)
  36. (define* (assume! predicate-expression responsible-party #:optional if-false)
  37. (define (do-false)
  38. (if (default-object? if-false)
  39. (add-assumption! `(false! ,predicate-expression) responsible-party)
  40. (if-false)))
  41. (define (default) (add-assumption! predicate-expression responsible-party))
  42. (define (default-numeric rator rands)
  43. (cond ((environment-bound? scmutils-base-environment rator)
  44. (let ((predicate
  45. (environment-lookup scmutils-base-environment rator)))
  46. (if (procedure? predicate)
  47. (let ((val (apply predicate rands)))
  48. (cond ((not val) (do-false))
  49. ((true? val) 'OK)
  50. (else (default))))
  51. (error "Bad assumption"
  52. predicate-expression responsible-party))))
  53. (else (default))))
  54. (define (simple-numeric rator rands)
  55. (case rator
  56. ((=) (if (or (inexact? (car rands)) (inexact? (cadr rands)))
  57. (if (close-enuf? (car rands) (cadr rands)
  58. *assumption-tolerance*)
  59. 'OK
  60. (do-false))
  61. (if (= (car rands) (cadr rands)) 'OK (do-false))))
  62. (else (default-numeric rator rands))))
  63. (define *assumption-tolerance*
  64. (* *assumption-tolerance-multiplier* *machine-epsilon*))
  65. (cond ((pair? predicate-expression)
  66. (let ((rator (operator predicate-expression))
  67. (rands (operands predicate-expression)) )
  68. (if (every number? rands)
  69. (simple-numeric rator rands)
  70. (default))))
  71. ((not predicate-expression) (do-false)) ;(eq? predicate-expression #f)
  72. ((true? predicate-expression) 'OK)
  73. (else (default))))
  74. (define (add-assumption! assumption responsible-party)
  75. (let ((a `(assuming ,assumption)))
  76. (eq-adjoin! a 'rules responsible-party)
  77. (note-that! a)))
  78. #|
  79. ;;; Replaced by for-all?, there-exists? in boole, with args reversed.
  80. (define (forall l p?)
  81. (let loop ((l l))
  82. (cond ((null? l) true)
  83. ((p? (car l)) (loop (cdr l)))
  84. (else false))))
  85. (define (exists p? l)
  86. (let loop ((l l))
  87. (cond ((null? l) false)
  88. ((p? (car l)) true)
  89. (else (loop (cdr l))))))
  90. |#
  91. (define (&or disjuncts)
  92. (cond ((null? disjuncts) false)
  93. ((car disjuncts) true)
  94. (else (&or (cdr disjuncts)))))
  95. (define (*or . disjuncts) (&or disjuncts))
  96. (define (&and conjuncts)
  97. (cond ((null? conjuncts) true)
  98. ((car conjuncts) (&and (cdr conjuncts)))
  99. (else false)))
  100. (define (*and . conjuncts) (&and conjuncts))
  101. (define (conjunction predicate1 predicate2)
  102. (lambda (x)
  103. (and (predicate1 x) (predicate2 x))))
  104. (define (disjunction predicate1 predicate2)
  105. (lambda (x)
  106. (or (predicate1 x) (predicate2 x))))
  107. (define (negation predicate)
  108. (lambda (x) (not (predicate x))))
  109. (define (implication antecedent consequent)
  110. (lambda (x) (or (not (antecedent x)) (consequent x))))