Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/artem/modulo-func' into artem/la…
Browse files Browse the repository at this point in the history
…mbda-reducer
  • Loading branch information
agureev committed Oct 9, 2023
2 parents 35b729f + 77d3901 commit f9b0b89
Show file tree
Hide file tree
Showing 14 changed files with 181 additions and 14 deletions.
4 changes: 3 additions & 1 deletion src/geb/geb.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@
nat-sub
nat-mult
nat-div
nat-const)
nat-const
nat-mod)
(nat-width (num x)))
(otherwise
(subclass-responsibility x))))
Expand Down Expand Up @@ -492,6 +493,7 @@ GEB> (gapply geb-bool:and
(nat-sub (- (car object) (cadr object)))
(nat-div (multiple-value-bind (q)
(floor (car object) (cadr object)) q))
(nat-mod (mod (car object) (cadr object)))
(nat-const (pos morph))
(nat-inj object)
(nat-concat (+ (* (expt 2 (num-right morph)) (car object)) (cadr object)))
Expand Down
4 changes: 4 additions & 0 deletions src/geb/trans.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,10 @@ get ((A x B) + (A x C)) as times appends sequences"
"Division is interpreted as divition"
(seqn-divide (geb.extension.spec:num obj)))

(defmethod to-seqn ((obj geb.extension.spec:nat-mod))
"Division is interpreted as divition"
(seqn-mod (geb.extension.spec:num obj)))

