Skip to content
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

don't specialize Lisp vector underlying VECTOR/SLICE :A #1005

Merged
merged 3 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions examples/quil-coalton/src/quil-coalton.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
(declare parse-quil-string (Parser String))
(define parse-quil-string
(map3
(fn (a b c) (into b))
(fn (_a b _c) (into b))
(char #\")
(many0
(alt
(not-char #\")
(map2 (fn (a b) b)
(map2 (fn (_a b) b)
(char #\\)
(char #\"))))
(char #\")))
Expand Down Expand Up @@ -259,14 +259,14 @@

(declare parse-quil-comment (Parser Unit))
(define parse-quil-comment
(map3 (fn (a b c) Unit)
(map3 (fn (_a _b _c) Unit)
(many0 non-newline-whitespace)
(char #\#)
(many0 (not-char #\Newline))))

(declare parse-quil-comment-line (Parser Unit))
(define parse-quil-comment-line
(map3 (fn (_ __ ___) Unit)
(map3 (fn (_a _b _c) Unit)
(many0 whitespace)
parse-quil-comment
(alt (map (const Unit) (char #\Newline))
Expand All @@ -285,10 +285,10 @@
QuilProgram
(map2 const
(many0
(map2 (fn (a _) a)
(map2 (fn (a _b) a)
;; Quil statements
(map4
(fn (_ a __ ___) a)
(fn (_a b _c _d) b)
;; Allow leading whitespace (including newlines) and comments
(many0 (alt parse-quil-comment-line whitespace))
parse-quil-statement
Expand Down
6 changes: 3 additions & 3 deletions examples/thih/src/thih.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@

(declare initialEnv ClassEnv)
(define initialEnv
(ClassEnv (fn (i) (fail "Class not defined"))
(ClassEnv (fn (_i) (fail "Class not defined"))
(make-list tInteger tDouble)))

;; (define-type-alias EnvTransformer (ClassEnv -> (Optional ClassEnv)))
Expand Down Expand Up @@ -924,7 +924,7 @@
(Ambiguity Tyvar (List Pred)))

(declare ambiguities (ClassEnv -> (List Tyvar) -> (List Pred) -> (List Ambiguity)))
(define (ambiguities ce vs ps)
(define (ambiguities _ce vs ps)
(map (fn (v)
(Ambiguity v (filter
(fn (x)
Expand Down Expand Up @@ -983,7 +983,7 @@

(declare defaultedPreds (MonadFail :m => (ClassEnv -> (List Tyvar) -> (List Pred) -> (:m (List Pred)))))
(define defaultedPreds
(withDefaults (fn (vps ts) (concat (map (fn (a)
(withDefaults (fn (vps _ts) (concat (map (fn (a)
(match a
((Ambiguity _ x) x)))
vps)))))
Expand Down
1 change: 0 additions & 1 deletion library/math/integral.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ are floored and truncated division, respectively."
(gcd (cl:intern (cl:concatenate 'cl:string (cl:symbol-name type) "-GCD")))
(^ (cl:intern (cl:concatenate 'cl:string (cl:symbol-name type) "-^")))
(lcm (cl:intern (cl:concatenate 'cl:string (cl:symbol-name type) "-LCM")))
(ilog (cl:intern (cl:concatenate 'cl:string (cl:symbol-name type) "-ILOG")))
(isqrt (cl:intern (cl:concatenate 'cl:string (cl:symbol-name type) "-ISQRT"))))
`(coalton-toplevel
(define-instance (Remainder ,type)
Expand Down
2 changes: 1 addition & 1 deletion library/seq.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ shifts the each member of `target` down by `n` positions. Mutates both
(vector:set! (- (vector:length v) 1) a cv)
cv))

(declare butfirst (iter:FromIterator (vector:Vector :a) :a => vector:Vector :a -> vector:Vector :a))
(declare butfirst (vector:Vector :a -> vector:Vector :a))
(define (butfirst v)
(iter:collect!
(map (flip vector:index-unsafe v)
Expand Down
59 changes: 23 additions & 36 deletions library/slice.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#:Slice
#:new
#:length
#:element-type
#:set!
#:index
#:index-unsafe
Expand All @@ -37,7 +36,7 @@
;; Slice
;;

(repr :native (cl:and (cl:vector cl:*) (cl:not cl:simple-array)))
(repr :native (cl:and (cl:vector cl:t) (cl:not cl:simple-vector)))
(define-type (Slice :a))

(define-class (Sliceable :a)
Expand All @@ -49,9 +48,9 @@
(define-instance (Sliceable (Slice :a))
(define %length length))

(declare new ((types:RuntimeRepr :a) (Sliceable (:b :a)) => UFix -> UFix -> :b :a -> Slice :a))
(declare new ((Sliceable (:b :a)) => UFix -> UFix -> :b :a -> Slice :a))
(define (new start length v)
"Create a new slice backed by V starting at index START and continuing for LENGTH elements."
"Create a new slice backed by `v` starting at index `start` and continuing for `length` elements."
(when (< start 0)
(error "Start of slice cannot be less than 0."))

Expand All @@ -62,54 +61,42 @@
(when (> end (%length v))
(error "Slice cannot extend beyond length of backing vector."))

(let p = types:Proxy)
(let p_ = (types:proxy-inner p))
(let t = (types:runtime-repr p_))

(types:as-proxy-of
(lisp (Slice :a) (v start length t)
(cl:make-array
length
:element-type t
:displaced-to v
:displaced-index-offset start))
p))
(lisp (Slice :a) (v start length)
(cl:make-array
length
:element-type cl:t
:displaced-to v
:displaced-index-offset start)))

(declare length (Slice :a -> UFix))
(define (length s)
"Returns the length of S"
"Returns the length of `s`."
(lisp UFix (s)
(cl:array-dimension s 0)))

(declare element-type (Slice :a -> types:LispType))
(define (element-type s)
"Returns the element type of S as a LispType"
(lisp types:LispType (s)
(cl:array-element-type s)))

(declare set! (UFix -> :a -> (Slice :a) -> Unit))
(define (set! index item s)
"Set the element at INDEX in S to ITEM"
"Set the element at `index` in `s` to `item`."
(lisp :a (index item s)
(cl:setf (cl:aref s index) item))
Unit)

(declare index (UFix -> (Slice :a) -> (Optional :a)))
(define (index idx s)
"Lookup the element at INDEX in S"
"Lookup the element at `index` in `s`."
(if (>= idx (length s))
None
(Some (index-unsafe idx s))))

(declare index-unsafe (UFix -> (Slice :a) -> :a))
(define (index-unsafe idx s)
"Lookup the element at INDEX in S without bounds checking"
"Lookup the element at `index` in `s` without bounds checking."
(lisp :a (idx s)
(cl:aref s idx)))

(declare iter-sliding ((types:RuntimeRepr :a) (Sliceable (:b :a)) => UFix -> :b :a -> iter:Iterator (Slice :a)))
(declare iter-sliding ((Sliceable (:b :a)) => UFix -> :b :a -> iter:Iterator (Slice :a)))
(define (iter-sliding size s)
"Returns an iterator that yeilds a series of overlapping slices of length SIZE."
"Returns an iterator that yeilds a series of overlapping slices of length `size`."
(let length = (%length s))
(let offset_ = (cell:new 0))
(iter:with-size
Expand All @@ -124,9 +111,9 @@
0
(- (+ length 1) size))))

(declare iter-chunked ((types:RuntimeRepr :a) (Sliceable (:b :a)) => UFix -> :b :a -> iter:Iterator (Slice :a)))
(declare iter-chunked ((Sliceable (:b :a)) => UFix -> :b :a -> iter:Iterator (Slice :a)))
(define (iter-chunked size s)
"Divide S into a series of slices of length SIZE. Will return a final shorter slice if S does not divide evenly."
"Divide `s` into a series of slices of length `size`. Will return a final shorter slice if `s` does not divide evenly."
(let length = (%length s))
(let offset_ = (cell:new 0))
(iter:with-size
Expand All @@ -152,9 +139,9 @@
(True
(+ 1 (div length size))))))

(declare iter-chunked-exact ((types:RuntimeRepr :a) (Sliceable (:b :a)) => UFix -> :b :a -> iter:Iterator (Slice :a)))
(declare iter-chunked-exact ((Sliceable (:b :a)) => UFix -> :b :a -> iter:Iterator (Slice :a)))
(define (iter-chunked-exact size s)
"Divide S into a series of slices of length SIZE. Will skip trailing elements if S does not divide evenly."
"Divide `s` into a series of slices of length `size`. Will skip trailing elements if `s` does not divide evenly."
(let length = (%length s))
(let offset_ = (cell:new 0))
(iter:with-size
Expand All @@ -181,7 +168,7 @@
res)
(length s))))

(define-instance (types:RuntimeRepr :a => iter:FromIterator (Slice :a) :a)
(define-instance (iter:FromIterator (Slice :a) :a)
(define (iter:collect! iter)
;; NOTE: This will create a non displaced array. It should be
;; fine, because it isn't observable with the slice API.
Expand Down Expand Up @@ -212,17 +199,17 @@
:initial-value init
:from-end cl:t))))

(define-instance (types:RuntimeRepr :a => Into (Slice :a) (Vector :a))
(define-instance (Into (Slice :a) (Vector :a))
(define (into s)
(let v = (vector:with-capacity (length s)))
(vector:extend! v (iter:into-iter s))
v))

(define-instance (types:RuntimeRepr :a => Into (Vector :a) (Slice :a))
(define-instance (Into (Vector :a) (Slice :a))
(define (into v)
(new 0 (vector:length v) v)))

(define-instance (types:RuntimeRepr :a => Iso (Slice :a) (Vector :a))))
(define-instance (Iso (Slice :a) (Vector :a))))

#+sb-package-locks
(sb-ext:lock-package "COALTON-LIBRARY/SLICE")
15 changes: 8 additions & 7 deletions library/string.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@

(declare strip-prefix (String -> String -> (Optional String)))
(define (strip-prefix prefix str)
"Returns a string without a give prefix, or None if the string
"Returns a string without a give prefix, or `None` if the string
does not have that suffix."
(let prefix-len = (length prefix))
(let substr = (substring str 0 prefix-len))
Expand All @@ -83,7 +83,7 @@ does not have that suffix."

(declare strip-suffix (String -> String -> (Optional String)))
(define (strip-suffix suffix str)
"Returns a string without a give suffix, or None if the string
"Returns a string without a give suffix, or `None` if the string
does not have that suffix."
(let suffix-len = (length suffix))
(let str-len = (length str))
Expand All @@ -94,7 +94,7 @@ does not have that suffix."

(declare parse-int (String -> (Optional Integer)))
(define (parse-int str)
"Parse the integer in string STR."
"Parse the integer in string `str`."
(lisp (Optional Integer) (str)
(cl:let ((x (cl:parse-integer str :junk-allowed cl:t)))
(cl:if x
Expand All @@ -103,13 +103,13 @@ does not have that suffix."

(declare ref-unchecked (String -> UFix -> Char))
(define (ref-unchecked str idx)
"Return the IDXth character of STR. This function is partial."
"Return the `idx`th character of `str`. This function is partial."
(lisp Char (str idx)
(cl:char str idx)))

(declare ref (String -> UFix -> (Optional Char)))
(define (ref str idx)
"Return the IDXth character of STR."
"Return the `idx`th character of `str`."
(if (< idx (length str))
(Some (ref-unchecked str idx))
None))
Expand All @@ -132,7 +132,7 @@ does not have that suffix."

(declare chars (String -> iter:Iterator Char))
(define (chars str)
"Returns an iterator over the characters in STR."
"Returns an iterator over the characters in `str`."
(iter:into-iter str))

;;
Expand Down Expand Up @@ -165,7 +165,8 @@ does not have that suffix."
(define-instance (iter:FromIterator String Char)
(define (iter:collect! iter)
(let vec = (the (Vector Char) (iter:collect! iter)))
(lisp String (vec) vec)))
(lisp String (vec)
(cl:coerce vec 'cl:string))))

;;
;; Conversions
Expand Down
Loading