Standard ML help datatype problem

  • Thread starter Thread starter Jimena29
  • Start date Start date
  • Tags Tags
    Standard
Click For Summary
SUMMARY

The discussion focuses on implementing functions for Peano numbers in Standard ML using the datatype 'a peano = P of ('a -> 'a) * 'a -> 'a. The user encountered syntax errors while attempting to define the successor function suc, specifically related to the placement of the fn keyword within a case of statement. The solution provided includes a corrected implementation of the suc function and additional functions such as create, peanoToInt, and double, demonstrating proper usage of pattern matching and function definitions in Standard ML.

PREREQUISITES
  • Understanding of Standard ML syntax and semantics
  • Familiarity with functional programming concepts
  • Knowledge of datatype definitions in Standard ML
  • Experience with pattern matching in Standard ML
NEXT STEPS
  • Study the implementation of recursive functions in Standard ML
  • Learn about higher-order functions and their applications in Standard ML
  • Explore error handling and debugging techniques in Standard ML
  • Investigate the use of the case expression in Standard ML for pattern matching
USEFUL FOR

Students and developers working with Standard ML, particularly those interested in functional programming and the implementation of mathematical concepts like Peano numbers.

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)));
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 8 ·
Replies
8
Views
5K
Replies
13
Views
8K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
Replies
1
Views
3K
  • · Replies 11 ·
Replies
11
Views
2K
Replies
9
Views
2K
  • · Replies 6 ·
Replies
6
Views
4K