F#: is it OK for developing theorem provers?

前端 未结 6 1393
离开以前
离开以前 2021-02-05 11:08

Please advise. I am a lawyer, I work in the field of Law Informatics. I have been a programmer for a long time (Basic, RPG, Fortran, Pascal, Cobol, VB.NET, C#). I am currently i

6条回答
  •  礼貌的吻别
    2021-02-05 11:18

    F# has many features that make this type of logic processing natural. To get a feel for what the language looks like, here is one possible way to decide which side of an argument has won, and by how much. Uses a random result for the argument, since the interesting (read "very hard to impossible") part will be parsing out the argument text and deciding how persuasive it would be to an actual human.

    /// Declare a 'weight' unit-of-measure, so the compiler can do static typechecking
    [] type weight
    
    /// Type of tokenized argument
    type Argument = string
    
    /// Type of argument reduced to side & weight
    type ArgumentResult =
        | Pro of float
        | Con of float
        | Draw
    
    /// Convert a tokenized argument into a side & weight
    /// Presently returns a random side and weight
    let ParseArgument =
        let rnd = System.Random()
        let nextArg() = rnd.NextDouble() * 1.0
        fun (line:string) ->
            // The REALLY interesting code goes here!
            match rnd.Next(0,3) with
            | 1 -> Pro(nextArg())
            | 2 -> Con(nextArg())
            | _ -> Draw
    
    /// Tally the argument scored
    let Score args = 
        // Sum up all pro & con scores, and keep track of count for avg calculation
        let totalPro, totalCon, count =
            args
            |> Seq.map ParseArgument
            |> Seq.fold
                (fun (pros, cons, count) arg ->
                    match arg with
                    | Pro(w) -> (pros+w, cons, count+1)
                    | Con(w) -> (pros, cons+w, count+1)
                    | Draw   -> (pros, cons,   count+1)
                    )
                 (0.0, 0.0, 0)
        let fcount = float(count)
        let avgPro, avgCon = totalPro/fcount, totalCon/ fcoun
        let diff = avgPro - avgCon
        match diff with
        // consider < 1% a draw
        | d when abs d < 0.01   -> Draw
        | d when d > 0.0        -> Pro(d)
        | d                             -> Con(-d)
    
    let testScore = ["yes"; "no"; "yes"; "no"; "no"; "YES!"; "YES!"]
                    |> Score
    printfn "Test score = %A" testScore
    

提交回复
热议问题