(defmethod to-seqn ((obj geb.extension.spec:nat-const))
"A choice of a natural number is the same exact choice in SeqN"
(seqn-nat (geb.extension.spec:num obj) (geb.extension.spec:pos obj)))
Expand Down
13 changes: 10 additions & 3 deletions src/lambda/lambda.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,12 @@ the context on the left as well. For more info check [LAMB][class]"))
(ann-term1 ctx rtm)
:ttype (coprod so1 so1)))
((lamb-lt ltm rtm) (lamb-lt (ann-term1 ctx ltm)
(ann-term1 ctx rtm)
:ttype (coprod so1 so1)))
((modulo ltm rtm) (let ((ant (ann-term1 ctx ltm)))
(modulo ant
(ann-term1 ctx rtm)
:ttype (coprod so1 so1)))
:ttype (ttype ant))))
((case-on on ltm rtm)
(let* ((ann-on (ann-term1 ctx on))
(type-of-on (ttype ann-on))
Expand Down Expand Up @@ -231,6 +235,7 @@ occurences - re-annotates the term and its subterms with actual
times
minus
divide
modulo
bit-choice
lamb-eq
lamb-lt)
Expand Down Expand Up @@ -322,7 +327,8 @@ nil"))
((or plus
minus
divide
times)
times
modulo)
(obj-equalp (ttype (ltm tterm))
(ttype (rtm tterm))))
((or lamb-eq
Expand Down Expand Up @@ -358,7 +364,8 @@ nil"))
(typep tterm 'times)
(typep tterm 'divide)
(typep tterm 'lamb-eq)
(typep tterm 'lamb-lt))
(typep tterm 'lamb-lt)
(typep tterm 'modulo))
(or (errorp (ltm tterm))
(errorp (rtm tterm))))
(t (errorp (term tterm)))))
Expand Down
9 changes: 6 additions & 3 deletions src/lambda/trans.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ error node"
(times #'nat-mult)
(divide #'nat-div)
(lamb-eq #'nat-eq)
(lamb-lt #'nat-lt)))
(lamb-lt #'nat-lt)
(modulo #'nat-mod)))

(defmethod to-cat-err (context (tterm <stlc>))
"Compiles a checked term with an error term in an appropriate context into the
Expand Down Expand Up @@ -257,7 +258,8 @@ This follows from the fact that bool arapped in maybe is 1 + (bool + bool)"
times
divide
lamb-eq
lamb-lt)
lamb-lt
modulo)
(let ((tyltm (ttype (ltm tterm))))
(labels ((arith (op)
(comp (mcase (terminal (prod (maybe tyltm)
Expand Down Expand Up @@ -392,7 +394,8 @@ iterated, so is the uncurrying."
times
divide
lamb-eq
lamb-lt)
lamb-lt
modulo)
(let ((ltm (ltm tterm)))
(labels ((arith (op)
(comp (funcall op (num (ttype ltm)))
Expand Down
4 changes: 4 additions & 0 deletions src/seqn/seqn.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ For a less formal desription consult the specs file"
(seqn-subtract (list (mcar x) (mcar x)))
(seqn-multiply (list (mcar x) (mcar x)))
(seqn-divide (list (mcar x) (mcar x)))
(seqn-mod (list (mcar x) (mcar x)))
(seqn-nat (list 0))
(seqn-concat (list (mcar x) (mcadr x)))
(seqn-decompose (list (mcar x)))
Expand Down Expand Up @@ -119,6 +120,7 @@ For a less formal description consult the specs file"
(seqn-multiply (list (mcar x)))
(seqn-divide (list (mcar x)))
(seqn-nat (list (mcar x)))
(seqn-mod (list (mcar x)))
(seqn-concat (list (+ (mcar x) (mcadr x))))
(seqn-decompose (list 1 (1- (mcar x))))
(seqn-eq (list 1 0))
Expand Down Expand Up @@ -167,6 +169,7 @@ is capable of succesfully evaluating all compiled terms"
(seqn-multiply (list (* (car vector) (cadr vector))))
(seqn-divide (list (multiple-value-bind (q)
(floor (car vector) (cadr vector)) q)))
(seqn-mod (list (mod (car vector) (cadr vector))))
(seqn-nat (list (mcadr morphism)))
(seqn-concat (list (+ (* (expt 2 (mcadr morphism)) (car vector))
(cadr vector))))
Expand Down Expand Up @@ -236,6 +239,7 @@ is capable of succesfully evaluating all compiled terms"
seqn-multiply
seqn-divide
seqn-subtract
seqn-mod
seqn-nat
seqn-concat
seqn-decompose
Expand Down
9 changes: 9 additions & 0 deletions src/seqn/trans.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,15 @@ removed already and hence we cannot count as usual"
:rhs cadr))
zero))))

(defmethod to-vampir ((obj seqn-mod) inputs constraints)
(declare (ignore constraints))
(let ((car (car inputs))
(cadr (cadr inputs)))
(if (const-check inputs)
(list (vamp:make-constant :const (mod (vamp:const car) (vamp:const cadr))))
(list (geb.vampir:mod-n (vamp:make-constant :const (mcar obj))
car cadr)))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Helpers
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Expand Down
13 changes: 12 additions & 1 deletion src/specs/extension.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ of natural numbers"))

(deftype natmorph ()
'(or nat-add nat-inj nat-mult nat-sub nat-div nat-const nat-concat
one-bit-to-bool nat-decompose nat-eq nat-lt))
one-bit-to-bool nat-decompose nat-eq nat-lt nat-mod))

(defclass nat-width (<natobj>)
((num :initarg :num
Expand Down Expand Up @@ -233,6 +233,14 @@ which evaluated to true iff both inputs are the same"))
(:documentation "Morphism nat-width n x nat-width n -> bool
which evaluated to true iff the first input is less than the second"))

(defclass nat-mod (<natmorph>)
((num :initarg :num
:accessor num
:documentation ""))
(:documentation "Morphism nat-width n x nat-width n -> nat-width n
which takes a modulo of the left projection of a pair by the second
projection of a pari"))

(defun nat-add (num)
(values
(make-instance 'nat-add :num num)))
Expand Down Expand Up @@ -284,6 +292,9 @@ which evaluated to true iff the first input is less than the second"))
(defun nat-lt (num)
(make-instance 'nat-lt :num num))

(defun nat-mod (num)
(make-instance 'nat-mod :num num))




Expand Down
1 change: 1 addition & 0 deletions src/specs/lambda-printer.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@
(easy-printer bit-choice)
(easy-printer lamb-eq)
(easy-printer lamb-lt)
(easy-printer modulo)
32 changes: 31 additions & 1 deletion src/specs/lambda.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ unit types as well as coproduct, product, and function types."))
which can be filled by auxillary functions or by user. Types are
represented as [SUBSTOBJ][GEB.SPEC:SUBSTOBJ]."
'(or absurd unit left right case-on pair fst snd lamb app index err
plus times minus divide bit-choice lamb-eq lamb-lt))
plus times minus divide bit-choice lamb-eq lamb-lt modulo))

;; New defgenerics

Expand Down Expand Up @@ -744,3 +744,33 @@ will be associated with the 0 input and teh second branch with 1."))
(defun lamb-lt (ltm rtm &key (ttype nil))
(values
(make-instance 'lamb-lt :ltm ltm :rtm rtm :ttype ttype)))

(defclass modulo (<stlc>)
((ltm :initarg :ltm
:accessor ltm
:documentation "")
(rtm :initarg :rtm
:accessor rtm
:documentation "")
(ttype :initarg :ttype
:initform nil
:accessor ttype
:documentation ""))
(:documentation "A term representing syntactic modulus of the first number
by the second number. The formal grammar of [MODULO] is
```lisp
(modulo ltm rtm)
```
where we can possibly supply typing info by
```lisp
(modulo ltm rtm :ttype ttype)
```
meaning that we take ltm mod rtm"))

(defun modulo (ltm rtm &key (ttype nil))
(values
(make-instance 'modulo :ltm ltm :rtm rtm :ttype ttype)))
16 changes: 14 additions & 2 deletions src/specs/package.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ constructors"
(seqn-concat pax:class)
(seqn-decompose pax:class)
(seqn-eq pax:class)
(seqn-lt pax:class))
(seqn-lt pax:class)
(seqn-mod pax:class))

(pax:defsection @seqn-constructors (:title "Seqn Constructors")
"Every accessor for each of the classes making up seqn"
Expand All @@ -203,7 +204,8 @@ constructors"
(seqn-concat pax:function)
(seqn-decompose pax:function)
(seqn-eq pax:function)
(seqn-lt pax:function))
(seqn-lt pax:function)
(seqn-mod pax:function))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Geb lambda Package Documentation
Expand Down Expand Up @@ -237,6 +239,7 @@ constructors"
(bit-choice pax:class)
(lamb-eq pax:class)
(lamb-lt pax:class)
(modulo pax:class)

(absurd pax:function)
(unit pax:function)
Expand All @@ -257,6 +260,7 @@ constructors"
(bit-choice pax:function)
(lamb-eq pax:function)
(lamb-lt pax:function)
(absurd pax:function)


"Accessors of [ABSURD][class]"
Expand Down Expand Up @@ -348,6 +352,11 @@ constructors"
(rtm (pax:method () (lamb-lt)))
(ttype (pax:method () (lamb-lt)))

"Accessors of [MODULO][class]"
(ltm (pax:method () (modulo)))
(rtm (pax:method () (modulo)))
(ttype (pax:method () (modulo)))

(tcod pax:generic-function)
(tdom pax:generic-function)
(term pax:generic-function)
Expand Down Expand Up @@ -579,6 +588,7 @@ throughout any piece of code"
(nat-decompose pax:class)
(nat-eq pax:class)
(nat-lt pax:class)
(nat-mod pax:class)

(num (pax:method () (nat-add)))
(num (pax:method () (nat-mult)))
Expand All @@ -592,6 +602,7 @@ throughout any piece of code"
(num (pax:method () (nat-decompose)))
(num (pax:method () (nat-eq)))
(num (pax:method () (nat-lt)))
(num (pax:method () (nat-mod)))

(nat-add pax:function)
(nat-mult pax:function)
Expand All @@ -604,6 +615,7 @@ throughout any piece of code"
(nat-decompose pax:function)
(nat-eq pax:function)
(nat-lt pax:function)
(nat-mod pax:function)

(num pax:generic-function)
(pos pax:generic-function)
Expand Down
1 change: 1 addition & 0 deletions src/specs/seqn-printer.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@
(easy-printer seqn-decompose)
(easy-printer seqn-eq)
(easy-printer seqn-lt)
(easy-printer seqn-mod)
17 changes: 16 additions & 1 deletion src/specs/seqn.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
'(or composition id fork-seq drop-nil remove-right remove-left drop-width
inj-length-left inj-length-right inj-size branch-seq shift-front zero-bit
one-bit parallel-seq seqn-add seqn-subtract seqn-multiply seqn-divide
seqn-nat seqn-concat seqn-decompose seqn-lt seqn-eq))
seqn-nat seqn-concat seqn-decompose seqn-lt seqn-eq seqn-mod))

(defclass <seqn> (geb.mixins:direct-pointwise-mixin cat-morph) ()
(:documentation "Seqn is a category whose objects are finite sequences of natural numbers.
Expand Down Expand Up @@ -262,6 +262,15 @@ seqn-eq n : (n, n) -> (1, 0) with the intended semantics being
that the morphism takes two n-bit integers and produces 1 iff the first
one is less than the second"))

(defclass seqn-mod (<seqn>)
((mcar :initarg :mcar
:accessor mcar
:documentation ""))
(:documentation " The type signature of the morphism is
seqn-mod n : (n, n) -> (n) with the intended semantics being
that the morphism takes two n-bit integers and produces the
modulo of the first integer by the second"))

(serapeum:-> composition (<seqn> <seqn>) composition)
(defun composition (mcar mcadr)
(values
Expand Down Expand Up @@ -378,6 +387,11 @@ one is less than the second"))
(values
(make-instance 'seqn-lt :mcar mcar)))

(serapeum:-> seqn-mod (fixnum) seqn-mod)
(defun seqn-mod (mcar)
(values
(make-instance 'seqn-mod :mcar mcar)))

(make-pattern composition mcar mcadr)
(make-pattern id mcar)
(make-pattern fork-seq mcar)
Expand All @@ -402,4 +416,5 @@ one is less than the second"))
(make-pattern seqn-decompose)
(make-pattern seqn-eq)
(make-pattern seqn-lt)
(make-pattern seqn-mod)

5 changes: 4 additions & 1 deletion src/vampir/package.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,8 @@
:pwmod32

:*range-n*
:range-n)))
:range-n

:*mod-n*
:mod-n)))

Loading

0 comments on commit f9b0b89

Please sign in to comment.