From a6321c932a51375833df081d84029cfc499719e3 Mon Sep 17 00:00:00 2001 From: Sergiu Ivanov Date: Fri, 17 Mar 2023 23:03:50 +0100 Subject: [PATCH] =?UTF-8?q?table->network=20=E2=86=92=20table+vars->networ?= =?UTF-8?q?k?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- networks.rkt | 24 ++++++++++-------------- scribblings/networks.scrbl | 28 +++++++++++++++------------- 2 files changed, 25 insertions(+), 27 deletions(-) diff --git a/networks.rkt b/networks.rkt index 1435212..ba422d7 100644 --- a/networks.rkt +++ b/networks.rkt @@ -48,7 +48,7 @@ tabulate-state+headers tabulate-state+headers/boolean tabulate-network tabulate-network+headers - table->network + table+vars->network ) (define-type (State a) (VariableMapping a)) @@ -931,15 +931,10 @@ (#t #f #f #f) (#t #t #f #t))))) - (: table->network (All (a) (-> (Listof (Listof a)) (Network a)))) - (define (table->network table) + (: table+vars->network (All (a) (-> (Listof Variable) (Listof (Listof a)) + (Network a)))) + (define (table+vars->network var-names table) (define n : Integer (quotient (length (car table)) 2)) - ;; Get the variable names from the table or generate them, if - ;; necessary. - (define var-names : (Listof Variable) - (for/list : (Listof Variable) - ([i (in-range 1 (add1 n))]) - (format-symbol "x~a" i))) ;; Split the table into the inputs and the outputs of the functions. (define-values (ins outs) (multi-split-at table n)) ;; Transpose outs to have functions define by lines instead of by @@ -969,11 +964,12 @@ domains)) (module+ test - (test-case "table->network" - (define n (table->network '((#f #f #f #f) - (#f #t #f #t) - (#t #f #t #f) - (#t #t #t #t)))) + (test-case "table+vars->network" + (define n (table+vars->network '(x1 x2) + '((#f #f #f #f) + (#f #t #f #t) + (#t #f #t #f) + (#t #t #t #t)))) (define f1 (hash-ref (network-functions n) 'x1)) (define f2 (hash-ref (network-functions n) 'x2)) diff --git a/scribblings/networks.scrbl b/scribblings/networks.scrbl index 3b646ee..4c3e057 100644 --- a/scribblings/networks.scrbl +++ b/scribblings/networks.scrbl @@ -832,17 +832,18 @@ function names in the corresponding column headers are of the form @section{Constructing functions and networks} -@defproc[(table->network [table (Listof (Listof a))]) - (Network a)]{ +@defproc[(table+vars->network [var-names (Listof Variable)] + [table (Listof (Listof a))]) + (Network a)]{ -Given a table like the one produced by @racket[tabulate-network], -constructs a network having this behaviour. +Given a @racket[table] like the one produced by +@racket[tabulate-network] and the list of variable names +@racket[var-names], constructs a network having this behaviour. -Variable names are generated as @tt{xi}, where 1 ≤ @tt{i} ≤ number of -variables. The columns defining the functions are taken to be in the -same order as the columns defining the variables. The domains of the -network are a mapping assigning to each variable the set of values -which can appear in the corresponding column in the table. +The columns defining the functions are taken to be in the same order +as the columns defining the variables. The domains of the network are +a mapping assigning to each variable the set of values which can +appear in the corresponding column in the table. This function relies on @racket[table->unary-function], so the same performance caveats apply. @@ -850,10 +851,11 @@ performance caveats apply. This function does not check whether the table is complete. @ex[ -(let ([n (table->network '((#f #f #f #f) - (#f #t #f #t) - (#t #f #t #f) - (#t #t #t #t)))]) +(let ([n (table+vars->network '(a b) + '((#f #f #f #f) + (#f #t #f #t) + (#t #f #t #f) + (#t #t #t #t)))]) (tabulate-network n)) ]}