text
stringlengths 0
601k
|
---|
let open_account (initial_pass: passwd) : bank_account = let cur_pwd = ref initial_pass in let balance = ref 0 in let failure_count = ref 0 in { update_pass = (fun attempted_pwd new_pwd -> if attempted_pwd = !cur_pwd then (failure_count := 0; cur_pwd := new_pwd) else if attempted_pwd <> !cur_pwd then (failure_count := !failure_count + 1; raise wrong_pass) ); retrieve = ( fun attempted_pwd amount -> if !failure_count > 4 then raise too_many_failures else if attempted_pwd = !cur_pwd then ( failure_count := 0; if amount <0 then raise negative_amount else if amount <= !balance then balance := !balance - amount else raise not_enough_balance ) else if attempted_pwd <> !cur_pwd then (failure_count := !failure_count + 1; raise wrong_pass) ); deposit = ( fun attempted_pwd amount -> if !failure_count > 4 then raise too_many_failures else if attempted_pwd = !cur_pwd then ( failure_count := 0; if amount < 0 then raise negative_amount else balance := !balance + amount ) else if attempted_pwd <> !cur_pwd then (failure_count := !failure_count + 1; raise wrong_pass) ); show_balance = ( fun attempted_pwd -> if !failure_count > 4 then raise too_many_failures else if attempted_pwd <> !cur_pwd then (failure_count := !failure_count + 1; raise wrong_pass) else ( failure_count := 0; !balance ) ); };; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let {nodes; edges = useful} = g in begin let rec helper useful vertex list = match useful with | [] -> list | [(v1, v2, w)] -> if v1 = vertex then helper [] vertex ((v2, w) :: list) else helper [] vertex list | [(v1, v2, w); rest] -> if v1 = vertex then helper [rest] vertex ((v2, w) :: list) else helper [rest] vertex list | (v1, v2, w)::rest -> if v1 = vertex then helper rest vertex ((v2, w) :: list) else helper rest vertex list in helper useful vertex [] end ;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) (total_weight: weight) : ('a list * weight) = let (a,weight) = node in if a = b then (List.rev (b :: visited), (total_weight + weight)) else if List.mem a visited then raise Fail else aux_list (neighbours g a) (a :: visited) (total_weight + weight) and aux_list (nodes: ('a * weight) list) (visited: 'a list) (total_weight: weight) : ('a list * weight) = match nodes with | node :: rest -> (try aux_node node visited total_weight with Fail -> aux_list rest visited total_weight) | [] -> raise Fail in aux_list (neighbours g a) [a] 0;; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) (total_weight: weight) : ('a list * weight)= let (a,weight) = node in if a = b then (List.rev (b :: visited), (total_weight + weight)) else if List.mem a visited then ([],0) else aux_list (neighbours g a) (a :: visited) (total_weight + weight) and aux_list (nodes: ('a * weight) list) (visited: 'a list) (total_weight: weight) : ('a list * weight) = match nodes with | node :: rest -> if aux_node node visited total_weight = ([],0) then aux_list rest visited total_weight else aux_node node visited total_weight | [] -> raise Fail in aux_list (neighbours g a) [a] 0;; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec helper paths_list big_list big_weight = match paths_list with | (list, weight):: rest -> if weight > big_weight then helper rest list weight else if weight = big_weight then if List.length list > List.length big_list then helper rest list weight else helper rest list weight else helper rest big_list big_weight | [] -> (big_list, big_weight) in if find_all_paths g a b = [] then None else Some (helper (find_all_paths g a b) [] 0) ;; |
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let balance = ref 0 in let error_count = ref 0 in { update_pass = (fun old_pass new_pass -> if old_pass = !password then (password := new_pass; error_count := 0) else (error_count := (!error_count + 1); raise wrong_pass)); deposit = (fun pass amount -> if !error_count >= 5 then raise too_many_failures else ( if pass = !password then ( error_count := 0; if amount < 0 then raise negative_amount else (balance := (!balance + amount))) else (error_count := (!error_count + 1); raise wrong_pass))); retrieve = (fun pass amount -> if !error_count >= 5 then raise too_many_failures else ( if pass = !password then ( error_count := 0; if amount < 0 then raise negative_amount else( if !balance < amount then raise not_enough_balance else balance := (!balance - amount))) else (error_count := (!error_count + 1); raise wrong_pass))); show_balance = (fun pass -> if !error_count >= 5 then (raise too_many_failures) else ( if pass = !password then (error_count := 0; !balance) else( error_count := (!error_count + 1); raise wrong_pass))); } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left (fun acc node -> let (start,dest,w) = node in if start = vertex then (dest, w) :: acc else acc ) [] g.edges ;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : ('a * weight) list) : ('a list * weight) = let (value,_) = node in if value = b then (List.map (fun ((n, _): ('a * weight)) -> n) visited, List.fold_right (fun (n: ('a * weight)) (w: weight) -> let (_,w1) = n in w + w1) visited 0) else (aux_list (neighbours g value) visited) and aux_list (nodes: ('a * weight) list) (visited: ('a * weight) list) : ('a list * weight) = let ns = (List.filter (fun ((n, _): ('a * weight)) -> (not (List.mem n (List.map (fun ((n, _): ('a * weight)) -> n) visited))) ) nodes) in match ns with | [] -> raise Fail | n :: ns -> try aux_node n (visited @ [n]) with Fail -> aux_list ns visited in aux_list (neighbours g a) [(a, 0)];; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : ('a * weight) list) fc sc : ('a list * weight)= let (value,_) = node in if value = b then sc (List.rev (List.map (fun ((n, _): ('a * weight)) -> n) visited), List.fold_left (fun (w: weight) (n: ('a * weight)) -> let (_,w1) = n in w + w1) 0 visited) else (match (neighbours g value) with | [] -> fc () | neighbour -> aux_list neighbour visited fc sc) and aux_list (nodes: ('a * weight) list) (visited: ('a * weight) list) fc sc : ('a list * weight) = let node_list = (List.filter (fun ((n, _): ('a * weight)) -> (not (List.mem n (List.map (fun ((n, _): ('a * weight)) -> n) visited))) ) nodes) in match node_list with | [] -> fc () | n :: ns -> let suc1 = sc in let fail1 = (fun () -> aux_node n (n :: visited) fc sc) in aux_list ns visited fail1 suc1 in aux_list (neighbours g a) [(a, 0)] (fun () -> raise Fail) (fun x -> x);; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = match (find_all_paths g a b) with | [] -> None | paths -> Some (List.fold_left (fun (max: ('a list * weight)) (path: ('a list * weight)) -> let (_, w1), (_, w2) = max, path in if w2 > w1 then path else max ) ([], 0) paths);; |
let open_account (initial_pass: passwd) : bank_account = let pw = ref initial_pass in let balance = ref 0 in let pw_tries = ref 0 in { update_pass = (fun old_pw new_pw -> if !pw <> old_pw then raise wrong_pass else (pw := new_pw; pw_tries := 0) ); deposit = (fun user_pw deposit_val -> if !pw_tries >= 5 then raise too_many_failures else if !pw <> user_pw then ((pw_tries := !pw_tries + 1); raise wrong_pass) else (pw_tries := 0; if deposit_val < 0 then raise negative_amount else (balance := !balance + deposit_val) ) ); retrieve = (fun user_pw retrieve_val -> if !pw_tries >= 5 then raise too_many_failures else if !pw <> user_pw then ((pw_tries := !pw_tries + 1); raise wrong_pass) else (pw_tries := 0; if retrieve_val < 0 then raise negative_amount else if retrieve_val > !balance then raise not_enough_balance else balance := !balance - retrieve_val ) ); show_balance = (fun user_pw -> if !pw_tries >= 5 then raise too_many_failures else if !pw <> user_pw then ((pw_tries := !pw_tries + 1); raise wrong_pass) else (pw_tries := 0; !balance) ) } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let rec neighbours_helper edges vertex acc = match edges with | [] -> acc | (v1, v2, weight) :: xs -> if v1 = vertex then neighbours_helper xs vertex ((v2, weight) :: acc) else neighbours_helper xs vertex acc in neighbours_helper g.edges vertex [] ;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (vertex : 'a) (visited: 'a list) total_weight : ('a list * weight) = let neighbours_lst = neighbours g vertex in let not_visited_neighbours = List.filter (fun (el,_) -> not (List.mem el visited)) neighbours_lst in aux_list not_visited_neighbours (vertex::visited) total_weight and aux_list (nodes: ('a * weight) list) (visited: 'a list) total_weight : ('a list * weight) = match nodes with | [] -> raise Fail | (vertex,weight) :: xs -> if vertex = b then ((vertex :: visited), total_weight + weight) else try aux_node vertex visited (total_weight + weight) with Fail -> aux_list xs visited total_weight in let res_l, res_w = aux_node a [] 0 in (List.rev res_l, res_w);; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= let vertex, _ = node in let neighbours_lst = neighbours g vertex in let not_visited_neighbours = List.filter (fun (el, _) -> not (List.mem el visited)) neighbours_lst in aux_list not_visited_neighbours (vertex::visited) fc sc and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | (vertex, weight) :: xs -> if vertex = b then sc ([vertex], weight) else let fail2 = (fun () -> aux_list xs visited fc sc) in let success2 = (fun (rp,rw) -> sc (vertex :: rp, rw + weight)) in aux_node (vertex, weight) visited fail2 success2 in aux_node (a,0) [] (fun () -> raise Fail) (fun (rp, rw) -> (a::rp, rw));; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let paths = find_all_paths g a b in let rec helper paths long_path long_weight = match paths with | [] -> long_path, long_weight | (path,weight) :: xs -> if weight > long_weight then helper xs path weight else helper xs long_path long_weight in let helper_path, helper_weight = helper paths [] 0 in if helper_path = [] then None else Some (helper_path, helper_weight);; |
let open_account (initial_pass: passwd) : bank_account = let p = ref initial_pass in let b = ref 0 in let w = ref 0 in { update_pass = (fun o n -> if o = !p then (p := n; w := 0;) else (w := !w + 1; raise wrong_pass)); retrieve = (fun d a -> if !w > 4 then raise too_many_failures else if d = !p then (w := 0; if a < 0 then raise negative_amount else if a > !b then raise not_enough_balance else b := !b - a ) else (w := !w + 1; raise wrong_pass)); deposit = (fun d a -> if !w > 4 then raise too_many_failures else if d = !p then (w := 0; if a < 0 then raise negative_amount else b := !b + a ) else (w := !w + 1; raise wrong_pass)); show_balance = (fun d -> if !w > 4 then raise too_many_failures else if d = !p then (w := 0; !b) else (w := !w + 1; raise wrong_pass)) } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = match g.nodes with | [] -> [] | _ -> let f = fun (p: 'a * 'a * weight) (l: ('a * weight) list) -> let (d, a, w) = p in if d = vertex then (a, w) :: l else l in List.fold_right f g.edges [];; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list): ('a list * weight) = let (v, w) = node in let p = v :: visited in if v = b then (List.rev p, w) else let j = fun (x: 'a * weight) (n: 'a) (r: bool) -> let (y, _) = x in r || (y = n) in let f = fun (n: 'a * weight) (l: ('a * weight) list) -> let (x, y) = n in let r = List.fold_right (j n) p false in if r then l else (x, y + w) :: l in let l = List.fold_right f (neighbours g v) [] in if l = [] then raise Fail else aux_list l p and aux_list (nodes: ('a * weight) list) (visited: 'a list): ('a list * weight) = let h = fun (n: 'a * weight) (l: 'a list * weight) -> try aux_node n visited with Fail -> l in List.fold_right h nodes ([], 0) in let z = aux_node (a, 0) [] in if z = ([], 0) then raise Fail else z;; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= let (v, w) = node in let p = v :: visited in if v = b then sc p w else let j = fun (x: 'a * weight) (n: 'a) (r: bool) -> let (y, _) = x in r || (y = n) in let f = fun (n: 'a * weight) (l: ('a * weight) list) -> let (x, y) = n in let r = List.fold_right (j n) p false in if r then l else (x, y + w) :: l in let l = List.fold_right f (neighbours g v) [] in aux_list l p fc sc and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | x :: xs -> aux_node x visited (fun () -> aux_list xs visited fc sc) sc in aux_node (a, 0) [] (fun () -> raise Fail) (fun p w -> (List.rev p, w));; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let x = List.fold_right (fun (p: 'a list * weight) (q: 'a list * weight): ('a list * weight) -> let (_, r) = p in let (_, s) = q in if r > s then p else q) (find_all_paths g a b) ([], 0) in if x = ([], 0) then None else Some x;; |
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let account_balance = ref 0 in let incorrect_guesses = ref 0 in let f1 previous_password_guess new_password = if previous_password_guess = !password then let _ = incorrect_guesses := 0 in password := new_password else let _ = incorrect_guesses := (!incorrect_guesses + 1) in raise wrong_pass in let f2 password_guess amount = if !incorrect_guesses != 5 then (if password_guess = !password then let _ = incorrect_guesses := 0 in (if amount >=0 then (if !account_balance - amount >= 0 then account_balance := (!account_balance - amount) else raise not_enough_balance) else raise negative_amount) else let _ = incorrect_guesses := (!incorrect_guesses + 1) in raise wrong_pass) else raise too_many_failures in let f3 password_guess amount = if !incorrect_guesses != 5 then (if password_guess = !password then let _ = incorrect_guesses := 0 in (if amount >= 0 then account_balance := (!account_balance + amount) else raise negative_amount) else let _ = incorrect_guesses := (!incorrect_guesses + 1) in raise wrong_pass) else raise too_many_failures in {update_pass = (fun input1 -> fun input2 -> f1 input1 input2); retrieve = (fun input1 -> fun input2 -> f2 input1 input2); deposit = (fun input1 -> fun input2 -> f3 input1 input2); show_balance = fun input -> if !incorrect_guesses != 5 then (if !password = input then let _ = incorrect_guesses := 0 in !account_balance else let _ = incorrect_guesses := (!incorrect_guesses + 1) in raise wrong_pass) else raise too_many_failures } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let f array element = match element with | (in_edge, out_edge, edge_weight) when in_edge = vertex -> (out_edge, edge_weight) :: array | _ -> array in List.fold_left f [] g.edges let is_visited node array = List.exists ((=) node) array;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) (total_weight : weight) : ('a list * weight) = let (node_value, node_weight) = node in if node_value = b then (node_value :: visited, total_weight + node_weight) else let path = node_value :: visited in let neighbors_array = neighbours g node_value in let already_visited = is_visited node_value visited in if List.length neighbors_array = 0 || already_visited then raise Fail else try aux_list neighbors_array path (total_weight + node_weight) with Fail -> raise Fail and aux_list (nodes: ('a * weight) list) (visited: 'a list) (total_weight : weight) : ('a list * weight) = match nodes with | [] -> raise Fail | x :: xs -> try aux_node x visited total_weight with Fail -> try aux_list xs visited total_weight with Fail -> raise Fail in let (list_result, value_result) = aux_list (neighbours g a) [a] 0 in (List.rev list_result, value_result);; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= notimplemented () and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = notimplemented () in notimplemented ();; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let max = ref 0 in let f acc path = let (acc_path, acc_value) = acc in let (path_path, path_value) = path in if path_value >= acc_value then (path_path, path_value) else (acc_path, acc_value) in let paths = find_all_paths g a b in match paths with | [] -> None | y -> let x = List.fold_left f ([], -1) y in Some x;; |
let open_account (initial_pass: passwd) : bank_account = let pass = ref initial_pass in let blc = ref 0 in let wrong_cnt = ref 0 in let update_pass (attempt_pass : passwd) (new_pass : passwd) : unit = if (attempt_pass = !pass) then ( wrong_cnt := 0; pass := new_pass; ) else ( wrong_cnt := !wrong_cnt + 1; raise wrong_pass ) in let retrieve (attempt_pass : passwd) (amt : int) : unit = if (!wrong_cnt >= 5) then raise too_many_failures else ( if (amt < 0) then raise negative_amount else ( if (attempt_pass = !pass) then ( wrong_cnt := 0; if (!blc >= amt) then blc := !blc - amt else raise not_enough_balance ) else ( wrong_cnt := !wrong_cnt + 1; raise wrong_pass ) ) ) in let deposit (attempt_pass : passwd) (amt : int) : unit = if (!wrong_cnt >= 5) then raise too_many_failures else ( if (amt < 0) then raise negative_amount else if (attempt_pass = !pass) then ( wrong_cnt := 0; blc := !blc + amt ) else ( wrong_cnt := !wrong_cnt + 1; raise wrong_pass ) ) in let show_balance (attempt_pass : passwd) : int = if (!wrong_cnt >= 5) then raise too_many_failures else ( if (attempt_pass = !pass) then ( wrong_cnt := 0; !blc ) else ( wrong_cnt := !wrong_cnt + 1; raise wrong_pass ) ) in {update_pass = update_pass; retrieve = retrieve; deposit = deposit; show_balance = show_balance};; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = if List.mem vertex g.nodes then ( let split (a, b, c) = (a, (b, c)) in List.fold_left (fun acc x -> if fst (split x) = vertex then snd (split x) :: acc else acc) [] g.edges ) else raise Fail ;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = let adj = neighbours g (fst node) in if (fst node = b) then ([b], snd node) else if List.mem (fst node) visited then raise Fail else ( let x = (aux_list adj ((fst node) :: visited)) in ((fst node) :: (fst x), (snd node) + (snd x)) ) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | x :: xs -> try aux_node x visited with | Fail -> aux_list xs visited in aux_node (a, 0) [] ;; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= let adj = neighbours g (fst node) in if (List.mem (fst node) visited) || (adj = []) then (fc ()) else ( aux_list adj ((fst node) :: visited) fc (fun (u, w) -> sc (((fst node) :: u), (snd node) + w )) ) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | [x] -> if (fst x) = b then sc ([b], snd x) else aux_node x visited fc sc | x :: xs ->if (fst x) = b then sc ([b], snd x) else aux_node x visited (fun () -> aux_list xs visited fc sc) sc in aux_node (a, 0) [] (fun () -> raise Fail) (fun u -> u);; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = match find_all_paths g a b with | [] -> None | x :: xs -> let rec find_longest (paths: ('a list * weight) list) (ans: ('a list * weight)) : ('a list * weight) option = match paths with | [] -> Some ans | h :: t -> if (snd h) > (snd ans) then find_longest t h else find_longest t ans in find_longest xs x ;; |
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let money = ref 0 in let wrong_pass_cnt = ref 0 in { update_pass = (fun old_p new_p -> if old_p <> !password then (wrong_pass_cnt := !wrong_pass_cnt + 1; print_int !wrong_pass_cnt; raise wrong_pass) else (password := new_p; wrong_pass_cnt := 0)); deposit = (fun provided_password money_to_add -> if !wrong_pass_cnt >= 5 then raise too_many_failures else (if provided_password <> !password then (wrong_pass_cnt := !wrong_pass_cnt + 1; print_int !wrong_pass_cnt; raise wrong_pass) else (wrong_pass_cnt := 0; if money_to_add < 0 then raise negative_amount else money := !money + money_to_add))); retrieve = (fun provided_password money_to_remove -> if !wrong_pass_cnt >= 5 then raise too_many_failures else (if provided_password <> !password then (wrong_pass_cnt := !wrong_pass_cnt + 1; print_int !wrong_pass_cnt; raise wrong_pass) else (wrong_pass_cnt := 0; if money_to_remove < 0 then raise negative_amount else (if !money < money_to_remove then raise not_enough_balance else money := !money - money_to_remove)))); show_balance = (fun provided_password -> if !wrong_pass_cnt >= 5 then raise too_many_failures else (if provided_password <> !password then (wrong_pass_cnt := !wrong_pass_cnt + 1; print_int !wrong_pass_cnt; raise wrong_pass) else (wrong_pass_cnt := 0; !money))); } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let rec neigh_h e v acc = match e with | [] -> acc | (a, b, c)::xs -> if a = v then neigh_h xs v ((b, c)::acc) else neigh_h xs v acc in neigh_h g.edges vertex [];; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) (acc: int) : ('a list * weight) = let v, w = node in if v = b then (v::visited, acc + w) else let neighbs = neighbours g v in let filter_func n = let v, _ = n in not (List.mem v visited) in let filtered_neighbs = List.filter filter_func neighbs in aux_list filtered_neighbs (v::visited) (acc + w) and aux_list (nodes: ('a * weight) list) (visited: 'a list) (acc: int) : ('a list * weight) = match nodes with | [] -> raise Fail | node::nodes -> let _, w = node in try aux_node node visited acc with Fail -> aux_list nodes visited acc in let n_lst, w = aux_node (a, 0) [] 0 in (List.rev n_lst, w);; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= let v, w = node in if v = b then sc ([], 0) else let neighbs = neighbours g v in let filter_func n = let v, _ = n in not (List.mem v visited) in let filtered_neighbs = List.filter filter_func neighbs in aux_list filtered_neighbs (v::visited) fc sc and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | node::nodes_tl -> let v, w = node in let fc2 = (fun () -> aux_list nodes_tl visited fc sc) in let sc2 = (fun (rp, rw) -> sc (v::rp, rw + w)) in aux_node node visited fc2 sc2; in aux_node (a, 0) [] (fun () -> raise Fail) (fun (rp, rw) -> (a::rp, rw));; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec helper (p: ('a list * weight) list) (l: 'a list * weight) : ('a list * weight) = let _, l_w = l in match p with | [] -> l | (path, w)::xs -> if w > l_w then helper xs (path, w) else helper xs l in let p = find_all_paths g a b in let ans = helper p ([], 0) in if ans = ([], 0) then None else Some (ans);; |
let open_account (initial_pass: passwd) : bank_account = let pass = ref initial_pass and mon = ref 0 and acc = ref 0 in { update_pass = (fun input newpass -> if (input = !pass) then ((pass := newpass );acc:=0) else ((acc:= !acc+1) ; raise wrong_pass) ); deposit = (fun input dep -> if ((input = !pass) && !acc<=4) then (if dep >=0 then ((mon:= dep + !mon);acc := 0) else raise negative_amount) else if !acc>4 then raise too_many_failures else ((acc := !acc+1) ;raise wrong_pass)) ; retrieve = (fun input ret -> if ((input = !pass) && !acc<=4) then ( if ret >=0 then (if ret <= !mon then ((mon:= !mon-ret);acc:=0) else raise not_enough_balance) else raise negative_amount) else if !acc >4 then raise too_many_failures else ((acc := !acc +1) ; raise wrong_pass)) ; show_balance = (function input -> if ((input = !pass) && !acc<=4) then ((acc:=0);!mon) else (if !acc>4 then raise too_many_failures else ((acc := !acc +1) ; raise wrong_pass)) ) } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let c = g.edges in let rec f1 c vertex acc = match c with |(x,y,z)::xl -> if x = vertex then f1 xl vertex ((y,z)::acc) else f1 xl vertex acc |[] -> acc in f1 c vertex [];; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec tr l k c : 'a list= match l with | [] -> c k | h :: t -> tr t k (fun r -> c (h::r)) in let rec aux_node (node: 'a * weight) (visited : 'a list) w : ('a list * weight) = if (fst node) = b then (visited,w) else aux_list (neighbours g (fst node)) visited w and aux_list (nodes: ('a * weight) list) (visited: 'a list) w : ('a list * weight) = match nodes with | x::xl -> if List.exists (fun p -> p=(fst x)) visited then aux_list xl visited w else (try aux_node x (visited@[(fst x)]) (w+ (snd x)) with Fail -> aux_list xl visited w) | [] -> raise Fail in aux_list (neighbours g a) (a::[]) 0;; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) w sc fc: ('a list * weight) = if (fst node) = b then sc (visited,w) else aux_list (neighbours g (fst node)) visited w sc fc and aux_list (nodes: ('a * weight) list) (visited: 'a list) w sc fc: ('a list * weight) = match nodes with | x::xl -> if List.exists (fun p -> p=(fst x)) visited then aux_list xl visited w sc fc else aux_node x (visited@[(fst x)]) (w+ (snd x)) sc (fun () -> aux_list xl visited w sc fc) | [] -> fc () in aux_list (neighbours g a) (a::[]) 0 (fun c -> c) (fun () -> raise Fail);; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let ll = find_all_paths g a b in match ll with |[] -> None |_ -> (fun l ma -> match l with |[] -> None |b::[]-> if (snd b) > (snd ma) then Some b else Some ma |a::b -> if (snd a) > (snd ma) then Some a else Some ma ) ll ([],0);; |
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let failures = ref 0 in let balance = ref 0 in let handle_wrong = fun (failed) -> failures := failed + 1; if !failures > 5 then raise too_many_failures else raise wrong_pass in { update_pass = (fun p -> (fun new_p -> if not (p = !password) then (failures := !failures + 1; raise wrong_pass) else password := new_p; failures := 0; )); show_balance = (fun p -> if !failures >= 5 then raise too_many_failures else if not (p = !password) then handle_wrong !failures else failures := 0; !balance ); deposit = (fun p -> (fun put -> if !failures >= 5 then raise too_many_failures else if not (p = !password) then handle_wrong !failures else failures := 0; if put < 0 then raise negative_amount else balance := !balance + put )); retrieve = fun p -> (fun take -> if !failures >= 5 then raise too_many_failures else if not (p = !password) then handle_wrong !failures else failures := 0; if take < 0 then raise negative_amount else if !balance >= take then balance := !balance - take else raise not_enough_balance ) } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let f (acc: ('a * weight) list) (edge: 'a * 'a * weight) = let (here, there, w) = edge in if compare here vertex = 0 then (there, w) :: acc else acc in List.fold_left f [] g.edges;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = let (v, w) = node in if (List.find_opt (fun tmp -> tmp = v) visited) = None then if v = b then (visited @ [v]), 0 else aux_list (neighbours g v) (visited @ [v]) else raise Fail and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | (v, w) :: t -> try let (path, total) = aux_node (v, w) visited in (path, total + w) with Fail -> aux_list t visited in aux_list (neighbours g a) [a];; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= let (v, w) = node in if (List.find_opt (fun tmp -> tmp = v) visited) = None then if v = b then sc [] 0 else aux_list (neighbours g v) (visited @ [v]) fc sc else fc() and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc() | (v, w) :: t -> let new_fc = fun () -> aux_list t visited fc sc in let new_sc = fun path total -> sc (v :: path) (total + w) in aux_node (v, w) visited new_fc new_sc in aux_list (neighbours g a) [a] (fun () -> raise Fail) (fun p w -> ((a :: p), w));; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let paths = find_all_paths g a b in if paths = [] then None else let f = fun (acc_l, acc_w) (l, w) -> if w > acc_w then (l, w) else (acc_l, acc_w) in Some (List.fold_left f ([], 0) paths);; |
let check_auth_exceedance (dat : bank_account_data ref) = let (_, _, auth_fails) = !dat in if auth_fails >= 5 then raise too_many_failures ;; let bank_auth f (dat : bank_account_data ref) (passwd : string) = let (amnt, real_passwd, auth_fails) = !dat in check_auth_exceedance dat; if passwd <> real_passwd then (dat := (amnt, real_passwd, auth_fails+1); raise wrong_pass) else dat := (amnt, real_passwd, 0); f dat ;; let bank_auth_reset f (dat : bank_account_data ref) (passwd : string) = let (amnt, real_passwd, auth_fails) = !dat in if passwd <> real_passwd then (dat := (amnt, real_passwd, auth_fails+1); raise wrong_pass;) else dat := (amnt, real_passwd, 0); f dat ;; let update_pass_base (dat :bank_account_data ref) (new_passwd : string) = let (amnt, _, auth_fails) = !dat in dat := (amnt, new_passwd, auth_fails) ;; let retrieve_base (dat : bank_account_data ref) (amnt : int) = let (base_amnt, real_passwd, auth_fails) = !dat in if amnt < 0 then raise negative_amount; let new_balance = base_amnt - amnt in if new_balance < 0 then raise not_enough_balance; dat := (new_balance, real_passwd, auth_fails); () ;; let deposit_base (dat : bank_account_data ref) (amnt : int) = let (base_amnt, real_passwd, auth_fails) = !dat in if amnt < 0 then raise negative_amount; let new_balance = base_amnt + amnt in dat := (new_balance, real_passwd, auth_fails); () ;; let show_balance_base (dat : bank_account_data ref) = let (amnt, _, _) = !dat in amnt ;; |
let open_account (initial_pass: passwd) : bank_account = let dat: bank_account_data ref = ref (0, initial_pass, 0) in { update_pass = bank_auth_reset update_pass_base dat; retrieve = bank_auth retrieve_base dat; deposit = bank_auth deposit_base dat; show_balance = bank_auth show_balance_base dat; } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let {edges=edges; nodes=nodes} = g in let op (base: ('a * weight) list) ((v1,v2,w) : ('a * 'a * weight)) = if v1 = vertex then (v2, w) :: base else base in List.fold_left (op) [] edges;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let {edges=edges; nodes=nodes} = g in let rec aux_node ((ndata, nweight): 'a * weight) (visited : 'a list) : ('a list * weight) = if List.exists (fun nvisited -> nvisited=ndata) visited then raise Fail; let updated_visited = ndata :: visited in if ndata=b then (updated_visited, nweight) else let candidate_nodes = List.map (fun ((v1,v2,w) : ('a * 'a * weight)) -> (v2, w+nweight)) (List.filter (fun ((v1,v2,w) : ('a * 'a * weight)) -> v1=ndata) edges) in aux_list candidate_nodes updated_visited and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | hd::tl -> try aux_node hd visited with Fail -> aux_list tl visited in let (lst, w) = (aux_node (a, 0) []) in (List.rev lst, w);; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let {edges=edges; nodes=nodes} = g in let rec aux_node ((ndata, nweight): 'a * weight) (visited : 'a list) fc : ('a list * weight) = if List.exists (fun nvisited -> nvisited=ndata) visited then (fc ()) else let updated_visited = ndata :: visited in if ndata=b then (updated_visited, nweight) else let candidate_nodes = List.map (fun ((v1,v2,w) : ('a * 'a * weight)) -> (v2, w+nweight)) (List.filter (fun ((v1,v2,w) : ('a * 'a * weight)) -> v1=ndata) edges) in aux_list candidate_nodes updated_visited fc and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc : ('a list * weight) = match nodes with | [] -> fc () | hd::tl -> aux_node hd visited (fun (s) -> (aux_list tl visited fc)) in let (lst, w) = (aux_node (a, 0) [] (fun (s) -> raise Fail) ) in (List.rev lst, w);; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec aux_find_longest_path (l: ('a list * weight) list) ((c_l, c_weight) : 'a list * weight) : ('a list * weight) = match l with | [] -> (c_l, c_weight) | hd::tl -> let (n_l, n_weight) = hd in if n_weight > c_weight then aux_find_longest_path tl hd else aux_find_longest_path tl (c_l, c_weight) in let paths = find_all_paths g a b in match paths with | [] -> None | _ -> Some(aux_find_longest_path paths ([], -1));; |
let open_account (initial_pass: passwd) : bank_account = let pass = ref initial_pass in let funds = ref 0 in let count = ref 1 in { update_pass = (fun (old_p : passwd) (new_p : passwd) -> match old_p with | old_p when (old_p = !pass) -> pass := new_p ; count := 0 | _ -> count := !count + 1 ; raise wrong_pass ); retrieve = ( fun (p : passwd) (m : int) -> match count with | count when !count >= 5 -> raise too_many_failures | _ -> match p with | p when (not(p = !pass)) -> count := !count + 1 ; raise wrong_pass | _ -> count:=0; match m with | m when ( m < 0) -> raise negative_amount | m when ( m <= (!funds) ) -> funds := !funds - m | _ -> raise not_enough_balance ); deposit = ( fun (p : passwd) (m : int) -> match count with | count when !count >= 5 -> raise too_many_failures | _ -> match p with | p when (not(p = !pass)) -> count := !count + 1 ; raise wrong_pass | _ -> count:=0; match m with | m when m < 0 -> raise negative_amount | _ -> funds := !funds + m ); show_balance = ( fun (p : passwd) -> match count with | count when !count >= 5 -> raise too_many_failures | _ -> match p with | p when p = !pass -> count := 0; !funds | _ -> count := !count + 1 ; raise wrong_pass ) } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let ls = g.edges in let check_append l n = match n with | (v,b,w) when v = vertex -> (b,w) :: l | _ -> l in List.fold_left check_append [] ls;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = match node with | (x,w) when x = b -> ([x],w) | (x,w) -> try let _ = List.find ((=)x) visited in raise Fail with Not_found -> try let (l,weight) = aux_list (neighbours g x) (x :: visited) in (x::l, w+weight) with Fail -> raise Fail and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | x::xs -> try aux_node x visited with Fail -> aux_list xs visited in let (l,w) = aux_list (neighbours g a) [a] in (a::l,w);; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= match node with | (x,w) when x = b -> (fc [x],sc w) | (x,w) -> try let _ = List.find ((=) x) visited in raise Fail with Not_found -> try aux_list (neighbours g x) (x::visited) (fun l -> fc( x:: l)) (fun s -> sc(w+s)) with Fail -> raise Fail and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> raise Fail | x::xs -> try aux_node x visited fc sc with Fail -> aux_list xs visited fc sc in aux_list (neighbours g a) [a] (fun l -> (a::l)) (fun w -> w);; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec helper (pre: ('a list * weight)) (ls : ('a list * weight) list)= let (_,w2) = pre in match ls with | [] -> pre | (l,w)::xs when w > w2 -> helper (l,w) xs | _::xs -> helper pre xs in match find_all_paths g a b with | [] -> None | x::xs -> Some (helper x xs);; |
let open_account (initial_pass: passwd) : bank_account = let incorrect_attempts = ref 0 in let initial_pass = ref initial_pass in let initial_balance = ref 0 in {update_pass = (fun pass1 pass2 -> (if pass1 = !initial_pass then (initial_pass := pass2; incorrect_attempts := 0) else ((incorrect_attempts := (!incorrect_attempts) + 1); raise wrong_pass))); retrieve = (fun pass amount -> (if !incorrect_attempts >= 5 then raise too_many_failures else (if pass = !initial_pass then (incorrect_attempts := 0; (if amount > !initial_balance then raise not_enough_balance else if amount < 0 then raise negative_amount else initial_balance := !initial_balance - amount)) else ((incorrect_attempts := (!incorrect_attempts) +1); raise wrong_pass)))); deposit = (fun pass amount -> (if !incorrect_attempts >= 5 then raise too_many_failures else (if pass = !initial_pass then (incorrect_attempts := 0; (if amount > 0 then(initial_balance := (!initial_balance) + amount) else raise negative_amount)) else ((incorrect_attempts := (!incorrect_attempts) +1); raise wrong_pass)))); show_balance = (fun pass -> (if !incorrect_attempts >= 5 then raise too_many_failures else (if pass = !initial_pass then ( incorrect_attempts := 0; !initial_balance;) else (incorrect_attempts := (!incorrect_attempts + 1); raise wrong_pass)))); } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.map (fun x -> let (a,b,c) = x in (b,c)) (List.filter (fun x -> let (a,b,c) = x in a = vertex) g.edges);; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) (w : weight): ('a list * weight) = let (x,y) = node in if x = b then (visited@[x], y+w) else if not(List.mem x visited) then (aux_list (neighbours g x) (visited@[x]) (w+y)) else raise Fail and aux_list (nodes: ('a * weight) list) (visited: 'a list) (w : weight): ('a list * weight) = match nodes with |[] -> raise Fail |fst::rest -> try (aux_node fst visited w) with Fail -> aux_list rest visited w in aux_list (neighbours g a) [a] 0;; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc (w : weight): ('a list * weight)= let (x,y) = node in if x = b then sc (visited@[x], y+w) else if not(List.mem x visited) then (aux_list (neighbours g x) (visited@[x]) fc sc (w+y)) else fc() and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc (w : weight): ('a list * weight) = match nodes with |[] -> fc() |fst::rest -> try (aux_node fst visited fc sc w) with Fail -> aux_list rest visited fc sc w in aux_list (neighbours g a) [a] (fun() -> raise Fail) (fun y -> y) 0;; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec helper paths acc = match paths with |[] -> acc |hd::tl -> let (a,b) = hd in helper tl (b::acc) in let rec helper2 paths acc = match paths with |[] -> None |hd::tl -> let (a,b) = hd in if List.for_all (fun x -> x < b+1) acc then Some hd else helper2 tl acc in helper2 (find_all_paths g a b) (helper (find_all_paths g a b) []);; |
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let bal = ref 0 in let attempts = ref 0 in { update_pass = (fun old_pw new_pw -> if old_pw = !password then attempts := 0 else attempts := !attempts + 1 ; if old_pw = !password then password := new_pw else raise wrong_pass ); deposit= ( fun old_pw amount -> if !attempts >= 5 then raise too_many_failures else (if old_pw = !password then attempts := 0 else attempts := !attempts + 1 ;if old_pw = !password then if amount < 0 then raise negative_amount else bal := !bal + amount else raise wrong_pass) ); retrieve= (fun old_pw amount -> if !attempts >= 5 then raise too_many_failures else (if old_pw = !password then attempts := 0 else attempts := !attempts + 1; if old_pw = !password then if amount > !bal then raise not_enough_balance else if amount < 0 then raise negative_amount else bal := !bal - amount else raise wrong_pass) ); show_balance = (fun old_pw -> if !attempts >= 5 then raise too_many_failures else (if old_pw = !password then attempts := 0 else attempts := !attempts + 1 ; if old_pw = !password then !bal else raise wrong_pass) ) } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_right(fun x y -> let (a,b,c) = x in if a = vertex then y@[b,c] else y) g.edges [] ;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let start = neighbours g a in let rec aux_node (poids : weight) (node: 'a * weight) (visited : 'a list) : ('a list * weight) = if not( List.exists(fun a -> a=(fst(node))) visited) then let visited = (fst(node))::visited in let poids = poids + (snd(node))in if (fst(node)) = b then (List.rev visited, poids) else let cur_n = neighbours g (fst(node)) in aux_list poids cur_n visited else raise Fail and aux_list (poids : weight) (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with |[]->raise Fail |(x,y)::tl -> try aux_node poids (x,y) visited with Fail -> aux_list poids tl visited in aux_list 0 start [a] ;; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc wc : ('a list * weight)= let (x,y) = node in if not( List.mem x visited) then if (x = b) then (List.rev (x::visited), wc y) else aux_list (neighbours g (fst(node))) (x::visited) fc (fun w -> wc(w +y)) else fc () and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc wc : ('a list * weight) = match nodes with |[]-> fc () |(x,y)::tl -> let wc2 = wc in let fc2 = fun () -> aux_list tl visited fc wc2 in aux_node (x,y) (visited) fc2 wc2 in aux_list (neighbours g a) [a] (fun () -> raise Fail) (fun w->w) ;; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let f = find_all_paths g a b in if f = [] then None else let rec compare (w :weight) (p :'a list) (lst : ('a list * weight) list ) : ('a list * weight) option= match lst with | [] -> Some(p,w) |(x,y)::tl -> if y > w then let p = x in let w = y in compare w p tl else compare w p tl in compare 0 [] f ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let rec neigh_aux l v = match l with | (a,b,w)::t -> if v = a then (b,w)::neigh_aux t v else neigh_aux t v | [] -> [] in neigh_aux g.edges vertex;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = let (city, weight) = node in if (city = b) then ([city],weight) else if ( (List.mem city visited) ) then raise Fail else let (path,cost) = (aux_list (neighbours g city) (city::visited)) in (city::path, cost + weight) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with |[] -> raise Fail |(city, weight)::t -> try aux_node (city, weight) (visited) with Fail -> aux_list t (visited) in aux_node (a,0) [] let fcity city weight k = let (c,w) = k in ((city::c), w + weight) ;; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fail succeed: ('a list * weight)= let (city, weight) = node in if (city = b) then succeed ([city],weight) else if ( (List.mem city visited) ) then fail () else let succeed2 = ((fun (z,k) -> succeed(city::z, k + weight))) in aux_list (neighbours g city) (city::visited) fail succeed2 and aux_list (nodes: ('a * weight) list) (visited: 'a list) fail succeed : ('a list * weight) = match nodes with |[] -> fail () |(city, weight)::t -> let fail2 = (fun () -> aux_list t (visited) fail succeed) in let succeed2 = succeed in aux_node (city, weight) (visited) fail2 succeed2 in aux_node (a,0) [] (fun () -> raise Fail) (fun (path,cost) -> (path,cost)) let fcity city weight k = let (c,w) = k in ((city::c), w + weight) ;; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let l = (find_all_paths g a b) in match l with | [] -> None | g::[] -> Some g | g::gs -> Some ( let rec long l (x,y) = match l with |[] -> (x,y) |(p,w)::[] -> if (w > y) then (p,w) else (x,y) |(p,w)::g -> if (w > y) then long g (p,w) else long g (x,y) in long l ([], 0) );; |
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let balance = ref 0 in let numOfTrial = ref 0 in { update_pass = (fun (old_passwd:passwd) (new_passwd:passwd) -> if ( old_passwd <> (!password)) then( numOfTrial := !numOfTrial + 1; raise wrong_pass ) else (password := new_passwd; numOfTrial := 0 ) ); retrieve= (fun (pwd:passwd) money -> if ( !numOfTrial >= 5) then raise too_many_failures else if ( !password <> pwd ) then ( numOfTrial := !numOfTrial + 1; raise wrong_pass) else match money with | m when m< 0 -> raise negative_amount | m when m> !balance -> raise not_enough_balance | _ -> balance := !balance-money; numOfTrial := 0 ); deposit = (fun (pwd:passwd) money -> if(!numOfTrial >= 5) then raise too_many_failures else if ( !password <> pwd ) then ( numOfTrial := !numOfTrial + 1; raise wrong_pass) else if ( money < 0 ) then raise negative_amount else (balance := !balance + money; numOfTrial := 0) ); show_balance = (fun pwd -> if(!numOfTrial >= 5) then raise too_many_failures else if ( !password <> pwd ) then ( numOfTrial := !numOfTrial + 1; raise wrong_pass) else ( numOfTrial := 0; !balance )); } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let filter result (e: ('a * 'a * weight))= match e with | (v,n,w) when (v = vertex)-> ((n,w)::result) | _ -> result in List.fold_left filter [] g.edges ;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = let vertex=(fst node) and vertex_weight=(snd node) in if (List.exists ((=) vertex) visited) then raise Fail else if ( vertex = b) then ( [b],vertex_weight ) else let path=(aux_list (neighbours g vertex) (vertex::visited)) in (vertex::(fst path),vertex_weight+(snd path)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | x::xs -> try (aux_node x visited) with | Fail -> (aux_list xs visited) | _ -> ( aux_node x visited) in aux_node (a,0) [] ;; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight)= let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight) = let vertex=(fst node) and vertex_weight=(snd node) in if (List.exists ((=) vertex) visited) then raise Fail else if ( vertex = b) then ( fc [b], sc vertex_weight ) else aux_list (neighbours g vertex) (vertex::visited) (fun r->(fc (vertex::r))) (fun n -> (sc (n+vertex_weight))) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> raise Fail | x::xs -> try (aux_node x visited fc sc) with | Fail -> (aux_list xs visited fc sc) | _ -> (aux_node x visited (fun r->r) (fun n -> n)) in (aux_node (a,0) [] (fun r -> r) (fun n -> n)) ;; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let paths = (find_all_paths g a b) in match paths with |[] -> None |_ -> let rec helper list = match list with |[] -> raise Fail |x::xs -> if List.for_all (fun r-> (snd r) <= snd x) paths then (Some x) else helper xs in helper paths ;; |
let open_account (initial_pass: passwd) : bank_account = let pd= ref initial_pass in let balance = ref 0 in let wrong= ref 0 in { update_pass =( fun a b -> if !pd = a then (let _ = (wrong:=0) in pd:=b) else (let _ = (incr wrong) in raise wrong_pass; ) ) ; deposit= (fun a b -> if !wrong>=5 then raise too_many_failures else if !pd = a then (let _ = (wrong:=0) in if b<0 then raise negative_amount else balance:= !balance + b) else (let _ = (incr wrong) in raise wrong_pass; ) ); retrieve=( fun a b -> if !wrong>=5 then raise too_many_failures else if !pd = a then (let _ = (wrong:=0) in if b<0 then raise negative_amount else if b<= !balance then balance:= !balance -b else raise not_enough_balance) else (let _ = (incr wrong) in raise wrong_pass; ) ); show_balance= (fun a -> if !wrong>=5 then raise too_many_failures else if !pd = a then (let _ = (wrong:=0) in !balance) else (let _ = (incr wrong) in raise wrong_pass; ) ); };; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let edge list (v1, v2, w ) = if v1 = vertex then (v2,w) :: list else list in List.fold_left edge [] g.edges;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = notimplemented () and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = notimplemented () in notimplemented ();; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= notimplemented () and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = notimplemented () in notimplemented ();; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = notimplemented ();; |
let open_account (initial_pass: passwd) : bank_account = let balance = ref 0 in let tryCount = ref 0 in let password = ref initial_pass in let update_pass (current_pass : passwd) (new_pass : passwd) : unit = if current_pass = !password then password := new_pass else ( tryCount := !tryCount + 1 ; raise wrong_pass; ) in let retrieve (current_pass : passwd) (amount : int) : unit = if !tryCount < 5 then if current_pass = !password then ( tryCount := 0; if amount > !balance then raise not_enough_balance else if amount < 0 then raise negative_amount else balance := !balance - amount ) else( tryCount := !tryCount + 1 ; raise wrong_pass; ) else raise too_many_failures ; in let deposit (current_pass : passwd) (amount : int) : unit = if !tryCount < 5 then if current_pass = !password then ( tryCount := 0; if amount < 0 then raise negative_amount else balance := !balance + amount ) else( tryCount := !tryCount + 1 ; raise wrong_pass; ) else raise too_many_failures ; in let show_balance (current_pass : passwd) : int = if !tryCount < 5 then if current_pass = !password then ( tryCount := 0; !balance ) else( tryCount := !tryCount + 1 ; raise wrong_pass; ) else raise too_many_failures ; in { update_pass = update_pass; retrieve = retrieve; deposit = deposit; show_balance = show_balance; } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = notimplemented ();; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = notimplemented () and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = notimplemented () in notimplemented ();; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= notimplemented () and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = notimplemented () in notimplemented ();; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = notimplemented ();; |
let rec unused_vars = function | Var _ | I _ | B _ -> [] | If (e, e1, e2) -> unused_vars e @ unused_vars e1 @ unused_vars e2 | Primop (_, args) -> List.fold_left (fun acc exp -> acc @ unused_vars exp) [] args | Let (x, e1, e2) -> let unused = unused_vars e1 @ unused_vars e2 in if List.mem x (free_variables e2) then unused else x :: unused | Rec (x, _, e) -> let unused = unused_vars e in if List.mem x (free_variables e) then unused else x :: unused | Fn (xs, e) -> let unused = unused_vars e in let inputVars = List.map fst xs in (delete (free_variables e) inputVars) @ unused | Apply (e, es) -> let unusedEs = List.fold_left (fun acc exp -> acc @ unused_vars exp) [] es in (unused_vars e) @ unusedEs ;; |
let rec subst ((e', x) as s) exp = match exp with | Var y -> if x = y then e' else Var y | I n -> I n | B b -> B b | Primop (po, args) -> Primop (po, List.map (subst s) args) | If (e, e1, e2) -> If (subst s e, subst s e1, subst s e2) | Let (y, e1, e2) -> let e1' = subst s e1 in if y = x then Let (y, e1', e2) else let (y, e2) = if List.mem y (free_variables e') then rename y e2 else (y, e2) in Let (y, e1', subst s e2) | Rec (y, t, e) -> if (x = y) then Rec (y, t, e) else if List.mem y (free_variables e') then let (y', e') = rename y e in Rec (y', t, subst s e') else Rec (y, t, subst s e) | Fn (xs, e2) -> let myRename_all (nt:(name * tp) list) expR = List.fold_right (fun (name, ty) (nt, expR) -> let (name', expR') = rename name expR in ((name',ty) :: nt, expR')) nt ([], expR) in let vn = List.map fst xs in if (List.mem x vn) then Fn (xs, e2) else let va = List.filter (fun r -> (fst r <> x)) xs in let freeE' = (free_variables e') in let vaNot = List.filter (fun r -> ((not) (List.mem (fst r) freeE'))) va in let vaYes = List.filter (fun r -> (List.mem (fst r) freeE')) va in let (renamed, e2') = myRename_all vaYes e2 in let xs' = renamed @ vaNot in Fn (xs', subst (e',x) e2') | Apply (e, es) -> Apply (subst s e, List.map (subst s) es) and rename x e = let x' = freshVar x in (x', subst (Var x', x) e) and rename_all names exp = List.fold_right (fun name (names, exp) -> let (name', exp') = rename name exp in (name' :: names, exp')) names ([], exp) ;; let subst_list subs exp = List.fold_left (fun exp sub -> subst sub exp) exp subs |
let rec eval exp = match exp with | I _ -> exp | B _ -> exp | Fn _ -> exp | Var x -> raise (Stuck (Free_variable x)) | Primop (po, args) -> let args = List.map eval args in begin match eval_op po args with | None -> raise (Stuck Bad_primop_args) | Some v -> v end | If (e, e1, e2) -> begin match eval e with | B true -> eval e1 | B false -> eval e2 | _ -> raise (Stuck If_non_true_false) end | Let (x, e1, e2) -> let e1 = eval e1 in eval (subst (e1, x) e2) | Rec (f, _, e) -> eval (subst (exp, f) e) | Apply (e, es) -> let fxn = eval e in match fxn with | Fn (nt, body ) -> if ( (List.length nt) = (List.length es) ) then let argNames = List.map fst nt in let inputs = List.map eval es in let subs = List.combine inputs argNames in eval ( subst_list subs body ) else raise (Stuck Arity_mismatch) | _ -> raise (Stuck Apply_non_fn) |
let rec infer ctx e = match e with | Var x -> begin try lookup x ctx with Not_found -> raise (TypeError (Free_variable x)) end | I _ -> Int | B _ -> Bool | Primop (po, exps) -> let (domain, range) = primopType po in check ctx exps domain range | If (e, e1, e2) -> begin match infer ctx e with | Bool -> let t1 = infer ctx e1 in let t2 = infer ctx e2 in if t1 = t2 then t1 else type_mismatch t1 t2 | t -> type_mismatch Bool t end | Let (x, e1, e2) -> let t1 = infer ctx e1 in infer (extend ctx (x, t1)) e2 | Rec (f, t, e) -> let ctx' = extend ctx (f, t) in let t' = infer ctx' e in if (t'= t) then t else type_mismatch t t' | Fn (xs, e) -> let ctx' = extend_list ctx xs in let outType = infer ctx' e in Arrow (List.map snd ctx', outType) | Apply (e, es) -> match infer ctx e with | Arrow (domain, range) -> check ctx es domain range | aType -> raise (TypeError (Apply_non_arrow aType)) and check ctx exps tps result = match exps, tps with | [], [] -> result | e :: es, t :: ts -> let t' = infer ctx e in if t = t' then check ctx es ts result else type_mismatch t t' | _ -> raise (TypeError Arity_mismatch) |
let unify : utp -> utp -> utp UTVarMap.t = let rec unify (substitution : utp UTVarMap.t) (t1 : utp) (t2 : utp) : utp UTVarMap.t = match t1, t2 with | UInt, UInt | UBool, UBool -> substitution | UTVar a, UTVar a' when a = a' -> substitution | UArrow (t1, t1'), UArrow (t2, t2') -> raise NotImplemented | UTVar a, _ -> unifyVar substitution a t2 | _, UTVar b -> unifyVar substitution b t1 | _, _ -> unif_error @@ UnifMismatch (t1, t2) and unifyVar (substitution : utp UTVarMap.t) (a : string) (t : utp) : utp UTVarMap.t = let rec occurs : utp -> bool = function | UInt | UBool -> false | UArrow (t1, t2) -> occurs t1 || occurs t2 | UTVar b -> if a = b then true else match UTVarMap.find_opt b substitution with | None -> false | Some t' -> occurs t' in raise NotImplemented in fun t1 t2 -> unify UTVarMap.empty t1 t2 |
let rec unused_vars = function | Var _ | I _ | B _ -> [] | If (e, e1, e2) -> unused_vars e @ unused_vars e1 @ unused_vars e2 | Primop (_, args) -> List.fold_left (fun acc exp -> acc @ unused_vars exp) [] args | Let (x, e1, e2) -> let unused = unused_vars e1 @ unused_vars e2 in if List.mem x (free_variables e2) then unused else x :: unused | Rec (x, _, e) -> let unused = unused_vars e in if List.mem x (free_variables e) then unused else x :: unused | Fn (xs, e) -> let unused = unused_vars e in let inputVars = List.map fst xs in (delete (free_variables e) inputVars) @ unused | Apply (e, es) -> let unusedEs = List.fold_left (fun acc exp -> acc @ unused_vars exp) [] es in (unused_vars e) @ unusedEs ;; |
let rec subst ((e', x) as s) exp = match exp with | Var y -> if x = y then e' else Var y | I n -> I n | B b -> B b | Primop (po, args) -> Primop (po, List.map (subst s) args) | If (e, e1, e2) -> If (subst s e, subst s e1, subst s e2) | Let (y, e1, e2) -> let e1' = subst s e1 in if y = x then Let (y, e1', e2) else let (y, e2) = if List.mem y (free_variables e') then rename y e2 else (y, e2) in Let (y, e1', subst s e2) | Rec (y, t, e) -> if (x = y) then Rec (y, t, e) else if List.mem y (free_variables e') then let (y', e') = rename y e in Rec (y', t, subst s e') else Rec (y, t, subst s e) | Fn (xs, e2) -> let myRename_all (nt:(name * tp) list) expR = List.fold_right (fun (name, ty) (nt, expR) -> let (name', expR') = rename name expR in ((name',ty) :: nt, expR')) nt ([], expR) in let vn = List.map fst xs in if (List.mem x vn) then Fn (xs, e2) else let va = List.filter (fun r -> (fst r <> x)) xs in let freeE' = (free_variables e') in let vaNot = List.filter (fun r -> ((not) (List.mem (fst r) freeE'))) va in let vaYes = List.filter (fun r -> (List.mem (fst r) freeE')) va in let (renamed, e2') = myRename_all vaYes e2 in let xs' = renamed @ vaNot in Fn (xs', subst (e',x) e2') | Apply (e, es) -> Apply (subst s e, List.map (subst s) es) and rename x e = let x' = freshVar x in (x', subst (Var x', x) e) and rename_all names exp = List.fold_right (fun name (names, exp) -> let (name', exp') = rename name exp in (name' :: names, exp')) names ([], exp) ;; let subst_list subs exp = List.fold_left (fun exp sub -> subst sub exp) exp subs |
let rec eval exp = match exp with | I _ -> exp | B _ -> exp | Fn _ -> exp | Var x -> raise (Stuck (Free_variable x)) | Primop (po, args) -> let args = List.map eval args in begin match eval_op po args with | None -> raise (Stuck Bad_primop_args) | Some v -> v end | If (e, e1, e2) -> begin match eval e with | B true -> eval e1 | B false -> eval e2 | _ -> raise (Stuck If_non_true_false) end | Let (x, e1, e2) -> let e1 = eval e1 in eval (subst (e1, x) e2) | Rec (f, _, e) -> eval (subst (exp, f) e) | Apply (e, es) -> let fxn = eval e in match fxn with | Fn (nt, body ) -> if ( (List.length nt) = (List.length es) ) then let argNames = List.map fst nt in let inputs = List.map eval es in let subs = List.combine inputs argNames in eval ( subst_list subs body ) else raise (Stuck Arity_mismatch) | _ -> raise (Stuck Apply_non_fn) let d1 = Fn ( [("x", Int) ; ("y", Int)], Primop(Plus, [ Var "x"; Var "y"]) ) ;; let d4 = Rec ("f", Arrow ( [Int], Int ), Fn ( [("n", Int)], ( If ( Primop(Equals, [Var "n"; I 0]), I 1, Primop(Times, [Var "n"; Apply ( Var "f" , [ Primop( Minus, [Var "n"; I 1])])]))))) ;; |
let rec infer ctx e = match e with | Var x -> begin try lookup x ctx with Not_found -> raise (TypeError (Free_variable x)) end | I _ -> Int | B _ -> Bool | Primop (po, exps) -> let (domain, range) = primopType po in check ctx exps domain range | If (e, e1, e2) -> begin match infer ctx e with | Bool -> let t1 = infer ctx e1 in let t2 = infer ctx e2 in if t1 = t2 then t1 else type_mismatch t1 t2 | t -> type_mismatch Bool t end | Let (x, e1, e2) -> let t1 = infer ctx e1 in infer (extend ctx (x, t1)) e2 | Rec (f, t, e) -> let ctx' = extend ctx (f, t) in let t' = infer ctx' e in if (t'= t) then t else type_mismatch t t' | Fn (xs, e) -> let ctx' = extend_list ctx xs in let outType = infer ctx' e in Arrow (List.map snd xs, outType) | Apply (e, es) -> match infer ctx e with | Arrow (domain, range) -> check ctx es domain range | aType -> raise (TypeError (Apply_non_arrow aType)) and check ctx exps tps result = match exps, tps with | [], [] -> result | e :: es, t :: ts -> let t' = infer ctx e in if t = t' then check ctx es ts result else type_mismatch t t' | _ -> raise (TypeError Arity_mismatch) |
let unify : utp -> utp -> utp UTVarMap.t = let rec unify (substitution : utp UTVarMap.t) (t1 : utp) (t2 : utp) : utp UTVarMap.t = match t1, t2 with | UInt, UInt | UBool, UBool -> substitution | UTVar a, UTVar a' when a = a' -> substitution | UArrow (t1, t1'), UArrow (t2, t2') -> raise NotImplemented | UTVar a, _ -> unifyVar substitution a t2 | _, UTVar b -> unifyVar substitution b t1 | _, _ -> unif_error @@ UnifMismatch (t1, t2) and unifyVar (substitution : utp UTVarMap.t) (a : string) (t : utp) : utp UTVarMap.t = let rec occurs : utp -> bool = function | UInt | UBool -> false | UArrow (t1, t2) -> occurs t1 || occurs t2 | UTVar b -> if a = b then true else match UTVarMap.find_opt b substitution with | None -> false | Some t' -> occurs t' in raise NotImplemented in fun t1 t2 -> unify UTVarMap.empty t1 t2 |
let rec unused_vars = function | Var _ | I _ | B _ -> [] | If (e, e1, e2) -> unused_vars e @ unused_vars e1 @ unused_vars e2 | Primop (_, args) -> List.fold_left (fun acc exp -> acc @ unused_vars exp) [] args | Let (x, e1, e2) -> let unused = unused_vars e1 @ unused_vars e2 in if List.mem x (free_variables e2) then unused else x :: unused | Rec (x, _, e) -> let unused = unused_vars e in if List.mem x (free_variables e) then unused else x :: unused | Fn (xs, e) -> let unused = unused_vars e in let inputVars = List.map fst xs in (delete (free_variables e) inputVars) @ unused | Apply (e, es) -> let unusedEs = List.fold_left (fun acc exp -> acc @ unused_vars exp) [] es in (unused_vars e) @ unusedEs ;; |
let rec subst ((e', x) as s) exp = match exp with | Var y -> if x = y then e' else Var y | I n -> I n | B b -> B b | Primop (po, args) -> Primop (po, List.map (subst s) args) | If (e, e1, e2) -> If (subst s e, subst s e1, subst s e2) | Let (y, e1, e2) -> let e1' = subst s e1 in if y = x then Let (y, e1', e2) else let (y, e2) = if List.mem y (free_variables e') then rename y e2 else (y, e2) in Let (y, e1', subst s e2) | Rec (y, t, e) -> if (x = y) then Rec (y, t, e) else if List.mem y (free_variables e') then let (y', e') = rename y e in Rec (y', t, subst s e') else Rec (y, t, subst s e) | Fn (xs, e2) -> let myRename_all (nt:(name * tp) list) expR = List.fold_right (fun (name, ty) (nt, expR) -> let (name', expR') = rename name expR in ((name',ty) :: nt, expR')) nt ([], expR) in let vn = List.map fst xs in if (List.mem x vn) then Fn (xs, e2) else let va = List.filter (fun r -> (fst r <> x)) xs in let freeE' = (free_variables e') in let vaNot = List.filter (fun r -> ((not) (List.mem (fst r) freeE'))) va in let vaYes = List.filter (fun r -> (List.mem (fst r) freeE')) va in let (renamed, e2') = myRename_all vaYes e2 in let xs' = renamed @ vaNot in Fn (xs', subst (e',x) e2') | Apply (e, es) -> Apply (subst s e, List.map (subst s) es) and rename x e = let x' = freshVar x in (x', subst (Var x', x) e) and rename_all names exp = List.fold_right (fun name (names, exp) -> let (name', exp') = rename name exp in (name' :: names, exp')) names ([], exp) ;; let subst_list subs exp = List.fold_left (fun exp sub -> subst sub exp) exp subs |
let rec eval exp = match exp with | I _ -> exp | B _ -> exp | Fn _ -> exp | Var x -> raise (Stuck (Free_variable x)) | Primop (po, args) -> let args = List.map eval args in begin match eval_op po args with | None -> raise (Stuck Bad_primop_args) | Some v -> v end | If (e, e1, e2) -> begin match eval e with | B true -> eval e1 | B false -> eval e2 | _ -> raise (Stuck If_non_true_false) end | Let (x, e1, e2) -> let e1 = eval e1 in eval (subst (e1, x) e2) | Rec (f, _, e) -> eval (subst (exp, f) e) | Apply (e, es) -> let fxn = eval e in match fxn with | Fn (nt, body ) -> if ( (List.length nt) = (List.length es) ) then let argNames = List.map fst nt in let inputs = List.map eval es in let subs = List.combine inputs argNames in eval ( subst_list subs body ) else raise (Stuck Arity_mismatch) | _ -> raise (Stuck Apply_non_fn) let d1 = Fn ( [("x", Int) ; ("y", Int)], Primop(Plus, [ Var "x"; Var "y"]) ) ;; let d4 = Rec ("f", Arrow ( [Int], Int ), Fn ( [("n", Int)], ( If ( Primop(Equals, [Var "n"; I 0]), I 1, Primop(Times, [Var "n"; Apply ( Var "f" , [ Primop( Minus, [Var "n"; I 1])])]))))) ;; let d0 = Fn ( [("x", Int)], Primop(LessThan, [ Var "x"; I 3]) ) ;; let d0b = Fn ( [("x", Int)], Primop(LessThan, [ Var "x"; Var "z"]) ) ;; let d0c = Fn ( [], Primop(LessThan, [ Var "x"; Var "z"]) ) ;; let d5 = Apply ( d0c, [] );; let d6 = Apply ( d0, [(I 4)] ) ;; |
let rec infer ctx e = match e with | Var x -> begin try lookup x ctx with Not_found -> raise (TypeError (Free_variable x)) end | I _ -> Int | B _ -> Bool | Primop (po, exps) -> let (domain, range) = primopType po in check ctx exps domain range | If (e, e1, e2) -> begin match infer ctx e with | Bool -> let t1 = infer ctx e1 in let t2 = infer ctx e2 in if t1 = t2 then t1 else type_mismatch t1 t2 | t -> type_mismatch Bool t end | Let (x, e1, e2) -> let t1 = infer ctx e1 in infer (extend ctx (x, t1)) e2 | Rec (f, t, e) -> let ctx' = extend ctx (f, t) in let t' = infer ctx' e in if (t'= t) then t else type_mismatch t t' | Fn (xs, e) -> let ctx' = extend_list ctx xs in let outType = infer ctx' e in Arrow (List.map snd xs, outType) | Apply (e, es) -> match infer ctx e with | Arrow (domain, range) -> check ctx es domain range | aType -> raise (TypeError (Apply_non_arrow aType)) and check ctx exps tps result = match exps, tps with | [], [] -> result | e :: es, t :: ts -> let t' = infer ctx e in if t = t' then check ctx es ts result else type_mismatch t t' | _ -> raise (TypeError Arity_mismatch) |
let unify : utp -> utp -> utp UTVarMap.t = let rec unify (substitution : utp UTVarMap.t) (t1 : utp) (t2 : utp) : utp UTVarMap.t = match t1, t2 with | UInt, UInt | UBool, UBool -> substitution | UTVar a, UTVar a' when a = a' -> substitution | UArrow (t1, t1'), UArrow (t2, t2') -> raise NotImplemented | UTVar a, _ -> unifyVar substitution a t2 | _, UTVar b -> unifyVar substitution b t1 | _, _ -> unif_error @@ UnifMismatch (t1, t2) and unifyVar (substitution : utp UTVarMap.t) (a : string) (t : utp) : utp UTVarMap.t = let rec occurs : utp -> bool = function | UInt | UBool -> false | UArrow (t1, t2) -> occurs t1 || occurs t2 | UTVar b -> if a = b then true else match UTVarMap.find_opt b substitution with | None -> false | Some t' -> occurs t' in raise NotImplemented in fun t1 t2 -> unify UTVarMap.empty t1 t2 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.