Type Safe Interpreters for Free Maximilian Algehed Slrn Halla - - PowerPoint PPT Presentation

type safe interpreters for free
SMART_READER_LITE
LIVE PREVIEW

Type Safe Interpreters for Free Maximilian Algehed Slrn Halla - - PowerPoint PPT Presentation

Type Safe Interpreters for Free Maximilian Algehed Slrn Halla Einarsdttir Alex Gerdes Patrik Jansson Functional Programming division, Chalmers University of Technology 2018-06-12 (TFP 2018) https://github.com/GRACeFUL-project/Saint


slide-1
SLIDE 1

Type Safe Interpreters for Free

Maximilian Algehed Sólrún Halla Einarsdóttir Alex Gerdes Patrik Jansson

Functional Programming division, Chalmers University of Technology

2018-06-12 (TFP 2018) https://github.com/GRACeFUL-project/Saint

Acknowledgments: Funding from Horizon 2020 through GRACeFUL (grant #640954) and CoeGSS (grant #676547) and from Knut and Alice Wallenberg Foundation through WASP. Algehed, . . . , Jansson (FP div., Chalmers) Type Safe Interpreters for Free 2018-06-12 (TFP 2018) 1 / 11

slide-2
SLIDE 2

How “Saint” Connects Your EDSL with the Cloud

Algehed, . . . , Jansson (FP div., Chalmers) Type Safe Interpreters for Free 2018-06-12 (TFP 2018) 2 / 11

slide-3
SLIDE 3

How “Saint” Connects Your EDSL with the Cloud

Algehed, . . . , Jansson (FP div., Chalmers) Type Safe Interpreters for Free 2018-06-12 (TFP 2018) 2 / 11

slide-4
SLIDE 4

How “Saint” Connects Your EDSL with the Cloud

Text: let sqrl = ...

  • - 10 more lines

in scale 100 (sqrl 3)

Algehed, . . . , Jansson (FP div., Chalmers) Type Safe Interpreters for Free 2018-06-12 (TFP 2018) 2 / 11

slide-5
SLIDE 5

How “Saint” Connects Your EDSL with the Cloud

String untyped Expr typed Expr DSL value (Image)

parse typeInference interpret

Algehed, . . . , Jansson (FP div., Chalmers) Type Safe Interpreters for Free 2018-06-12 (TFP 2018) 2 / 11

slide-6
SLIDE 6

How “Saint” Connects Your EDSL with the Cloud

Image: first 14000 splines, then raw PNG data

Algehed, . . . , Jansson (FP div., Chalmers) Type Safe Interpreters for Free 2018-06-12 (TFP 2018) 2 / 11

slide-7
SLIDE 7

How “Saint” Connects Your EDSL with the Cloud

Algehed, . . . , Jansson (FP div., Chalmers) Type Safe Interpreters for Free 2018-06-12 (TFP 2018) 2 / 11

slide-8
SLIDE 8

Example DSL: Henderson’s functional geometry (fish)

beside :: Image → Image → Image above :: Image → Image → Image

  • ver

:: Image → Image → Image

  • - overlay

rot :: Image → Image

  • - 90 degrees

natrec :: Image →

  • - base case

(Int → Image → Image) →

  • - step function

Int → Image fish :: Image data Image

  • - just a list of splines

Algehed, . . . , Jansson (FP div., Chalmers) Type Safe Interpreters for Free 2018-06-12 (TFP 2018) 3 / 11

slide-9
SLIDE 9

Example: the fish DSL API as a Library

For Saint to help, we need to describe the API as a value. fishLib :: Library fishLib = Library "fish" [Item "beside" $ beside ::: image image image , Item "above" $ above ::: image image image , Item "over" $ over ::: image image image , Item "rot" $ rot ::: image image , Item "natrec" $ natrec ::: image (int image image) int image , Item "fish" $ fish ::: image ]

Algehed, . . . , Jansson (FP div., Chalmers) Type Safe Interpreters for Free 2018-06-12 (TFP 2018) 4 / 11

slide-10
SLIDE 10

Example: the fish DSL API as a Library

For Saint to help, we need to describe the API as a value. fishLib :: Library fishLib = Library "fish" [Item "beside" $ beside ::: image image image , Item "above" $ above ::: image image image , Item "over" $ over ::: image image image , Item "rot" $ rot ::: image image , Item "natrec" $ natrec ::: Tag "Recursion over Nat" image Tag "The step function" (int image image) int image , Item "fish" $ fish ::: Tag "The fish base image" image ]

Algehed, . . . , Jansson (FP div., Chalmers) Type Safe Interpreters for Free 2018-06-12 (TFP 2018) 4 / 11

slide-11
SLIDE 11

Exposing the Library API

Any lib :: Library is a type-annotated lookup table. data Library = Library String [Item] data Item = Item String TypedValue data TypedValue where

  • - basically Dynamic

(:::) :: a → TRep a → TypedValue infixr 0 ::: Codes for types (more general in the paper): data TRep t where TImage :: TRep Image TInt :: TRep Int TFun :: TRep a → TRep b → TRep (a → b) Tag :: String → TRep a → TRep a image = TImage; int = TInt; () = TFun infixr 1

Algehed, . . . , Jansson (FP div., Chalmers) Type Safe Interpreters for Free 2018-06-12 (TFP 2018) 5 / 11

slide-12
SLIDE 12

Simple type representations

data TRep t where

  • - Codes for types:

TImage :: TRep Image TInt :: TRep Int TFun :: TRep a → TRep b → TRep (a → b) Tag :: String → TRep a → TRep a Working with (dynamic) typed values: data a ≡ b where Refl :: a ≡ a ( ? =) :: TRep a → TRep b → Maybe (a ≡ b)

  • - ?

= def. by simple syntactic equality (elided) coerce :: TRep a → TypedValue → a coerce a (x ::: b) = case a ? = b of Just Refl → x Nothing → error "coerce: the types don’t match"

Algehed, . . . , Jansson (FP div., Chalmers) Type Safe Interpreters for Free 2018-06-12 (TFP 2018) 6 / 11

slide-13
SLIDE 13

Example use of the interpreter

data Expr where Var :: String → Expr App :: Expr → Expr → Expr Lam :: String → TRep a → Expr → TRep b → Expr Example: App Var "rot" App Var "rot" Var "fish" → Just (rot (rot fish) ::: image)

Algehed, . . . , Jansson (FP div., Chalmers) Type Safe Interpreters for Free 2018-06-12 (TFP 2018) 7 / 11

slide-14
SLIDE 14

Towards a Type Safe Interpreter

app :: TypedValue → TypedValue → Maybe TypedValue app (f ::: TFun a b) (x ::: a′) = do Refl ← a ? = a′ return (f x ::: b) app = Nothing data Expr where Var :: String → Expr App :: Expr → Expr → Expr Lam :: String → TRep a → Expr → TRep b → Expr type Env = String → Maybe TypedValue

  • - or similar

extend :: String → TypedValue → Env → Env

  • - simple

libToEnv :: Library → Env

  • - also simple

Algehed, . . . , Jansson (FP div., Chalmers) Type Safe Interpreters for Free 2018-06-12 (TFP 2018) 8 / 11

slide-15
SLIDE 15

Type Safe Interpreter

data Expr where Var :: String → Expr App :: Expr → Expr → Expr Lam :: String → TRep a → Expr → TRep b → Expr interpret :: Env → Expr → Maybe TypedValue interpret env e = case e of Var v → env v App f a → do f ′ ← interpret env f a′ ← interpret env a app f ′ a′ Lam v t bo t′ → return (lam ::: (t t′)) where lam x = let env′ = extend v (x ::: t) env Just res = interpret env′ bo in coerce t′ res

Algehed, . . . , Jansson (FP div., Chalmers) Type Safe Interpreters for Free 2018-06-12 (TFP 2018) 9 / 11

slide-16
SLIDE 16

Saint paper summary

a framework (Saint) for exposing a typed API to an untyped world

Algehed, . . . , Jansson (FP div., Chalmers) Type Safe Interpreters for Free 2018-06-12 (TFP 2018) 10 / 11

slide-17
SLIDE 17

Saint paper summary

a framework (Saint) for exposing a typed API to an untyped world a version of Typeable supporting tags (annotations in the TRep) a generic, type safe interpreter in Haskell

Algehed, . . . , Jansson (FP div., Chalmers) Type Safe Interpreters for Free 2018-06-12 (TFP 2018) 10 / 11

slide-18
SLIDE 18

Saint paper summary

a framework (Saint) for exposing a typed API to an untyped world a version of Typeable supporting tags (annotations in the TRep) a generic, type safe interpreter in Haskell two case studies: FISH and GRACe

Algehed, . . . , Jansson (FP div., Chalmers) Type Safe Interpreters for Free 2018-06-12 (TFP 2018) 10 / 11

slide-19
SLIDE 19

Questions?

Type Safe Interpreters for Free

Maximilian Algehed Sólrún Halla Einarsdóttir Alex Gerdes Patrik Jansson

Functional Programming division, Chalmers University of Technology

2018-06-12 (TFP 2018) https://github.com/GRACeFUL-project/Saint

Acknowledgments: Funding from Horizon 2020 through GRACeFUL (grant #640954) and CoeGSS (grant #676547) and from Knut and Alice Wallenberg Foundation through WASP. Algehed, . . . , Jansson (FP div., Chalmers) Type Safe Interpreters for Free 2018-06-12 (TFP 2018) 1 / 11

How “Saint” Connects Your EDSL with the Cloud

String untyped Expr typed Expr DSL value (Image)

parse typeInference interpret

Algehed, . . . , Jansson (FP div., Chalmers) Type Safe Interpreters for Free 2018-06-12 (TFP 2018) 2 / 11

Algehed, . . . , Jansson (FP div., Chalmers) Type Safe Interpreters for Free 2018-06-12 (TFP 2018) 11 / 11