Skip to content

Commit

Permalink
Change data to enum
Browse files Browse the repository at this point in the history
Matches Rust. `Data` is already super overloaded and product ADTs, which are
more common, are better done with `struct`.
  • Loading branch information
dougalm committed Dec 13, 2023
1 parent 1cbdee7 commit 0ae018e
Show file tree
Hide file tree
Showing 13 changed files with 28 additions and 28 deletions.
10 changes: 5 additions & 5 deletions examples/raytrace.dx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def cross(a:Vec 3, b:Vec 3) -> Vec 3 =
[a2 * b3 - a3 * b2, a3 * b1 - a1 * b3, a1 * b2 - a2 * b1]

# TODO: Use `data Color = Red | Green | Blue` and ADTs for index sets
data Image =
enum Image =
MkImage(height:Nat, width:Nat, Fin height => Fin width => Color)

xHat : Vec 3 = [1., 0., 0.]
Expand Down Expand Up @@ -92,20 +92,20 @@ BlockHalfWidths = Vec 3
Radius = Float
Radiance = Color

data ObjectGeom =
enum ObjectGeom =
Wall(Direction, Distance)
Block(Position, BlockHalfWidths, Angle)
Sphere(Position, Radius)

data Surface =
enum Surface =
Matte(Color)
Mirror

struct OrientedSurface =
normal : Direction
surface : Surface

data Object =
enum Object =
PassiveObject(ObjectGeom, Surface)
# position, half-width, intensity (assumed to point down)
Light(Position, Float, Radiance)
Expand Down Expand Up @@ -169,7 +169,7 @@ def sdScene(scene:Scene n, pos:Position) -> (Object, Distance) given (n|Ix) =
def calcNormal(obj:Object, pos:Position) -> Direction =
grad(\p:Position. sdObject(p, obj)) pos | normalize

data RayMarchResult =
enum RayMarchResult =
# incident ray, surface normal, surface properties
HitObj(Ray, OrientedSurface)
HitLight(Radiance)
Expand Down
2 changes: 1 addition & 1 deletion lib/diagram.dx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ struct Point =
x : Float
y : Float

data Geom =
enum Geom =
PointGeom
Circle(Float)
Rectangle(Float, Float) # width, height
Expand Down
2 changes: 1 addition & 1 deletion lib/fft.dx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def odd_sized_palindrome(mid:a, seq:n=>a) -> ((n `Either` () `Either` n)=>a) giv

'## Inner FFT functions

data FTDirection =
enum FTDirection =
ForwardFT
InverseFT

Expand Down
8 changes: 4 additions & 4 deletions lib/linalg.dx
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ def lower_tri_identity() -> LowerTriMat n a given(n|Ix, a|Add|Mul) =
'## Representing inequalities between indices

# These would be unnecessary if there were syntax for dependent pairs.
data LowerTriIx(n) = MkLowerTriIx( i:n, j:(..i))
data UpperTriIx(n) = MkUpperTriIx( i:n, j:(i..))
data LowerTriIxExc(n) = MkLowerTriIxExc(i:n, j:(..<i))
data UpperTriIxExc(n) = MkUpperTriIxExc(i:n, j:(i<..))
enum LowerTriIx(n) = MkLowerTriIx( i:n, j:(..i))
enum UpperTriIx(n) = MkUpperTriIx( i:n, j:(i..))
enum LowerTriIxExc(n) = MkLowerTriIxExc(i:n, j:(..<i))
enum UpperTriIxExc(n) = MkUpperTriIxExc(i:n, j:(i<..))

'## Flipping inequalities between indices

Expand Down
2 changes: 1 addition & 1 deletion lib/netpbm.dx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import parser

data Image =
enum Image =
MkImage(rows:Nat, cols:Nat, pixels:(Fin rows => Fin cols => Fin 3 => Word8))

parse_p6 : Parser Image = MkParser \h.
Expand Down
2 changes: 1 addition & 1 deletion lib/parser.dx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ struct ParserHandle(h:Heap) =
input: String
offset: Ref h Nat

data Parser(a:Type) =
enum Parser(a:Type) =
MkParser((given(h:Heap), ParserHandle h )-> {Except, State h} a)

def parse(handle:ParserHandle h, parser:Parser a) -> {Except, State h} a given (a, h) =
Expand Down
2 changes: 1 addition & 1 deletion lib/plot.dx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import diagram
import png

data CompactSet(a:Type) =
enum CompactSet(a:Type) =
Interval(a, a)
Singleton(a)

Expand Down
18 changes: 9 additions & 9 deletions lib/prelude.dx
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ instance VSpace(())

'## Boolean type

data Bool =
enum Bool =
False
True

Expand Down Expand Up @@ -530,7 +530,7 @@ def b_to_f(x:Bool) -> Float = i_to_f(b_to_i x)
'## Ordering
TODO: move this down to with `Ord`?

