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

Define ordering and equality on Char #306

Merged
merged 4 commits into from
Dec 7, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
15 changes: 15 additions & 0 deletions examples/eval-tests.dx
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,21 @@ s1 = "hello world"
:p s1
> ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']

:p codepoint 'a'
97

:p 'a' == 'a'
True

:p 'a' == 'A'
False

:p 'a' < 'b'
True

:p 'a' > 'b'
false
oxinabox marked this conversation as resolved.
Show resolved Hide resolved

:p
x = 2 + 2
y = 2 + 4
Expand Down
12 changes: 10 additions & 2 deletions prelude.dx
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,6 @@ def argmin (_:Ord o) ?=> (xs:n=>o) : n =
zipped = for i. (i, xs.i)
fst $ reduce zeroth compare zipped


'Automatic differentiation

-- TODO: add vector space constraints
Expand Down Expand Up @@ -793,6 +792,8 @@ splitV : Iso a ({|} | a) =
def sliceFields (iso: Iso ({|} | a) (b | c)) (tab: a=>v) : b=>v =
reindex (buildWith $ splitV &>> iso) tab

'## Strings and Characters

Char : Type = %Char
def MkChar (c:Int8) : Char = %MkChar c

Expand All @@ -803,6 +804,14 @@ CharPtr : Type = %CharPtr
interface Show a:Type where
show : a -> String

-- TODO. This is ASCII code point. It really should be Int32 for Unicode codepoint
def codepoint (c:Char) : Int8 = %codePoint c

@instance charEq : Eq Char = MkEq \x y. codepoint x == codepoint y
@instance charOrd : Ord Char = (MkOrd charEq (\x y. codepoint x > codepoint y)
(\x y. codepoint x < codepoint y))


def showFloat' (x:Float) : String =
(n, ptr) = %ffi showFloat (Int & CharPtr) x
AsList n $ for i:(Fin n).
Expand All @@ -811,7 +820,6 @@ def showFloat' (x:Float) : String =
instance showFloat : Show Float where
show = showFloat'


'## Floating point helper functions

def sign (x:Float) : Float =
Expand Down
4 changes: 3 additions & 1 deletion src/lib/Autodiff.hs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ linearizeOp op = case op of
UnsafeFromOrdinal _ _ -> emitDiscrete
ToOrdinal _ -> emitDiscrete
IdxSetSize _ -> emitDiscrete
CodePoint _ -> emitDiscrete
ThrowError _ -> emitWithZero
CastOp t v -> do
if tangentType vt == vt && tangentType t == t
Expand Down Expand Up @@ -627,8 +628,9 @@ transposeOp op ct = case op of
SliceOffset _ _ -> notLinear
SliceCurry _ _ -> notLinear
UnsafeFromOrdinal _ _ -> notLinear
ToOrdinal _ -> notLinear
ToOrdinal _ -> notLinear
IdxSetSize _ -> notLinear
CodePoint _ -> notLinear
ThrowError _ -> notLinear
FFICall _ _ _ -> notLinear
where
Expand Down
1 change: 1 addition & 0 deletions src/lib/Imp.hs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ toImpOp (maybeDest, op) = case op of
tileOffset' <- iaddI (fromScalarAtom tileOffset) extraOffset
returnVal $ toScalarAtom tileOffset'
ThrowError _ -> throwError ()
CodePoint ~(Con (CharCon x)) -> returnVal x
CastOp destTy x -> case (getType x, destTy) of
(BaseTy _, BaseTy bt) -> returnVal =<< toScalarAtom <$> cast (fromScalarAtom x) bt
_ -> error $ "Invalid cast: " ++ pprint (getType x) ++ " -> " ++ pprint destTy
Expand Down
4 changes: 3 additions & 1 deletion src/lib/Syntax.hs
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ data PrimOp e =
| ToOrdinal e
| IdxSetSize e
| ThrowError e
| CodePoint e
| CastOp e e -- Type, then value. See Type.hs for valid coercions.
-- Extensible record and variant operations:
-- Add fields to a record (on the left). Left arg contains values to add.
Expand Down Expand Up @@ -1473,7 +1474,8 @@ builtinNames = M.fromList
, ("vfadd", vbinOp FAdd), ("vfsub", vbinOp FSub), ("vfmul", vbinOp FMul)
, ("idxSetSize" , OpExpr $ IdxSetSize ())
, ("unsafeFromOrdinal", OpExpr $ UnsafeFromOrdinal () ())
, ("toOrdinal" , OpExpr $ ToOrdinal ())
, ("toOrdinal" , OpExpr $ ToOrdinal ())
, ("codePoint" , OpExpr $ CodePoint ())
, ("throwError" , OpExpr $ ThrowError ())
, ("ask" , OpExpr $ PrimEffect () $ MAsk)
, ("tell" , OpExpr $ PrimEffect () $ MTell ())
Expand Down
3 changes: 3 additions & 0 deletions src/lib/Type.hs
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,9 @@ typeCheckOp op = case op of
i |: TC (IntRange (IdxRepVal 0) (IdxRepVal $ fromIntegral vectorWidth))
return $ BaseTy $ Scalar sb
ThrowError ty -> ty|:TyKind $> ty
-- TODO: type check that c is a character
-- TODO: this should really be a 32 bit integer for unicode code point: but for now is 8 bit ASCII code point
CodePoint c -> return $ BaseTy $ Scalar Int8Type
Copy link
Contributor Author

@oxinabox oxinabox Dec 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Presumably there is more checking i should do to make sure the c is a character not some other type?
I don't know how though.

Right now i get an error that crashes the repl if i try to do nonsense

>=> %codePoint 1
dex: src/lib/Imp.hs:(194,27)-(275,2): Non-exhaustive patterns in Con (CharCon x)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the check would be something like c |: CharTy. But you might still get a compiler error, just an earlier one, if you write %codePoint 1. The %-prefixed names are supposed to be wrapped in the prelude and not used directly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does c |: CharTy do?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It checks that c has type CharTy. (|:) is defined here and CharTy is defined here.

Copy link
Contributor Author

@oxinabox oxinabox Dec 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, now we get a better error that doesn't crash the REPL

>=> %codePoint 'a'
97
>=> %codePoint 1
Compiler bug!
Please report this at github.com/google-research/dex-lang/issues

assertion failure ():
Char != Int32

checking decl:
tmp:Int8 = %codePoint 1
Checking body types
Checking module:
Module (Typed)
  unevaluated decls:
  tmp:Int8 = %codePoint 1
  tmp1:Int8 = tmp
  _ans_:Int8 = tmp1

  evaluated bindings:
  ()

Though still i guess not great since it says it is a compiler bug.
But still noone should use %codePoint directly anyway, we have codepoint in the prelude for that, as you say.

CastOp t@(Var _) _ -> t |: TyKind $> t
CastOp destTy e -> do
sourceTy <- typeCheck e
Expand Down