Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Type Inference We extend the MicroCaml programming language in Assignment 5 with an additional con - struct called Pair. A pair { 1 , 2
Type Inference We extend the MicroCaml programming language in Assignment with an additional con struct called Pair. A pair consists of two elements and each of which may have a different type. typeopAddSub type var string type expr Int of int Bool of bool String of string ID of var Fun of var expr Not of expr Binop of op expr expr If of expr expr expr FunctionCall of expr expr Let of var bool expr expr Pair of exper expr Extend MicroCaml with Pair type typeScheme TNum TBool TStr T of string TFun of typeScheme typeScheme TPair of typeScheme typeScheme Add pair types We assume the parser has also been extended so it can parse a pair in the syntax as an abstract syntax tree Pair For example, the extended MicroCaml language allows you to write a program as follows: let rec f x y if x then f y y else y f x We also extended typeScheme to support pair types. A pair type for Pair is in the shape of where is the type of and is the type of For example, the type of the expression "hello" is int string or TPair TNum TStr a point Provide a typing rule in a similar style as in the Assignment description for pairs CS Final, Page of th Dec, b point Consider the unify implementation in Assignment What would you put in place for the blank such that unify can be applied for type inference of MicroCaml programs with pairs? let rec unify constraints : typeScheme typeScheme substitutions match constraints with l i s t : and x y :: xs let t unify xs in let t unifyone apply t xapply t y in t @ t unifyone t : typeScheme t : typeScheme : substitutions match t t with TNum, TNum TBool, TBool TStr TStr Tx z z Txx z TFuna b TFunx y unify a x; b y Fill in the blank to complete unify for pairs TPair a b TPair x y raise failwith "mismatched types" c point Write down the type of let rec f x y if x then f y y else y f x d point Consider the following program: let f fun x x in let y f in f hello Is the type inference algorithm, as implemented in Assignment capable of deducing the type of this program? If your response is negative, provide a rationale for your answer.
Type Inference
We extend the MicroCaml programming language in Assignment with an additional con struct called Pair. A pair consists of two elements and each of which may have a different type.
typeopAddSub
type var string
type expr
Int of int
Bool of bool
String of string
ID of var
Fun of var expr
Not of expr
Binop of op expr expr
If of expr expr expr
FunctionCall of expr expr
Let of var bool expr expr
Pair of exper expr Extend MicroCaml with Pair
type typeScheme
TNum TBool TStr T of string
TFun of typeScheme typeScheme
TPair of typeScheme typeScheme Add pair types
We assume the parser has also been extended so it can parse a pair in the syntax as an abstract syntax tree Pair For example, the extended MicroCaml language allows you to write a program as follows:
let rec f x y if x then f y y else y f x
We also extended typeScheme to support pair types. A pair type for Pair is in the shape of where is the type of and is the type of For example, the type of the expression "hello" is int string or TPair TNum TStr
a point Provide a typing rule in a similar style as in the Assignment description for pairs
CS
Final, Page of
th
Dec,
b point Consider the unify implementation in Assignment What would you put in place for the blank such that unify can be applied for type inference of MicroCaml programs with pairs?
let rec unify constraints : typeScheme typeScheme substitutions
match constraints with
l i s t :
and
x y :: xs
let t unify xs in
let t unifyone apply t xapply t y in t @ t
unifyone t : typeScheme t : typeScheme : substitutions
match t t with
TNum, TNum TBool, TBool TStr TStr
Tx z z Txx z
TFuna b TFunx y unify a x; b y Fill in the blank to complete unify for pairs TPair a b TPair x y
raise failwith "mismatched types"
c point Write down the type of
let rec f x y if x then f y y else y f x
d point Consider the following program:
let f fun x x in let y f in
f hello
Is the type inference algorithm, as implemented in Assignment capable of deducing the type of this program? If your response is negative, provide a rationale for your answer.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started