Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello, I\'m having difficulty solving this error in f#, it is a most general uni

ID: 3679679 • Letter: H

Question

Hello, I'm having difficulty solving this error in f#, it is a most general unifier in code.

1) In function unify, I have to put in a tuple of type (char*typExp) into (type substitution = (char*typExp) list) and I tried using @, ::, substitution.list, etc... all I can think of but it does not let me add the tuples.

2) I don't know how to start for the applySubst function. How should I approach on coding this function?

code:

type typExp = TypInt | TypVar of char | Arrow of typExp * typExp | Lst of typExp

type substitution = (char*typExp) list

(* check if a variable occurs in a term *)
let rec occurCheck (v:char) (tau:typExp) : bool =
match tau with
| TypVar a -> if v.Equals(a) then true else false
| Lst a -> occurCheck v a
| Arrow (a, b) -> (occurCheck v a) || (occurCheck v b)
| _ -> false

(* substitute typExp tau1 for all occurrences of type variable v in typExp tau2 *)
let rec substitute (tau1:typExp) (v:char) (tau2:typExp) : typExp =
match occurCheck v tau2 with
| true -> tau1
| false -> tau2
  
let applySubst (sigma:substitution) (tau:typExp) : typExp =failwith "Error - not implemented"
(* This is a one-line program *)

let rec helper (v:char) (list:substitution) = //helper for unify.
match list with
| [] -> TypVar v
| (a, b)::xs -> if a.Equals(v) then b
else helper v xs

let rec unify (tau1:typExp) (tau2:typExp) : substitution =
match tau1 with
| Arrow (a, b) -> match tau2 with
| Arrow (c, d) -> (unify a c)@(unify b d)@list //here
| Lst a -> failwith "Clash in principal type constructor"
| TypVar c -> if occurCheck c tau1 then failwith "Failed occurs check"
else (c, Arrow (a, b))::list //here
| _ -> []@list /here
| Lst a -> unify tau1 a
| TypVar a -> match tau2 with
| Arrow (c, d) -> if occurCheck a tau1 then failwith "Failed occurs check"
else (a, Arrow (c, d))::list //here
| Lst b -> unify (TypVar a) b //here
| TypVar b -> if a.Equals(b) then []@list else (a, helper b list)::list //here
| TypInt -> (a, TypInt)::list //here
| TypInt -> match tau2 with
| TypInt -> []@list //here
| TypVar a -> []@list //here
| _ -> failwith "Not unifiable"

(*

Examples

> let te3 = Arrow (TypVar 'a',Arrow (TypVar 'b', TypVar 'c'));;
val te3 : typExp = Arrow (TypVar 'a' ,Arrow (TypVar 'b', TypVar 'c')

> let te4 = Arrow (TypInt, Arrow(TypVar 'c', TypVar 'a'));;
val te4 : typExp = Arrow (TypInt, Arrow (TypVar 'c', TypVar 'a'))

> unify te3 te4;;
val it : substitution = [('c', TypInt); ('b', TypVar 'c'); ('a', TypInt)]

> let result = it;;
val result : substitution = [('c', TypInt); ('b', TypVar 'c'); ('a', TypInt)]

> applySubst result te3;;
val it : typExp = Arrow (TypInt, Arrow (TypInt, TypInt))
> applySubst result te4;;
val it : typExp = Arrow (TypInt, Arrow (TypInt, TypInt))
*)

Thank you.

Explanation / Answer

// like C# = C SHARP, F# = F SHARP
// Visual Studio 2015 onwards includes f# ( = f sharp ) along with Visual basic and c# in the dot net framework
// I am a single line comment
(* we are multiline comments
line 1
line 2
etc
*)
//
// function unify

// like C# = C SHARP, F# = F SHARP
// Visual Studio 2015 onwards includes f# ( = f sharp ) along with Visual basic and c# in the dot net framework
// I am a single line comment
(* we are multiline comments
line 1
line 2
etc
*)
//
// function unify

tuple of type char *typExp

let unifyList = [5; 97; 67]
let unifyFunction =
   let unifyList2 = [91; 72; 39]
   let unifyList3 = [2,76]

let record unify (tau1.typExpression) tau2.typicalExpression): substitution = match tau1 with | Arrow (a, b) -> match tau2 with | matchings ... | List a -> unifyFunction tau1 a | Typical Variable a-> match tau2 with | matchings ..... | TypicalInteger -> match tau2 with | matchings.....

type typeExpression =
   | TypeInteger
   | TypeVariable of character
   | Arrow of typeExpression * typeExpression
   | List of typeExpression
let te5 = Arrow(TypeInteger, Arrow(TypeVariable 'p', List (TypeVariable 'q')));;
val te5 : typeExpression = Arrow (TypicalInteger, Arrow(TypeVariable 'p', List (TypeVariable 'q')))

let te6 = Arrow(TypeVariable 'p', Arrow (TypeInteger, List TypeInteger ));;
val te6 : typeExpression = Arrow (TypicalInteger, Arrow(TypeVariable 'p', List (TypeVariable 'q')))

let typeUnifier1 = unify te5 te6;;
val typeUnifier1 : substitution = [ ('q', TypeInteger); ('p', TypeInteger)]

applySubstitution typeUnifier te5;;
val val1 : typeExpression = Arrow(TypeInteger, Arrow (TypeInteger, List TypeInteger))

applySubstitution typeUnifier te6;;
val val1 : typeExpression = Arrow(TypeInteger, Arrow (TypeInteger, List TypeInteger))

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote