snoeathurc.h
This commit is contained in:
@ -130,22 +130,32 @@ and checkExp (ftab : FunTable)
|
||||
See `AbSyn.fs` for the expression constructors of `Times`, ...
|
||||
*)
|
||||
| Times (e1, e2, pos) ->
|
||||
failwith "Unimplemented type check of multiplication"
|
||||
let (e1_dec, e2_dec) = checkBinOp ftab vtab (pos, Int, e1, e2)
|
||||
(Int, Times (e1_dec, e2_dec, pos))
|
||||
|
||||
| Divide (_, _, _) ->
|
||||
failwith "Unimplemented type check of division"
|
||||
| Divide (e1, e2, pos) ->
|
||||
let (e1_dec, e2_dec) = checkBinOp ftab vtab (pos, Int, e1, e2)
|
||||
(Int, Divide (e1_dec, e2_dec, pos))
|
||||
|
||||
| And (_, _, _) ->
|
||||
failwith "Unimplemented type check of &&"
|
||||
| And (e1, e2, pos) ->
|
||||
let (e1_dec, e2_dec) = checkBinOp ftab vtab (pos, Bool, e1, e2)
|
||||
(Bool, And (e1_dec, e2_dec, pos))
|
||||
|
||||
| Or (_, _, _) ->
|
||||
failwith "Unimplemented type check of ||"
|
||||
| Or (e1, e2, pos) ->
|
||||
let (e1_dec, e2_dec) = checkBinOp ftab vtab (pos, Bool, e1, e2)
|
||||
(Bool, Or (e1_dec, e2_dec, pos))
|
||||
|
||||
| Not (_, _) ->
|
||||
failwith "Unimplemented type check of not"
|
||||
| Not (e, pos) ->
|
||||
let (t, e_dec) = checkExp ftab vtab e
|
||||
if (t = Bool) then
|
||||
(Bool, Not (e_dec, pos))
|
||||
else raise (MyError("Not on non-bool type", pos))
|
||||
|
||||
| Negate (_, _) ->
|
||||
failwith "Unimplemented type check of negate"
|
||||
| Negate (e, pos) ->
|
||||
let (t, e_dec) = checkExp ftab vtab e
|
||||
if (t = Int) then
|
||||
(Int, Negate (e_dec, pos))
|
||||
else raise (MyError("Negation of non-int type", pos))
|
||||
|
||||
(* The types for e1, e2 must be the same. The result is always a Bool. *)
|
||||
| Equal (e1, e2, pos) ->
|
||||
@ -294,8 +304,12 @@ and checkExp (ftab : FunTable)
|
||||
- assuming `a` is of type `t` the result type
|
||||
of replicate is `[t]`
|
||||
*)
|
||||
| Replicate (_, _, _, _) ->
|
||||
failwith "Unimplemented type check of replicate"
|
||||
| Replicate (n_exp, a_exp, _, pos) ->
|
||||
let (n_t, n_dec) = checkExp ftab vtab n_exp
|
||||
let (a_t, a_dec) = checkExp ftab vtab a_exp
|
||||
if (n_t = Int) then
|
||||
(Array a_t, Replicate (n_dec, a_dec, a_t, pos))
|
||||
else raise (MyError("parameter n not Int", pos))
|
||||
|
||||
(* TODO project task 2: Hint for `filter(f, arr)`
|
||||
Look into the type-checking lecture slides for the type rule of `map`
|
||||
@ -306,8 +320,21 @@ and checkExp (ftab : FunTable)
|
||||
- `arr` should be of type `[ta]`
|
||||
- the result of filter should have type `[tb]`
|
||||
*)
|
||||
| Filter (_, _, _, _) ->
|
||||
failwith "Unimplemented type check of filter"
|
||||
| Filter (f, arr_exp, _, pos) ->
|
||||
let (arr_type, arr_exp_dec) = checkExp ftab vtab arr_exp
|
||||
let elem_type =
|
||||
match arr_type with
|
||||
| Array t -> t
|
||||
| _ -> reportTypeWrongKind "second argument of map" "array" arr_type pos
|
||||
let (f', f_res_type, f_arg_type) =
|
||||
match checkFunArg ftab vtab pos f with
|
||||
| (f', res, [a1]) -> (f', res, a1)
|
||||
| (_, res, args) ->
|
||||
reportArityWrong "first argument of map" 1 (args,res) pos
|
||||
if elem_type <> f_arg_type then
|
||||
reportTypesDifferent "function-argument and array-element types in map"
|
||||
f_arg_type elem_type pos
|
||||
(Array f_res_type, Map (f', arr_exp_dec, elem_type, f_res_type, pos))
|
||||
|
||||
(* TODO project task 2: `scan(f, ne, arr)`
|
||||
Hint: Implementation is very similar to `reduce(f, ne, arr)`.
|
||||
@ -315,8 +342,32 @@ and checkExp (ftab : FunTable)
|
||||
scan's return type is the same as the type of `arr`,
|
||||
while reduce's return type is that of an element of `arr`).
|
||||
*)
|
||||
| Scan (_, _, _, _, _) ->
|
||||
failwith "Unimplemented type check of scan"
|
||||
| Scan (f, e_exp, arr_exp, _, pos) ->
|
||||
let (e_type , e_dec ) = checkExp ftab vtab e_exp
|
||||
let (arr_type, arr_dec) = checkExp ftab vtab arr_exp
|
||||
let elem_type =
|
||||
match arr_type with
|
||||
| Array t -> t
|
||||
| _ -> reportTypeWrongKind "third argument of reduce" "array" arr_type pos
|
||||
let (f', f_argres_type) =
|
||||
match checkFunArg ftab vtab pos f with
|
||||
| (f', res, [a1; a2]) ->
|
||||
if a1 <> a2 then
|
||||
reportTypesDifferent "argument types of operation in reduce"
|
||||
a1 a2 pos
|
||||
if res <> a1 then
|
||||
reportTypesDifferent "argument and return type of operation in reduce"
|
||||
a1 res pos
|
||||
(f', res)
|
||||
| (_, res, args) ->
|
||||
reportArityWrong "operation in reduce" 2 (args,res) pos
|
||||
if elem_type <> f_argres_type then
|
||||
reportTypesDifferent "operation and array-element types in reduce"
|
||||
f_argres_type elem_type pos
|
||||
if e_type <> f_argres_type then
|
||||
reportTypesDifferent "operation and start-element types in scan"
|
||||
f_argres_type e_type pos
|
||||
(f_argres_type, Reduce (f', e_dec, arr_dec, elem_type, pos))
|
||||
|
||||
and checkFunArg (ftab : FunTable)
|
||||
(vtab : VarTable)
|
||||
|
Reference in New Issue
Block a user