-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add method to compile geb to boolean circuits. #105
Closed
Closed
Changes from 15 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
c7394b8
boolean circuit compliation port (not tested)
AHartNtkn f8bc753
Merge pull request #1 from anoma/main
AHartNtkn 9b4cab9
Save progress on bug fixing
AHartNtkn c860949
Further bug fixing (imports broken?)
AHartNtkn 386aec5
Fix more bugs, but still not working
AHartNtkn 23c86ea
Merge branch 'main' into main
AHartNtkn e791bf6
Merge pull request #2 from anoma/main
AHartNtkn f90cc4b
Move dom and codom
AHartNtkn e70c313
Bug fixing (It works now)
AHartNtkn 55bdd6b
Add comments for future generations
AHartNtkn 621c535
Make 1 make constant
AHartNtkn 5f570fd
Rename bits.lisp to bitc.lisp
AHartNtkn ee2b783
Fix bugs when translating from pairs and cases to bitc
AHartNtkn e897481
Make implementation slightly closer to original
AHartNtkn 9ebdc89
Add comments to to-bitc
AHartNtkn f23043d
Allow substobj within substmorph to be handled by to-bitc
AHartNtkn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
(in-package :geb.bitc.main) | ||
|
||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
;; Domain and codomain definitions | ||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
|
||
(defmethod dom ((x <bitc>)) | ||
(typecase-of bitc x | ||
(compose (dom (mcadr x))) | ||
(fork (dom (mcar x))) | ||
(parallel (+ (dom (mcar x)) (dom (mcadr x)))) | ||
(swap (+ (mcar x) (mcadr x))) | ||
(one 0) | ||
(zero 0) | ||
(ident (mcar x)) | ||
(drop (mcar x)) | ||
(branch (+ 1 (dom (mcar x)))) | ||
(otherwise | ||
(subclass-responsibility x)))) | ||
|
||
(defmethod codom ((x <bitc>)) | ||
(typecase-of bitc x | ||
(compose (codom (mcar x))) | ||
(fork (* 2 (codom (mcar x)))) | ||
(parallel (+ (codom (mcar x)) (codom (mcadr x)))) | ||
(swap (+ (mcar x) (mcadr x))) | ||
(one 1) | ||
(zero 1) | ||
(ident (mcar x)) | ||
(drop 0) | ||
(branch (codom (mcar x))) | ||
(otherwise | ||
(subclass-responsibility x)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
(in-package :geb.utils) | ||
|
||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
;;; trans module | ||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
|
||
(muffle-package-variance | ||
(defpackage #:geb.bitc.trans | ||
(:local-nicknames (:vamp :geb.vampir.spec)) | ||
(:use #:geb.common #:geb.bitc.spec) | ||
(:shadowing-import-from #:geb.bitc.spec #:drop #:fork) | ||
)) | ||
|
||
(in-package :geb.bitc.trans) | ||
|
||
(pax:defsection @bitc-trans (:title "Bits (Boolean Circuit) Transformations") | ||
"This covers transformation functions from" | ||
(to-vampir pax:generic-function) | ||
(to-circuit pax:function)) | ||
|
||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
;;; bitc module | ||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
|
||
(geb.utils:muffle-package-variance | ||
(uiop:define-package #:geb.bitc.main | ||
(:use #:geb.common #:geb.mixins) | ||
(:shadowing-import-from #:geb.bitc.spec #:drop #:fork) | ||
(:use-reexport #:geb.bitc.trans #:geb.bitc.spec))) | ||
|
||
(geb.utils:muffle-package-variance | ||
(uiop:define-package #:geb.bitc | ||
(:use #:geb.common) | ||
(:shadowing-import-from #:geb.bitc.spec :fork :drop) | ||
(:use-reexport #:geb.bitc.trans #:geb.bitc.spec #:geb.bitc.main))) | ||
|
||
(in-package :geb.bitc.main) | ||
|
||
(in-package :geb.bitc) | ||
|
||
(pax:defsection @bitc-manual (:title "Bits (Boolean Circuit) Specification") | ||
"This covers a GEB view of Boolean Circuits. In particular this type will | ||
be used in translating GEB's view of Boolean Circuits into Vampir" | ||
(@bitc pax:section) | ||
(@bitc-constructors pax:section) | ||
(@bitc-trans pax:section)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
(in-package :geb.bitc.trans) | ||
|
||
(defgeneric to-vampir (morphism values) | ||
(:documentation "Turns a BITC term into a Vamp-IR term with a given value")) | ||
|
||
(defun to-circuit (morphism name) | ||
"Turns a BITC term into a Vamp-IR Gate with the given name" | ||
(let* ((wire-count (dom morphism)) | ||
(wires (loop for i from 1 to wire-count | ||
collect (vamp:make-wire :var (intern (format nil "x~a" i)))))) | ||
(vamp:make-alias :name name | ||
:inputs wires | ||
:body (to-vampir morphism wires)))) | ||
|
||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
;; Bits to Vampir Implementation | ||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
|
||
(defmethod to-vampir ((obj <bitc>) values) | ||
(declare (ignore values)) | ||
(subclass-responsibility obj)) | ||
|
||
(-> direct-fields-to-list-vampir (geb.mixins:direct-pointwise-mixin) list) | ||
(defun direct-fields-to-list (obj) | ||
(mapcar #'cdr (geb.mixins:to-pointwise-list obj))) | ||
|
||
(defun infix-creation (symbol value1 value2) | ||
(vamp:make-infix :op symbol | ||
:lhs value1 | ||
:rhs value2)) | ||
|
||
|
||
(defmethod to-vampir ((obj compose) values) | ||
(to-vampir (mcar obj) | ||
(to-vampir (mcadr obj) values))) | ||
|
||
(defmethod to-vampir ((obj fork) values) | ||
; Copy input n intput bits into 2*n output bits | ||
(append values values)) | ||
|
||
(defmethod to-vampir ((obj parallel) values) | ||
; toElem[par[car_, cadr_]] := | ||
; Function[{inp}, | ||
; Module[{cx, inp1, inp2}, | ||
; cx = dom[car]; | ||
; inp1 = inp[[1 ;; cx]]; | ||
; inp2 = inp[[cx + 1 ;; All]]; | ||
; Flatten[{toElem[car][inp1], toElem[cadr][inp2]}, 1] | ||
; ]] | ||
|
||
|
||
; Take n + m bits, execute car the n bits and cadr on the m bits and | ||
; concat the results from car and cadr | ||
(let* ((car (mcar obj)) | ||
(cadr (mcadr obj)) | ||
(cx (dom car)) | ||
(inp1 (subseq values 0 cx)) | ||
(inp2 (subseq values cx))) | ||
(concatenate 'list (to-vampir car inp1) (to-vampir cadr inp2)))) | ||
|
||
(defmethod to-vampir ((obj swap) values) | ||
; toElem[swap[n_, m_]] := Flatten[{#[[n + 1 ;; All]], #[[1 ;; n]]}, 1] & | ||
; Turn n + m bits into m + n bits by swapping | ||
(let ((n (mcar obj))) | ||
(append (subseq values (+ 1 n)) (subseq values 0 (+ 1 n))))) | ||
|
||
(defmethod to-vampir ((obj one) values) | ||
; toElem[True] := {1} & | ||
; Produce a bitvector of length 1 containing 1 | ||
(declare (ignore values)) | ||
(list (vamp:make-constant :const 1))) | ||
|
||
(defmethod to-vampir ((obj zero) values) | ||
; toElem[False] := {0} & | ||
; Produce a bitvector of length 1 containing 0 | ||
(declare (ignore values)) | ||
(list (vamp:make-constant :const 0))) | ||
|
||
(defmethod to-vampir ((obj ident) values) | ||
; toElem[id[_]] := # & | ||
; turn n bits into n bits by doing nothing | ||
values) | ||
|
||
(defmethod to-vampir ((obj drop) values) | ||
; toElem[drop[_]] := {} & | ||
; turn n bits into an empty bitvector | ||
(declare (ignore values)) | ||
nil) | ||
|
||
(defmethod to-vampir ((obj branch) values) | ||
; toElem[branch[f_, g_]][{x_, values__}] := | ||
; Thread@Plus[ | ||
; Times[1 - x, #] & /@ toElem[f][{values}], | ||
; Times[x, #] & /@ toElem[g][{values}] | ||
; ] | ||
|
||
; Look at the first bit. | ||
; If its 1, run f on the remaining bits. | ||
; If its 0, run g on the remaining bits. | ||
(let* ((x (car values)) | ||
(xs (cdr values)) | ||
(f (mcar obj)) | ||
(g (mcadr obj)) | ||
(f-elems (to-vampir f xs)) | ||
(g-elems (to-vampir g xs))) | ||
(mapcar #'(lambda (f-elem g-elem) | ||
(infix-creation :+ | ||
(infix-creation :* (infix-creation :- (vamp:make-constant :const 1) x) f-elem) | ||
(infix-creation :* x g-elem))) | ||
f-elems g-elems))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a question, if we see a
<substobj>
in amorphism
slot then it's the identity, is this saved at all?I know for things that take
objects
this is correctFor example
is just
identity . identity
for the type so1There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know what you mean by "saved". to-bitc, when applied to a substobj, just calculates the bit-width required to store the object (in other words, it converts a GEB object into a BITC object). It's generally used when calculating the bitwidth of a domain. Although, it may not be necessary.
(to-bitc (dom X))
should be the same as(dom (to-bitc X))
, etc.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wasn't aware that objects become identities in geb morphism slots. I did not write any code with that in mind.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
might be good to update the code with that if possible
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright, I think I've fixed that.