data Ordering =
enum Ordering =
LT
EQ
GT
Expand All @@ -542,7 +542,7 @@ A [sum type, or tagged union](https://en.wikipedia.org/wiki/Tagged_union) can ho
For those familiar with the C language, they can be though of as a combination of an `enum` with a `union`.
Here we define several basic kinds, and some operators on them.

data Maybe(a:Type) =
enum Maybe(a:Type) =
Nothing
Just(a)

Expand All @@ -558,7 +558,7 @@ def maybe(d:b, f:(a)->b, x:Maybe a) -> b given (a:Type, b:Type) =
Nothing -> d
Just(x') -> f x'

data Either(a:Type, b:Type) =
enum Either(a:Type, b:Type) =
Left(a)
Right(b)

Expand Down Expand Up @@ -630,7 +630,7 @@ def (:=)(ref:Ref h s, x:s) -> {State h} () given (h:Heap, s|Data) = %put(ref, x)

def ask(ref:Ref h r) -> {Read h} r given (h:Heap, r|Data) = %ask(ref)

data AccumMonoidData(h:Heap, w:Type) = UnsafeMkAccumMonoidData(b:Type, Monoid b)
enum AccumMonoidData(h:Heap, w:Type) = UnsafeMkAccumMonoidData(b:Type, Monoid b)

interface AccumMonoid(h:Heap, w:Type)
getAccumMonoidData : AccumMonoidData(h, w)
Expand Down Expand Up @@ -1329,7 +1329,7 @@ def deriv_rev(f:(Float)->Float, x:Float) -> Float = (snd vjp(f, x))(1.0)

# XXX: Watch out when editing this data type! We depend on its structure
# deep inside the compiler (mostly in linearization and during rule registration).
data SymbolicTangent(a:Type) =
enum SymbolicTangent(a:Type) =
ZeroTangent
SomeTangent(a)

Expand Down Expand Up @@ -1398,7 +1398,7 @@ def check_deriv(f:(Float)->Float, x:Float) -> Bool =

'## Length-erased lists

data List(a:Type) =
enum List(a:Type) =
AsList(n:Nat, elements:(Fin n => a))

instance Eq(List a) given (a|Eq)
Expand Down Expand Up @@ -1588,7 +1588,7 @@ def from_nullable_raw_ptr(ptr:RawPtr) -> Maybe (Ptr a) given (a:Type) =

def c_string_ptr(s:CString) -> Maybe (Ptr Char) = from_nullable_raw_ptr s.ptr

data StreamMode =
enum StreamMode =
ReadMode
WriteMode

Expand Down Expand Up @@ -1629,7 +1629,7 @@ def while(body: () -> {|eff} Bool) -> {|eff} () given (eff:Effects) =
body' : () -> {|eff} Word8 = \. b_to_w8 $ body()
%while(body')

data IterResult(a|Data) =
enum IterResult(a|Data) =
Continue
Done(a)

Expand Down
2 changes: 1 addition & 1 deletion lib/set.dx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def remove_duplicates_from_sorted(xs:n=>a) -> List a given (n|Ix, a|Eq) =

'## Sets

data Set(a|Ord) =
enum Set(a|Ord) =
# Guaranteed to be in sorted order with unique elements,
# as long as no one else uses this constructor.
# Instead use the "toSet" function below.
Expand Down
2 changes: 1 addition & 1 deletion misc/dex.el
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
("^:\\w*" . font-lock-preprocessor-face)
(,(concat
"\\bdef\\b\\|\\bfor\\b\\|\\brof\\b\\|\\bcase\\b\\|"
"\\bstruct\\b\\|\\bdata\\b\\|\\bwhere\\b\\|\\bof\\b\\|"
"\\bstruct\\b\\|\\benum\\b\\|\\bwhere\\b\\|\\bof\\b\\|"
"\\bif\\b\\|\\bthen\\b\\|\\belse\\b\\|\\binterface\\b\\|"
"\\binstance\\b\\|\\bgiven\\b\\|\\bdo\\b\\|\\bview\\b\\|"
"\\bwith\\b\\|\\bself\\b\\|"
Expand Down
2 changes: 1 addition & 1 deletion src/lib/Lexing.hs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ keyWordToken = \case
ThenKW -> "then"
ElseKW -> "else"
OfKW -> "of"
DataKW -> "data"
DataKW -> "enum"
StructKW -> "struct"
InterfaceKW -> "interface"
InstanceKW -> "instance"
Expand Down
2 changes: 1 addition & 1 deletion src/lib/Types/Core.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2079,7 +2079,7 @@ instance Pretty (TyConParams n) where
pretty (TyConParams _ _) = undefined

instance Pretty (TyConDef n) where
pretty (TyConDef name _ bs cons) = "data" <+> pretty name <+> pretty bs <> pretty cons
pretty (TyConDef name _ bs cons) = "enum" <+> pretty name <+> pretty bs <> pretty cons

instance Pretty (DataConDefs n) where
pretty = undefined
Expand Down
2 changes: 1 addition & 1 deletion src/lib/Types/Source.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1089,7 +1089,7 @@ instance Pretty (UAlt n) where
instance Pretty (UTopDecl n l) where
pretty = \case
UDataDefDecl (UDataDef nm bs dataCons) bTyCon bDataCons ->
"data" <+> p bTyCon <+> p nm <+> spaced (unsafeFromNest bs) <+> "where" <> nest 2
"enum" <+> p bTyCon <+> p nm <+> spaced (unsafeFromNest bs) <+> "where" <> nest 2
(prettyLines (zip (toList $ unsafeFromNest bDataCons) dataCons))
UStructDecl bTyCon (UStructDef nm bs fields defs) ->
"struct" <+> p bTyCon <+> p nm <+> spaced (unsafeFromNest bs) <+> "where" <> nest 2
Expand Down

0 comments on commit 0ae018e

Please sign in to comment.