Standard ML help datatype problem

  • Thread starter Thread starter Jimena29
  • Start date Start date
  • Tags Tags
    Standard
AI Thread Summary
The discussion revolves around creating a function for Peano numbers in Standard ML using a specific datatype. The user initially encounters syntax errors while attempting to implement a function to find the successor of a Peano number. Feedback highlights issues with pattern matching against functions and suggests reviewing documentation for proper syntax. Ultimately, a corrected implementation of the Peano number functions, including creation, conversion to integer, and doubling, is provided. The conversation emphasizes understanding the limitations of the ML type system and proper function handling in pattern matching.
Jimena29
Messages
4
Reaction score
0
Standard ML help! datatype problem

I have to create a function about peano numbers defined as the following datatype:

datatype 'a peano = P of ('a -> 'a) * 'a -> 'a


I have to create a function about peano numbers defined as the following datatype:
datatype 'a peano = P of ('a -> 'a) * 'a -> 'a
val zero = P(fn (f, x) => x)


The function that i have to implement finds the succesive peano number of the peano parameter P(p).
This is what I have written:

fun suc (P(p)) = case P(p) of P(fn(f,x)=>x) => P(fn(f,x)=>f(x));

The problem is that i get these errors:
stdIn:4.33-4.36 Error: syntax error: deleting FN LPAREN
stdIn:4.43 Error: syntax error found at RPAREN

I don't know what I am doing wrong please help!
 
Technology news on Phys.org


So... it has been a very long time since I have done any S/ML and I don't have an interpreter handy, but at least maybe I can help you decode the error message.

When it says 4.33-4.36 it's referring to line 4, characters 33 through 36. What it says is the tokens "FN LPAREN" are not recognized in the place where you put them. This of course is "fn" followed by a "left parenthesis", which is indeed at the 33rd character position in your line of code.

Then when it gets to character 43 it hits a right parenthesis. Because it previously decided to ignore the lparen, the rparen of course also becomes a syntax error.

Are you absolutely sure it is legal to put an fn in that place, or in general on the right-hand side of a "case of"? The ml type system is very smart but it has limits. You seem to be wanting it to pattern match against a function that takes (f,x) and returns x. I really don't see how the pattern matcher could possibly know whether a function fits that pattern without evaluating the function (and really not even then). Maybe try to check your documentation or textbook and see exactly what kinds of things are legal after that "of". (And come to think of it, why are you pattern matching at all?-- that is, what is the point of a "case" statement with only one "of"?)
 


Sorry, I didn't notice you had replied until now. But thank you so much!
If anyone has a problem like this I solved it, here's my code :
datatype 'a peano = P of ('a -> 'a) * 'a -> 'a

val zero = P(fn (f, x) => x)(* create : int -> 'a peano
* create(n) = the Peano number representing n
* Precondition: n >= 0)

fun create 0=zero
|create num =
let
fun helper (1,f,x) = f(x)
| helper (num,f,x) = helper (num-1,f,f(x))
in
P(fn(f,x)=>helper(num,f,x))
end;

(* peanoToInt : int peano -> int
* Postcondition: result >= 0
*)
fun peanoToInt (P(p)) = let fun f(x)=x+1 in p(f,0) end;

(* suc : peano -> 'a peano
* suc(p) = successor of the Peano number p
*)
fun suc (P(p)) = P(fn(f,x)=>p(f,f(x)));

* double : 'a peano -> 'a peano
* double p = (Peano number p) * 2
*)
fun double (P(p)) = P(fn(f,x)=> p(f,p(f,x)));
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
I am trying to run an .ipynb file and have installed Miniconda as well as created an environment as such -conda create -n <env_name> python=3.7 ipykernel jupyter I am assuming this is successful as I can activate this environment via the anaconda prompt and following command -conda activate <env_name> Then I downloaded and installed VS code and I am trying to edit an .ipynb file. I want to select a kernel, via VS Code but when I press the button on the upper right corner I am greeted...
Back
Top