The return type of a method is not consistent2019 Community Moderator ElectionAbusing the algebra of algebraic data types - why does this work?How to write a pattern match in Ocaml so it is easy to scale?OCaml: Effective Path to GUI Programming?Typing in ocaml methodsRepresent Python-style namespaces using Ocaml data structuresWhy does this type error keep occurring?Possible OCaml code generation bugUsing camlidl for interface with opaque structWhy aren't Haskell variables polymorphic when bound by pattern matching?problem with how we define tuples as the funtion input in OCaml
Is there a symmetric-key algorithm which we can use for creating a signature?
Is it normal that my co-workers at a fitness company criticize my food choices?
How difficult is it to simply disable/disengage the MCAS on Boeing 737 Max 8 & 9 Aircraft?
As a new Ubuntu desktop 18.04 LTS user, do I need to use ufw for a firewall or is iptables sufficient?
Does .bashrc contain syntax errors?
Happy pi day, everyone!
If I can solve Sudoku, can I solve the Travelling Salesman Problem (TSP)? If so, how?
Employee lack of ownership
How to terminate ping <dest> &
Official degrees of earth’s rotation per day
What is the significance behind "40 days" that often appears in the Bible?
Is it good practice to use Linear Least-Squares with SMA?
Is honey really a supersaturated solution? Does heating to un-crystalize redissolve it or melt it?
Min function accepting varying number of arguments in C++17
When to use a slotted vs. solid turner?
How to make healing in an exploration game interesting
Why is a white electrical wire connected to 2 black wires?
Python if-else code style for reduced code for rounding floats
Brexit - No Deal Rejection
Have the tides ever turned twice on any open problem?
Can I use USB data pins as a power source?
Time travel from stationary position?
Violin - Can double stops be played when the strings are not next to each other?
PTIJ: Who should I vote for? (21st Knesset Edition)
The return type of a method is not consistent
2019 Community Moderator ElectionAbusing the algebra of algebraic data types - why does this work?How to write a pattern match in Ocaml so it is easy to scale?OCaml: Effective Path to GUI Programming?Typing in ocaml methodsRepresent Python-style namespaces using Ocaml data structuresWhy does this type error keep occurring?Possible OCaml code generation bugUsing camlidl for interface with opaque structWhy aren't Haskell variables polymorphic when bound by pattern matching?problem with how we define tuples as the funtion input in OCaml
I'm writing a function in OCaml to check whether two types are unifiable and will produce a unifier if there is one or print the appropriate message.
Here is the type system :
type typExp =
| TypInt
| TypVar of char
| Arrow of typExp * typExp
| Lst of typExp;;
type substitution = (char * typExp) list;;
I wrote a method to perform substitution of a variable by a type expression given substitution rules of type substitution.
let rec substitute (tau1 : typExp) (v : char) (tau2 : typExp) : typExp =
match tau2 with
|TypInt -> TypInt
|TypVar q -> (if(q=v) then tau1 else TypVar q)
|Arrow (q,w) -> Arrow ((substitute tau1 v q), (substitute tau1 v w))
|Lst q -> Lst (substitute tau1 v q)
;;
let rec applySubst (sigma: substitution) (tau: typExp) : typExp =
let reversedList = List.rev sigma in
match reversedList with
|(a,s)::w -> applySubst (List.rev w) (substitute s a tau)
|[]->tau
;;
I used those methods to implement the unifiable check function, however, when two types are not unifiable, it should print a message on the screen and the print method return a unit type not of that of substitution. I don't know how to deal with that.
let unify (tau1: typExp) (tau2:typExp) : substitution =
let rec helper acc t1 t2=
match t1, t2 with
| TypInt,TypInt -> acc(*not the problem*)
| TypInt, TypVar q -> (q,TypInt)::acc
| TypInt, Arrow (a,b) -> print_string "Not Unifyable" (* aproblem here*)
| TypInt, Lst a -> print_string "Not Unifyable"
| TypVar q, TypInt -> (q, TypInt)::acc
| TypVar q, Arrow (a,s) -> (q,Arrow(a,s))::acc
| TypVar q, Lst w -> (q, Lst w)::acc
| TypVar a, TypVar b ->( if(a=b) then acc else (a,TypVar b)::acc)
| Arrow(q,w), Arrow(a,s) -> if (helper [] w s)=[] then []
else helper (helper [] w s) (applySubst (helper [] w s) q) (applySubst (helper [] w s) a)
| Arrow (q,w), TypInt -> print_string "Not Unifyable"
| Arrow (q,w), TypVar a -> (a, Arrow(q,w))::acc
| Arrow (q,w), Lst a -> []
| Lst q, TypInt -> []
| Lst q, TypVar a -> (a,Lst q)::acc
| Lst q, Arrow (s,t) -> []
| Lst q, Lst w -> helper acc q w
in helper [] tau1 tau2
I'm wondering without using option type, is there another way to deal with this?
ocaml
add a comment |
I'm writing a function in OCaml to check whether two types are unifiable and will produce a unifier if there is one or print the appropriate message.
Here is the type system :
type typExp =
| TypInt
| TypVar of char
| Arrow of typExp * typExp
| Lst of typExp;;
type substitution = (char * typExp) list;;
I wrote a method to perform substitution of a variable by a type expression given substitution rules of type substitution.
let rec substitute (tau1 : typExp) (v : char) (tau2 : typExp) : typExp =
match tau2 with
|TypInt -> TypInt
|TypVar q -> (if(q=v) then tau1 else TypVar q)
|Arrow (q,w) -> Arrow ((substitute tau1 v q), (substitute tau1 v w))
|Lst q -> Lst (substitute tau1 v q)
;;
let rec applySubst (sigma: substitution) (tau: typExp) : typExp =
let reversedList = List.rev sigma in
match reversedList with
|(a,s)::w -> applySubst (List.rev w) (substitute s a tau)
|[]->tau
;;
I used those methods to implement the unifiable check function, however, when two types are not unifiable, it should print a message on the screen and the print method return a unit type not of that of substitution. I don't know how to deal with that.
let unify (tau1: typExp) (tau2:typExp) : substitution =
let rec helper acc t1 t2=
match t1, t2 with
| TypInt,TypInt -> acc(*not the problem*)
| TypInt, TypVar q -> (q,TypInt)::acc
| TypInt, Arrow (a,b) -> print_string "Not Unifyable" (* aproblem here*)
| TypInt, Lst a -> print_string "Not Unifyable"
| TypVar q, TypInt -> (q, TypInt)::acc
| TypVar q, Arrow (a,s) -> (q,Arrow(a,s))::acc
| TypVar q, Lst w -> (q, Lst w)::acc
| TypVar a, TypVar b ->( if(a=b) then acc else (a,TypVar b)::acc)
| Arrow(q,w), Arrow(a,s) -> if (helper [] w s)=[] then []
else helper (helper [] w s) (applySubst (helper [] w s) q) (applySubst (helper [] w s) a)
| Arrow (q,w), TypInt -> print_string "Not Unifyable"
| Arrow (q,w), TypVar a -> (a, Arrow(q,w))::acc
| Arrow (q,w), Lst a -> []
| Lst q, TypInt -> []
| Lst q, TypVar a -> (a,Lst q)::acc
| Lst q, Arrow (s,t) -> []
| Lst q, Lst w -> helper acc q w
in helper [] tau1 tau2
I'm wondering without using option type, is there another way to deal with this?
ocaml
add a comment |
I'm writing a function in OCaml to check whether two types are unifiable and will produce a unifier if there is one or print the appropriate message.
Here is the type system :
type typExp =
| TypInt
| TypVar of char
| Arrow of typExp * typExp
| Lst of typExp;;
type substitution = (char * typExp) list;;
I wrote a method to perform substitution of a variable by a type expression given substitution rules of type substitution.
let rec substitute (tau1 : typExp) (v : char) (tau2 : typExp) : typExp =
match tau2 with
|TypInt -> TypInt
|TypVar q -> (if(q=v) then tau1 else TypVar q)
|Arrow (q,w) -> Arrow ((substitute tau1 v q), (substitute tau1 v w))
|Lst q -> Lst (substitute tau1 v q)
;;
let rec applySubst (sigma: substitution) (tau: typExp) : typExp =
let reversedList = List.rev sigma in
match reversedList with
|(a,s)::w -> applySubst (List.rev w) (substitute s a tau)
|[]->tau
;;
I used those methods to implement the unifiable check function, however, when two types are not unifiable, it should print a message on the screen and the print method return a unit type not of that of substitution. I don't know how to deal with that.
let unify (tau1: typExp) (tau2:typExp) : substitution =
let rec helper acc t1 t2=
match t1, t2 with
| TypInt,TypInt -> acc(*not the problem*)
| TypInt, TypVar q -> (q,TypInt)::acc
| TypInt, Arrow (a,b) -> print_string "Not Unifyable" (* aproblem here*)
| TypInt, Lst a -> print_string "Not Unifyable"
| TypVar q, TypInt -> (q, TypInt)::acc
| TypVar q, Arrow (a,s) -> (q,Arrow(a,s))::acc
| TypVar q, Lst w -> (q, Lst w)::acc
| TypVar a, TypVar b ->( if(a=b) then acc else (a,TypVar b)::acc)
| Arrow(q,w), Arrow(a,s) -> if (helper [] w s)=[] then []
else helper (helper [] w s) (applySubst (helper [] w s) q) (applySubst (helper [] w s) a)
| Arrow (q,w), TypInt -> print_string "Not Unifyable"
| Arrow (q,w), TypVar a -> (a, Arrow(q,w))::acc
| Arrow (q,w), Lst a -> []
| Lst q, TypInt -> []
| Lst q, TypVar a -> (a,Lst q)::acc
| Lst q, Arrow (s,t) -> []
| Lst q, Lst w -> helper acc q w
in helper [] tau1 tau2
I'm wondering without using option type, is there another way to deal with this?
ocaml
I'm writing a function in OCaml to check whether two types are unifiable and will produce a unifier if there is one or print the appropriate message.
Here is the type system :
type typExp =
| TypInt
| TypVar of char
| Arrow of typExp * typExp
| Lst of typExp;;
type substitution = (char * typExp) list;;
I wrote a method to perform substitution of a variable by a type expression given substitution rules of type substitution.
let rec substitute (tau1 : typExp) (v : char) (tau2 : typExp) : typExp =
match tau2 with
|TypInt -> TypInt
|TypVar q -> (if(q=v) then tau1 else TypVar q)
|Arrow (q,w) -> Arrow ((substitute tau1 v q), (substitute tau1 v w))
|Lst q -> Lst (substitute tau1 v q)
;;
let rec applySubst (sigma: substitution) (tau: typExp) : typExp =
let reversedList = List.rev sigma in
match reversedList with
|(a,s)::w -> applySubst (List.rev w) (substitute s a tau)
|[]->tau
;;
I used those methods to implement the unifiable check function, however, when two types are not unifiable, it should print a message on the screen and the print method return a unit type not of that of substitution. I don't know how to deal with that.
let unify (tau1: typExp) (tau2:typExp) : substitution =
let rec helper acc t1 t2=
match t1, t2 with
| TypInt,TypInt -> acc(*not the problem*)
| TypInt, TypVar q -> (q,TypInt)::acc
| TypInt, Arrow (a,b) -> print_string "Not Unifyable" (* aproblem here*)
| TypInt, Lst a -> print_string "Not Unifyable"
| TypVar q, TypInt -> (q, TypInt)::acc
| TypVar q, Arrow (a,s) -> (q,Arrow(a,s))::acc
| TypVar q, Lst w -> (q, Lst w)::acc
| TypVar a, TypVar b ->( if(a=b) then acc else (a,TypVar b)::acc)
| Arrow(q,w), Arrow(a,s) -> if (helper [] w s)=[] then []
else helper (helper [] w s) (applySubst (helper [] w s) q) (applySubst (helper [] w s) a)
| Arrow (q,w), TypInt -> print_string "Not Unifyable"
| Arrow (q,w), TypVar a -> (a, Arrow(q,w))::acc
| Arrow (q,w), Lst a -> []
| Lst q, TypInt -> []
| Lst q, TypVar a -> (a,Lst q)::acc
| Lst q, Arrow (s,t) -> []
| Lst q, Lst w -> helper acc q w
in helper [] tau1 tau2
I'm wondering without using option type, is there another way to deal with this?
ocaml
ocaml
asked Mar 7 at 15:30
user42493user42493
345
345
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
The issue is due to the fact that the return type of helper
is a list of substitution
and some of your match do not return this type but the unit type instead. Therefore, the compiler points this error.
Now, one way to fix that is to raise an exception at those point.
exception NotUnifiable;;
And replace all lines similar to :
| TypInt, Arrow (a,b) -> print_string "Not Unifyable"
By :
| TypInt, Arrow (a,b) -> raise NotUnifiable
And the usage of unify:
try
unify ...the arguments...
with NotUnifiable -> print "Not unifiable"
But doing this may not be what you really want : as soon as there is an exception, you stop everything.
add a comment |
If I understand your question, you're trying to decide what value to return when unification isn't possible. This is just a basic design decision for your implementation, so I don't think there's one answer that anyone can give you.
You can definitely change the function's type to substitution option
. That is a nice clean solution. The return value of None
would indicate that unification isn't possible. These cases would look something like this:
print_string "Not Unifyable"; None
You could also raise an exception for this case. That can be a very effective solution in some cases, as it avoids allocating space for Some
for all the successful results (and the work to extract the substitution
value). However, the difference in time is usually not worth the extra complexity of dealing with exceptions (in my opinion).
You could also just return an empty list. This feels less clean, as it would be a legitimate result for a null unification (I suspect).
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55047409%2fthe-return-type-of-a-method-is-not-consistent%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
The issue is due to the fact that the return type of helper
is a list of substitution
and some of your match do not return this type but the unit type instead. Therefore, the compiler points this error.
Now, one way to fix that is to raise an exception at those point.
exception NotUnifiable;;
And replace all lines similar to :
| TypInt, Arrow (a,b) -> print_string "Not Unifyable"
By :
| TypInt, Arrow (a,b) -> raise NotUnifiable
And the usage of unify:
try
unify ...the arguments...
with NotUnifiable -> print "Not unifiable"
But doing this may not be what you really want : as soon as there is an exception, you stop everything.
add a comment |
The issue is due to the fact that the return type of helper
is a list of substitution
and some of your match do not return this type but the unit type instead. Therefore, the compiler points this error.
Now, one way to fix that is to raise an exception at those point.
exception NotUnifiable;;
And replace all lines similar to :
| TypInt, Arrow (a,b) -> print_string "Not Unifyable"
By :
| TypInt, Arrow (a,b) -> raise NotUnifiable
And the usage of unify:
try
unify ...the arguments...
with NotUnifiable -> print "Not unifiable"
But doing this may not be what you really want : as soon as there is an exception, you stop everything.
add a comment |
The issue is due to the fact that the return type of helper
is a list of substitution
and some of your match do not return this type but the unit type instead. Therefore, the compiler points this error.
Now, one way to fix that is to raise an exception at those point.
exception NotUnifiable;;
And replace all lines similar to :
| TypInt, Arrow (a,b) -> print_string "Not Unifyable"
By :
| TypInt, Arrow (a,b) -> raise NotUnifiable
And the usage of unify:
try
unify ...the arguments...
with NotUnifiable -> print "Not unifiable"
But doing this may not be what you really want : as soon as there is an exception, you stop everything.
The issue is due to the fact that the return type of helper
is a list of substitution
and some of your match do not return this type but the unit type instead. Therefore, the compiler points this error.
Now, one way to fix that is to raise an exception at those point.
exception NotUnifiable;;
And replace all lines similar to :
| TypInt, Arrow (a,b) -> print_string "Not Unifyable"
By :
| TypInt, Arrow (a,b) -> raise NotUnifiable
And the usage of unify:
try
unify ...the arguments...
with NotUnifiable -> print "Not unifiable"
But doing this may not be what you really want : as soon as there is an exception, you stop everything.
answered Mar 7 at 17:11
Pierre G.Pierre G.
3,4231723
3,4231723
add a comment |
add a comment |
If I understand your question, you're trying to decide what value to return when unification isn't possible. This is just a basic design decision for your implementation, so I don't think there's one answer that anyone can give you.
You can definitely change the function's type to substitution option
. That is a nice clean solution. The return value of None
would indicate that unification isn't possible. These cases would look something like this:
print_string "Not Unifyable"; None
You could also raise an exception for this case. That can be a very effective solution in some cases, as it avoids allocating space for Some
for all the successful results (and the work to extract the substitution
value). However, the difference in time is usually not worth the extra complexity of dealing with exceptions (in my opinion).
You could also just return an empty list. This feels less clean, as it would be a legitimate result for a null unification (I suspect).
add a comment |
If I understand your question, you're trying to decide what value to return when unification isn't possible. This is just a basic design decision for your implementation, so I don't think there's one answer that anyone can give you.
You can definitely change the function's type to substitution option
. That is a nice clean solution. The return value of None
would indicate that unification isn't possible. These cases would look something like this:
print_string "Not Unifyable"; None
You could also raise an exception for this case. That can be a very effective solution in some cases, as it avoids allocating space for Some
for all the successful results (and the work to extract the substitution
value). However, the difference in time is usually not worth the extra complexity of dealing with exceptions (in my opinion).
You could also just return an empty list. This feels less clean, as it would be a legitimate result for a null unification (I suspect).
add a comment |
If I understand your question, you're trying to decide what value to return when unification isn't possible. This is just a basic design decision for your implementation, so I don't think there's one answer that anyone can give you.
You can definitely change the function's type to substitution option
. That is a nice clean solution. The return value of None
would indicate that unification isn't possible. These cases would look something like this:
print_string "Not Unifyable"; None
You could also raise an exception for this case. That can be a very effective solution in some cases, as it avoids allocating space for Some
for all the successful results (and the work to extract the substitution
value). However, the difference in time is usually not worth the extra complexity of dealing with exceptions (in my opinion).
You could also just return an empty list. This feels less clean, as it would be a legitimate result for a null unification (I suspect).
If I understand your question, you're trying to decide what value to return when unification isn't possible. This is just a basic design decision for your implementation, so I don't think there's one answer that anyone can give you.
You can definitely change the function's type to substitution option
. That is a nice clean solution. The return value of None
would indicate that unification isn't possible. These cases would look something like this:
print_string "Not Unifyable"; None
You could also raise an exception for this case. That can be a very effective solution in some cases, as it avoids allocating space for Some
for all the successful results (and the work to extract the substitution
value). However, the difference in time is usually not worth the extra complexity of dealing with exceptions (in my opinion).
You could also just return an empty list. This feels less clean, as it would be a legitimate result for a null unification (I suspect).
answered Mar 7 at 17:11
Jeffrey ScofieldJeffrey Scofield
48.5k24979
48.5k24979
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55047409%2fthe-return-type-of-a-method-is-not-consistent%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown