interoperability_第1頁(yè)
interoperability_第2頁(yè)
interoperability_第3頁(yè)
interoperability_第4頁(yè)
interoperability_第5頁(yè)
已閱讀5頁(yè),還剩42頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、Embedded InterpretersNick BentonMicrosoft ResearchCambridge UKWriting interpreters in functional languages is easyEvery introductory text includes a metacircular interpreter for lambda calculus (and some parser combinators).What more is there to say?But in practice there are two kinds of interpreter

2、Those for self-contained new languagesDomain-specific command or scripting languages added to applicationsApplication scripting languagesStart with an application written in the host language (metalanguage, and in this talk it will be SML)Application comprises many interesting higher-type values and

3、 new type definitionsPurpose of scripting language (object language) is to give the user a flexible way to glue those bits together at runtimeRequires more sophisticated interoperability between the two levels than in the self-contained caseSML tradition is to avoid the problem by not defining an ob

4、ject language at all just use interactive top-level loop instead. Not really viable for stand-alone applications, libraries, interesting object-level syntaxes, situations in which commands come from files, network, etc.Scheme is a bit more flexible (dynamic typing, eval, macros) than SML for this so

5、rt of thing. But I like SML.Starting point: A Tactical Theorem Prover AppletSample for MLj (Benton, Kennedy, Russell)HAL is a theorem prover for first order logic written in SML by PaulsonNo interface intended to be used from the interactive SML environmentWe wanted to compile it as an applet so one

6、 could do interactive theorem proving in a web browser (dont ask why)Problem 1: Applets dont get any simple scrolling text UI by default.Solution: Download 3rd party terminal emulator in Java, strip out network bits and link into SML code with MLjs interlanguage working extensions. Easy.Problem 2: H

7、ave to parse and evaluate user commandsNon-Solution: Package a complete ML environment as an applet to provide interface to an application of a few hundred linesHALs command languageSimple combinatory functional languageIntegers, strings and tactics as base values, functions and tuples as constructo

8、rsEasy to write parser and interpreter for such a languageBut HAL itself comprises about 30 ML values, some of which have higher-order typesHow to make those available within the interpreted language?Wed like to avoid special-casing them all in the interpreter itself (effectively making them new lan

9、guage constructs)Lets look at an interpreterdatatype Exp = EId of string (* identifiers *) | EI of int (* integer consts *) | ES of string (* string consts *) | EApp of Exp*Exp (* application *) | EP of Exp*Exp (* pairs *)datatype U = UF of U-U | UP of U*U | UUnit | UI of int | US of string | UT of

10、tactic Expressions:Build the interpreter using a universal datatype UNote:Object language functions interpreted using ML functionsMapping into UTo make an ML values of type A available in the object language, we need a mapFor base types this is easy, eint = UI for exampleBut to embed a function of t

11、ype AB we need to map it to one of type U U so we can wrap it with UFWe can only do that if we also have a projectionThen eAB f = UF (eB o f o pA)These projections will be partialEmbedding-Projection Pairs in MLHow do we program these type-indexed functions?We represent each type explicitly by its a

12、ssociated embedding-projection pair and define combinators for each constructortype a EPval embed : a EP - (a-U)val project : a EP - (U-a)val unit : unit EPval int : int EPval string : string EPval * : (a EP)*(b EP) - (a*b) EPval - : (a EP)*(b EP) - (a-b) EPMatching structuretype a EP = (a-U)*(U-a)f

13、un embed (e,p) = efun project (e,p) = pfun PF (UF(f)=f (* : U - (U-U) *)fun PP (UP(p)=p (* : U - (U*U) other similar elided *)val int = (UI,PI)val string = (US,PS) (* etc for other base types *)infix *fun cross (f,g) (x,y) = (f x,g y)fun (e,p)*(e,p) = (UP o cross(e,e), cross(p,p) o PP)infixr - fun a

14、rrow (f,g) h = g o h o ffun (e,p)-(e,p) = (UF o arrow (p,e), arrow (e,p) o PF)Using embeddings to define an environmentval rules = map (cross (I, (embed (int-tactic) (basic, Rule.basic), (conjL, Rule.conjL),.val comms = (goal, embed (string-unit) Command.goal), (by, embed (tactic-unit) Command.by)va

15、l tacs = (|, embed (tactic*tactic-tactic) Tacs.|), (repeat, embed (tactic-tactic) Tacs.repeat), .val builtins = rules comms tacsDefining and using the interpreterfun interpret e = case e of EI n = UI n | ES s = US s | EId s = lookup s builtins | EP (e1,e2) = UP(interpret e1,interpret e2) | EApp (e1,

16、e2) = let val UF(f) = interpret e1 val a = interpret e2 in f a endTop level loop just repeatedly reads expressions from the terminal window, parses them and calls interpret.E.g. interpret (parse “by (repeat (conjR 1)”) Were done! But lets see how far the idea goesEmbedding Polymorphic FunctionsJust

17、instantiate at U. Givenfun I x = xfun K x y = xfun S x y z = x z (y z)val any : (U EP) = (I,I)val combinators = (I, embed (any-any) I), (K, embed (any-any-any) K), (S, embed (any-any-any)-(any-any)- any-any) S)Evaluating interpret (read (S K K 2, S K K two)yields UP (UI 2, US two) : UMultilevel Prog

18、rammingWe can project as well as embedSo we can construct object-level programs and reflect them back as ML valuesFor examplelet val eSucc = interpret(read fn x=x+1,) val succ = project (int-int) eSucc in (succ 3) end;val it = 4 : intBut thats a bit boringThe traditional power function- local fun p

19、0 = %1 | p n = %y * (p (n-1) in fun pow x = project (int-int) (interpret (%fn y = (p x),) ) end;val pow = fn : int - int - int- val p5 = pow 5;val p5 = fn : int - int- p5 2;val it = 32 : int- p5 3;val it = 243 : intNote: %() is “antiquote” like parse but allows parser results to be spliced inProject

20、ing Polymorphic FunctionsRepresent type abstraction and application by MLs value abstraction and application:let val eK = embed (any-any-any) K val pK = fn a = fn b = project (a-b-a) eKin (pK int string 3 three, pK string unit four ()endUntypeable object expressions- let val embY = interpret (read f

21、n f=(fn g= f (fn a= (g g) a) (fn g= f (fn a= (g g) a),) val polyY = fn a = fn b= project (a-b)-a-b)-a-b) embY val sillyfact = polyY int int (fn f=fn n=if n=0 then 1 else n*(f (n-1) in (sillyfact 5) end;val it = 120 : intMultistage computation?fun run s = interpret (read s,run) embed (string-any) run

22、;val run = fn : string - U- run let val x= run 3+4 in x+2;val it = UI 9 : URecursive datatypesdatatype U = . | UT of int*Uval wrap : (a - b) * (b - a) - b EP - a EPval sum : a EP list - a EPval mu : (a EP - a EP) - a EPfun wrap (decon,con) ep = (embed ep) o decon, con o (project ep)fun sum ss = let

23、fun cases brs n x = UT(n, embed (hd brs) x) handle Match = cases (tl brs) (n+1) x in (fn x= cases ss 0 x, fn (UT(n,u) = project (List.nth(ss,n) u) endfun mu f = (fn x = embed (f (mu f) x, fn u = project (f (mu f) u)Usage patternGivenThe associated EP isExample: lists- fun list elem = mu ( fn l = (su

24、m wrap (fn =(),fn()=) unit, wrap (fn (x:xs)=(x,xs), fn (x,xs)=(x:xs) (elem * l);val list : a EP - a list EP(* now extend the environment *). (cons, embed (any*(list any)-(list any) (op :), (nil, embed (list any) ), (null, embed (list any)-bool) null), . Lists continued- interpret (read let fun map f

25、 l = if null l then nil else cons(f (hd l),map f (tl l) in map, ) ;val it = UF fn : U- project (int-int)-(list int)-(list int) it;val it = fn : (int - int) - int list - int list- it (fn x=x*x) 1,2,3;val it = 1,4,9 : int listThats semantically elegant, butIts also absurdly inefficientEvery time a val

26、ue crosses the boundary between the two languages (twice for each embedded primitive) its entire representation is changedLaziness doesnt really help even in Haskell, that version of map is quadraticThere is a more efficient approach based on using the extensibility of exceptions to implement a Dyna

27、mic type, but It doesnt allow datatypes to be treated polymorphically.If you embed the same type twice, the results are incompatibleMore Advanced: Monadic InterpretersWhat about parameterizing our interpreter by an arbitrary monad T (e.g. for non-determinism, probabilities, continuations,)?Assume CB

28、V translation, so an expression in the object language which appears to have type A will be given a semantics of type TA* whereint* = int(AB)* = A*TB*Embedding seems impossibleAn ML function value of type (int int) int needs to be given a semantics in the interpreter of type (int T int) T int and th

29、ats not possible extensionally. (How can the ML function “know what to do” with the extra monadic information returned by calls to its argument?)More generally, need an extensional version of the CBV monadic translation, which cannot be defined in core ML (or Haskell)ButSemantically, an ML function

30、of type (int int) int is already really of type (int M int) M int where M is the implicit monad for ML. Always includes references, exceptions, non-termination and IO, but for SML/NJ and MLton it also includes first-class continuationsAmazing fact (Filinski): MNJ is universal, in the sense that any

31、ML-expressible monad T is a retract of MNJ.How does that help?For any monad T in ML can define polymorphic functions val reflect : a T - a val reify : (unit - a) - a TThis cunning idea of Filinski combines with representing types by embedding-projection pairs to allow the definition of an extensiona

32、l monadic translation just as we wantedA* is not parametric in A (like A EP was) but can still represent the type by a pair of a translation function t : AA* and an “untranslation” function n : A*A with combinators for type constructors being well-typedLike thisval int = (I,I)val string = (I,I)fun (

33、t,n)*(t,n) = (cross(t,t), cross(n,n)fun (t,n)-(t,n) = (fn f= fn x= reify (fn ()= t (f (n x), fn g= fn x= n( reflect (g (t x)Like thisval int = (I,I)val string = (I,I)fun (t,n)*(t,n) = (cross(t,t), cross(n,n)fun (t,n)-(t,n) = (fn f= fn x= reify (fn ()= t (f (n x), fn g= fn x= n( reflect (g (t x)ABA*A

34、*ABBB*(unitB*) T B*The translation at workstructure IntStateMonad : sig type a T = int-int*a val return : a-a T val bind : a T - (a - b T) - b T val add : int - unit T (* = fn m = fn n = (n+m,() *) endfun translate (t,n) x = t x- fun apptwice f = (f 1; f 2; “done”)val apptwice : (int-unit)-string- v

35、al tapptwice = translate (int-unit)-string) apptwice;val tapptwice : (int-unit T)-string T- tapptwice add 0;val it = (3,”done”) : int * stringThe embedded monadic interpreterNow combine the embedding-projection pairs with the monadic translation-untranslation functionsThere is a choice: the monad ca

36、n be either implicit or explicit in the universal datatype and the code for the interpreterWell choose implicitEach type A is represented by a 4-tupleeA : AUpA : UAtA : AA*nA : A*AWith the implicit monad, the definition of the universal datatype and the code for the interpreter itself remains exactl

37、y as it was in the case of the non-monadic interpreter!Embedding and projecting in the monadic caseOrdinary ML values of type A are still embedded with eA.The ML values which represent the operations of the monad will have ML types which are already in the image of the (.)* translationWe embed them

38、by first untranslating them, to get an ML value of the type which they will appear to have in the object language and then embedding the result, i.e. eA o nAWhen projecting an object expression of type A we want to see it as a computation of type A* which requires another use of reification:fun proj

39、ect (e,p,t,n) f x = R.reify (fn ()= t (p (f x)Example: Non-determinismUse list monad with monad operations for choice and failurefun choose (x,y) = x,y (* choose : a*a-a T *)fun fail () = (* fail : unit-a T *)val builtins = (choose, membed (any*any-any) choose), (fail, membed (unit-any) fail), (+, e

40、mbed (int*int-int) Int.+), . - project int (interpret (read let val n = (choose(3,4)+(choose(7,9) in if n12 then fail() else 2*n,) ;val it = 20,24,22 : int ListMonad.tEven more advanced:-calculus(Asynchronous) -calculus is a first-order process calculus based on name passingThere is a well-known tra

41、nslation of (CBV) -calculus into .Goal: write an interpreter for with embeddings which turn ML functions into processes ,and projections which turn (suitably well-behaved) processes into ML functionsAn interpreter for asynchronous type a chan = (a Q.queue) * (a C.cont Q.queue) datatype BaseValue = V

42、I of int | VS of string | VB of bool | VU | VN of Name and Name = Name of (BaseValue list) chan type Value = BaseValue list val readyQ = Q.mkQueue() : unit C.cont Q.queue fun new() = Name (Q.mkQueue(),Q.mkQueue() fun scheduler () = C.throw (Q.dequeue readyQ) () fun send (Name (sent,blocked),value) =

43、 if Q.isEmpty blocked then Q.enqueue (sent,value) else C.callcc (fn k = (Q.enqueue(readyQ,k); C.throw (Q.dequeue blocked) value) fun receive (Name (sent,blocked) = if Q.isEmpty sent then C.callcc (fn k = (Q.enqueue (blocked,k); scheduler () else Q.dequeue sentEtcPict-style syntax on top- val pp = re

44、ad new ping new pong (ping?* = echo!ping | pong!) | (pong?* = echo!pong | ping!) | ping!;val pp = - : Exp- schedule (interpret (pp, Builtins.static) Builtins.dynamic);val it = () : unit- sync ();pingpongpingpongpingpongpingpongpingpongpingpong.Embeddings and projectionssignature EMBEDDINGS =sig type

45、 a EP val embed : (a EP) - a - Process.BaseValue val project : (a EP) - Process.BaseValue - a val int : int EP val string : string EP val bool : bool EP val unit : unit EP val * : (a EP)*(b EP) - (a*b) EP val - : (a EP)*(b EP) - (a-b) EPendLooks just as before, but now side-effectingFunction casefun

46、 (ea,pa)-(eb,pb) = ( fn f = let val c = P.new() fun action () = let val ac,VN rc = P.receive c val _ = P.fork action val resc = eb (f (pa ac) in P.send(rc,resc) end in (P.fork action; VN c) end, fn (VN fc) = fn arg = let val ac = ea arg val rc = P.new () val _ = P.send(fc,ac,VN rc) val resloc = P.re

47、ceive(rc) in pb resloc end )And it works- fun test s = let val p = Ierpret (Exp.read s, Builtins.static) Builtins.dynamic in (schedule p; sync() end;val test = fn : string - unit- test new r1 new r2 twice!inc r1 | r1?f = f!3 r2 | r2?n = itos!n echo;5This is the translation of print (In

48、t.toString (twice inc 3)and does do the right thing (note TCO)Can interact in non-functional waysfun appupto f n = if n unit)-int-unit, can then do- test new r1 new r2 new c appupto!printn r1 | (r1?f = c?*n r = r! | f!n devnull) | appupto!c r2 | r2?g = g!10 devnull;0010011022013012012310234201343012

49、454123552346634574565676787889910For each n from 0 to 10, print each integer from 0 to n, all run in parallelProjection- fun ltest name s = let val n = newname() val p = Ierpret (Exp.read s, name : Builtins.static) (n : Builtins.dynamic) in (schedule p; n) end;val ltest = fn : string -

50、 string - BaseValue- val ctr = project (unit-int) (ltest c new v v!0 | v?*n = c?r=r!n | inc!n v);val ctr = fn : unit - int- ctr();val it = 0 : int- ctr();val it = 1 : int- ctr();val it = 2 : intTwo counters on same channel- val dctr = project (unit - int) (ltest c (new v v!0 | v?*n = c?x r=r!n | inc!n v) | (new v v!0 | v?*n = c?x r=r!n | inc!n v);val dctr = fn : unit - int- dctr();val it = 0 : int- dctr();val it = 0 : int- dctr();val it = 1 : int- dctr();val it = 1 : intFixpoints- val y = project (int-int

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論