Add table->function.

This commit is contained in:
Sergiu Ivanov 2022-04-14 20:46:40 +02:00
parent 3b51a4ba51
commit 1324be292e
2 changed files with 25 additions and 25 deletions

View File

@ -19,7 +19,7 @@
pseudovariadic-lambda pvλ pseudovariadic-define pvdefine
tabulate* tabulate*/strict tabulate*/pv tabulate tabulate/strict tabulate/pv
tabulate*/pv/boolean tabulate/pv/boolean tabulate*/pv/01 tabulate/pv/01
table->function/list)
table->function/list table->function)
(module+ test
(require typed/rackunit))
@ -206,6 +206,17 @@
(check-true (negation/list '(#f)))
(check-false (negation/list '(#t)))))
(: table->function (All (a) (-> (Listof (Listof a)) (-> a * a))))
(define (table->function table)
(define func (table->function/list table))
(λ args (func args)))
(module+ test
(test-case "table->function"
(define negation (table->function '((#t #f) (#f #t))))
(check-true (negation #f))
(check-false (negation #t))))
(module untyped racket
(module+ test
(require rackunit))
@ -287,7 +298,7 @@
tabulate tabulate/strict tabulate/pv
tabulate*/pv/boolean tabulate/pv/boolean
tabulate*/pv/01 tabulate/pv/01
table->function/list)
table->function/list table->function)
(require (rename-in (submod 'typed untyped)
[tabulate tabulate/untyped]
@ -299,7 +310,6 @@
[struct tbf ((weights (vectorof number?)) (threshold number?))])
;; Functions
(contract-out
[table->function (-> (listof (*list/c any/c any/c)) procedure?)]
[enumerate-boolean-tables (-> number? (stream/c (listof (*list/c boolean? boolean?))))]
[enumerate-boolean-functions (-> number? (stream/c procedure?))]
[enumerate-boolean-functions/list (-> number? (stream/c procedure?))]
@ -332,28 +342,6 @@
;;; Constructing functions
;;; ======================
;;; Given a table like the one produced by the tabulate functions,
;;; creates a function which has this behaviour.
;;;
;;; More exactly, the input is a list of lists of values. All but the
;;; last elements of every list give the values of the parameters of
;;; the function, while the the last element of every list gives the
;;; value of the function. Thus, every list should have at least two
;;; elements.
;;;
;;; The produced function is implemented via lookups in hash tables,
;;; meaning that it may be sometimes more expensive to compute than by
;;; using an direct symbolic implementation.
(define (table->function table)
(let ([func (table->function/list table)])
(λ args (func args))))
(module+ test
(test-case "table->function"
(define negation (table->function '((#t #f) (#f #t))))
(check-true (negation #f))
(check-false (negation #t))))
;;; Returns the stream of the truth tables of all Boolean functions of
;;; a given arity.
;;;

View File

@ -261,6 +261,18 @@ last element.
(and/list '(#t #t))
]}
@defproc[(table->function [table (Listof (Listof a))])
(-> a * a)]{
Like @racket[table->function/list], but the resulting function takes a variable
number of arguments rather than a list of values.
@examples[#:eval functions-evaluator
(define my-and (table->function tab))
(my-and #f #t)
(my-and #t #t)
]}
@section{Random functions}
@section{Threshold Boolean functions}