syndicate-2017/racket/typed/roles.rkt

704 lines
29 KiB
Racket
Raw Normal View History

2018-07-25 21:26:47 +00:00
#lang turnstile
(provide #%module-begin
2018-07-25 21:26:47 +00:00
(rename-out [typed-app #%app])
(rename-out [typed-quote quote])
2018-07-25 21:26:47 +00:00
#%top-interaction
require only-in
;; Start dataspace programs
run-ground-dataspace
2018-07-25 21:26:47 +00:00
;; Types
2019-04-26 19:16:08 +00:00
Int Bool String Tuple Bind Discard ByteString Symbol
Role Reacts Shares Know ¬Know Message OnDataflow Stop OnStart OnStop
FacetName Field ★/t
Observe Inbound Outbound Actor U
Computation Value Endpoints Roles Spawns
2018-07-25 21:26:47 +00:00
;; Statements
2018-08-14 21:02:39 +00:00
let let* if spawn dataspace start-facet set! begin stop begin/dataflow #;unsafe-do
2018-09-12 21:03:19 +00:00
when unless send!
2018-08-01 15:30:25 +00:00
;; Derived Forms
2018-08-14 20:35:39 +00:00
during define/query-value define/query-set
2018-07-25 21:26:47 +00:00
;; endpoints
2018-08-08 19:20:09 +00:00
assert on field
2018-07-25 21:26:47 +00:00
;; expressions
2018-08-14 22:23:35 +00:00
tuple select lambda ref observe inbound outbound
2018-08-01 14:35:22 +00:00
;; making types
define-type-alias
define-constructor define-constructor*
2018-07-25 21:26:47 +00:00
;; values
#%datum
;; patterns
bind discard
;; primitives
+ - * / and or not > < >= <= = equal? displayln printf define
gensym symbol->string string->symbol bytes->string/utf-8 string->bytes/utf-8
~a
;; lists
2019-04-26 19:16:08 +00:00
(all-from-out "list.rkt")
;; sets
2019-04-26 19:16:08 +00:00
(all-from-out "set.rkt")
2018-07-25 21:26:47 +00:00
;; DEBUG and utilities
print-type print-role
;; Extensions
2018-07-31 18:03:15 +00:00
match cond
;; require & provides
require provide
submod for-syntax for-meta only-in except-in
2019-01-03 19:01:09 +00:00
require/typed
2019-01-03 17:06:14 +00:00
require-struct
2018-07-25 21:26:47 +00:00
)
2019-04-26 19:16:08 +00:00
(require "core-types.rkt")
(require "list.rkt")
(require "set.rkt")
2018-07-25 21:26:47 +00:00
(require (prefix-in syndicate: syndicate/actor-lang))
(require (for-meta 2 macrotypes/stx-utils racket/list syntax/stx syntax/parse racket/base))
(require (for-syntax turnstile/examples/util/filter-maximal))
2019-01-03 17:06:14 +00:00
(require (for-syntax racket/struct-info))
2018-07-25 21:26:47 +00:00
(require macrotypes/postfix-in)
(require (rename-in racket/math [exact-truncate exact-truncate-]))
(require (postfix-in - racket/list))
(require (postfix-in - racket/set))
2018-07-31 19:51:20 +00:00
(require (postfix-in - racket/match))
(require (postfix-in - (only-in racket/format ~a)))
2018-07-25 21:26:47 +00:00
2019-01-03 17:06:14 +00:00
2018-07-25 21:26:47 +00:00
(module+ test
(require rackunit)
(require rackunit/turnstile))
2018-07-25 21:26:47 +00:00
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Core forms
2018-08-08 19:20:09 +00:00
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2018-07-25 21:26:47 +00:00
2018-08-08 19:20:09 +00:00
(define-typed-syntax (start-facet name:id ep ...+)
#:with name- (syntax-local-identifier-as-binding (syntax-local-introduce (generate-temporary #'name)))
#:with name+ (syntax-local-identifier-as-binding #'name)
#:with facet-name-ty (type-eval #'FacetName)
#:do [(define ctx (syntax-local-make-definition-context))
(define unique (gensym 'start-facet))
(define name-- (internal-definition-context-introduce ctx #'name- 'add))
2018-08-10 01:42:20 +00:00
(int-def-ctx-bind-type-rename #'name+ #'name- #'facet-name-ty ctx)
2018-08-10 02:06:08 +00:00
(define-values (ep-... τ... ep-effects facet-effects spawn-effects)
(walk/bind #'(ep ...) ctx unique))
(unless (and (stx-null? facet-effects) (stx-null? spawn-effects))
(type-error #:src #'(ep ...) #:msg "only endpoint effects allowed"))]
2018-08-08 19:20:09 +00:00
#:with ((~or (~and τ-a (~Shares _))
2018-09-12 21:03:19 +00:00
;; untyped syndicate might allow this - TODO
#;(~and τ-m (~Sends _))
2018-08-10 02:06:08 +00:00
(~and τ-r (~Reacts _ ...))
~MakesField)
2018-08-08 19:20:09 +00:00
...)
2018-08-10 02:06:08 +00:00
ep-effects
2018-08-08 19:20:09 +00:00
#:with τ (type-eval #`(Role (#,name--)
τ-a ...
2018-09-12 21:03:19 +00:00
;; τ-m ...
2018-08-08 19:20:09 +00:00
τ-r ...))
2018-07-25 21:26:47 +00:00
--------------------------------------------------------------
2018-08-08 19:20:09 +00:00
[ (syndicate:react (let- ([#,name-- (syndicate:current-facet-id)])
2018-08-10 02:06:08 +00:00
#,@ep-...))
2018-07-25 21:26:47 +00:00
( : ★/t)
( ν-f (τ))])
2018-07-25 21:26:47 +00:00
2018-08-08 19:20:09 +00:00
(define-typed-syntax (field [x:id τ-f:type e:expr] ...)
#:fail-unless (stx-andmap flat-type? #'(τ-f ...)) "keep your uppity data outta my fields"
[ e e- ( : τ-f)] ...
#:fail-unless (stx-andmap pure? #'(e- ...)) "field initializers not allowed to have effects"
#:with (x- ...) (generate-temporaries #'(x ...))
#:with (τ ...) (stx-map type-eval #'((Field τ-f.norm) ...))
2018-08-10 01:02:24 +00:00
#:with MF (type-eval #'MakesField)
2018-08-08 19:20:09 +00:00
----------------------------------------------------------------------
2018-08-10 01:02:24 +00:00
[ (field/intermediate [x x- τ e-] ...)
2018-08-08 19:20:09 +00:00
( : ★/t)
( ν-ep (MF))])
2018-07-25 21:26:47 +00:00
(define-typed-syntax (assert e:expr)
2018-07-27 15:37:22 +00:00
[ e e- ( : τ)]
#:fail-unless (pure? #'e-) "expression not allowed to have effects"
2018-08-08 19:20:09 +00:00
#:with τs (type-eval #'(Shares τ))
2018-07-25 21:26:47 +00:00
-------------------------------------
[ (syndicate:assert e-) ( : ★/t)
( ν-ep (τs))])
2018-07-25 21:26:47 +00:00
2018-09-12 21:03:19 +00:00
(define-typed-syntax (send! e:expr)
[ e e- ( : τ)]
#:fail-unless (pure? #'e-) "expression not allowed to have effects"
#:with τm (type-eval #'(Sends τ))
--------------------------------------
[ (syndicate:send! e-) ( : ★/t)
( ν-f (τm))])
2018-09-12 21:03:19 +00:00
2018-07-31 18:03:15 +00:00
(define-typed-syntax (stop facet-name:id cont ...)
2018-07-30 18:01:56 +00:00
[ facet-name facet-name- ( : FacetName)]
[ (begin #f cont ...) cont- ( ν-ep (~effs)) ( ν-s (~effs)) ( ν-f (~effs τ-f ...))]
2018-07-31 18:03:15 +00:00
#:with τ (mk-Stop- #`(facet-name- τ-f ...))
2018-07-30 18:01:56 +00:00
---------------------------------------------------------------------------------
[ (syndicate:stop-facet facet-name- cont-) ( : ★/t)
( ν-f (τ))])
2018-07-30 18:01:56 +00:00
2018-07-25 21:26:47 +00:00
(begin-for-syntax
2018-09-12 21:03:19 +00:00
(define-syntax-class asserted/retracted/message
#:datum-literals (asserted retracted message)
2018-07-25 21:26:47 +00:00
(pattern (~or (~and asserted
(~bind [syndicate-kw #'syndicate:asserted]
[react-con #'Know]))
(~and retracted
(~bind [syndicate-kw #'syndicate:retracted]
2018-09-12 21:03:19 +00:00
[react-con #'¬Know]))
(~and message
(~bind [syndicate-kw #'syndicate:message]
[react-con #'Message]))))))
2018-07-25 21:26:47 +00:00
(define-typed-syntax on
2018-07-31 19:51:20 +00:00
[(on (~literal start) s ...)
[ (begin s ...) s- ( ν-ep (~effs))
( ν-f (~effs τ-f ...))
( ν-s (~effs τ-s ...))]
2018-08-08 19:20:09 +00:00
#:with τ-r (type-eval #'(Reacts OnStart τ-f ... τ-s ...))
2018-07-25 21:26:47 +00:00
-----------------------------------
2018-07-31 19:51:20 +00:00
[ (syndicate:on-start s-) ( : ★/t)
( ν-ep (τ-r))]]
2018-07-31 19:51:20 +00:00
[(on (~literal stop) s ...)
[ (begin s ...) s- ( ν-ep (~effs))
( ν-f (~effs τ-f ...))
( ν-s (~effs τ-s ...))]
2018-08-08 19:20:09 +00:00
#:with τ-r (type-eval #'(Reacts OnStop τ-f ... τ-s ...))
2018-07-25 21:26:47 +00:00
-----------------------------------
2018-07-31 19:51:20 +00:00
[ (syndicate:on-stop s-) ( : ★/t)
( ν-ep (τ-r))]]
2018-09-12 21:03:19 +00:00
[(on (a/r/m:asserted/retracted/message p) s ...)
2018-07-27 15:37:22 +00:00
[ p p-- ( : τp)]
#:fail-unless (pure? #'p--) "pattern not allowed to have effects"
2018-07-25 21:26:47 +00:00
#:with ([x:id τ:type] ...) (pat-bindings #'p)
2018-07-31 19:51:20 +00:00
[[x x- : τ] ... (begin s ...) s-
( ν-ep (~effs))
( ν-f (~effs τ-f ...))
( ν-s (~effs τ-s ...))]
2018-07-31 18:46:24 +00:00
#:with p- (substs #'(x- ...) #'(x ...) (compile-syndicate-pattern #'p))
2018-09-12 21:03:19 +00:00
#:with τ-r (type-eval #'(Reacts (a/r/m.react-con τp) τ-f ... τ-s ...))
2018-07-25 21:26:47 +00:00
-----------------------------------
2018-09-12 21:03:19 +00:00
[ (syndicate:on (a/r/m.syndicate-kw p-)
2018-07-31 18:46:24 +00:00
s-)
2018-07-25 21:26:47 +00:00
( : ★/t)
( ν-ep (τ-r))]])
2018-07-25 21:26:47 +00:00
2018-08-14 21:02:39 +00:00
(define-typed-syntax (begin/dataflow s ...+)
[ (begin s ...) s-
( : _)
( ν-ep (~effs))
( ν-f (~effs τ-f ...))
( ν-s (~effs τ-s ...))]
2018-08-14 21:02:39 +00:00
#:with τ-r (type-eval #'(Reacts OnDataflow τ-f ... τ-s ...))
--------------------------------------------------
[ (syndicate:begin/dataflow s-)
( : ★/t)
( ν-ep (τ-r))])
2018-08-14 21:02:39 +00:00
2018-07-25 21:26:47 +00:00
;; pat -> ([Id Type] ...)
(define-for-syntax (pat-bindings stx)
(syntax-parse stx
#:datum-literals (bind tuple)
[(bind x:id τ:type)
#'([x τ])]
[(tuple p ...)
#:with (([x:id τ:type] ...) ...) (stx-map pat-bindings #'(p ...))
#'([x τ] ... ...)]
[(k:kons1 p)
(pat-bindings #'p)]
[(~constructor-exp cons p ...)
#:with (([x:id τ:type] ...) ...) (stx-map pat-bindings #'(p ...))
#'([x τ] ... ...)]
[_
#'()]))
2018-08-01 15:30:25 +00:00
;; TODO - figure out why this needs different list identifiers
(define-for-syntax (compile-pattern pat list-binding bind-id-transformer exp-transformer)
2018-07-31 18:46:24 +00:00
(define (l-e stx) (local-expand stx 'expression '()))
(let loop ([pat pat])
(syntax-parse pat
#:datum-literals (tuple discard bind)
[(tuple p ...)
2018-08-01 15:30:25 +00:00
#`(#,list-binding 'tuple #,@(stx-map loop #'(p ...)))]
2018-07-31 18:46:24 +00:00
[(k:kons1 p)
#`(#,(kons1->constructor #'k) #,(loop #'p))]
[(bind x:id τ:type)
(bind-id-transformer #'x)]
[discard
#'_]
[(~constructor-exp ctor p ...)
(define/with-syntax uctor (untyped-ctor #'ctor))
#`(uctor #,@(stx-map loop #'(p ...)))]
[_
;; local expanding "expression-y" syntax allows variable references to transform
;; according to the mappings set up by turnstile.
(exp-transformer (l-e pat))])))
2018-07-25 21:26:47 +00:00
(define-for-syntax (compile-syndicate-pattern pat)
(compile-pattern pat
2018-08-01 15:30:25 +00:00
#'list-
2018-07-25 21:26:47 +00:00
(lambda (id) #`($ #,id))
identity))
2018-07-30 21:36:42 +00:00
(define-for-syntax (compile-match-pattern pat)
(compile-pattern pat
2018-08-01 15:30:25 +00:00
#'list
2018-07-30 21:36:42 +00:00
identity
2018-07-31 19:51:20 +00:00
(lambda (exp) #`(==- #,exp))))
2018-07-30 21:36:42 +00:00
2018-07-25 21:26:47 +00:00
(define-typed-syntax (spawn τ-c:type s)
#:fail-unless (flat-type? #'τ-c.norm) "Communication type must be first-order"
2018-07-30 18:01:56 +00:00
;; TODO: check that each τ-f is a Role
[ s s- ( ν-ep (~effs)) ( ν-s (~effs)) ( ν-f (~effs τ-f ...))]
2018-07-25 21:26:47 +00:00
;; TODO: s shouldn't refer to facets or fields!
2018-07-31 19:51:20 +00:00
#:with (τ-i τ-o τ-a) (analyze-roles #'(τ-f ...))
#:fail-unless (<: #'τ-o #'τ-c.norm)
(format "Output ~a not valid in dataspace ~a" (type->str #'τ-o) (type->str #'τ-c.norm))
2018-07-31 19:51:20 +00:00
#:fail-unless (<: #'τ-a
(type-eval #'(Actor τ-c.norm)))
"Spawned actors not valid in dataspace"
#:fail-unless (project-safe? ( (strip-? #'τ-o) #'τ-c.norm)
#'τ-i)
"Not prepared to handle all inputs"
2018-07-31 19:51:20 +00:00
#:with τ-final (type-eval #'(Actor τ-c.norm))
2018-07-25 21:26:47 +00:00
--------------------------------------------------------------------------------------------
2018-07-27 15:37:22 +00:00
[ (syndicate:spawn (syndicate:on-start s-)) ( : ★/t)
( ν-s (τ-final))])
2018-07-25 21:26:47 +00:00
2018-07-30 15:54:05 +00:00
(define-typed-syntax (dataspace τ-c:type s ...)
#:fail-unless (flat-type? #'τ-c.norm) "Communication type must be first-order"
[ s s- ( ν-ep (~effs)) ( ν-s (~effs τ-s ...)) ( ν-f (~effs))] ...
2018-07-30 15:54:05 +00:00
#:with τ-actor (type-eval #'(Actor τ-c.norm))
#:fail-unless (stx-andmap (lambda (t) (<: t #'τ-actor)) #'(τ-s ... ...))
"Not all actors conform to communication type"
#:with τ-ds-i (strip-inbound #'τ-c.norm)
#:with τ-ds-o (strip-outbound #'τ-c.norm)
#:with τ-relay (relay-interests #'τ-c.norm)
-----------------------------------------------------------------------------------
[ (syndicate:dataspace s- ...) ( : ★/t)
( ν-s ((Actor (U τ-ds-i τ-ds-o τ-relay))))])
2018-07-30 15:54:05 +00:00
2018-07-25 21:26:47 +00:00
(define-typed-syntax (set! x:id e:expr)
2018-07-27 15:37:22 +00:00
[ e e- ( : τ)]
#:fail-unless (pure? #'e-) "expression not allowed to have effects"
2018-07-25 21:26:47 +00:00
[ x x- ( : (~Field τ-x:type))]
#:fail-unless (<: #'τ #'τ-x) "Ill-typed field write"
----------------------------------------------------
[ (x- e-) ( : ★/t)])
2018-07-25 21:26:47 +00:00
2018-08-01 15:30:25 +00:00
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Derived Forms
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define-typed-syntax (during p s ...)
#:with inst-p (instantiate-pattern #'p)
----------------------------------------
[ (on (asserted p)
(start-facet during-inner
(on (retracted inst-p)
(stop during-inner))
s ...))])
;; TODO - reconcile this with `compile-pattern`
(define-for-syntax (instantiate-pattern pat)
(let loop ([pat pat])
(syntax-parse pat
#:datum-literals (tuple discard bind)
[(tuple p ...)
#`(tuple #,@(stx-map loop #'(p ...)))]
[(k:kons1 p)
#`(k #,(loop #'p))]
[(bind x:id τ)
#'x]
2018-08-13 23:32:23 +00:00
;; not sure about this
2018-08-01 15:30:25 +00:00
[discard
2018-08-13 23:32:23 +00:00
#'discard]
2018-08-01 15:30:25 +00:00
[(~constructor-exp ctor p ...)
(define/with-syntax uctor (untyped-ctor #'ctor))
#`(ctor #,@(stx-map loop #'(p ...)))]
[_
pat])))
2018-08-14 19:43:51 +00:00
(define-typed-syntax (define/query-value x:id e0 p e)
[ e0 e0- ( : τ)]
#:fail-unless (pure? #'e0-) "expression must be pure"
----------------------------------------
[ (begin (field [x τ e0-])
(on (asserted p)
(set! x e))
(on (retracted p)
(set! x e0-)))])
2018-08-14 22:23:35 +00:00
;; TODO: #:on-add
2018-08-14 20:35:39 +00:00
(define-typed-syntax (define/query-set x:id p e)
#:with ([y τ] ...) (pat-bindings #'p)
;; e will be re-expanded :/
[[y y- : τ] ... e e- τ-e]
----------------------------------------
[ (begin (field [x (Set τ-e) (set)])
(on (asserted p)
(set! x (set-add (ref x) e)))
(on (retracted p)
(set! x (set-remove (ref x) e))))])
2018-07-25 21:26:47 +00:00
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Expressions
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define-typed-syntax (ref x:id)
[ x x- (~Field τ)]
------------------------
[ (x-) ( : τ)])
2018-07-25 21:26:47 +00:00
(define-typed-syntax (typed-app e_fn e_arg ...)
2018-07-30 21:00:42 +00:00
[ e_fn e_fn- ( : (~→ τ_in ... (~Computation (~Value τ-out)
2018-08-08 19:20:09 +00:00
(~Endpoints τ-ep ...)
2018-07-30 21:00:42 +00:00
(~Roles τ-f ...)
(~Spawns τ-s ...))))]
2018-07-27 15:37:22 +00:00
#:fail-unless (pure? #'e_fn-) "expression not allowed to have effects"
2018-07-25 21:26:47 +00:00
#:fail-unless (stx-length=? #'[τ_in ...] #'[e_arg ...])
(num-args-fail-msg #'e_fn #'[τ_in ...] #'[e_arg ...])
2018-07-27 15:37:22 +00:00
[ e_arg e_arg- ( : τ_in)] ...
#:fail-unless (stx-andmap pure? #'(e_arg- ...)) "expressions not allowed to have effects"
2018-07-25 21:26:47 +00:00
------------------------------------------------------------------------
2018-07-30 21:00:42 +00:00
[ (#%app- e_fn- e_arg- ...) ( : τ-out)
( ν-ep (τ-ep ...))
( ν-s (τ-s ...))
( ν-f (τ-f ...))])
2018-07-25 21:26:47 +00:00
(define-typed-syntax (tuple e:expr ...)
2018-07-27 15:37:22 +00:00
[ e e- ( : τ)] ...
#:fail-unless (stx-andmap pure? #'(e- ...)) "expressions not allowed to have effects"
2018-07-25 21:26:47 +00:00
-----------------------
[ (list- 'tuple e- ...) ( : (Tuple τ ...))])
2018-07-25 21:26:47 +00:00
(define-typed-syntax (select n:nat e:expr)
2018-07-27 15:37:22 +00:00
[ e e- ( : (~Tuple τ ...))]
#:fail-unless (pure? #'e-) "expression not allowed to have effects"
2018-07-25 21:26:47 +00:00
#:do [(define i (syntax->datum #'n))]
#:fail-unless (< i (stx-length #'(τ ...))) "index out of range"
#:with τr (list-ref (stx->list #'(τ ...)) i)
--------------------------------------------------------------
[ (tuple-select n e-) ( : τr)])
2018-07-25 21:26:47 +00:00
(define- (tuple-select n t)
(list-ref- t (add1 n)))
;; it would be nice to abstract over these three
(define-typed-syntax (observe e:expr)
2018-07-27 15:37:22 +00:00
[ e e- ( : τ)]
#:fail-unless (pure? #'e-) "expression not allowed to have effects"
2018-07-25 21:26:47 +00:00
---------------------------------------------------------------------------
[ (syndicate:observe e-) ( : (Observe τ))])
2018-07-25 21:26:47 +00:00
(define-typed-syntax (inbound e:expr)
2018-07-27 15:37:22 +00:00
[ e e- ( : τ)]
#:fail-unless (pure? #'e-) "expression not allowed to have effects"
2018-07-25 21:26:47 +00:00
---------------------------------------------------------------------------
[ (syndicate:inbound e-) ( : (Inbound τ))])
2018-07-25 21:26:47 +00:00
(define-typed-syntax (outbound e:expr)
2018-07-27 15:37:22 +00:00
[ e e- ( : τ)]
#:fail-unless (pure? #'e-) "expression not allowed to have effects"
2018-07-25 21:26:47 +00:00
---------------------------------------------------------------------------
[ (syndicate:outbound e-) ( : (Outbound τ))])
2018-07-25 21:26:47 +00:00
2018-09-12 21:03:19 +00:00
(define-typed-syntax (message e:expr)
[ e e- ( : τ)]
#:fail-unless (pure? #'e-) "expression must be pure"
------------------------------
[ (syndicate:message e-) ( : (Message τ))])
2018-07-25 21:26:47 +00:00
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Patterns
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define-typed-syntax (bind x:id τ:type)
----------------------------------------
[ (error- 'bind "escaped") ( : (Bind τ))])
2018-07-25 21:26:47 +00:00
(define-typed-syntax discard
[_
--------------------
[ (error- 'discard "escaped") ( : Discard)]])
2018-07-25 21:26:47 +00:00
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Core-ish forms
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; copied from stlc
(define-typed-syntax (ann e (~optional (~datum :)) τ:type)
[ e e- ( : τ.norm)]
#:fail-unless (pure? #'e-) "expression must be pure"
------------------------------------------------
[ e- ( : τ.norm) ])
2018-07-30 19:17:30 +00:00
;; copied from ext-stlc
(define-typed-syntax if
[(_ e_tst e1 e2) τ-expected
[ e_tst e_tst- _] ; Any non-false value is truthy.
#:fail-unless (pure? #'e_tst-) "expression must be pure"
[ e1 e1- ( : τ-expected)
( ν-ep (~effs eps1 ...)) ( ν-f (~effs fs1 ...)) ( ν-s (~effs ss1 ...))]
2018-07-30 19:17:30 +00:00
[ e2 e2- ( : τ-expected)
( ν-ep (~effs eps2 ...)) ( ν-f (~effs fs2 ...)) ( ν-s (~effs ss2 ...))]
2018-07-30 19:17:30 +00:00
--------
[ (if- e_tst- e1- e2-)
( ν-ep (eps1 ... eps2 ...))
( ν-f #,(make-Branch #'((fs1 ...) (fs2 ...))))
( ν-s (ss1 ... ss2 ...))]]
2018-07-30 19:17:30 +00:00
[(_ e_tst e1 e2)
[ e_tst e_tst- _] ; Any non-false value is truthy.
#:fail-unless (pure? #'e_tst-) "expression must be pure"
[ e1 e1- ( : τ1)
( ν-ep (~effs eps1 ...)) ( ν-f (~effs fs1 ...)) ( ν-s (~effs ss1 ...))]
2018-07-30 19:17:30 +00:00
[ e2 e2- ( : τ2)
( ν-ep (~effs eps2 ...)) ( ν-f (~effs fs2 ...)) ( ν-s (~effs ss2 ...))]
2018-07-30 19:17:30 +00:00
#:with τ (type-eval #'(U τ1 τ2))
--------
[ (if- e_tst- e1- e2-) ( : τ)
( ν-ep (eps1 ... eps2 ...))
( ν-f #,(make-Branch #'((fs1 ...) (fs2 ...))))
( ν-s (ss1 ... ss2 ...))]])
2018-07-30 19:17:30 +00:00
2018-08-14 22:23:35 +00:00
(define-typed-syntax (when e s ...+)
------------------------------------
[ (if e (begin s ...) #f)])
(define-typed-syntax (unless e s ...+)
------------------------------------
[ (if e #f (begin s ...))])
2018-07-30 19:17:30 +00:00
;; copied from ext-stlc
(define-typed-syntax let
[(_ ([x e] ...) e_body ...) τ_expected
[ e e- : τ_x] ...
#:fail-unless (stx-andmap pure? #'(e- ...)) "expressions must be pure"
[[x x- : τ_x] ... (begin e_body ...) e_body- ( : τ_expected)
( ν-ep (~effs eps ...))
( ν-f (~effs fs ...))
( ν-s (~effs ss ...))]
2018-07-30 19:17:30 +00:00
----------------------------------------------------------
[ (let- ([x- e-] ...) e_body-)
( ν-ep (eps ...))
( ν-f (fs ...))
( ν-s (ss ...))]]
2018-07-30 19:17:30 +00:00
[(_ ([x e] ...) e_body ...)
[ e e- : τ_x] ...
#:fail-unless (stx-andmap pure? #'(e- ...)) "expressions must be pure"
[[x x- : τ_x] ... (begin e_body ...) e_body- ( : τ_body)
( ν-ep (~effs eps ...))
( ν-f (~effs fs ...))
( ν-s (~effs ss ...))]
2018-07-30 19:17:30 +00:00
----------------------------------------------------------
[ (let- ([x- e-] ...) e_body-) ( : τ_body)
( ν-ep (eps ...))
( ν-f (fs ...))
( ν-s (ss ...))]])
2018-07-30 19:17:30 +00:00
;; copied from ext-stlc
(define-typed-syntax let*
[(_ () e_body ...)
--------
[ (begin e_body ...)]]
[(_ ([x e] [x_rst e_rst] ...) e_body ...)
--------
[ (let ([x e]) (let* ([x_rst e_rst] ...) e_body ...))]])
2018-07-30 21:36:42 +00:00
(define-typed-syntax (cond [pred:expr s ...+] ...+)
[ pred pred- ( : Bool)] ...
#:fail-unless (stx-andmap pure? #'(pred- ...)) "predicates must be pure"
[ (begin s ...) s- ( : τ-s)
( ν-ep (~effs eps ...))
( ν-f (~effs fs ...))
( ν-s (~effs ss ...))] ...
2018-07-30 21:36:42 +00:00
------------------------------------------------
[ (cond- [pred- s-] ...) ( : (U τ-s ...))
( ν-ep (eps ... ...))
( ν-f #,(make-Branch #'((fs ...) ...)))
( ν-s (ss ... ...))])
2018-07-30 21:36:42 +00:00
(define-typed-syntax (match e [p s ...+] ...+)
[ e e- ( : τ-e)]
#:fail-unless (pure? #'e-) "expression must be pure"
#:with (([x τ] ...) ...) (stx-map pat-bindings #'(p ...))
[[x x- : τ] ... (begin s ...) s- ( : τ-s)
( ν-ep (~effs eps ...))
( ν-f (~effs fs ...))
( ν-s (~effs ss ...))] ...
2018-07-30 21:36:42 +00:00
;; REALLY not sure how to handle p/p-/p.match-pattern,
;; particularly w.r.t. typed terms that appear in p.match-pattern
[ p p-- τ-p] ...
#:fail-unless (project-safe? #'τ-e (type-eval #'(U τ-p ...))) "possibly unsafe pattern match"
#:fail-unless (stx-andmap pure? #'(p-- ...)) "patterns must be pure"
2018-07-31 18:46:24 +00:00
#:with (p- ...) (stx-map (lambda (p x-s xs) (substs x-s xs (compile-match-pattern p)))
#'(p ...)
#'((x- ...) ...)
#'((x ...) ...))
2018-07-30 21:36:42 +00:00
--------------------------------------------------------------
2018-07-31 18:46:24 +00:00
[ (match- e- [p- s-] ...
2018-07-30 21:36:42 +00:00
[_ (error "incomplete pattern match")])
( : (U τ-s ...))
( ν-ep (eps ... ...))
( ν-f #,(make-Branch #'((fs ...) ...)))
( ν-s (ss ... ...))])
2018-07-30 21:36:42 +00:00
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Ground Dataspace
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; n.b. this is a blocking operation, so an actor that uses this internally
;; won't necessarily terminate.
(define-typed-syntax (run-ground-dataspace τ-c:type s ...)
[ (dataspace τ-c s ...) ((~literal erased) ((~literal syndicate:dataspace) s- ...)) ( : t)]
-----------------------------------------------------------------------------------
[ (syndicate:run-ground s- ...) ( : (AssertionSet τ-c))])
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Primitives
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; hmmm
(define-primop + ( Int Int (Computation (Value Int) (Endpoints) (Roles) (Spawns))))
(define-primop - ( Int Int (Computation (Value Int) (Endpoints) (Roles) (Spawns))))
(define-primop * ( Int Int (Computation (Value Int) (Endpoints) (Roles) (Spawns))))
#;(define-primop and ( Bool Bool Bool))
(define-primop or ( Bool Bool (Computation (Value Bool) (Endpoints) (Roles) (Spawns))))
(define-primop not ( Bool (Computation (Value Bool) (Endpoints) (Roles) (Spawns))))
(define-primop < ( Int Int (Computation (Value Bool) (Endpoints) (Roles) (Spawns))))
(define-primop > ( Int Int (Computation (Value Bool) (Endpoints) (Roles) (Spawns))))
(define-primop <= ( Int Int (Computation (Value Bool) (Endpoints) (Roles) (Spawns))))
(define-primop >= ( Int Int (Computation (Value Bool) (Endpoints) (Roles) (Spawns))))
(define-primop = ( Int Int (Computation (Value Bool) (Endpoints) (Roles) (Spawns))))
(define-primop bytes->string/utf-8 ( ByteString (Computation (Value String) (Endpoints) (Roles) (Spawns))))
(define-primop string->bytes/utf-8 ( String (Computation (Value ByteString) (Endpoints) (Roles) (Spawns))))
(define-primop gensym ( Symbol (Computation (Value Symbol) (Endpoints) (Roles) (Spawns))))
(define-primop symbol->string ( Symbol (Computation (Value String) (Endpoints) (Roles) (Spawns))))
(define-primop string->symbol ( String (Computation (Value Symbol) (Endpoints) (Roles) (Spawns))))
(define-typed-syntax (/ e1 e2)
[ e1 e1- ( : Int)]
[ e2 e2- ( : Int)]
#:fail-unless (pure? #'e1-) "expression not allowed to have effects"
#:fail-unless (pure? #'e2-) "expression not allowed to have effects"
------------------------
[ (exact-truncate- (/- e1- e2-)) ( : Int)])
;; for some reason defining `and` as a prim op doesn't work
(define-typed-syntax (and e ...)
[ e e- ( : Bool)] ...
#:fail-unless (stx-andmap pure? #'(e- ...)) "expressions not allowed to have effects"
------------------------
[ (and- e- ...) ( : Bool)])
(define-typed-syntax (equal? e1:expr e2:expr)
[ e1 e1- ( : τ1)]
#:fail-unless (flat-type? #'τ1)
(format "equality only available on flat data; got ~a" (type->str #'τ1))
[ e2 e2- ( : τ1)]
#:fail-unless (pure? #'e1-) "expression not allowed to have effects"
#:fail-unless (pure? #'e2-) "expression not allowed to have effects"
---------------------------------------------------------------------------
[ (equal?- e1- e2-) ( : Bool)])
(define-typed-syntax (displayln e:expr)
[ e e- ( : τ)]
#:fail-unless (pure? #'e-) "expression not allowed to have effects"
---------------
[ (displayln- e-) ( : ★/t)])
(define-typed-syntax (printf e ...+)
[ e e- ( : τ)] ...
#:fail-unless (stx-andmap pure? #'(e- ...)) "expression not allowed to have effects"
---------------
[ (printf- e- ...) ( : ★/t)])
(define-typed-syntax (~a e ...)
[ e e- ( : τ)] ...
#:fail-unless (stx-andmap flat-type? #'(τ ...))
"expressions must be string-able"
--------------------------------------------------
[ (~a- e- ...) ( : String)])
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Basic Values
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define-typed-syntax #%datum
[(_ . n:integer)
----------------
[ (#%datum- . n) ( : Int)]]
[(_ . b:boolean)
----------------
[ (#%datum- . b) ( : Bool)]]
[(_ . s:string)
----------------
[ (#%datum- . s) ( : String)]])
(define-typed-syntax (typed-quote x:id)
-------------------------------
[ (quote- x) ( : Symbol)])
2018-07-25 21:26:47 +00:00
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Utilities
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define-typed-syntax (print-type e)
[ e e- ( : τ) ( ν-ep (~effs eps ...)) ( ν-f (~effs fs ...)) ( ν-s (~effs ss ...))]
#:do [(pretty-display (type->str #'τ))]
2018-07-25 21:26:47 +00:00
----------------------------------
[ e- ( : τ) ( ν-ep (eps ...)) ( ν-f (fs ...)) ( ν-s (ss ...))])
2018-07-25 21:26:47 +00:00
(define-typed-syntax (print-role e)
[ e e- ( : τ) ( ν-ep (~effs eps ...)) ( ν-f (~effs fs ...)) ( ν-s (~effs ss ...))]
2018-08-08 19:20:09 +00:00
#:do [(for ([r (in-syntax #'(fs ...))])
(pretty-display (type->str r)))]
2018-07-25 21:26:47 +00:00
----------------------------------
[ e- ( : τ) ( ν-ep (eps ...)) ( ν-f (fs ...)) ( ν-s (ss ...))])
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Tests
2018-09-12 19:06:08 +00:00
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2018-09-12 21:03:19 +00:00
(module+ test
(check-type (spawn (U (Message (Tuple String Int))
(Observe (Tuple String ★/t)))
(start-facet echo
(on (message (tuple "ping" (bind x Int)))
(send! (tuple "pong" x)))))
: ★/t)
(typecheck-fail (spawn (U (Message (Tuple String Int))
(Message (Tuple String String))
(Observe (Tuple String ★/t)))
(start-facet echo
(on (message (tuple "ping" (bind x Int)))
(send! (tuple "pong" x)))))))
2018-09-12 19:06:08 +00:00
;; local definitions
#;(module+ test
;; these cause an error in rackunit-typechecking, don't know why :/
#;(check-type (let ()
(begin
(define id : Int 1234)
id))
: Int
-> 1234)
#;(check-type (let ()
(define (spawn-cell [initial-value : Int])
(define id 1234)
id)
(typed-app spawn-cell 42))
: Int
-> 1234)
(check-equal? (let ()
(define id : Int 1234)
id)
1234)
#;(check-equal? (let ()
(define (spawn-cell [initial-value : Int])
(define id 1234)
id)
(typed-app spawn-cell 42))
1234))