<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Aug 29, 2017, at 2:21 PM, David Sweeris <<a href="mailto:davesweeris@mac.com" class="">davesweeris@mac.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=us-ascii" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Aug 29, 2017, at 1:49 PM, Slava Pestov <<a href="mailto:spestov@apple.com" class="">spestov@apple.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class=""><br class=""><blockquote type="cite" class="">On Aug 29, 2017, at 11:03 AM, David Sweeris via swift-dev <<a href="mailto:swift-dev@swift.org" class="">swift-dev@swift.org</a>> wrote:<br class=""><br class="">Hi everyone! I'm trying to implement literal values as generic types.<br class=""></blockquote><br class="">Can you briefly explain what you mean by this?<br class=""><br class="">Are you referring to let-polymorphism, like<br class=""><br class="">let fn = { $0 }<br class="">let f1: (Int) -> Int = fn<br class="">let f2: (Float) -> Float = fn<br class=""></div></div></blockquote></div><br class=""><div class="">No, I mean so that a vector's or matrix's dimensions can be part of its type (strawman syntax and protocol name, but this is pretty much what I'll be trying to support, at least at first):</div></div></div></blockquote><div><br class=""></div><div>I think instead of modeling these generic parameters as types, you should look into generalizing GenericSignatures to contain ‘literal requirements’. Right now, we have four kinds of requirements:</div><div><br class=""></div><div>- A is a subclass of B</div><div>- A conforms to B</div><div>- A is the same type as B</div><div>- A has a known layout</div><div><br class=""></div><div>All of these except for the last one have a generic parameter as their right hand side. All of them have a generic parameter on their left hand side. I think what you want is to add a new ‘value parameter’ that is not a type, but instead has a value. Requirements would be placed on these to constrain them to known kinds of literals (integers, strings, etc).</div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: 'Fantasque Sans Mono'; color: rgb(127, 135, 207); background-color: rgb(0, 57, 70);" class=""><span style="color: #de5194" class="">struct</span><span style="color: #a4b0b1" class=""> Vector<T: </span>ExpressibleByIntegerLiteral<span style="color: #a4b0b1" class="">, L: </span>IntegerLiteralExpr<span style="color: #a4b0b1" class="">> {</span></div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: 'Fantasque Sans Mono'; color: rgb(164, 176, 177); background-color: rgb(0, 57, 70);" class=""> <span style="color: #de5194" class="">var</span> elements: [<span style="color: #7f87cf" class="">T</span>]</div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: 'Fantasque Sans Mono'; color: rgb(164, 176, 177); background-color: rgb(0, 57, 70);" class=""> <span style="color: #de5194" class="">init</span>() {</div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: 'Fantasque Sans Mono'; background-color: rgb(0, 57, 70);" class=""><span style="color: rgb(164, 176, 177);" class=""> <span style="color: #2fafa9" class="">elements</span> = [<span style="color: #7f87cf" class="">T</span>](repeating: <span style="color: #e5493d" class="">0</span>, count: </span><font color="#7f87cf" class="">L</font><font color="#a4b0b1" class="">)</font></div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: 'Fantasque Sans Mono'; color: rgb(164, 176, 177); background-color: rgb(0, 57, 70);" class=""> }</div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: 'Fantasque Sans Mono'; color: rgb(164, 176, 177); background-color: rgb(0, 57, 70);" class="">}</div><div style="margin: 0px; line-height: normal; background-color: rgb(0, 57, 70); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: 'Fantasque Sans Mono'; color: rgb(164, 176, 177); background-color: rgb(0, 57, 70);" class=""><span style="color: #de5194" class="">let</span> vect = <span style="color: #7f87cf" class="">Vector</span><span style="text-decoration: underline" class=""><</span><span style="color: #7f87cf" class="">Int</span>, <span style="text-decoration: underline ; color: #e5493d" class="">5</span>>()</div></div><div class=""><br class=""></div><div class="">And, once that's working, I'm going to add support simple "type functions":</div><div class=""><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: 'Fantasque Sans Mono'; color: rgb(164, 176, 177); background-color: rgb(0, 57, 70);" class=""><span style="color: #de5194" class="">func</span> join <T, L1, L2> (<span style="color: #de5194" class="">_</span> lhs: <span style="color: #7f87cf" class="">Vector</span><<span style="color: #7f87cf" class="">T</span>, <span style="color: #7f87cf" class="">L1</span>>, <span style="color: #de5194" class="">_</span> rhs: <span style="color: #7f87cf" class="">Vector</span><<span style="color: #7f87cf" class="">T</span>, <span style="color: #7f87cf" class="">L2</span>>) -> <span style="color: rgb(127, 135, 207);" class="">Vector</span><<span style="color: rgb(127, 135, 207);" class="">T</span>, <span style="color: rgb(127, 135, 207);" class="">L1</span> + <span style="color: rgb(127, 135, 207);" class="">L2</span> > {...}</div></div><div class="">I think restricting the supported "type functions" to expressions that could be evaluated by the compiler's "constant folding" code would be a reasonable place to start,</div></div></div></blockquote><div><br class=""></div><div>The compiler’s constant folding operates on SIL instructions, not Exprs directly. However constant folding is not generally what you want for this, because constant folding is a ‘best effort’ kind of optimization (it may or may not fold your calculation down to a constant) and also it produces code that evaluates the result (even if its a constant) and not the result itself.</div><div><br class=""></div><div>I think if you want to explore type-level computation like this, you should define a very small subset of the language which can be computed by the type checker like this.</div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""> until we figure out what we want to do about "pure"/"constexpr" stuff... even just "+" for numeric and string literals, and "-" for numeric literals, seems like a reasonable starting goal, and I <i class="">think</i> that'd be simple enough to implement (famous last words, right?)... It's all academic until I get the simple cases working first, though.</div><div class=""><br class=""></div><div class="">Anyway, I think it'll be worth writing up as a proposal, once it's working and I've figured out how to present these "literal types" to Swift's type system (I'd like to keep it as a literal of some sort, so that, say, `L` in this example could be used to set the value of any type that conforms to `ExpressibleByIntegerLiteral`).</div><div class=""><br class=""></div><div class="">- Dave Sweeris</div><div class=""><br class=""></div></div></div></blockquote></div><br class=""></body></html>