add operators

This commit is contained in:
2024-09-14 09:54:20 -07:00
parent 33015dd060
commit 4e8f15c3fb
13 changed files with 260 additions and 81 deletions

24
newt/equality.newt Normal file
View File

@@ -0,0 +1,24 @@
module Equality
data Eq : {A : U} -> A -> A -> U where
Refl : {A : U} {a : A} -> Eq a a
-- Some magic is not happening here.
sym : {A : U} {x y : A} -> Eq x y -> Eq y x
sym Refl = Refl
trans : {A : U} {x y z : A} -> Eq x y -> Eq y z -> Eq x z
trans Refl Refl = Refl
coerce : {A B : U} -> Eq A B -> A -> B
coerce Refl a = a
J : {A : U} ->
{C : (x y : A) -> Eq x y -> U} ->
(c : (x : _) -> C x x Refl) ->
(x y : A) ->
(p : Eq x y) ->
C x y p
-- this was failing until I constrained scrutinee to the constructor + args
J c x y Refl = c x

41
newt/oper.newt Normal file
View File

@@ -0,0 +1,41 @@
module Oper
-- These are hard-coded at the moment
-- For now they must be of the form _op_, we'll circle back
-- with a different parser, but that works today.
-- this will be parsed as a top level decl, collected in TopContext, and
-- injected into the Parser. It'll need to be passed around or available
-- for read in the monad.
-- long term, I might want TopContext in the parser, and parse a top-level
-- declaration at a time (for incremental updates), but much longer term.
infixl 4 _+_
infixl 4 _-_
infixl 5 _*_
infixl 5 _/_
ptype Int
ptype String
ptype JVoid
-- If we had a different quote here, we could tell vscode it's javascript.
-- or actually just switch modes inside pfunc
pfunc log : String -> JVoid := "(x) => console.log(x)"
pfunc plus : Int -> Int -> Int := "(x,y) => x + y"
pfunc _*_ : Int -> Int -> Int := "(x,y) => x * y"
-- We now have to clean JS identifiers
_+_ : Int -> Int -> Int
_+_ x y = plus x y
test : Int -> Int
test x = 42 + x * 3 + 2
infixr 2 _,_
data Pair : U -> U -> U where
_,_ : {A B : U} -> A -> B -> Pair A B
blah : Int -> Int -> Int -> Pair Int (Pair Int Int)
blah x y z = (x , y, z)

15
newt/tutorial.newt Normal file
View File

@@ -0,0 +1,15 @@
-- Files begin with a module declaration, modules not implemented yet
module Tutorial
-- import Prelude not implemented yet
-- declare a primitive type
ptype Int
-- declare a more complex primitive type
ptype Array : U -> U
-- declare a primitive function
pfunc alength : {a : U} -> Array a -> Int := "(x) => x.length"

View File

@@ -2,6 +2,8 @@ module TypeClass
-- experiment on one option for typeclass (we don't have record yet)
-- this would be nicer with records and copatterns
-- we need a bit more than this, but
data Monad : (U -> U) -> U where
MkMonad : { M : U -> U } ->