Skip to content

Commit 11e8925

Browse files
committed
feat: implement in and is operators
Implementation of in operator forn tuples, lists, and strings. Implementation of is operator for the spacial case of None.
1 parent 1007c72 commit 11e8925

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

KLR/NKI/Annotations.lean

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ abbrev Ann := Pass Unit
4141

4242
private def isValidName' : Name -> Bool
4343
| .str `neuronxcc.nki._pre_prod_kernels _
44+
| .str `neuronxcc.nki._private_nkl _
4445
| .str `neuronxcc.nki._pre_prod_nkl _ => true
4546
| .str _ "neuronxcc" => false
4647
| .str n _ => isValidName' n

KLR/NKI/Simplify.lean

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,14 @@ private def expr' (e' : Python.Expr') : Simplify Expr' :=
202202
| .boolOp op l => return (<- booleanOp op (<- exprs l)).expr
203203
| .binOp op l r => return .binOp (<- binOp op) (<- expr l) (<- expr r)
204204
| .unaryOp op e => return (<- unaryOp op) (<- expr e)
205+
| .compare a [.is] [⟨.const .none, pos⟩] =>
206+
return .binOp .eq (<- expr a) ⟨ .value .none, pos ⟩
207+
| .compare a [.isNot] [⟨.const .none, pos⟩] =>
208+
return .binOp .ne (<- expr a) ⟨ .value .none, pos ⟩
209+
| .compare a [.isIn] [b] =>
210+
return .call ⟨ .var `builtin.op.in, a.pos ⟩ [<- expr a, <- expr b] []
211+
| .compare a [.notIn] [b] =>
212+
return .call ⟨ .var `builtin.op.notin, a.pos ⟩ [<- expr a, <- expr b] []
205213
| .compare a ops l => do
206214
let a <- expr a
207215
let ops <- ops.mapM cmpOp
@@ -396,10 +404,10 @@ private def params (args : Python.Args) : Simplify (List Param) := do
396404
throw "varargs are not supported in NKI"
397405
if args.kwarg.isSome then
398406
warn "variable keyword arguments are not supported in NKI"
399-
if args.posonlyargs.length > 0 then
400-
warn "position-only arguments are not supported in NKI"
401-
if args.kwonlyargs.length > 0 then
402-
warn "keyword-only arguments are not supported in NKI"
407+
--if args.posonlyargs.length > 0 then
408+
-- warn "position-only arguments are not supported in NKI"
409+
--if args.kwonlyargs.length > 0 then
410+
-- warn "keyword-only arguments are not supported in NKI"
403411
let defaults := args.all_defaults
404412
let mut params := []
405413
for name in args.names do

KLR/Trace/Python.lean

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
-/
1616

17+
import Batteries.Data.String
1718
import KLR.Core
1819
import KLR.Trace.ISA
1920
import KLR.Trace.Types
@@ -24,6 +25,7 @@ Python related builtins
2425
-/
2526

2627
namespace KLR.Trace
28+
open Substring (containsSubstr)
2729
open Core
2830

2931
/-
@@ -53,6 +55,22 @@ nki builtin.op.invert (t : Term) := do
5355
let i : Int <- fromNKI? t
5456
return .int i.toInt32.complement.toInt
5557

58+
private def isin (t : Term) (l : Term) : Trace Bool := do
59+
let l <- match l with
60+
| .ref name _ => lookup name
61+
| _ => pure l
62+
match t, l with
63+
| _, .tuple l => return l.contains t
64+
| _, .list a => return a.contains t
65+
| .string t, .string s => return containsSubstr s t
66+
| _ , _ => throw "in operator not support on types {kindStr t} and {kindStr l}"
67+
68+
nki builtin.op.in (t : Term) (l : Term) := do
69+
return .bool (<- isin t l)
70+
71+
nki builtin.op.notin (t : Term) (l : Term) := do
72+
return .bool (<- isin t l).not
73+
5674
/-
5775
The builtin.python namespace is mapped to the top-level namespace.
5876
For example, builtin.python.f will appear as f.

0 commit comments

Comments
 (0)