Skip to content

Commit

Permalink
*: add special type check rule for (set s v), add demo
Browse files Browse the repository at this point in the history
now there some demo code under the example/ dir
  • Loading branch information
tiancaiamao committed Oct 28, 2024
1 parent b8b7f7f commit ef82741
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 117 deletions.
File renamed without changes.
43 changes: 43 additions & 0 deletions example/type.cora
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
(import "cora/lib/infer")
(typecheck true)

(declare 'member (let a (tvar) b (tvar) `(,a -> ((list ,b) -> bool))))
(func member
x [] => false
x [y . z] => (or (= x y)
(member x z)))

(declare 'assq (let a (tvar) b (tvar) `(,a -> ((list (tuple ,a ,b)) -> (maybe (tuple ,a ,b))))))
(func assq
var [] => ()
var [(cons x y) . _] => (cons x y) where (= var x)
var [_ . y] => (assq var y))

(declare 'foldl (let a (tvar) b (tvar) c (tvar) `((,a -> (,b -> ,c)) -> (,a -> ((list ,b) -> ,c)))))
(func foldl
f acc [] => acc
f acc [x . y] => (foldl f (f acc x) y))

(declare 'for-each (let a (tvar) b (tvar) c (tvar) `((,a -> ,b) -> ((list ,a) -> ,c))))
(func for-each
fn [] => []
fn [x . y] => (begin
(fn x)
(for-each fn y)))

(deftype 'ignore-check (lambda (exp typ env subst) ['succ 'ignore]))
(declare 'type-check-for-fruit `(ignore-check))
(func type-check-for-fruit
['quote x] ['fruit] env s => ['succ s] where (elem? x ['apple 'orange 'banana])
_ _ env s => ['fail])


(deftype 'fruit type-check-for-fruit)
(declare 'f `((fruit) -> bool))
(defun f (x)
(= x 'xxx))

;; (f 'asdfdsf) uncomment it and type check error!
(f 'apple)


2 changes: 1 addition & 1 deletion init.c
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ coraCall(co, 3, globalRef(intern("cora/init.add-to-*macros*")), intern("backquot

void _35clofun1220(struct Cora* co) {
Obj _35val1006 = co->args[1];
Obj _35reg1009 = primSet(intern("typecheck"), makeNative(0, _35clofun1221, 2, 0));
Obj _35reg1009 = primSet(intern("typechecker"), makeNative(0, _35clofun1221, 2, 0));
coraReturn(co, _35reg1009);
return;
}
Expand Down
2 changes: 1 addition & 1 deletion init.cora
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,6 @@
(.rewrite-backquote (cadr exp)))

;; the pseudo type checker, overloaded later.
(defun typecheck (filename pkg)
(defun typechecker (filename pkg)
['succ 'fake])

Loading

0 comments on commit ef82741

Please sign in to comment.